delete games

This commit is contained in:
Adam 2024-08-22 16:15:18 -04:00
parent 98921dbce1
commit 5845e4793b
3 changed files with 37 additions and 17 deletions

View file

@ -97,6 +97,7 @@ pub fn Browser() -> impl IntoView {
</button>
<button
type="button"
value=&game.uuid
on:click=move |e| {
set_delete_id(event_target_value(&e));
}

View file

@ -19,16 +19,11 @@ use uuid::Uuid;
/// For interacting with the game handler
pub enum GameHandlerMessage {
NewGame {
addr: SocketAddr,
new_game: NewGameRequest,
},
JoinGame {
addr: SocketAddr,
id: String,
},
NewGame(NewGameRequest, SocketAddr),
JoinGame(String, SocketAddr),
MoveRequest(PlayerMoveRequest, SocketAddr),
JudgeDecision(JudgeDecisionRequest, SocketAddr),
DeleteGame(GameDeleteRequest),
}
/// Handles game stuff
@ -46,13 +41,32 @@ impl GameHandler {
/// Handles incoming messages
pub async fn handle(&self, message: GameHandlerMessage) {
match message {
NewGame { addr, new_game } => self.create_new_game(addr, new_game).await,
JoinGame { addr, id } => self.join_game(addr, id).await,
NewGame(request, addr) => self.create_new_game(request, addr).await,
JoinGame(request, addr) => self.join_game(request, addr).await,
MoveRequest(request, addr) => self.handle_player_move(request, addr).await,
JudgeDecision(request, addr) => self.handle_judging(request, addr).await,
DeleteGame(request) => self.delete_game(request).await,
}
}
/// Delete game
async fn delete_game(&self, request: GameDeleteRequest) {
// TODO: add auth lol
let _ = self
.state
.games
.write()
.unwrap()
.remove(&request.delete_game_id)
.unwrap();
// Broadcast game browser update
self.state
.broadcast_tx
.send(meta_games_browser_update(&self.state))
.unwrap();
}
/// Process judging
async fn handle_judging(&self, request: JudgeDecisionRequest, addr: SocketAddr) {
// Get pointers
@ -213,7 +227,7 @@ impl GameHandler {
}
/// Puts a user in a game
async fn join_game(&self, addr: SocketAddr, id: String) {
async fn join_game(&self, id: String, addr: SocketAddr) {
// Get pointers
let this_game = self.state.games.read().unwrap().get(&id).unwrap().clone();
let this_user = self
@ -300,7 +314,7 @@ impl GameHandler {
}
/// Creates a new game
async fn create_new_game(&self, addr: SocketAddr, new_game: NewGameRequest) {
async fn create_new_game(&self, new_game: NewGameRequest, addr: SocketAddr) {
if new_game.packs.is_empty() {
tracing::error!("New game cards are empty!");
return;

View file

@ -48,7 +48,7 @@ impl MessageHandler {
_new_game_request if let Ok(new_game) = from_str::<NewGameRequest>(&text) => {
self.state
.games_tx
.send(NewGame { addr, new_game })
.send(NewGame(new_game, addr))
.await
.unwrap();
}
@ -56,10 +56,7 @@ impl MessageHandler {
_join_game_request if let Ok(join_request) = from_str::<GameJoinRequest>(&text) => {
self.state
.games_tx
.send(JoinGame {
addr,
id: join_request.id,
})
.send(JoinGame(join_request.id, addr))
.await
.unwrap();
}
@ -93,6 +90,14 @@ impl MessageHandler {
.unwrap();
}
_delete_game if let Ok(delete_request) = from_str::<GameDeleteRequest>(&text) => {
self.state
.games_tx
.send(DeleteGame(delete_request))
.await
.unwrap();
}
_ => tracing::debug!("Unhandled text from {}", addr),
},