From 73c536b4a6d8cbc788ca4a22ed1c62744f3fa12f Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 20 Jul 2022 23:21:06 +0200 Subject: wrap bot api in oneof for extendability --- planetwars-server/src/modules/bot_api.rs | 42 ++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'planetwars-server/src/modules/bot_api.rs') diff --git a/planetwars-server/src/modules/bot_api.rs b/planetwars-server/src/modules/bot_api.rs index 33f5d87..cb12275 100644 --- a/planetwars-server/src/modules/bot_api.rs +++ b/planetwars-server/src/modules/bot_api.rs @@ -66,12 +66,12 @@ impl PlayerRouter { #[tonic::async_trait] impl pb::bot_api_service_server::BotApiService for BotApiServer { - type ConnectBotStream = UnboundedReceiverStream>; + type ConnectPlayerStream = UnboundedReceiverStream>; - async fn connect_bot( + async fn connect_player( &self, - req: Request>, - ) -> Result, Status> { + req: Request>, + ) -> Result, Status> { // TODO: clean up errors let player_key = req .metadata() @@ -141,8 +141,8 @@ impl pb::bot_api_service_server::BotApiService for BotApiServer { // TODO: please rename me struct SyncThingData { - tx: oneshot::Sender>, - server_messages: mpsc::UnboundedReceiver>, + tx: oneshot::Sender>, + server_messages: mpsc::UnboundedReceiver>, } struct RemoteBotSpec { @@ -199,29 +199,41 @@ impl runner::BotSpec for RemoteBotSpec { async fn handle_bot_messages( player_id: u32, event_bus: Arc>, - mut messages: Streaming, + mut messages: Streaming, ) { + // TODO: can this be writte nmore nicely? while let Some(message) = messages.message().await.unwrap() { - let request_id = (player_id, message.request_id as u32); - event_bus - .lock() - .unwrap() - .resolve_request(request_id, Ok(message.content)); + match message.client_message { + Some(pb::client_message::ClientMessage::RequestResponse(resp)) => { + let request_id = (player_id, resp.request_id as u32); + event_bus + .lock() + .unwrap() + .resolve_request(request_id, Ok(resp.content)); + } + _ => (), + } } } struct RemoteBotHandle { - sender: mpsc::UnboundedSender>, + sender: mpsc::UnboundedSender>, player_id: u32, event_bus: Arc>, } impl PlayerHandle for RemoteBotHandle { fn send_request(&mut self, r: RequestMessage) { - let res = self.sender.send(Ok(pb::PlayerRequest { + let req = pb::PlayerRequest { request_id: r.request_id as i32, content: r.content, - })); + }; + + let server_message = pb::ServerMessage { + server_message: Some(pb::server_message::ServerMessage::PlayerRequest(req)), + }; + + let res = self.sender.send(Ok(server_message)); match res { Ok(()) => { // schedule a timeout. See comments at method implementation -- cgit v1.2.3