Przeglądaj źródła

update world_fetch method

jblanked 8 miesięcy temu
rodzic
commit
65dd1e3d04
3 zmienionych plików z 49 dodań i 10 usunięć
  1. 42 2
      game/level.c
  2. 6 7
      game/world.c
  3. 1 1
      game/world.h

+ 42 - 2
game/level.c

@@ -1,6 +1,7 @@
 #include <game/level.h>
 #include <flip_storage/storage.h>
 #include <game/storage.h>
+#include <callback/game.h>
 bool allocate_level(GameManager *manager, int index)
 {
     GameContext *game_context = game_manager_game_context_get(manager);
@@ -47,7 +48,7 @@ void level_set_world(Level *level, GameManager *manager, char *id)
 
     if (!is_enough_heap(20000, true))
     {
-        FURI_LOG_E("Game", "Not enough heap memory.. ending game early.");
+        FURI_LOG_E("Game", "level_set_world: Not enough heap memory.. ending game early.");
         GameContext *game_context = game_manager_game_context_get(manager);
         game_context->end_reason = GAME_END_MEMORY;
         game_context->ended_early = true;
@@ -160,12 +161,51 @@ static void level_start(Level *level, GameManager *manager, void *context)
     if (!world_exists(level_context->id))
     {
         FURI_LOG_E("Game", "World does not exist.. downloading now");
-        FuriString *world_data = world_fetch(level_context->id);
+        FuriString *world_data = NULL;
+        if (game_context->game_mode != GAME_MODE_STORY)
+        {
+            if (game_context->fhttp)
+            {
+                flipper_http_free(game_context->fhttp);
+            }
+            game_context->fhttp = flipper_http_alloc();
+            if (!game_context->fhttp)
+            {
+                FURI_LOG_E("Game", "Failed to allocate FlipperHTTP");
+                game_context->is_switching_level = false;
+                game_context->ended_early = true;
+                game_context->end_reason = GAME_END_MEMORY;
+                player_spawn(level, manager);
+                return;
+            }
+            flipper_http_websocket_stop(game_context->fhttp); // close websocket if open
+            furi_delay_ms(200);
+            world_data = world_fetch(game_context->fhttp, level_context->id);
+            furi_delay_ms(200);
+            game_start_ws(game_context->fhttp, game_ws_lobby_name); // start websocket again
+        }
+        else
+        {
+            FlipperHTTP *fhttp = flipper_http_alloc();
+            if (!fhttp)
+            {
+                FURI_LOG_E("Game", "Failed to allocate FlipperHTTP");
+                game_context->is_switching_level = false;
+                game_context->ended_early = true;
+                game_context->end_reason = GAME_END_MEMORY;
+                player_spawn(level, manager);
+                return;
+            }
+            world_data = world_fetch(fhttp, "pvp_world");
+            flipper_http_free(fhttp);
+        }
         if (!world_data)
         {
             FURI_LOG_E("Game", "Failed to fetch world data");
             // draw_town_world(manager, level);
             game_context->is_switching_level = false;
+            game_context->ended_early = true;
+            game_context->end_reason = GAME_END_NETWORK;
             // furi_delay_ms(1000);
             player_spawn(level, manager);
             return;

+ 6 - 7
game/world.c

@@ -228,7 +228,7 @@ const LevelBehaviour *world_pvp()
     return &_world_pvp;
 }
 
-FuriString *world_fetch(const char *name)
+FuriString *world_fetch(FlipperHTTP *fhttp, const char *name)
 {
     if (!name)
     {
@@ -236,10 +236,9 @@ FuriString *world_fetch(const char *name)
         return NULL;
     }
 
-    FlipperHTTP *fhttp = flipper_http_alloc();
     if (!fhttp)
     {
-        FURI_LOG_E("Game", "Failed to allocate HTTP");
+        FURI_LOG_E("Game", "world_fetch: FlipperHTTP is NULL");
         return NULL;
     }
 
@@ -249,8 +248,8 @@ FuriString *world_fetch(const char *name)
     fhttp->save_received_data = true;
     if (!flipper_http_request(fhttp, GET, url, "{\"Content-Type\": \"application/json\"}", NULL))
     {
-        FURI_LOG_E("Game", "Failed to send HTTP request");
-        flipper_http_free(fhttp);
+        FURI_LOG_E("Game", "world_fetch: Failed to send HTTP request");
+
         return NULL;
     }
     fhttp->state = RECEIVING;
@@ -264,10 +263,10 @@ FuriString *world_fetch(const char *name)
     if (fhttp->state != IDLE)
     {
         FURI_LOG_E("Game", "Failed to receive world data");
-        flipper_http_free(fhttp);
+
         return NULL;
     }
-    flipper_http_free(fhttp);
+
     FuriString *returned_data = load_furi_world(name);
     if (!returned_data)
     {

+ 1 - 1
game/world.h

@@ -13,4 +13,4 @@
 
 const LevelBehaviour *world_pvp();
 bool world_json_draw(GameManager *manager, Level *level, const FuriString *json_data);
-FuriString *world_fetch(const char *name);
+FuriString *world_fetch(FlipperHTTP *fhttp, const char *name);