Просмотр исходного кода

re-enter the user if they're already in the game

jblanked 8 месяцев назад
Родитель
Сommit
d60bbff88d
6 измененных файлов с 62 добавлено и 65 удалено
  1. 18 12
      callback/callback.c
  2. 36 3
      callback/game.c
  3. 3 2
      callback/game.h
  4. 2 1
      game/game.c
  5. 0 37
      game/storage.c
  6. 3 10
      game/world.c

+ 18 - 12
callback/callback.c

@@ -505,7 +505,7 @@ void callback_submenu_lobby_pvp_choices(void *context, uint32_t index)
     /* Handle other game lobbies
     /* Handle other game lobbies
              1. when clicked on, send request to fetch the selected game lobby details
              1. when clicked on, send request to fetch the selected game lobby details
              2. start the websocket session
              2. start the websocket session
-             3. start the game thread (the rest will be handled by game_start and player_update)
+             3. start the game thread (the rest will be handled by game_start_game and player_update)
              */
              */
     FlipWorldApp *app = (FlipWorldApp *)context;
     FlipWorldApp *app = (FlipWorldApp *)context;
     furi_check(app, "FlipWorldApp is NULL");
     furi_check(app, "FlipWorldApp is NULL");
@@ -570,11 +570,14 @@ void callback_submenu_lobby_pvp_choices(void *context, uint32_t index)
                 // check if the user is in the lobby
                 // check if the user is in the lobby
                 if (game_in_lobby(fhttp, lobby))
                 if (game_in_lobby(fhttp, lobby))
                 {
                 {
-                    FURI_LOG_I(TAG, "User is in the lobby");
-                    easy_flipper_dialog("Error", "You are already in the lobby. Press BACK to return.");
-                    flipper_http_free(fhttp);
-                    furi_string_free(lobby);
-                    return;
+                    if (!game_remove_from_lobby(fhttp))
+                    {
+                        FURI_LOG_I(TAG, "User is in the lobby but failed to remove");
+                        easy_flipper_dialog("Error", "You're already in the lobby.\nContact JBlanked.\n\nPress BACK to return.");
+                        flipper_http_free(fhttp);
+                        furi_string_free(lobby);
+                        return;
+                    }
                 }
                 }
                 // add the user to the lobby
                 // add the user to the lobby
                 if (!game_join_lobby(fhttp, lobby_list[lobby_index]))
                 if (!game_join_lobby(fhttp, lobby_list[lobby_index]))
@@ -600,11 +603,14 @@ void callback_submenu_lobby_pvp_choices(void *context, uint32_t index)
             // check if the user is in the lobby
             // check if the user is in the lobby
             if (game_in_lobby(fhttp, lobby))
             if (game_in_lobby(fhttp, lobby))
             {
             {
-                FURI_LOG_I(TAG, "User is in the lobby");
-                easy_flipper_dialog("Error", "You are already in the lobby. Press BACK to return.");
-                flipper_http_free(fhttp);
-                furi_string_free(lobby);
-                return;
+                if (!game_remove_from_lobby(fhttp))
+                {
+                    FURI_LOG_I(TAG, "User is in the lobby but failed to remove");
+                    easy_flipper_dialog("Error", "You're already in the lobby.\nContact JBlanked.\n\nPress BACK to return.");
+                    flipper_http_free(fhttp);
+                    furi_string_free(lobby);
+                    return;
+                }
             }
             }
             // add the user to the lobby
             // add the user to the lobby
             if (!game_join_lobby(fhttp, lobby_list[lobby_index]))
             if (!game_join_lobby(fhttp, lobby_list[lobby_index]))
@@ -617,6 +623,6 @@ void callback_submenu_lobby_pvp_choices(void *context, uint32_t index)
             }
             }
         }
         }
 
 
-        game_start(fhttp, lobby, app); // this will free both the fhttp and lobby, and start the game
+        game_start_game(fhttp, lobby, app); // this will free both the fhttp and lobby, and start the game
     }
     }
 }
 }

+ 36 - 3
callback/game.c

@@ -126,7 +126,7 @@ static int32_t game_waiting_app_callback(void *p)
     }
     }
     // if we reach here, it means we timed out or the user hit back
     // if we reach here, it means we timed out or the user hit back
     FURI_LOG_E(TAG, "No players joined within the timeout or user hit back");
     FURI_LOG_E(TAG, "No players joined within the timeout or user hit back");
-    remove_player_from_lobby(fhttp);
+    game_remove_from_lobby(fhttp);
     flipper_http_free(fhttp);
     flipper_http_free(fhttp);
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenuOther);
     return 0;
     return 0;
@@ -1036,7 +1036,7 @@ bool game_in_lobby(FlipperHTTP *fhttp, FuriString *lobby)
 }
 }
 
 
 // this will free both the fhttp and lobby
 // this will free both the fhttp and lobby
-void game_start(FlipperHTTP *fhttp, FuriString *lobby, void *context)
+void game_start_game(FlipperHTTP *fhttp, FuriString *lobby, void *context)
 {
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     FlipWorldApp *app = (FlipWorldApp *)context;
     furi_check(app, "FlipWorldApp is NULL");
     furi_check(app, "FlipWorldApp is NULL");
@@ -1112,7 +1112,7 @@ void game_waiting_process(FlipperHTTP *fhttp, void *context)
     if (count == 2)
     if (count == 2)
     {
     {
         // break out of this and start the game
         // break out of this and start the game
-        game_start(fhttp, lobby, app); // this will free both the fhttp and lobby
+        game_start_game(fhttp, lobby, app); // this will free both the fhttp and lobby
         return;
         return;
     }
     }
     furi_string_free(lobby);
     furi_string_free(lobby);
@@ -1139,3 +1139,36 @@ void game_waiting_lobby(void *context)
     // finally, switch to the waiting lobby view
     // finally, switch to the waiting lobby view
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
 };
 };
+
+bool game_remove_from_lobby(FlipperHTTP *fhttp)
+{
+    if (!fhttp)
+    {
+        FURI_LOG_E(TAG, "FlipperHTTP is NULL");
+        return false;
+    }
+    char username[32];
+    if (!load_char("Flip-Social-Username", username, sizeof(username)))
+    {
+        FURI_LOG_E(TAG, "Failed to load data/Flip-Social-Username");
+        return false;
+    }
+    char lobby_type[4];
+    snprintf(lobby_type, sizeof(lobby_type), game_mode_index == 0 ? "pve" : "pvp");
+    char url[128];
+    char payload[128];
+    snprintf(payload, sizeof(payload), "{\"username\":\"%s\", \"game_id\":\"%s\"}", username, game_ws_lobby_name);
+    snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/%s/lobby/remove/", lobby_type);
+    fhttp->state = IDLE;
+    if (!flipper_http_request(fhttp, POST, url, "{\"Content-Type\":\"application/json\"}", payload))
+    {
+        FURI_LOG_E(TAG, "Failed to remove player from lobby");
+        return false;
+    }
+    fhttp->state = RECEIVING;
+    while (fhttp->state != IDLE)
+    {
+        furi_delay_ms(100);
+    }
+    return true;
+}

+ 3 - 2
callback/game.h

@@ -15,6 +15,7 @@ bool game_fetch_lobby(FlipperHTTP *fhttp, const char *lobby_name);
 bool game_join_lobby(FlipperHTTP *fhttp, const char *lobby_name);
 bool game_join_lobby(FlipperHTTP *fhttp, const char *lobby_name);
 size_t game_lobby_count(FlipperHTTP *fhttp, FuriString *lobby);
 size_t game_lobby_count(FlipperHTTP *fhttp, FuriString *lobby);
 bool game_in_lobby(FlipperHTTP *fhttp, FuriString *lobby);
 bool game_in_lobby(FlipperHTTP *fhttp, FuriString *lobby);
-void game_start(FlipperHTTP *fhttp, FuriString *lobby, void *context);
+void game_start_game(FlipperHTTP *fhttp, FuriString *lobby, void *context);
 void game_waiting_lobby(void *context);
 void game_waiting_lobby(void *context);
-void game_waiting_process(FlipperHTTP *fhttp, void *context);
+void game_waiting_process(FlipperHTTP *fhttp, void *context);
+bool game_remove_from_lobby(FlipperHTTP *fhttp);

+ 2 - 1
game/game.c

@@ -2,6 +2,7 @@
 #include <game/game.h>
 #include <game/game.h>
 #include <game/storage.h>
 #include <game/storage.h>
 #include <alloc/alloc.h>
 #include <alloc/alloc.h>
+#include <callback/game.h>
 
 
 // very simple tutorial check
 // very simple tutorial check
 static bool game_tutorial_done(GameContext *game_context)
 static bool game_tutorial_done(GameContext *game_context)
@@ -177,7 +178,7 @@ static void game_stop(void *ctx)
         if (game_context->fhttp)
         if (game_context->fhttp)
         {
         {
             flipper_http_websocket_stop(game_context->fhttp); // close websocket
             flipper_http_websocket_stop(game_context->fhttp); // close websocket
-            remove_player_from_lobby(game_context->fhttp);    // remove player from lobby
+            game_remove_from_lobby(game_context->fhttp);      // remove player from lobby
             flipper_http_free(game_context->fhttp);
             flipper_http_free(game_context->fhttp);
         }
         }
     }
     }

+ 0 - 37
game/storage.c

@@ -499,43 +499,6 @@ bool websocket_player_context(PlayerContext *player_context, FlipperHTTP *fhttp)
     return true;
     return true;
 }
 }
 
 
-bool remove_player_from_lobby(FlipperHTTP *fhttp)
-{
-    if (!fhttp)
-    {
-        FURI_LOG_E(TAG, "FlipperHTTP is NULL");
-        return false;
-    }
-    char username[32];
-    if (!load_char("Flip-Social-Username", username, sizeof(username)))
-    {
-        FURI_LOG_E(TAG, "Failed to load data/Flip-Social-Username");
-        return false;
-    }
-    char lobby_name[32];
-    if (!load_char("pvp_lobby_name", lobby_name, sizeof(lobby_name)))
-    {
-        FURI_LOG_E(TAG, "Failed to load data/pvp_lobby_name");
-        return false;
-    }
-    char url[128];
-    char payload[128];
-    snprintf(payload, sizeof(payload), "{\"username\":\"%s\", \"game_id\":\"%s\"}", username, lobby_name);
-    snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/pvp/lobby/remove/");
-    fhttp->state = IDLE;
-    if (!flipper_http_request(fhttp, POST, url, "{\"Content-Type\":\"application/json\"}", payload))
-    {
-        FURI_LOG_E(TAG, "Failed to remove player from lobby");
-        return false;
-    }
-    fhttp->state = RECEIVING;
-    while (fhttp->state != IDLE)
-    {
-        furi_delay_ms(100);
-    }
-    return true;
-}
-
 // Helper function to load an integer
 // Helper function to load an integer
 static bool load_number(const char *path_name, int *value)
 static bool load_number(const char *path_name, int *value)
 {
 {

+ 3 - 10
game/world.c

@@ -245,7 +245,9 @@ FuriString *world_fetch(FlipperHTTP *fhttp, const char *name)
     char url[256];
     char url[256];
     snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/v8/get/world/%s/", name);
     snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/world/v8/get/world/%s/", name);
     snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
     snprintf(fhttp->file_path, sizeof(fhttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
+
     fhttp->save_received_data = true;
     fhttp->save_received_data = true;
+    fhttp->state = IDLE;
     if (!flipper_http_request(fhttp, GET, url, "{\"Content-Type\": \"application/json\"}", NULL))
     if (!flipper_http_request(fhttp, GET, url, "{\"Content-Type\": \"application/json\"}", NULL))
     {
     {
         FURI_LOG_E("Game", "world_fetch: Failed to send HTTP request");
         FURI_LOG_E("Game", "world_fetch: Failed to send HTTP request");
@@ -253,19 +255,10 @@ FuriString *world_fetch(FlipperHTTP *fhttp, const char *name)
         return NULL;
         return NULL;
     }
     }
     fhttp->state = RECEIVING;
     fhttp->state = RECEIVING;
-    furi_timer_start(fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
-    while (fhttp->state == RECEIVING && furi_timer_is_running(fhttp->get_timeout_timer) > 0)
+    while (fhttp->state != IDLE)
     {
     {
-        // Wait for the request to be received
         furi_delay_ms(100);
         furi_delay_ms(100);
     }
     }
-    furi_timer_stop(fhttp->get_timeout_timer);
-    if (fhttp->state != IDLE)
-    {
-        FURI_LOG_E("Game", "Failed to receive world data");
-
-        return NULL;
-    }
 
 
     FuriString *returned_data = load_furi_world(name);
     FuriString *returned_data = load_furi_world(name);
     if (!returned_data)
     if (!returned_data)