From d279ee0569fbdb5c7fd97640591f4c2c5cf307e7 Mon Sep 17 00:00:00 2001 From: ThePerkinrex Date: Sun, 20 Dec 2020 17:08:14 +0100 Subject: [PATCH] Update game status --- README.md | 2 +- protobuf/game.proto | 9 +++++---- server/src/db.rs | 11 +++++++++++ server/src/games/run.rs | 2 +- server/src/server/game.rs | 7 +++++++ server/src/server/grpc/game.rs | 6 +++++- 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 61936b9..35a5ecf 100644 --- a/README.md +++ b/README.md @@ -110,4 +110,4 @@ See for example the `shuffle` function, which is provided by the environment. Go to the [rhai book](https://schungx.github.io/rhai/language/index.html) for the language reference. ### Common errors: -* When taking more cards than are available from a pile, `()` (null or undefined) is returned, that happens for example when too many people try to play a game where a specific number of cards from a deck are give to each player, and there are too many players for the amount of cards in the deck +* When taking more cards than are available from a pile, `()` (null or undefined) is returned, that happens for example when too many people try to play a game where a specific number of cards from a deck are given to each player, and there are too many players for the amount of cards in the deck diff --git a/protobuf/game.proto b/protobuf/game.proto index 670092c..d5267fc 100644 --- a/protobuf/game.proto +++ b/protobuf/game.proto @@ -1,7 +1,7 @@ syntax = "proto3"; import "google/protobuf/empty.proto"; -// import "common.proto"; +import "common.proto"; package game; @@ -54,7 +54,8 @@ message MessageStatus { map piles = 1; } - Piles commonPiles = 1; - repeated Piles playerPiles = 2; - uint32 currentTurn = 3; + Piles commonPiles = 1; // {a: [""], b:[""]} + repeated Piles playerPiles = 2; // [{...}, {...}] + repeated common.Name names = 3; + uint32 currentTurn = 4; } \ No newline at end of file diff --git a/server/src/db.rs b/server/src/db.rs index 39f10d8..51046b5 100644 --- a/server/src/db.rs +++ b/server/src/db.rs @@ -96,6 +96,17 @@ impl Db { .unwrap() } + // FIXME: return Results intead of crashing + pub fn get_name_for_uuid(&mut self, uuid: Uuid) -> String { + let mut prepared = self.conn.prepare_cached("SELECT Name FROM Users WHERE UUID = ?").unwrap(); + prepared + .query(params![uuid.as_bytes().to_vec()]) + .and_then(|mut r| { + r.next().and_then(|r| r.unwrap().get::<_, String>(0)) + }) + .unwrap() + } + // FIXME: return Results intead of crashing pub fn create_lobby(&mut self, public: bool) -> u32 { let id = rand::random(); diff --git a/server/src/games/run.rs b/server/src/games/run.rs index 94b2ade..436914d 100644 --- a/server/src/games/run.rs +++ b/server/src/games/run.rs @@ -24,7 +24,7 @@ pub struct RunningGame { functions: Functions, current_player: Player, data: HashMap, - players: HashMap, + pub players: HashMap, } // TODO add errors diff --git a/server/src/server/game.rs b/server/src/server/game.rs index d28d484..cd9c50c 100644 --- a/server/src/server/game.rs +++ b/server/src/server/game.rs @@ -139,6 +139,12 @@ impl game_server::Game for GameService { } }; let game = &game_lock.1; + let mut names = vec![]; + for (uuid, id) in &game.players { + names.push((conn.get_name_for_uuid(uuid.clone()).await, *id)) + } + names.sort_by(|(_, a), (_, b)| a.cmp(b)); + let names = names.into_iter().map(|(x, _)| super::grpc::common::Name {name: x}).collect(); let status = MessageStatus { current_turn: game.get_current_player(), common_piles: Some(Piles { @@ -164,6 +170,7 @@ impl game_server::Game for GameService { .collect(), }), player_piles: vec![], + names, }; Ok(Response::new(status)) } diff --git a/server/src/server/grpc/game.rs b/server/src/server/grpc/game.rs index c6f0d7e..d8b53c7 100644 --- a/server/src/server/grpc/game.rs +++ b/server/src/server/grpc/game.rs @@ -51,11 +51,15 @@ pub struct CardId { } #[derive(Clone, PartialEq, ::prost::Message)] pub struct MessageStatus { + /// {a: [""], b:[""]} #[prost(message, optional, tag = "1")] pub common_piles: ::std::option::Option, + /// [{...}, {...}] #[prost(message, repeated, tag = "2")] pub player_piles: ::std::vec::Vec, - #[prost(uint32, tag = "3")] + #[prost(message, repeated, tag = "3")] + pub names: ::std::vec::Vec, + #[prost(uint32, tag = "4")] pub current_turn: u32, } pub mod message_status {