add creation handling

This commit is contained in:
ajuvercr 2019-09-14 21:16:29 +02:00
parent fc6bcca0f8
commit 2ad7354de5
4 changed files with 23 additions and 28 deletions

1
backend/map.json Normal file

File diff suppressed because one or more lines are too long

View file

@ -23,27 +23,6 @@ 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;
@ -62,14 +41,21 @@ pub fn run(args : Vec<String>) {
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();
let ids: HashMap<util::Identifier, util::PlayerId> = (0..number_of_clients).map(|x| (rand::thread_rng().gen::<u64>().into(), x.into())).collect();
println!("Ids: {:?}", ids);
let config = planetwars::Config { map_file: String::from("map.json"), max_turns: 500 };
let game = planetwars::PlanetWarsGame::new(config.create_game(number_of_clients as usize));
println!("Tokens:");
let keys: Vec<u64> = ids.keys().map(|&x| x.into()).collect();
for key in keys {
println!("key {}", key);
}
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(welcomer_id.clone(), game::GameReactor::params(steplock_id.clone(), Box::new(game)), "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(

View file

@ -12,7 +12,7 @@ mod pw_rules;
mod pw_protocol;
use pw_protocol::{ self as proto, CommandError };
use pw_rules::Dispatch;
pub use pw_config::Config;
pub struct PlanetWarsGame {
state: pw_rules::PlanetWars,
@ -21,6 +21,14 @@ pub struct PlanetWarsGame {
impl PlanetWarsGame {
pub fn new(state: pw_rules::PlanetWars) -> Self {
let planet_map = state.planets.iter().map(|p| (p.name.clone(), p.id)).collect();
Self {
state, planet_map
}
}
fn dispatch_state(&self, were_alive: Vec<usize>, updates: &mut Vec<game::Update>, ) {
let state = pw_serializer::serialize(&self.state);
println!("{}", serde_json::to_string(&state).unwrap());

View file

@ -14,9 +14,9 @@ pub struct Config {
}
impl Config {
pub fn create_game(&self, clients: Vec<usize>) -> PlanetWars {
let planets = self.load_map(clients.len());
let players = clients.into_iter()
pub fn create_game(&self, clients: usize) -> PlanetWars {
let planets = self.load_map(clients);
let players = (0..clients)
.map(|client_id| Player { id: client_id, alive: true })
.collect();