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

cleanup callback/callback and callback/game

jblanked 9 месяцев назад
Родитель
Сommit
288f6f786c
5 измененных файлов с 210 добавлено и 204 удалено
  1. 16 16
      callback/alloc.c
  2. 118 20
      callback/callback.c
  3. 17 16
      callback/callback.h
  4. 50 151
      callback/game.c
  5. 9 1
      callback/game.h

+ 16 - 16
callback/alloc.c

@@ -15,13 +15,13 @@ bool alloc_message_view(void *context, MessageState state)
     switch (state)
     {
     case MessageStateAbout:
-        easy_flipper_set_view(&app->view_message, FlipWorldViewMessage, message_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app);
+        easy_flipper_set_view(&app->view_message, FlipWorldViewMessage, callback_message_draw, NULL, callback_to_submenu, &app->view_dispatcher, app);
         break;
     case MessageStateLoading:
-        easy_flipper_set_view(&app->view_message, FlipWorldViewMessage, message_draw_callback, NULL, NULL, &app->view_dispatcher, app);
+        easy_flipper_set_view(&app->view_message, FlipWorldViewMessage, callback_message_draw, NULL, NULL, &app->view_dispatcher, app);
         break;
     case MessageStateWaitingLobby:
-        easy_flipper_set_view(&app->view_message, FlipWorldViewMessage, message_draw_callback, message_input_callback, NULL, &app->view_dispatcher, app);
+        easy_flipper_set_view(&app->view_message, FlipWorldViewMessage, callback_message_draw, callback_message_input, NULL, &app->view_dispatcher, app);
         break;
     }
     if (!app->view_message)
@@ -67,9 +67,9 @@ bool alloc_text_input_view(void *context, char *title)
                 title,
                 app->text_input_temp_buffer,
                 app->text_input_buffer_size,
-                is_str(title, "SSID") ? updated_wifi_ssid : is_str(title, "Password")     ? updated_wifi_pass
-                                                        : is_str(title, "Username-Login") ? updated_username
-                                                                                          : updated_password,
+                is_str(title, "SSID") ? callback_updated_wifi_ssid : is_str(title, "Password")     ? callback_updated_wifi_pass
+                                                                 : is_str(title, "Username-Login") ? callback_updated_username
+                                                                                                   : callback_updated_password,
                 callback_to_wifi_settings,
                 &app->view_dispatcher,
                 app))
@@ -123,7 +123,7 @@ bool alloc_variable_item_list(void *context, uint32_t view_id)
         switch (view_id)
         {
         case FlipWorldSubmenuIndexWiFiSettings:
-            if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, wifi_settings_select, callback_to_settings, &app->view_dispatcher, app))
+            if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, callback_wifi_settings_select, callback_to_settings, &app->view_dispatcher, app))
             {
                 FURI_LOG_E(TAG, "Failed to allocate variable item list");
                 return false;
@@ -156,7 +156,7 @@ bool alloc_variable_item_list(void *context, uint32_t view_id)
             }
             break;
         case FlipWorldSubmenuIndexGameSettings:
-            if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, game_settings_select, callback_to_settings, &app->view_dispatcher, app))
+            if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, callback_game_settings_select, callback_to_settings, &app->view_dispatcher, app))
             {
                 FURI_LOG_E(TAG, "Failed to allocate variable item list");
                 return false;
@@ -175,43 +175,43 @@ bool alloc_variable_item_list(void *context, uint32_t view_id)
             }
             if (!app->variable_item_game_player_sprite)
             {
-                app->variable_item_game_player_sprite = variable_item_list_add(app->variable_item_list, "Weapon", 4, player_on_change, NULL);
+                app->variable_item_game_player_sprite = variable_item_list_add(app->variable_item_list, "Weapon", 4, callback_player_on_change, NULL);
                 variable_item_set_current_value_index(app->variable_item_game_player_sprite, 1);
                 variable_item_set_current_value_text(app->variable_item_game_player_sprite, player_sprite_choices[1]);
             }
             if (!app->variable_item_game_fps)
             {
-                app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, fps_change, NULL);
+                app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, callback_fps_change, NULL);
                 variable_item_set_current_value_index(app->variable_item_game_fps, 0);
                 variable_item_set_current_value_text(app->variable_item_game_fps, fps_choices_str[0]);
             }
             if (!app->variable_item_game_vgm_x)
             {
-                app->variable_item_game_vgm_x = variable_item_list_add(app->variable_item_list, "VGM Horizontal", 12, vgm_x_change, NULL);
+                app->variable_item_game_vgm_x = variable_item_list_add(app->variable_item_list, "VGM Horizontal", 12, callback_vgm_x_change, NULL);
                 variable_item_set_current_value_index(app->variable_item_game_vgm_x, 2);
                 variable_item_set_current_value_text(app->variable_item_game_vgm_x, vgm_levels[2]);
             }
             if (!app->variable_item_game_vgm_y)
             {
-                app->variable_item_game_vgm_y = variable_item_list_add(app->variable_item_list, "VGM Vertical", 12, vgm_y_change, NULL);
+                app->variable_item_game_vgm_y = variable_item_list_add(app->variable_item_list, "VGM Vertical", 12, callback_vgm_y_change, NULL);
                 variable_item_set_current_value_index(app->variable_item_game_vgm_y, 2);
                 variable_item_set_current_value_text(app->variable_item_game_vgm_y, vgm_levels[2]);
             }
             if (!app->variable_item_game_screen_always_on)
             {
-                app->variable_item_game_screen_always_on = variable_item_list_add(app->variable_item_list, "Keep Screen On?", 2, screen_on_change, NULL);
+                app->variable_item_game_screen_always_on = variable_item_list_add(app->variable_item_list, "Keep Screen On?", 2, callback_screen_on_change, NULL);
                 variable_item_set_current_value_index(app->variable_item_game_screen_always_on, 1);
                 variable_item_set_current_value_text(app->variable_item_game_screen_always_on, yes_or_no_choices[1]);
             }
             if (!app->variable_item_game_sound_on)
             {
-                app->variable_item_game_sound_on = variable_item_list_add(app->variable_item_list, "Sound On?", 2, sound_on_change, NULL);
+                app->variable_item_game_sound_on = variable_item_list_add(app->variable_item_list, "Sound On?", 2, callback_sound_on_change, NULL);
                 variable_item_set_current_value_index(app->variable_item_game_sound_on, 0);
                 variable_item_set_current_value_text(app->variable_item_game_sound_on, yes_or_no_choices[0]);
             }
             if (!app->variable_item_game_vibration_on)
             {
-                app->variable_item_game_vibration_on = variable_item_list_add(app->variable_item_list, "Vibration On?", 2, vibration_on_change, NULL);
+                app->variable_item_game_vibration_on = variable_item_list_add(app->variable_item_list, "Vibration On?", 2, callback_vibration_on_change, NULL);
                 variable_item_set_current_value_index(app->variable_item_game_vibration_on, 0);
                 variable_item_set_current_value_text(app->variable_item_game_vibration_on, yes_or_no_choices[0]);
             }
@@ -303,7 +303,7 @@ bool alloc_variable_item_list(void *context, uint32_t view_id)
             }
             break;
         case FlipWorldSubmenuIndexUserSettings:
-            if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, user_settings_select, callback_to_settings, &app->view_dispatcher, app))
+            if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, callback_user_settings_select, callback_to_settings, &app->view_dispatcher, app))
             {
                 FURI_LOG_E(TAG, "Failed to allocate variable item list");
                 return false;

+ 118 - 20
callback/callback.c

@@ -6,7 +6,7 @@
 #include "alloc/alloc.h"
 #include <flip_storage/storage.h>
 
-bool message_input_callback(InputEvent *event, void *context)
+bool callback_message_input(InputEvent *event, void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     furi_check(app);
@@ -18,7 +18,7 @@ bool message_input_callback(InputEvent *event, void *context)
     return true;
 }
 
-void message_draw_callback(Canvas *canvas, void *model)
+void callback_message_draw(Canvas *canvas, void *model)
 {
     MessageModel *message_model = model;
     canvas_clear(canvas);
@@ -83,15 +83,15 @@ void callback_submenu_choices(void *context, uint32_t index)
         break;
     case FlipWorldSubmenuIndexStory:
         game_mode_index = 2; // GAME_MODE_STORY
-        run(app);
+        game_run(app);
         break;
     case FlipWorldSubmenuIndexPvP:
         game_mode_index = 1; // GAME_MODE_PVP
-        run(app);
+        game_run(app);
         break;
     case FlipWorldSubmenuIndexPvE:
         game_mode_index = 0; // GAME_MODE_PVE
-        run(app);
+        game_run(app);
         break;
     case FlipWorldSubmenuIndexMessage:
         // About menu.
@@ -145,7 +145,7 @@ void callback_submenu_choices(void *context, uint32_t index)
     }
 }
 
-void updated_wifi_ssid(void *context)
+void callback_updated_wifi_ssid(void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     if (!app)
@@ -205,7 +205,7 @@ void updated_wifi_ssid(void *context)
     // switch to the settings view
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
 }
-void updated_wifi_pass(void *context)
+void callback_updated_wifi_pass(void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     if (!app)
@@ -265,7 +265,7 @@ void updated_wifi_pass(void *context)
     // switch to the settings view
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
 }
-void updated_username(void *context)
+void callback_updated_username(void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     if (!app)
@@ -290,7 +290,7 @@ void updated_username(void *context)
     }
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
 }
-void updated_password(void *context)
+void callback_updated_password(void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     if (!app)
@@ -331,7 +331,7 @@ void updated_password(void *context)
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
 }
 
-void wifi_settings_select(void *context, uint32_t index)
+void callback_wifi_settings_select(void *context, uint32_t index)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     if (!app)
@@ -380,7 +380,7 @@ void wifi_settings_select(void *context, uint32_t index)
         break;
     }
 }
-void fps_change(VariableItem *item)
+void callback_fps_change(VariableItem *item)
 {
     uint8_t index = variable_item_get_current_value_index(item);
     fps_index = index;
@@ -388,7 +388,7 @@ void fps_change(VariableItem *item)
     variable_item_set_current_value_index(item, index);
     save_char("Game-FPS", fps_choices_str[index]);
 }
-void screen_on_change(VariableItem *item)
+void callback_screen_on_change(VariableItem *item)
 {
     uint8_t index = variable_item_get_current_value_index(item);
     screen_always_on_index = index;
@@ -396,7 +396,7 @@ void screen_on_change(VariableItem *item)
     variable_item_set_current_value_index(item, index);
     save_char("Game-Screen-Always-On", yes_or_no_choices[index]);
 }
-void sound_on_change(VariableItem *item)
+void callback_sound_on_change(VariableItem *item)
 {
     uint8_t index = variable_item_get_current_value_index(item);
     sound_on_index = index;
@@ -404,7 +404,7 @@ void sound_on_change(VariableItem *item)
     variable_item_set_current_value_index(item, index);
     save_char("Game-Sound-On", yes_or_no_choices[index]);
 }
-void vibration_on_change(VariableItem *item)
+void callback_vibration_on_change(VariableItem *item)
 {
     uint8_t index = variable_item_get_current_value_index(item);
     vibration_on_index = index;
@@ -412,7 +412,7 @@ void vibration_on_change(VariableItem *item)
     variable_item_set_current_value_index(item, index);
     save_char("Game-Vibration-On", yes_or_no_choices[index]);
 }
-void player_on_change(VariableItem *item)
+void callback_player_on_change(VariableItem *item)
 {
     uint8_t index = variable_item_get_current_value_index(item);
     player_sprite_index = index;
@@ -420,7 +420,7 @@ void player_on_change(VariableItem *item)
     variable_item_set_current_value_index(item, index);
     save_char("Game-Player-Sprite", player_sprite_choices[index]);
 }
-void vgm_x_change(VariableItem *item)
+void callback_vgm_x_change(VariableItem *item)
 {
     uint8_t index = variable_item_get_current_value_index(item);
     vgm_x_index = index;
@@ -428,7 +428,7 @@ void vgm_x_change(VariableItem *item)
     variable_item_set_current_value_index(item, index);
     save_char("Game-VGM-X", vgm_levels[index]);
 }
-void vgm_y_change(VariableItem *item)
+void callback_vgm_y_change(VariableItem *item)
 {
     uint8_t index = variable_item_get_current_value_index(item);
     vgm_y_index = index;
@@ -460,7 +460,7 @@ static char *_parse_worlds(DataLoaderModel *model)
     UNUSED(model);
     return "World Pack Installed";
 }
-void switch_to_view_get_worlds(FlipWorldApp *app)
+static void switch_to_view_get_worlds(FlipWorldApp *app)
 {
     if (!loader_view_alloc(app))
     {
@@ -469,7 +469,7 @@ void switch_to_view_get_worlds(FlipWorldApp *app)
     }
     loader_switch_to_view(app, "Fetching World Pack..", _fetch_worlds, _parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
 }
-void game_settings_select(void *context, uint32_t index)
+void callback_game_settings_select(void *context, uint32_t index)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     if (!app)
@@ -497,7 +497,7 @@ void game_settings_select(void *context, uint32_t index)
         break;
     }
 }
-void user_settings_select(void *context, uint32_t index)
+void callback_user_settings_select(void *context, uint32_t index)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     if (!app)
@@ -526,4 +526,102 @@ void user_settings_select(void *context, uint32_t index)
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
         break;
     }
+}
+
+void callback_submenu_lobby_choices(void *context, uint32_t index)
+{
+    /* Handle other game lobbies
+             1. when clicked on, send request to fetch the selected game lobby details
+             2. start the websocket session
+             3. start the game thread (the rest will be handled by game_start and player_update)
+             */
+    FlipWorldApp *app = (FlipWorldApp *)context;
+    furi_check(app, "FlipWorldApp is NULL");
+    if (index >= FlipWorldSubmenuIndexLobby && index < FlipWorldSubmenuIndexLobby + 10)
+    {
+        lobby_index = index - FlipWorldSubmenuIndexLobby;
+
+        FlipperHTTP *fhttp = flipper_http_alloc();
+        if (!fhttp)
+        {
+            FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
+            easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
+            return;
+        }
+
+        // fetch the lobby details
+        if (!game_fetch_lobby(fhttp, lobby_list[lobby_index]))
+        {
+            FURI_LOG_E(TAG, "Failed to fetch lobby details");
+            easy_flipper_dialog("Error", "Failed to fetch lobby details. Press BACK to return.");
+            flipper_http_free(fhttp);
+            return;
+        }
+
+        // load the lobby details
+        FuriString *lobby = flipper_http_load_from_file(fhttp->file_path);
+        if (!lobby)
+        {
+            FURI_LOG_E(TAG, "Failed to load lobby details");
+            flipper_http_free(fhttp);
+            return;
+        }
+
+        // if there are no players, add the user to the lobby and make the user wait until another player joins
+        // if there is one player and it's the user, make the user wait until another player joins
+        // if there is one player and it's not the user, parse_lobby and start websocket
+        // if there are 2 players (which there shouldn't be at this point), show an error message saying the lobby is full
+        switch (game_lobby_count(fhttp, lobby))
+        {
+        case -1:
+            FURI_LOG_E(TAG, "Failed to get player count");
+            easy_flipper_dialog("Error", "Failed to get player count. Press BACK to return.");
+            flipper_http_free(fhttp);
+            furi_string_free(lobby);
+            return;
+        case 0:
+            // add the user to the lobby
+            if (!game_join_lobby(fhttp, lobby_list[lobby_index]))
+            {
+                FURI_LOG_E(TAG, "Failed to join lobby");
+                easy_flipper_dialog("Error", "Failed to join lobby. Press BACK to return.");
+                flipper_http_free(fhttp);
+                furi_string_free(lobby);
+                return;
+            }
+            // send the user to the waiting screen
+            game_waiting_lobby(app);
+            return;
+        case 1:
+            // check if the user is in the lobby
+            if (game_in_lobby(fhttp, lobby))
+            {
+                // send the user to the waiting screen
+                FURI_LOG_I(TAG, "User is in the lobby");
+                flipper_http_free(fhttp);
+                furi_string_free(lobby);
+                game_waiting_lobby(app);
+                return;
+            }
+            // add the user to the lobby
+            if (!game_join_lobby(fhttp, lobby_list[lobby_index]))
+            {
+                FURI_LOG_E(TAG, "Failed to join lobby");
+                easy_flipper_dialog("Error", "Failed to join lobby. Press BACK to return.");
+                flipper_http_free(fhttp);
+                furi_string_free(lobby);
+                return;
+            }
+            break;
+        case 2:
+            // show an error message saying the lobby is full
+            FURI_LOG_E(TAG, "Lobby is full");
+            easy_flipper_dialog("Error", "Lobby is full. Press BACK to return.");
+            flipper_http_free(fhttp);
+            furi_string_free(lobby);
+            return;
+        };
+
+        game_start_pvp(fhttp, lobby, app); // this will free both the fhttp and lobby, and start the game
+    }
 }

+ 17 - 16
callback/callback.h

@@ -2,19 +2,20 @@
 #include <flip_world.h>
 
 void callback_submenu_choices(void *context, uint32_t index);
-bool message_input_callback(InputEvent *event, void *context);
-void message_draw_callback(Canvas *canvas, void *model);
-void wifi_settings_select(void *context, uint32_t index);
-void updated_wifi_ssid(void *context);
-void updated_wifi_pass(void *context);
-void updated_username(void *context);
-void updated_password(void *context);
-void fps_change(VariableItem *item);
-void game_settings_select(void *context, uint32_t index);
-void user_settings_select(void *context, uint32_t index);
-void screen_on_change(VariableItem *item);
-void sound_on_change(VariableItem *item);
-void vibration_on_change(VariableItem *item);
-void player_on_change(VariableItem *item);
-void vgm_x_change(VariableItem *item);
-void vgm_y_change(VariableItem *item);
+bool callback_message_input(InputEvent *event, void *context);
+void callback_message_draw(Canvas *canvas, void *model);
+void callback_wifi_settings_select(void *context, uint32_t index);
+void callback_updated_wifi_ssid(void *context);
+void callback_updated_wifi_pass(void *context);
+void callback_updated_username(void *context);
+void callback_updated_password(void *context);
+void callback_fps_change(VariableItem *item);
+void callback_game_settings_select(void *context, uint32_t index);
+void callback_user_settings_select(void *context, uint32_t index);
+void callback_screen_on_change(VariableItem *item);
+void callback_sound_on_change(VariableItem *item);
+void callback_vibration_on_change(VariableItem *item);
+void callback_player_on_change(VariableItem *item);
+void callback_vgm_x_change(VariableItem *item);
+void callback_vgm_y_change(VariableItem *item);
+void callback_submenu_lobby_choices(void *context, uint32_t index);

+ 50 - 151
callback/game.c

@@ -1,13 +1,17 @@
 #include <callback/game.h>
+//
 #include "engine/engine.h"
 #include "engine/game_engine.h"
 #include "engine/game_manager_i.h"
 #include "engine/level_i.h"
 #include "engine/entity_i.h"
+//
 #include "game/storage.h"
+//
 #include <callback/loader.h>
 #include <callback/free.h>
 #include <callback/alloc.h>
+#include <callback/callback.h>
 #include "alloc/alloc.h"
 #include <flip_storage/storage.h>
 
@@ -17,19 +21,13 @@ char *lobby_list[10];
 
 static uint8_t timer_iteration = 0; // timer iteration for the loading screen
 static uint8_t timer_refresh = 5;   // duration for timer to refresh
-//
-static void waiting_loader_process_callback(FlipperHTTP *fhttp, void *context);
-static void waiting_lobby(void *context);
-static bool fetch_lobby(FlipperHTTP *fhttp, char *lobby_name);
-//
+
 FuriThread *game_thread = NULL;
 FuriThread *waiting_thread = NULL;
 bool game_thread_running = false;
 bool waiting_thread_running = false;
-//
-static void callback_submenu_lobby_choices(void *context, uint32_t index);
 
-static void frame_cb(GameEngine *engine, Canvas *canvas, InputState input, void *context)
+static void game_frame_cb(GameEngine *engine, Canvas *canvas, InputState input, void *context)
 {
     UNUSED(engine);
     GameManager *game_manager = context;
@@ -53,7 +51,7 @@ static int32_t game_app(void *p)
     settings.target_fps = atof_(fps_choices_str[fps_index]);
     settings.show_fps = game.show_fps;
     settings.always_backlight = strstr(yes_or_no_choices[screen_always_on_index], "Yes") != NULL;
-    settings.frame_callback = frame_cb;
+    settings.frame_callback = game_frame_cb;
     settings.context = game_manager;
     GameEngine *engine = game_engine_alloc(settings);
     if (!engine)
@@ -104,7 +102,7 @@ static int32_t game_app(void *p)
     return 0;
 }
 
-static int32_t waiting_app_callback(void *p)
+static int32_t game_waiting_app_callback(void *p)
 {
     FlipWorldApp *app = (FlipWorldApp *)p;
     furi_check(app);
@@ -120,7 +118,7 @@ static int32_t waiting_app_callback(void *p)
     while (timer_iteration < 60 && !user_hit_back)
     {
         FURI_LOG_I(TAG, "Waiting for more players...");
-        waiting_loader_process_callback(fhttp, app);
+        game_waiting_process(fhttp, app);
         FURI_LOG_I(TAG, "Waiting for more players... %d", timer_iteration);
         timer_iteration++;
         furi_delay_ms(1000 * timer_refresh);
@@ -133,7 +131,7 @@ static int32_t waiting_app_callback(void *p)
     return 0;
 }
 
-static bool start_waiting_thread(void *context)
+static bool game_start_waiting_thread(void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     furi_check(app);
@@ -149,7 +147,7 @@ static bool start_waiting_thread(void *context)
         }
     }
     // start waiting thread
-    FuriThread *thread = furi_thread_alloc_ex("waiting_thread", 2048, waiting_app_callback, app);
+    FuriThread *thread = furi_thread_alloc_ex("waiting_thread", 2048, game_waiting_app_callback, app);
     if (!thread)
     {
         FURI_LOG_E(TAG, "Failed to allocate waiting thread");
@@ -162,7 +160,7 @@ static bool start_waiting_thread(void *context)
     return true;
 }
 
-static bool fetch_world_list(FlipperHTTP *fhttp)
+static bool game_fetch_world_list(FlipperHTTP *fhttp)
 {
     if (!fhttp)
     {
@@ -187,7 +185,7 @@ static bool fetch_world_list(FlipperHTTP *fhttp)
 }
 // we will load the palyer stats from the API and save them
 // in player_spawn game method, it will load the player stats that we saved
-static bool fetch_player_stats(FlipperHTTP *fhttp)
+static bool game_fetch_player_stats(FlipperHTTP *fhttp)
 {
     if (!fhttp)
     {
@@ -276,7 +274,7 @@ static bool fetch_player_stats(FlipperHTTP *fhttp)
 //     }
 // }
 
-static bool start_game_thread(void *context)
+static bool game_thread_start(void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     if (!app)
@@ -318,7 +316,7 @@ static bool start_game_thread(void *context)
     return true;
 }
 // combine register, login, and world list fetch into one function to switch to the loader view
-static bool _fetch_game(DataLoaderModel *model)
+static bool game_fetch(DataLoaderModel *model)
 {
     FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
     if (!app)
@@ -393,13 +391,13 @@ static bool _fetch_game(DataLoaderModel *model)
         else
         {
             model->title = "Fetching World List..";
-            return fetch_world_list(model->fhttp);
+            return game_fetch_world_list(model->fhttp);
         }
     }
     else if (model->request_index == 2)
     {
         model->title = "Fetching World List..";
-        return fetch_world_list(model->fhttp);
+        return game_fetch_world_list(model->fhttp);
     }
     else if (model->request_index == 3)
     {
@@ -429,7 +427,7 @@ static bool _fetch_game(DataLoaderModel *model)
             furi_string_free(world_list);
             furi_string_free(first_world);
 
-            if (!start_game_thread(app))
+            if (!game_thread_start(app))
             {
                 FURI_LOG_E(TAG, "Failed to start game thread");
                 easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
@@ -451,7 +449,7 @@ static bool _fetch_game(DataLoaderModel *model)
     FURI_LOG_E(TAG, "Unknown request index");
     return false;
 }
-static char *_parse_game(DataLoaderModel *model)
+static char *game_parse(DataLoaderModel *model)
 {
     FlipWorldApp *app = (FlipWorldApp *)model->parser_context;
 
@@ -572,7 +570,7 @@ static char *_parse_game(DataLoaderModel *model)
         }
         else
         {
-            if (!start_game_thread(app))
+            if (!game_thread_start(app))
             {
                 FURI_LOG_E(TAG, "Failed to start game thread");
                 easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
@@ -589,7 +587,7 @@ static char *_parse_game(DataLoaderModel *model)
     }
     else if (model->request_index == 3)
     {
-        if (!start_game_thread(app))
+        if (!game_thread_start(app))
         {
             FURI_LOG_E(TAG, "Failed to start game thread");
             easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
@@ -603,16 +601,16 @@ static char *_parse_game(DataLoaderModel *model)
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
     return "Unknown error";
 }
-static void switch_to_view_get_game(FlipWorldApp *app)
+static void game_switch_to_view(FlipWorldApp *app)
 {
     if (!loader_view_alloc(app))
     {
         FURI_LOG_E(TAG, "Failed to allocate view loader");
         return;
     }
-    loader_switch_to_view(app, "Starting Game..", _fetch_game, _parse_game, 5, callback_to_submenu, FlipWorldViewLoader);
+    loader_switch_to_view(app, "Starting Game..", game_fetch, game_parse, 5, callback_to_submenu, FlipWorldViewLoader);
 }
-void run(FlipWorldApp *app)
+void game_run(FlipWorldApp *app)
 {
     if (!app)
     {
@@ -621,7 +619,7 @@ void run(FlipWorldApp *app)
     }
     free_all_views(app, true, true, false);
     // only need to check if they have 50k free (game needs about 38k currently)
-    if (!is_enough_heap(50000, false))
+    if (!is_enough_heap(40000, false))
     {
         const size_t min_free = memmgr_get_free_heap();
         char message[64];
@@ -639,18 +637,18 @@ void run(FlipWorldApp *app)
             easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
             return;
         }
-        bool fetch_world_list_i()
+        bool game_fetch_world_list_i()
         {
-            return fetch_world_list(fhttp);
+            return game_fetch_world_list(fhttp);
         }
         bool parse_world_list_i()
         {
             return fhttp->state != ISSUE;
         }
 
-        bool fetch_player_stats_i()
+        bool game_fetch_player_stats_i()
         {
-            return fetch_player_stats(fhttp);
+            return game_fetch_player_stats(fhttp);
         }
 
         if (!alloc_message_view(app, MessageStateLoading))
@@ -663,7 +661,7 @@ void run(FlipWorldApp *app)
         // Make the request
         if (game_mode_index != 1) // not GAME_MODE_PVP
         {
-            if (!flipper_http_process_response_async(fhttp, fetch_world_list_i, parse_world_list_i) || !flipper_http_process_response_async(fhttp, fetch_player_stats_i, set_player_context))
+            if (!flipper_http_process_response_async(fhttp, game_fetch_world_list_i, parse_world_list_i) || !flipper_http_process_response_async(fhttp, game_fetch_player_stats_i, set_player_context))
             {
                 FURI_LOG_E(HTTP_TAG, "Failed to make request");
                 flipper_http_free(fhttp);
@@ -673,7 +671,7 @@ void run(FlipWorldApp *app)
                 flipper_http_free(fhttp);
             }
 
-            if (!start_game_thread(app))
+            if (!game_thread_start(app))
             {
                 FURI_LOG_E(TAG, "Failed to start game thread");
                 easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
@@ -755,7 +753,7 @@ void run(FlipWorldApp *app)
             }
 
             // load pvp lobbies and player stats
-            if (!flipper_http_process_response_async(fhttp, fetch_pvp_lobbies, parse_pvp_lobbies) || !flipper_http_process_response_async(fhttp, fetch_player_stats_i, set_player_context))
+            if (!flipper_http_process_response_async(fhttp, fetch_pvp_lobbies, parse_pvp_lobbies) || !flipper_http_process_response_async(fhttp, game_fetch_player_stats_i, set_player_context))
             {
                 // unlike the pve/story, receiving data is necessary
                 // so send the user back to the main menu if it fails
@@ -776,11 +774,11 @@ void run(FlipWorldApp *app)
     }
     else
     {
-        switch_to_view_get_game(app);
+        game_switch_to_view(app);
     }
 }
 
-static bool fetch_lobby(FlipperHTTP *fhttp, char *lobby_name)
+bool game_fetch_lobby(FlipperHTTP *fhttp, char *lobby_name)
 {
     if (!fhttp)
     {
@@ -815,7 +813,7 @@ static bool fetch_lobby(FlipperHTTP *fhttp, char *lobby_name)
     }
     return true;
 }
-static bool join_lobby(FlipperHTTP *fhttp, char *lobby_name)
+bool game_join_lobby(FlipperHTTP *fhttp, char *lobby_name)
 {
     if (!fhttp)
     {
@@ -850,7 +848,7 @@ static bool join_lobby(FlipperHTTP *fhttp, char *lobby_name)
     }
     return true;
 }
-static bool create_pvp_enemy(FuriString *lobby_details)
+static bool game_create_pvp_enemy(FuriString *lobby_details)
 {
     if (!lobby_details)
     {
@@ -978,9 +976,8 @@ static bool create_pvp_enemy(FuriString *lobby_details)
 
     return true;
 }
-// since we aren't using FURI_LOG, we will use easy_flipper_dialog and the last_error_message
-// char last_error_message[64];
-static size_t lobby_count(FlipperHTTP *fhttp, FuriString *lobby)
+
+size_t game_lobby_count(FlipperHTTP *fhttp, FuriString *lobby)
 {
     if (!fhttp)
     {
@@ -1003,7 +1000,7 @@ static size_t lobby_count(FlipperHTTP *fhttp, FuriString *lobby)
     furi_string_free(player_count);
     return count;
 }
-static bool in_lobby(FlipperHTTP *fhttp, FuriString *lobby)
+bool game_in_lobby(FlipperHTTP *fhttp, FuriString *lobby)
 {
     if (!fhttp)
     {
@@ -1028,7 +1025,7 @@ static bool in_lobby(FlipperHTTP *fhttp, FuriString *lobby)
     return in_game;
 }
 
-static bool start_ws(FlipperHTTP *fhttp, char *lobby_name)
+static bool game_start_ws(FlipperHTTP *fhttp, char *lobby_name)
 {
     if (!fhttp)
     {
@@ -1056,12 +1053,12 @@ static bool start_ws(FlipperHTTP *fhttp, char *lobby_name)
     return true;
 }
 // this will free both the fhttp and lobby
-static void start_pvp(FlipperHTTP *fhttp, FuriString *lobby, void *context)
+void game_start_pvp(FlipperHTTP *fhttp, FuriString *lobby, void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     furi_check(app, "FlipWorldApp is NULL");
     // only thing left to do is create the enemy data and start the websocket session
-    if (!create_pvp_enemy(lobby))
+    if (!game_create_pvp_enemy(lobby))
     {
         FURI_LOG_E(TAG, "Failed to create pvp enemy context.");
         easy_flipper_dialog("Error", "Failed to create pvp enemy context. Press BACK to return.");
@@ -1073,7 +1070,7 @@ static void start_pvp(FlipperHTTP *fhttp, FuriString *lobby, void *context)
     furi_string_free(lobby);
 
     // start the websocket session
-    if (!start_ws(fhttp, lobby_list[lobby_index]))
+    if (!game_start_ws(fhttp, lobby_list[lobby_index]))
     {
         FURI_LOG_E(TAG, "Failed to start websocket session");
         easy_flipper_dialog("Error", "Failed to start websocket session. Press BACK to return.");
@@ -1084,14 +1081,14 @@ static void start_pvp(FlipperHTTP *fhttp, FuriString *lobby, void *context)
     flipper_http_free(fhttp);
 
     // start the game thread
-    if (!start_game_thread(app))
+    if (!game_thread_start(app))
     {
         FURI_LOG_E(TAG, "Failed to start game thread");
         easy_flipper_dialog("Error", "Failed to start game thread. Press BACK to return.");
         return;
     }
 };
-static void waiting_loader_process_callback(FlipperHTTP *fhttp, void *context)
+void game_waiting_process(FlipperHTTP *fhttp, void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     if (!app)
@@ -1107,7 +1104,7 @@ static void waiting_loader_process_callback(FlipperHTTP *fhttp, void *context)
         return;
     }
     // fetch the lobby details
-    if (!fetch_lobby(fhttp, lobby_list[lobby_index]))
+    if (!game_fetch_lobby(fhttp, lobby_list[lobby_index]))
     {
         FURI_LOG_E(TAG, "Failed to fetch lobby details");
         flipper_http_free(fhttp);
@@ -1126,21 +1123,21 @@ static void waiting_loader_process_callback(FlipperHTTP *fhttp, void *context)
         return;
     }
     // get the player count
-    const size_t count = lobby_count(fhttp, lobby);
+    const size_t count = game_lobby_count(fhttp, lobby);
     if (count == 2)
     {
         // break out of this and start the game
-        start_pvp(fhttp, lobby, app); // this will free both the fhttp and lobby
+        game_start_pvp(fhttp, lobby, app); // this will free both the fhttp and lobby
         return;
     }
     furi_string_free(lobby);
 }
 
-static void waiting_lobby(void *context)
+void game_waiting_lobby(void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
     furi_check(app, "waiting_lobby: FlipWorldApp is NULL");
-    if (!start_waiting_thread(app))
+    if (!game_start_waiting_thread(app))
     {
         FURI_LOG_E(TAG, "Failed to start waiting thread");
         easy_flipper_dialog("Error", "Failed to start waiting thread. Press BACK to return.");
@@ -1155,101 +1152,3 @@ static void waiting_lobby(void *context)
     // finally, switch to the waiting lobby view
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMessage);
 };
-
-static void callback_submenu_lobby_choices(void *context, uint32_t index)
-{
-    /* Handle other game lobbies
-             1. when clicked on, send request to fetch the selected game lobby details
-             2. start the websocket session
-             3. start the game thread (the rest will be handled by game_start and player_update)
-             */
-    FlipWorldApp *app = (FlipWorldApp *)context;
-    furi_check(app, "FlipWorldApp is NULL");
-    if (index >= FlipWorldSubmenuIndexLobby && index < FlipWorldSubmenuIndexLobby + 10)
-    {
-        lobby_index = index - FlipWorldSubmenuIndexLobby;
-
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (!fhttp)
-        {
-            FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
-            easy_flipper_dialog("Error", "Failed to allocate FlipperHTTP. Press BACK to return.");
-            return;
-        }
-
-        // fetch the lobby details
-        if (!fetch_lobby(fhttp, lobby_list[lobby_index]))
-        {
-            FURI_LOG_E(TAG, "Failed to fetch lobby details");
-            easy_flipper_dialog("Error", "Failed to fetch lobby details. Press BACK to return.");
-            flipper_http_free(fhttp);
-            return;
-        }
-
-        // load the lobby details
-        FuriString *lobby = flipper_http_load_from_file(fhttp->file_path);
-        if (!lobby)
-        {
-            FURI_LOG_E(TAG, "Failed to load lobby details");
-            flipper_http_free(fhttp);
-            return;
-        }
-
-        // if there are no players, add the user to the lobby and make the user wait until another player joins
-        // if there is one player and it's the user, make the user wait until another player joins
-        // if there is one player and it's not the user, parse_lobby and start websocket
-        // if there are 2 players (which there shouldn't be at this point), show an error message saying the lobby is full
-        switch (lobby_count(fhttp, lobby))
-        {
-        case -1:
-            FURI_LOG_E(TAG, "Failed to get player count");
-            easy_flipper_dialog("Error", "Failed to get player count. Press BACK to return.");
-            flipper_http_free(fhttp);
-            furi_string_free(lobby);
-            return;
-        case 0:
-            // add the user to the lobby
-            if (!join_lobby(fhttp, lobby_list[lobby_index]))
-            {
-                FURI_LOG_E(TAG, "Failed to join lobby");
-                easy_flipper_dialog("Error", "Failed to join lobby. Press BACK to return.");
-                flipper_http_free(fhttp);
-                furi_string_free(lobby);
-                return;
-            }
-            // send the user to the waiting screen
-            waiting_lobby(app);
-            return;
-        case 1:
-            // check if the user is in the lobby
-            if (in_lobby(fhttp, lobby))
-            {
-                // send the user to the waiting screen
-                FURI_LOG_I(TAG, "User is in the lobby");
-                flipper_http_free(fhttp);
-                furi_string_free(lobby);
-                waiting_lobby(app);
-                return;
-            }
-            // add the user to the lobby
-            if (!join_lobby(fhttp, lobby_list[lobby_index]))
-            {
-                FURI_LOG_E(TAG, "Failed to join lobby");
-                easy_flipper_dialog("Error", "Failed to join lobby. Press BACK to return.");
-                flipper_http_free(fhttp);
-                furi_string_free(lobby);
-                return;
-            }
-            break;
-        case 2:
-            // show an error message saying the lobby is full
-            FURI_LOG_E(TAG, "Lobby is full");
-            easy_flipper_dialog("Error", "Lobby is full. Press BACK to return.");
-            flipper_http_free(fhttp);
-            furi_string_free(lobby);
-            return;
-        };
-
-        start_pvp(fhttp, lobby, app); // this will free both the fhttp and lobby, and start the game
-    }
-}

+ 9 - 1
callback/game.h

@@ -7,4 +7,12 @@ extern FuriThread *game_thread;
 extern FuriThread *waiting_thread;
 extern bool game_thread_running;
 extern bool waiting_thread_running;
-void run(FlipWorldApp *app);
+//
+void game_run(FlipWorldApp *app);
+bool game_fetch_lobby(FlipperHTTP *fhttp, char *lobby_name);
+bool game_join_lobby(FlipperHTTP *fhttp, char *lobby_name);
+size_t game_lobby_count(FlipperHTTP *fhttp, FuriString *lobby);
+bool game_in_lobby(FlipperHTTP *fhttp, FuriString *lobby);
+void game_start_pvp(FlipperHTTP *fhttp, FuriString *lobby, void *context);
+void game_waiting_lobby(void *context);
+void game_waiting_process(FlipperHTTP *fhttp, void *context);