From 97b7900a99ed3d3331738d8eab53ad1bed233dbf Mon Sep 17 00:00:00 2001 From: Derek Jamison Date: Mon, 6 Mar 2023 21:41:32 -0500 Subject: [PATCH] Remove game when another Flipper does a join. --- subghz/plugins/rock_paper_scissors/README.md | 2 +- .../rock_paper_scissors/rock_paper_scissors.c | 51 +++++++++++++++++++ .../rock_paper_scissors/rock_paper_scissors.h | 1 + 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/subghz/plugins/rock_paper_scissors/README.md b/subghz/plugins/rock_paper_scissors/README.md index 37e447f..d21038d 100644 --- a/subghz/plugins/rock_paper_scissors/README.md +++ b/subghz/plugins/rock_paper_scissors/README.md @@ -20,10 +20,10 @@ Completed work: - Receiving a Join game does an ACK (to cause game on joiner to start). - Log game results & contact info onto SD card. - Allow viewing past games/scores. +- A join ACK removes it from the list of available games. Remaining work (for subghz version): -- A join ACK removes it from the list of available games. - Config - Allow changing hard-coded CONTACT_INFO message. - Refactor the code, so it has less duplication. - Write tutorial. diff --git a/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c b/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c index 7899a59..9e018b5 100644 --- a/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c +++ b/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c @@ -1247,6 +1247,54 @@ static void remote_games_add(GameContext* game_context, GameEvent* game_event) { furi_mutex_release(game_context->mutex); } +static void remote_games_remove(GameContext* game_context, GameEvent* game_event) { + furi_assert(game_context); + furi_assert(game_event); + + FURI_LOG_I(TAG, "Removing game number %d.", game_event->game_number); + + GameInfo* game = remote_games_find(game_context, game_event->game_number + 1); + if(game && game->game_number == game_event->game_number) { + // We found the game, so remove it from the list. + FURI_LOG_I(TAG, "Found game to remove."); + + // Check to see if the currently selected game is this game. + if(game_context->data->remote_selected_game == game) { + if(game_context->data->remote_selected_game->next_game) { + // Move to next available game. + game_context->data->remote_selected_game = + game_context->data->remote_selected_game->next_game; + } else { + // Move to first game. + game_context->data->remote_selected_game = game_context->data->remote_games; + } + + if(game_context->data->remote_selected_game == game) { + game_context->data->remote_selected_game = NULL; + FURI_LOG_I(TAG, "Clearing remote_selected_game."); + } + } + + // Remove the game from the list. + GameInfo* game_previous = remote_games_find(game_context, game->game_number); + if(!game_previous) { + game_context->data->remote_games = game->next_game; + if(game->sender_name) { + furi_string_free(game->sender_name); + } + free(game); + } else { + game_previous->next_game = game->next_game; + if(game->sender_name) { + furi_string_free(game->sender_name); + } + free(game); + } + + FURI_LOG_I(TAG, "Done removing game."); + } +} + // Saves a game result to the file system. // @param game_context pointer to a GameContext. static void save_result(GameContext* game_context) { @@ -1868,12 +1916,15 @@ int32_t rock_paper_scissors_app(void* p) { // Take ownership of the name and contact event.sender_name = NULL; event.sender_contact = NULL; + } else { + remote_games_remove(game_context, &event); } } else { FURI_LOG_T( TAG, "Remote joining another Flipper on game %03u.", event.game_number); + remote_games_remove(game_context, &event); } furi_mutex_release(game_context->mutex); } else { diff --git a/subghz/plugins/rock_paper_scissors/rock_paper_scissors.h b/subghz/plugins/rock_paper_scissors/rock_paper_scissors.h index 08388b7..e36c2ea 100644 --- a/subghz/plugins/rock_paper_scissors/rock_paper_scissors.h +++ b/subghz/plugins/rock_paper_scissors/rock_paper_scissors.h @@ -419,6 +419,7 @@ static bool remote_games_has_previous(GameContext* game_context); static void remote_games_next(GameContext* game_context); static void remote_games_previous(GameContext* game_context); static void remote_games_add(GameContext* game_context, GameEvent* game_event); +static void remote_games_remove(GameContext* game_context, GameEvent* game_event); // Saves a game result to the file system. // @param game_context pointer to a GameContext.