@@ -58,7 +73,15 @@ pub fn Game() -> impl IntoView {
.map(|card| {
view! {
-
{card}
+
{&card.text}
+
}
})
diff --git a/lib/src/lib.rs b/lib/src/lib.rs
index 5e6f3dd..b476e15 100644
--- a/lib/src/lib.rs
+++ b/lib/src/lib.rs
@@ -6,6 +6,20 @@ pub struct GameJoinRequest {
pub id: String,
}
+/// Player Move Request
+#[derive(Clone, Debug, Serialize, Deserialize)]
+pub struct PlayerMoveRequest {
+ pub game_id: String,
+ pub card_id: String,
+}
+
+/// White Card Meta
+#[derive(Clone, Debug, Serialize, Deserialize)]
+pub struct WhiteCardMeta {
+ pub uuid: String,
+ pub text: String,
+}
+
/// Game meta
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GameStateMeta {
@@ -15,7 +29,7 @@ pub struct GameStateMeta {
pub players: Vec
,
pub czar: String,
pub black: (String, u8),
- pub white: Vec,
+ pub white: Vec,
pub packs: Vec,
}
diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs
index 2241781..1d6e4b2 100644
--- a/server/src/game_handler.rs
+++ b/server/src/game_handler.rs
@@ -25,6 +25,11 @@ pub enum GameHandlerMessage {
addr: SocketAddr,
id: String,
},
+ MoveRequest {
+ card_id: String,
+ game_id: String,
+ addr: SocketAddr,
+ },
}
/// Handles game stuff
@@ -42,11 +47,62 @@ impl GameHandler {
/// Handles incoming messages
pub async fn handle(&self, message: GameHandlerMessage) {
match message {
- NewGame { addr, new_game } => self.new_game(addr, new_game).await,
+ NewGame { addr, new_game } => self.create_new_game(addr, new_game).await,
JoinGame { addr, id } => self.join_game(addr, id).await,
+ MoveRequest {
+ card_id,
+ game_id,
+ addr,
+ } => self.handle_player_move(card_id, game_id, addr).await,
}
}
+ /// Process player move request
+ async fn handle_player_move(&self, card_id: String, game_id: String, addr: SocketAddr) {
+ let this_player_id = self
+ .state
+ .online_users
+ .read()
+ .unwrap()
+ .get(&addr)
+ .unwrap()
+ .read()
+ .unwrap()
+ .uuid
+ .to_string();
+
+ let this_game = self
+ .state
+ .games
+ .read()
+ .unwrap()
+ .get(&game_id)
+ .unwrap()
+ .clone();
+
+ if this_game
+ .read()
+ .unwrap()
+ .czar
+ .read()
+ .unwrap()
+ .uuid
+ .to_string()
+ == this_player_id
+ {
+ tracing::error!("No! User id is same as current czar");
+ } else {
+ tracing::error!("Ok, but i have nothing to do");
+ }
+
+ tracing::debug!(
+ "Player move received:\nCard: {}\nGame: {}\nPlayer: {}",
+ card_id,
+ game_id,
+ this_player_id,
+ );
+ }
+
/// Puts a user in a game
async fn join_game(&self, addr: SocketAddr, id: String) {
// Get pointers
@@ -82,7 +138,14 @@ impl GameHandler {
this_game.read().unwrap().current_black.text.clone(),
this_game.read().unwrap().current_black.pick,
),
- white: player.white.iter().map(|card| card.text.clone()).collect(),
+ white: player
+ .white
+ .iter()
+ .map(|card| WhiteCardMeta {
+ uuid: card.uuid.to_string(),
+ text: card.text.clone(),
+ })
+ .collect(),
packs: this_game.read().unwrap().packs.clone(),
};
@@ -106,7 +169,7 @@ impl GameHandler {
}
/// Creates a new game
- async fn new_game(&self, addr: SocketAddr, new_game: NewGameRequest) {
+ async fn create_new_game(&self, addr: SocketAddr, new_game: NewGameRequest) {
if new_game.packs.is_empty() {
tracing::error!("New game cards are empty!");
return;
@@ -148,7 +211,7 @@ impl GameHandler {
.iter()
.map(|player| {
self.state
- .user_uuid
+ .users_by_id
.read()
.unwrap()
.get(player.0)
@@ -170,7 +233,10 @@ impl GameHandler {
.unwrap()
.white
.iter()
- .map(|card| card.text.clone())
+ .map(|card| WhiteCardMeta {
+ uuid: card.uuid.to_string(),
+ text: card.text.clone(),
+ })
.collect(),
packs: new_game_object.packs.clone(),
};
@@ -301,6 +367,8 @@ pub struct Game {
pub host: Arc>,
/// White draw pile
pub white: Vec>,
+ /// Current card czar
+ pub czar: Arc>,
/// Black draw pile
black: Vec>,
pub players: HashMap,
@@ -349,6 +417,7 @@ impl Game {
uuid: Uuid::now_v7(),
name: request.name,
host: request.host.clone(),
+ czar: request.host.clone(),
white,
black,
players: HashMap::new(),
diff --git a/server/src/lib.rs b/server/src/lib.rs
index e230e65..f1c7d7e 100644
--- a/server/src/lib.rs
+++ b/server/src/lib.rs
@@ -67,7 +67,7 @@ pub struct AppState {
pub first_names: Vec,
pub last_names: Vec,
pub reserved_names: RwLock>,
- pub user_uuid: RwLock>>>,
+ pub users_by_id: RwLock>>>,
pub online_users: RwLock>>>,
pub offline_users: RwLock>>>,
pub packs: CardPacks,
diff --git a/server/src/main.rs b/server/src/main.rs
index ef516df..4848368 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -35,7 +35,7 @@ async fn main() -> Result<()> {
let first_names = load_names("data/first.txt")?;
let last_names = load_names("data/last.txt")?;
let reserved_names = RwLock::new(HashSet::::new());
- let user_uuid = RwLock::new(HashMap::>>::new());
+ let users_by_id = RwLock::new(HashMap::>>::new());
let online_users = RwLock::new(HashMap::>>::new());
let offline_users = RwLock::new(HashMap::>>::new());
let (packs, packs_meta) = load_cards_from_json("data/cah-cards-full.json")?;
@@ -49,7 +49,7 @@ async fn main() -> Result<()> {
first_names,
last_names,
reserved_names,
- user_uuid,
+ users_by_id,
online_users,
offline_users,
packs,
diff --git a/server/src/message_handler.rs b/server/src/message_handler.rs
index b4de9cb..c9e6b9e 100644
--- a/server/src/message_handler.rs
+++ b/server/src/message_handler.rs
@@ -1,6 +1,7 @@
use crate::user_handler::*;
use crate::AppState;
-use crate::GameHandlerMessage;
+use crate::GameHandlerMessage::*;
+use crate::UserHandlerMessage::*;
use axum::extract::ws::{CloseFrame, Message};
use lib::*;
use serde_json::{from_str, to_string};
@@ -37,7 +38,7 @@ impl MessageHandler {
{
self.state
.users_tx
- .send(UserHandlerMessage::UserLogIn {
+ .send(UserLogIn {
username: user_log_in.username,
addr,
})
@@ -48,7 +49,7 @@ impl MessageHandler {
_new_game_request if let Ok(new_game) = from_str::(&text) => {
self.state
.games_tx
- .send(GameHandlerMessage::NewGame { addr, new_game })
+ .send(NewGame { addr, new_game })
.await
.unwrap();
}
@@ -56,7 +57,7 @@ impl MessageHandler {
_join_game_request if let Ok(join_request) = from_str::(&text) => {
self.state
.games_tx
- .send(GameHandlerMessage::JoinGame {
+ .send(JoinGame {
addr,
id: join_request.id,
})
@@ -64,6 +65,29 @@ impl MessageHandler {
.unwrap();
}
+ _player_move_request
+ if let Ok(move_request) = from_str::(&text) =>
+ {
+ if move_request.card_id == "".to_string() {
+ tracing::error!("Move request card_id is empty! Ignoring...");
+ return;
+ }
+ if move_request.game_id == "".to_string() {
+ tracing::error!("Move request game_id is empty! Ignoring...");
+ return;
+ } else {
+ self.state
+ .games_tx
+ .send(MoveRequest {
+ card_id: move_request.card_id,
+ game_id: move_request.game_id,
+ addr,
+ })
+ .await
+ .unwrap();
+ }
+ }
+
_ => tracing::debug!("Unhandled text from {}", addr),
},
diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs
index df5d5f3..bff23a8 100644
--- a/server/src/user_handler.rs
+++ b/server/src/user_handler.rs
@@ -88,7 +88,7 @@ impl UserHandler {
// Register uuid
self.state
- .user_uuid
+ .users_by_id
.write()
.unwrap()
.insert(new_user.read().unwrap().uuid.clone(), new_user.clone());