Browse Source

Update game status

new_protocol
ThePerkinrex 5 years ago
parent
commit
d279ee0569
No known key found for this signature in database GPG Key ID: 1F45A7C4BFB41607
  1. 2
      README.md
  2. 9
      protobuf/game.proto
  3. 11
      server/src/db.rs
  4. 2
      server/src/games/run.rs
  5. 7
      server/src/server/game.rs
  6. 6
      server/src/server/grpc/game.rs

2
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

9
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<string, Pile> 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;
}

11
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();

2
server/src/games/run.rs

@ -24,7 +24,7 @@ pub struct RunningGame {
functions: Functions,
current_player: Player,
data: HashMap<String, serde_json::Value>,
players: HashMap<Uuid, u32>,
pub players: HashMap<Uuid, u32>,
}
// TODO add errors

7
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))
}

6
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<message_status::Piles>,
/// [{...}, {...}]
#[prost(message, repeated, tag = "2")]
pub player_piles: ::std::vec::Vec<message_status::Piles>,
#[prost(uint32, tag = "3")]
#[prost(message, repeated, tag = "3")]
pub names: ::std::vec::Vec<super::common::Name>,
#[prost(uint32, tag = "4")]
pub current_turn: u32,
}
pub mod message_status {

Loading…
Cancel
Save