141 lines
3.8 KiB
Rust
141 lines
3.8 KiB
Rust
extern crate serde;
|
|
#[macro_use]
|
|
extern crate serde_derive;
|
|
extern crate serde_json;
|
|
|
|
extern crate futures;
|
|
extern crate mozaic;
|
|
extern crate rand;
|
|
extern crate tokio;
|
|
|
|
extern crate tracing;
|
|
extern crate tracing_futures;
|
|
extern crate tracing_subscriber;
|
|
|
|
use mozaic::errors;
|
|
use mozaic::messaging::types::*;
|
|
use std::env;
|
|
use std::net::SocketAddr;
|
|
|
|
use tracing::{span, Level};
|
|
use tracing_futures::Instrument;
|
|
use tracing_subscriber::{fmt, EnvFilter};
|
|
|
|
use mozaic::modules::{game, Aggregator, Steplock};
|
|
|
|
mod planetwars;
|
|
|
|
// Load the config and start the game.
|
|
fn main() {
|
|
let args: Vec<String> = env::args().collect();
|
|
let name = args[0].clone();
|
|
match run(args) {
|
|
None => print_info(&name),
|
|
_ => {}
|
|
};
|
|
}
|
|
|
|
use errors::Consumable;
|
|
use mozaic::modules::util;
|
|
use mozaic::modules::ConnectionManager;
|
|
use mozaic::runtime::Broker;
|
|
use rand::Rng;
|
|
use std::collections::HashMap;
|
|
|
|
fn print_info(name: &str) {
|
|
println!(
|
|
"Usage: {} map_location [number_of_clients [output [max_turns]]]",
|
|
name
|
|
);
|
|
}
|
|
|
|
pub fn run(args: Vec<String>) -> Option<()> {
|
|
let subscriber = fmt::Subscriber::builder()
|
|
.with_env_filter(EnvFilter::try_from_default_env().unwrap_or(EnvFilter::from("info")))
|
|
.without_time()
|
|
.inherit_fields(true)
|
|
.finish();
|
|
let _ = tracing::subscriber::set_global_default(subscriber);
|
|
|
|
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 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);
|
|
|
|
let ids: HashMap<util::Identifier, util::PlayerId> = (0..number_of_clients)
|
|
.map(|x| (rand::thread_rng().gen::<u64>().into(), x.into()))
|
|
.collect();
|
|
|
|
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);
|
|
|
|
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 || {
|
|
mozaic::graph::set_graph(mozaic::graph::Graph::new());
|
|
let mut broker = Broker::new().unwrap();
|
|
|
|
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(
|
|
manager_id.clone(),
|
|
ConnectionManager::params(broker.clone(), ids, aggregator_id.clone(), addr),
|
|
"Connection Manager",
|
|
)
|
|
.display();
|
|
|
|
Ok(())
|
|
}).instrument(span!(Level::TRACE, "main")),
|
|
);
|
|
|
|
Some(())
|
|
}
|