add places for backend and client

This commit is contained in:
ajuvercr 2019-09-14 13:16:16 +02:00
parent a289936913
commit b1d59e3e18
9 changed files with 114 additions and 0 deletions

2
backend/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target/
Cargo.lock

13
backend/Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "backend"
version = "0.1.0"
authors = ["ajuvercr <arthur.vercruysse@ugent.be>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
mozaic = { git = "https://github.com/ajuvercr/MOZAIC" }
tokio = "0.1.22"
rand = { version = "0.6.5", default-features = true }
futures = "0.1.28"

3
backend/README.md Normal file
View file

@ -0,0 +1,3 @@
# Planetwars backend
The main planetwars server that instanciates planetwars matches etc...

0
backend/log.log Normal file
View file

76
backend/src/main.rs Normal file
View file

@ -0,0 +1,76 @@
extern crate tokio;
extern crate futures;
extern crate mozaic;
extern crate rand;
use std::env;
use std::net::SocketAddr;
use mozaic::messaging::types::*;
use mozaic::errors;
use mozaic::modules::{Aggregator, Steplock, game};
// Load the config and start the game.
fn main() {
run(env::args().collect());
}
use std::str;
struct Server;
impl game::GameController for Server {
fn step<'a>(&mut self, turns: Vec<game::PlayerTurn<'a>>) -> Vec<game::Update> {
let mut out = Vec::new();
for (id, turn) in turns.iter() {
let postfix = match turn {
game::Turn::Action(bytes) => str::from_utf8(bytes).unwrap(),
game::Turn::Timeout => "Timed out",
};
let msg = format!("{}: {}", **id, postfix);
out.push(game::Update::Global(msg.as_bytes().to_vec()));
}
return out;
}
}
use mozaic::server::runtime::{Broker};
use rand::Rng;
use errors::Consumable;
use mozaic::modules::ConnectionManager;
use mozaic::modules::util;
use std::collections::HashMap;
pub fn run(args : Vec<String>) {
let addr = "127.0.0.1:9142".parse::<SocketAddr>().unwrap();
let manager_id: ReactorId = rand::thread_rng().gen();
let welcomer_id: ReactorId = rand::thread_rng().gen();
let aggregator_id: ReactorId = rand::thread_rng().gen();
let steplock_id: ReactorId = rand::thread_rng().gen();
let number_of_clients = args.get(1).map(|x| x.parse().unwrap_or(1)).unwrap_or(1);
let ids: HashMap<_, util::PlayerId> = (0..number_of_clients).map(|x| (x.into(), (10 - x).into())).collect();
println!("Ids: {:?}", ids);
tokio::run(futures::lazy(move || {
let mut broker = Broker::new().unwrap();
broker.spawn(welcomer_id.clone(), game::GameReactor::params(steplock_id.clone(), Box::new(Server)), "Main").display();
broker.spawn(steplock_id.clone(), Steplock::new(broker.clone(), ids.values().cloned().collect(), welcomer_id.clone(), aggregator_id.clone()).with_timeout(5000).params(), "Steplock").display();
broker.spawn(aggregator_id.clone(), Aggregator::params(manager_id.clone(), steplock_id.clone()), "Aggregator").display();
broker.spawn(
manager_id.clone(),
ConnectionManager::params(broker.clone(), ids, aggregator_id.clone(), addr),
"Connection Manager"
).display();
Ok(())
}));
}

2
client/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target/
Cargo.lock

9
client/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "client"
version = "0.1.0"
authors = ["ajuvercr <arthur.vercruysse@ugent.be>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

6
client/README.md Normal file
View file

@ -0,0 +1,6 @@
# Planetwars client
This client is used to drive bots who want to participate in a planetwars match.
Planetwars matches are hosted using ../backend.
It is required to use a client like this because the backend uses the MOZAIC framework that sends and receives messages in a specific format.

3
client/src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}