diff --git a/client/Cargo.toml b/client/Cargo.toml deleted file mode 100644 index 288146e..0000000 --- a/client/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[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] -mozaic = { git = "https://github.com/ZeusWPI/MOZAICP" } -tokio = "0.1.22" -capnp = "0.10.1" -futures = "0.1.28" -serde = "1.0.100" -serde_derive = "1.0.100" -serde_json = "1.0" -rand = { version = "0.6.5", default-features = true } diff --git a/client/README.md b/client/README.md deleted file mode 100644 index b20e140..0000000 --- a/client/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# 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/bot.py b/client/bot.py index 57618b9..3fb2cde 100755 --- a/client/bot.py +++ b/client/bot.py @@ -45,6 +45,7 @@ def main(): threading.Thread(target=lambda: handle_input(it, sock), daemon=True).start() line = f.readline() + content = "Nothing" while line: content = json.loads(line) if content["type"] == "game_state": diff --git a/client/log.log b/client/log.log deleted file mode 100644 index e69de29..0000000 diff --git a/client/run.sh b/client/run.sh index 1965c69..ef34c29 100755 --- a/client/run.sh +++ b/client/run.sh @@ -1,4 +1,3 @@ #!/bin/bash -rm bot*.txt -cargo run $1 python3 simple.py +python bot.py -p 9142 -i $1 python simple.py diff --git a/client/run2.sh b/client/run2.sh deleted file mode 100755 index ef34c29..0000000 --- a/client/run2.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -python bot.py -p 9142 -i $1 python simple.py diff --git a/client/src/main.rs b/client/src/main.rs deleted file mode 100644 index a24b4e4..0000000 --- a/client/src/main.rs +++ /dev/null @@ -1,295 +0,0 @@ -extern crate capnp; -extern crate futures; -extern crate mozaic; -extern crate rand; -extern crate tokio; - -extern crate serde; -#[macro_use] -extern crate serde_derive; -extern crate serde_json; - -use rand::Rng; - -use mozaic::base_capnp::{client_message, host_message}; -use mozaic::cmd_capnp::{bot_input, bot_return}; -use mozaic::connection_capnp::client_kicked; -use mozaic::core_capnp::{actor_joined, identify, initialize, terminate_stream}; -use mozaic::errors::*; -use mozaic::messaging::reactor::*; -use mozaic::messaging::types::*; -use mozaic::modules::BotReactor; -use mozaic::runtime; -use mozaic::runtime::{Broker, BrokerHandle}; - -use std::env; -use std::str; -use std::process; - -mod types; - -fn main() { - let args: Vec = env::args().collect(); - let id = args.get(1).unwrap().parse().unwrap(); - let client_args = args - .get(2..) - .expect("How do you expect me to spawn your bot?") - .to_vec(); - - let addr = "127.0.0.1:9142".parse().unwrap(); - let self_id: ReactorId = rand::thread_rng().gen(); - - tokio::run(futures::lazy(move || { - let mut broker = Broker::new().unwrap(); - let reactor = ClientReactor { - server: None, - id, - broker: broker.clone(), - args: client_args, - }; - broker - .spawn(self_id.clone(), reactor.params(), "main") - .display(); - - tokio::spawn(runtime::connect_to_server(broker, self_id, &addr)); - Ok(()) - })); -} - -// Main client logic -/// ? greeter_id is the server, the tcp stream that you connected to -/// ? runtime_id is your own runtime, to handle visualisation etc -/// ? user are you ... -struct ClientReactor { - server: Option, - broker: BrokerHandle, - id: u64, - args: Vec, -} - -impl ClientReactor { - fn params(self) -> CoreParams { - let mut params = CoreParams::new(self); - params.handler(initialize::Owned, CtxHandler::new(Self::initialize)); - params.handler(actor_joined::Owned, CtxHandler::new(Self::open_host)); - return params; - } - - // reactor setup - fn initialize( - &mut self, - handle: &mut ReactorHandle, - _: initialize::Reader, - ) -> Result<()> { - // open link with runtime, for communicating with chat GUI - let runtime_link = RuntimeLink::params(handle.id().clone()); - handle.open_link(runtime_link)?; - - let bot = BotReactor::new(self.broker.clone(), handle.id().clone(), self.args.clone()); - let bot_id = handle.spawn(bot.params(), "Bot Driver")?; - - handle.open_link(BotLink::params(bot_id))?; - - return Ok(()); - } - - fn open_host( - &mut self, - handle: &mut ReactorHandle, - r: actor_joined::Reader, - ) -> Result<()> { - let id = r.get_id()?; - - if let Some(server) = &self.server { - handle.open_link(HostLink::params(ReactorId::from(id)))?; - self.broker.register_as(id.into(), server.clone(), "Server"); - - // Fake bot msg - let mut chat_message = MsgBuffer::::new(); - chat_message.build(|b| { - b.set_message(b""); - }); - handle.send_internal(chat_message)?; - } else { - handle.open_link(ServerLink::params(id.into()))?; - self.server = Some(id.into()); - - let mut identify = MsgBuffer::::new(); - identify.build(|b| { - b.set_key(self.id); - }); - handle.send_internal(identify).display(); - } - - Ok(()) - } -} - -impl Drop for ClientReactor { - fn drop(&mut self) { - println!("Client reactor dropped"); - } -} - -// Handler for the connection with the chat server -struct ServerLink; -impl ServerLink { - fn params(foreign_id: ReactorId) -> LinkParams { - let mut params = LinkParams::new(foreign_id, Self); - - params.external_handler( - terminate_stream::Owned, - CtxHandler::new(Self::close_handler), - ); - params.external_handler(actor_joined::Owned, CtxHandler::new(actor_joined::e_to_i)); - - params.internal_handler(identify::Owned, CtxHandler::new(Self::identify)); - - return params; - } - - fn identify(&mut self, handle: &mut LinkHandle, id: identify::Reader) -> Result<()> { - let id = id.get_key(); - - let mut chat_message = MsgBuffer::::new(); - chat_message.build(|b| { - b.set_key(id); - }); - - handle.send_message(chat_message).display(); - Ok(()) - } - - fn close_handler( - &mut self, - handle: &mut LinkHandle, - _: terminate_stream::Reader, - ) -> Result<()> { - // also close our end of the stream - handle.close_link()?; - return Ok(()); - } -} - -struct HostLink; -impl HostLink { - fn params(remote_id: ReactorId) -> LinkParams { - let mut params = LinkParams::new(remote_id, HostLink); - - params.external_handler( - host_message::Owned, - CtxHandler::new(Self::receive_host_message), - ); - - params.internal_handler(bot_return::Owned, CtxHandler::new(Self::send_chat_message)); - - params.external_handler(client_kicked::Owned, CtxHandler::new(client_kicked::e_to_i)); - - return params; - } - - // pick up a 'send_message' event from the reactor, and put it to effect - // by constructing the chat message and sending it to the chat server. - fn send_chat_message( - &mut self, - handle: &mut LinkHandle, - send_message: bot_return::Reader, - ) -> Result<()> { - let message = send_message.get_message()?; - - println!("Our bot sent"); - println!("{}", str::from_utf8(&message).unwrap()); - - let mut chat_message = MsgBuffer::::new(); - chat_message.build(|b| { - b.set_data(message); - }); - - handle.send_message(chat_message)?; - - return Ok(()); - } - - // receive a chat message from the chat server, and broadcast it on the - // reactor. - fn receive_host_message( - &mut self, - handle: &mut LinkHandle, - host_message: host_message::Reader, - ) -> Result<()> { - let message = host_message.get_data()?; - - let message: types::ServerMessage = serde_json::from_slice(message).unwrap(); - - println!(""); - match message { - types::ServerMessage::GameState(state) => { - // println!("New game state"); - let mut bot_msg = MsgBuffer::::new(); - - bot_msg.build(|b| { - b.set_input(&serde_json::to_vec(&state).unwrap()); - }); - handle.send_internal(bot_msg).display(); - } - types::ServerMessage::FinalState(state) => { - println!("Game finished with"); - println!("{:?}", state); - process::exit(0); - } - types::ServerMessage::PlayerAction(action) => { - println!("Out bot did"); - println!("{:?}", action); - } - } - - return Ok(()); - } -} - -struct BotLink; -impl BotLink { - fn params(foreign_id: ReactorId) -> LinkParams { - let mut params = LinkParams::new(foreign_id, Self); - - params.external_handler(bot_return::Owned, CtxHandler::new(bot_return::e_to_i)); - params.internal_handler(bot_input::Owned, CtxHandler::new(bot_input::i_to_e)); - params.internal_handler(client_kicked::Owned, CtxHandler::new(Self::close)); - return params; - } - - // pick up a 'send_message' event from the reactor, and put it to effect - // by constructing the chat message and sending it to the chat server. - fn close( - &mut self, - handle: &mut LinkHandle, - _: client_kicked::Reader, - ) -> Result<()> { - handle.close_link()?; - - return Ok(()); - } -} - -struct RuntimeLink; -impl RuntimeLink { - fn params(foreign_id: ReactorId) -> LinkParams { - let mut params = LinkParams::new(foreign_id, Self); - - params.external_handler(actor_joined::Owned, CtxHandler::new(actor_joined::e_to_i)); - params.internal_handler(client_kicked::Owned, CtxHandler::new(Self::close)); - return params; - } - - // pick up a 'send_message' event from the reactor, and put it to effect - // by constructing the chat message and sending it to the chat server. - fn close( - &mut self, - handle: &mut LinkHandle, - _: client_kicked::Reader, - ) -> Result<()> { - handle.close_link()?; - - return Ok(()); - } -} diff --git a/client/src/types.rs b/client/src/types.rs deleted file mode 100644 index fd580b3..0000000 --- a/client/src/types.rs +++ /dev/null @@ -1,69 +0,0 @@ -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -#[serde(tag = "type", content = "content")] -pub enum ServerMessage { - /// Game state in current turn - GameState(State), - /// The action that was performed - PlayerAction(PlayerAction), - /// The game is over, and this is the concluding state. - FinalState(State), -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -#[serde(tag = "type", content = "value")] -pub enum PlayerAction { - Timeout, - ParseError(String), - Commands(Vec), -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct PlayerCommand { - pub command: Command, - #[serde(skip_serializing_if = "Option::is_none")] - pub error: Option, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub enum CommandError { - NotEnoughShips, - OriginNotOwned, - ZeroShipMove, - OriginDoesNotExist, - DestinationDoesNotExist, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Command { - pub origin: String, - pub destination: String, - pub ship_count: u64, -} - - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct State { - pub planets: Vec, - pub expeditions: Vec, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Expedition { - pub id: u64, - pub ship_count: u64, - pub origin: String, - pub destination: String, - pub owner: usize, - pub turns_remaining: u64, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Planet { - pub ship_count: u64, - pub x: f64, - pub y: f64, - pub owner: Option, - pub name: String, -}