planet-wars/backend/src/main.rs

127 lines
3.4 KiB
Rust
Raw Normal View History

extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
2019-09-14 11:16:16 +00:00
2020-03-24 17:24:20 +00:00
extern crate async_std;
2019-09-14 11:16:16 +00:00
extern crate futures;
extern crate mozaic;
extern crate rand;
2019-11-15 18:01:44 +00:00
extern crate tracing;
extern crate tracing_futures;
extern crate tracing_subscriber;
2020-03-24 17:24:20 +00:00
use tracing_subscriber::{EnvFilter, FmtSubscriber};
2019-09-14 11:16:16 +00:00
use std::net::SocketAddr;
2020-03-24 17:24:20 +00:00
use std::{env, time};
use mozaic::modules::types::*;
use mozaic::modules::{game, StepLock};
2019-09-14 11:16:16 +00:00
2020-03-24 17:24:20 +00:00
use futures::executor::ThreadPool;
use futures::future::FutureExt;
2019-09-14 11:16:16 +00:00
2020-03-24 17:24:20 +00:00
use mozaic::graph;
use mozaic::modules::*;
mod planetwars;
2019-09-14 11:16:16 +00:00
// Load the config and start the game.
2020-03-24 17:24:20 +00:00
#[async_std::main]
async fn main() {
let args: Vec<String> = env::args().collect();
let name = args[0].clone();
2020-03-24 17:24:20 +00:00
match run(args).await {
None => print_info(&name),
2019-11-15 18:01:44 +00:00
_ => {}
};
2019-09-14 11:16:16 +00:00
}
2020-03-24 17:24:20 +00:00
fn build_builder(
pool: ThreadPool,
number_of_clients: u64,
max_turns: u64,
map: &str,
location: &str,
) -> game::Builder<planetwars::PlanetWarsGame> {
let config = planetwars::Config {
map_file: map.to_string(),
max_turns: max_turns,
};
2019-09-14 11:16:16 +00:00
2020-03-24 17:24:20 +00:00
let game =
planetwars::PlanetWarsGame::new(config.create_game(number_of_clients as usize), location);
let players: Vec<PlayerId> = (0..number_of_clients).collect();
game::Builder::new(players.clone(), game).with_step_lock(
StepLock::new(players.clone(), pool.clone())
.with_timeout(std::time::Duration::from_secs(1)),
)
}
2020-03-24 17:24:20 +00:00
async fn run(args: Vec<String>) -> Option<()> {
let fut = graph::set_default();
let sub = FmtSubscriber::builder()
.with_env_filter(EnvFilter::from_default_env())
2019-11-15 18:01:44 +00:00
.finish();
2020-03-24 17:24:20 +00:00
tracing::subscriber::set_global_default(sub).unwrap();
2019-09-14 11:16:16 +00:00
let addr = "127.0.0.1:9142".parse::<SocketAddr>().unwrap();
let map = args.get(1)?;
2019-11-15 18:01:44 +00:00
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");
2020-03-24 17:24:20 +00:00
let max_turns: u64 = args
2019-11-15 18:01:44 +00:00
.get(4)
.map(|x| x.parse().expect("Max turns should be a number"))
.unwrap_or(500);
2020-03-24 17:24:20 +00:00
let pool = ThreadPool::new().ok()?;
pool.spawn_ok(fut.map(|_| ()));
let (gmb, handle) = game::Manager::builder(pool.clone());
let ep = TcpEndpoint::new(addr, pool.clone());
let gmb = gmb.add_endpoint(ep, "TCP endpoint");
let mut gm = gmb.build();
let game_builder = build_builder(pool.clone(), number_of_clients, max_turns, map, location);
std::thread::sleep(time::Duration::from_millis(3000));
let mut current_game = gm.start_game(game_builder).await.unwrap();
loop {
match gm.get_state(current_game).await {
None => {
println!("Game finished, let's play a new one");
let game_builder =
build_builder(pool.clone(), number_of_clients, max_turns, map, location);
current_game = gm.start_game(game_builder).await.unwrap();
}
Some(state) => {
println!("{:?}", state);
}
}
std::thread::sleep(time::Duration::from_millis(3000));
2019-09-14 19:16:29 +00:00
}
2019-09-14 11:16:16 +00:00
2020-03-24 17:24:20 +00:00
handle.await;
std::thread::sleep(time::Duration::from_millis(100));
Some(())
2019-09-14 11:16:16 +00:00
}
2020-03-24 17:24:20 +00:00
fn print_info(name: &str) {
println!(
"Usage: {} map_location [number_of_clients [output [max_turns]]]",
name
);
}