diff --git a/backend/.gitignore b/backend/.gitignore new file mode 100644 index 0000000..2c96eb1 --- /dev/null +++ b/backend/.gitignore @@ -0,0 +1,2 @@ +target/ +Cargo.lock diff --git a/backend/Cargo.toml b/backend/Cargo.toml new file mode 100644 index 0000000..b9d6f95 --- /dev/null +++ b/backend/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "backend" +version = "0.1.0" +authors = ["ajuvercr "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +mozaic = { git = "https://github.com/ajuvercr/MOZAIC" } +tokio = "0.1.22" +rand = { version = "0.6.5", default-features = true } +futures = "0.1.28" diff --git a/backend/README.md b/backend/README.md new file mode 100644 index 0000000..e22697f --- /dev/null +++ b/backend/README.md @@ -0,0 +1,3 @@ +# Planetwars backend + +The main planetwars server that instanciates planetwars matches etc... diff --git a/backend/log.log b/backend/log.log new file mode 100644 index 0000000..e69de29 diff --git a/backend/src/main.rs b/backend/src/main.rs new file mode 100644 index 0000000..23e0956 --- /dev/null +++ b/backend/src/main.rs @@ -0,0 +1,76 @@ + +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}; + +// Load the config and start the game. +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; +use mozaic::modules::ConnectionManager; +use mozaic::modules::util; +use std::collections::HashMap; + +pub fn run(args : Vec) { + + let addr = "127.0.0.1:9142".parse::().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 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(); + + println!("Ids: {:?}", ids); + + 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(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(()) + })); +} diff --git a/client/.gitignore b/client/.gitignore new file mode 100644 index 0000000..2c96eb1 --- /dev/null +++ b/client/.gitignore @@ -0,0 +1,2 @@ +target/ +Cargo.lock diff --git a/client/Cargo.toml b/client/Cargo.toml new file mode 100644 index 0000000..594bb4a --- /dev/null +++ b/client/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "client" +version = "0.1.0" +authors = ["ajuvercr "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/client/README.md b/client/README.md new file mode 100644 index 0000000..b20e140 --- /dev/null +++ b/client/README.md @@ -0,0 +1,6 @@ +# Planetwars client + +This client is used to drive bots who want to participate in a planetwars match. +Planetwars matches are hosted using ../backend. +It is required to use a client like this because the backend uses the MOZAIC framework that sends and receives messages in a specific format. + diff --git a/client/src/main.rs b/client/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/client/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}