From 2ad7354de5b48d9b943881f755bfabb02b11c580 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Sat, 14 Sep 2019 21:16:29 +0200 Subject: [PATCH] add creation handling --- backend/map.json | 1 + backend/src/main.rs | 34 +++++++++-------------------- backend/src/planetwars/mod.rs | 10 ++++++++- backend/src/planetwars/pw_config.rs | 6 ++--- 4 files changed, 23 insertions(+), 28 deletions(-) create mode 100644 backend/map.json diff --git a/backend/map.json b/backend/map.json new file mode 100644 index 0000000..b7be71d --- /dev/null +++ b/backend/map.json @@ -0,0 +1 @@ +{"planets":[{"name":"PIET9+0","x":27,"y":0,"ship_count":6},{"name":"PIET10+0","x":30,"y":0,"ship_count":6},{"name":"PIET11+0","x":33,"y":0,"ship_count":6},{"name":"PIET12+0","x":36,"y":0,"ship_count":6},{"name":"PIET13+0","x":39,"y":0,"ship_count":6},{"name":"PIET7+1","x":21,"y":3,"ship_count":6},{"name":"PIET8+1","x":24,"y":3,"ship_count":6},{"name":"PIET14+1","x":42,"y":3,"ship_count":6},{"name":"PIET6+2","x":18,"y":6,"ship_count":6},{"name":"PIET15+2","x":45,"y":6,"ship_count":6},{"name":"PIET6+3","x":18,"y":9,"ship_count":6},{"name":"PIET7+3","x":21,"y":9,"ship_count":6},{"name":"PIET8+3","x":24,"y":9,"ship_count":6},{"name":"PIET11+3","x":33,"y":9,"ship_count":6},{"name":"PIET12+3","x":36,"y":9,"ship_count":6},{"name":"PIET15+3","x":45,"y":9,"ship_count":6},{"name":"PIET6+4","x":18,"y":12,"ship_count":6},{"name":"PIET7+4","x":21,"y":12,"ship_count":6,"owner":1},{"name":"PIET11+4","x":33,"y":12,"ship_count":6},{"name":"PIET15+4","x":45,"y":12,"ship_count":6},{"name":"PIET5+5","x":15,"y":15,"ship_count":6},{"name":"PIET9+5","x":27,"y":15,"ship_count":6},{"name":"PIET14+5","x":42,"y":15,"ship_count":6,"owner":2},{"name":"PIET5+6","x":15,"y":18,"ship_count":6},{"name":"PIET6+6","x":18,"y":18,"ship_count":6},{"name":"PIET7+6","x":21,"y":18,"ship_count":6},{"name":"PIET8+6","x":24,"y":18,"ship_count":6},{"name":"PIET9+6","x":27,"y":18,"ship_count":6},{"name":"PIET10+6","x":30,"y":18,"ship_count":6},{"name":"PIET11+6","x":33,"y":18,"ship_count":6},{"name":"PIET14+6","x":42,"y":18,"ship_count":6},{"name":"PIET4+7","x":12,"y":21,"ship_count":6},{"name":"PIET5+7","x":15,"y":21,"ship_count":6},{"name":"PIET14+7","x":42,"y":21,"ship_count":6},{"name":"PIET4+8","x":12,"y":24,"ship_count":6},{"name":"PIET13+8","x":39,"y":24,"ship_count":6},{"name":"PIET14+8","x":42,"y":24,"ship_count":6},{"name":"PIET4+9","x":12,"y":27,"ship_count":6},{"name":"PIET13+9","x":39,"y":27,"ship_count":6},{"name":"PIET4+10","x":12,"y":30,"ship_count":6},{"name":"PIET10+10","x":30,"y":30,"ship_count":6},{"name":"PIET11+10","x":33,"y":30,"ship_count":6},{"name":"PIET13+10","x":39,"y":30,"ship_count":6},{"name":"PIET21+10","x":63,"y":30,"ship_count":6},{"name":"PIET22+10","x":66,"y":30,"ship_count":6},{"name":"PIET4+11","x":12,"y":33,"ship_count":6},{"name":"PIET10+11","x":30,"y":33,"ship_count":6},{"name":"PIET11+11","x":33,"y":33,"ship_count":6},{"name":"PIET13+11","x":39,"y":33,"ship_count":6},{"name":"PIET19+11","x":57,"y":33,"ship_count":6},{"name":"PIET20+11","x":60,"y":33,"ship_count":6},{"name":"PIET22+11","x":66,"y":33,"ship_count":6},{"name":"PIET4+12","x":12,"y":36,"ship_count":6},{"name":"PIET10+12","x":30,"y":36,"ship_count":6},{"name":"PIET13+12","x":39,"y":36,"ship_count":6},{"name":"PIET14+12","x":42,"y":36,"ship_count":6},{"name":"PIET18+12","x":54,"y":36,"ship_count":6},{"name":"PIET22+12","x":66,"y":36,"ship_count":6},{"name":"PIET4+13","x":12,"y":39,"ship_count":6},{"name":"PIET9+13","x":27,"y":39,"ship_count":6},{"name":"PIET10+13","x":30,"y":39,"ship_count":6},{"name":"PIET13+13","x":39,"y":39,"ship_count":6},{"name":"PIET15+13","x":45,"y":39,"ship_count":6},{"name":"PIET17+13","x":51,"y":39,"ship_count":6},{"name":"PIET18+13","x":54,"y":39,"ship_count":6},{"name":"PIET21+13","x":63,"y":39,"ship_count":6},{"name":"PIET4+14","x":12,"y":42,"ship_count":6},{"name":"PIET9+14","x":27,"y":42,"ship_count":6},{"name":"PIET10+14","x":30,"y":42,"ship_count":6},{"name":"PIET13+14","x":39,"y":42,"ship_count":6},{"name":"PIET16+14","x":48,"y":42,"ship_count":6},{"name":"PIET20+14","x":60,"y":42,"ship_count":6},{"name":"PIET4+15","x":12,"y":45,"ship_count":6},{"name":"PIET8+15","x":24,"y":45,"ship_count":6},{"name":"PIET10+15","x":30,"y":45,"ship_count":6},{"name":"PIET15+15","x":45,"y":45,"ship_count":6},{"name":"PIET16+15","x":48,"y":45,"ship_count":6},{"name":"PIET19+15","x":57,"y":45,"ship_count":6},{"name":"PIET4+16","x":12,"y":48,"ship_count":6},{"name":"PIET14+16","x":42,"y":48,"ship_count":6},{"name":"PIET17+16","x":51,"y":48,"ship_count":6},{"name":"PIET20+16","x":60,"y":48,"ship_count":6},{"name":"PIET4+17","x":12,"y":51,"ship_count":6},{"name":"PIET5+17","x":15,"y":51,"ship_count":6},{"name":"PIET18+17","x":54,"y":51,"ship_count":6},{"name":"PIET19+17","x":57,"y":51,"ship_count":6},{"name":"PIET5+18","x":15,"y":54,"ship_count":6},{"name":"PIET6+18","x":18,"y":54,"ship_count":6},{"name":"PIET17+18","x":51,"y":54,"ship_count":6},{"name":"PIET6+19","x":18,"y":57,"ship_count":6},{"name":"PIET7+19","x":21,"y":57,"ship_count":6},{"name":"PIET17+19","x":51,"y":57,"ship_count":6},{"name":"PIET6+20","x":18,"y":60,"ship_count":6},{"name":"PIET8+20","x":24,"y":60,"ship_count":6},{"name":"PIET16+20","x":48,"y":60,"ship_count":6},{"name":"PIET17+20","x":51,"y":60,"ship_count":6},{"name":"PIET3+21","x":9,"y":63,"ship_count":6},{"name":"PIET5+21","x":15,"y":63,"ship_count":6},{"name":"PIET6+21","x":18,"y":63,"ship_count":6},{"name":"PIET9+21","x":27,"y":63,"ship_count":6},{"name":"PIET10+21","x":30,"y":63,"ship_count":6},{"name":"PIET11+21","x":33,"y":63,"ship_count":6},{"name":"PIET12+21","x":36,"y":63,"ship_count":6},{"name":"PIET13+21","x":39,"y":63,"ship_count":6},{"name":"PIET14+21","x":42,"y":63,"ship_count":6},{"name":"PIET15+21","x":45,"y":63,"ship_count":6},{"name":"PIET4+22","x":12,"y":66,"ship_count":6},{"name":"PIET5+22","x":15,"y":66,"ship_count":6},{"name":"PIET8+22","x":24,"y":66,"ship_count":6},{"name":"PIET11+22","x":33,"y":66,"ship_count":6},{"name":"PIET9+23","x":27,"y":69,"ship_count":6},{"name":"PIET11+23","x":33,"y":69,"ship_count":6},{"name":"PIET10+24","x":30,"y":72,"ship_count":6}]} \ No newline at end of file diff --git a/backend/src/main.rs b/backend/src/main.rs index c2f7495..cb9b5ab 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -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>) -> Vec { - 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) { 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 = (0..number_of_clients).map(|x| (rand::thread_rng().gen::().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 = 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( diff --git a/backend/src/planetwars/mod.rs b/backend/src/planetwars/mod.rs index 90d0e24..fe195e3 100644 --- a/backend/src/planetwars/mod.rs +++ b/backend/src/planetwars/mod.rs @@ -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, updates: &mut Vec, ) { let state = pw_serializer::serialize(&self.state); println!("{}", serde_json::to_string(&state).unwrap()); diff --git a/backend/src/planetwars/pw_config.rs b/backend/src/planetwars/pw_config.rs index a34aee3..efe99e5 100644 --- a/backend/src/planetwars/pw_config.rs +++ b/backend/src/planetwars/pw_config.rs @@ -14,9 +14,9 @@ pub struct Config { } impl Config { - pub fn create_game(&self, clients: Vec) -> 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();