2019-09-14 16:15:19 +00:00
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
extern crate serde_json;
|
2019-09-14 11:16:16 +00:00
|
|
|
|
|
|
|
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};
|
|
|
|
|
2019-09-14 16:15:19 +00:00
|
|
|
|
|
|
|
mod planetwars;
|
|
|
|
|
2019-09-14 11:16:16 +00:00
|
|
|
// Load the config and start the game.
|
|
|
|
fn main() {
|
2019-09-21 10:31:59 +00:00
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
let name = args[0].clone();
|
|
|
|
match run(args) {
|
|
|
|
None => print_info(&name),
|
|
|
|
_ => {},
|
|
|
|
};
|
2019-09-14 11:16:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 11:26:43 +00:00
|
|
|
use mozaic::runtime::{Broker};
|
2019-09-14 11:16:16 +00:00
|
|
|
use rand::Rng;
|
|
|
|
use errors::Consumable;
|
|
|
|
use mozaic::modules::ConnectionManager;
|
|
|
|
use mozaic::modules::util;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2019-09-21 10:31:59 +00:00
|
|
|
fn print_info(name: &str) {
|
|
|
|
println!("Usage: {} map_location [number_of_clients [output [max_turns]]]", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(args : Vec<String>) -> Option<()> {
|
2019-09-14 11:16:16 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2019-09-21 10:31:59 +00:00
|
|
|
let map = args.get(1)?;
|
|
|
|
let number_of_clients = args.get(2).map(|x| x.parse().expect("Client number should be a number")).unwrap_or(1);
|
|
|
|
let location = args.get(3).map(|x| x.as_str()).unwrap_or("game.json");
|
|
|
|
let max_turns = args.get(4).map(|x| x.parse().expect("Max turns should be a number")).unwrap_or(500);
|
2019-09-14 11:16:16 +00:00
|
|
|
|
2019-09-14 19:16:29 +00:00
|
|
|
let ids: HashMap<util::Identifier, util::PlayerId> = (0..number_of_clients).map(|x| (rand::thread_rng().gen::<u64>().into(), x.into())).collect();
|
2019-09-14 11:16:16 +00:00
|
|
|
|
2019-09-21 10:31:59 +00:00
|
|
|
let config = planetwars::Config { map_file: map.to_string(), max_turns: max_turns };
|
|
|
|
let game = planetwars::PlanetWarsGame::new(config.create_game(number_of_clients as usize), location);
|
2019-09-14 19:16:29 +00:00
|
|
|
|
|
|
|
println!("Tokens:");
|
|
|
|
let keys: Vec<u64> = ids.keys().map(|&x| x.into()).collect();
|
|
|
|
for key in keys {
|
|
|
|
println!("key {}", key);
|
|
|
|
}
|
2019-09-14 11:16:16 +00:00
|
|
|
|
|
|
|
tokio::run(futures::lazy(move || {
|
|
|
|
let mut broker = Broker::new().unwrap();
|
|
|
|
|
2019-09-14 19:16:29 +00:00
|
|
|
broker.spawn(welcomer_id.clone(), game::GameReactor::params(steplock_id.clone(), Box::new(game)), "Main").display();
|
2019-09-14 11:16:16 +00:00
|
|
|
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(())
|
|
|
|
}));
|
2019-09-21 10:31:59 +00:00
|
|
|
|
|
|
|
Some(())
|
2019-09-14 11:16:16 +00:00
|
|
|
}
|