فهرست منبع

save player attributes as intended

This was a tricky one, something is up with the game's player context. Temporary solution is to save the context to the SD before the game ends, then allocate a new player context to set the saved context to, and finally, send the stats to the API.
jblanked 10 ماه پیش
والد
کامیت
a6696faecb
2فایلهای تغییر یافته به همراه26 افزوده شده و 10 حذف شده
  1. 24 10
      game/game.c
  2. 2 0
      game/player.c

+ 24 - 10
game/game.c

@@ -84,16 +84,23 @@ static void game_stop(void *ctx)
         level_clear(game_context->levels[game_context->current_level]);
     }
 
-    if (game_context->player_context)
+    PlayerContext *player_context = malloc(sizeof(PlayerContext));
+    if (!player_context)
     {
-        if (!game_context->ended_early)
-            easy_flipper_dialog(
-                "Game Over",
-                "Thanks for playing FlipWorld!\nHit BACK then wait for\nthe game to save.");
-        else
-            easy_flipper_dialog(
-                "Game Over", "Ran out of memory so the\ngame ended early.\nHit BACK to exit.");
+        FURI_LOG_E("Game", "Failed to allocate PlayerContext");
+        return;
+    }
 
+    if (!game_context->ended_early)
+        easy_flipper_dialog(
+            "Game Over",
+            "Thanks for playing FlipWorld!\nHit BACK then wait for\nthe game to save.");
+    else
+        easy_flipper_dialog(
+            "Game Over", "Ran out of memory so the\ngame ended early.\nHit BACK to exit.");
+
+    if (load_player_context(player_context))
+    {
         ViewPort *view_port = view_port_alloc();
         view_port_draw_callback_set(view_port, thanks, NULL);
         Gui *gui = furi_record_open(RECORD_GUI);
@@ -101,9 +108,9 @@ static void game_stop(void *ctx)
         uint32_t tick_count = furi_get_tick();
         furi_delay_ms(800);
 
-        save_player_context_api(game_context->player_context);
+        save_player_context_api(player_context);
 
-        const uint32_t delay = 2500;
+        const uint32_t delay = 3500;
         tick_count = (tick_count + delay) - furi_get_tick();
         if (tick_count <= delay)
         {
@@ -117,6 +124,13 @@ static void game_stop(void *ctx)
         gui_remove_view_port(gui, view_port);
         furi_record_close(RECORD_GUI);
     }
+
+    // free the player context
+    if (player_context)
+    {
+        free(player_context);
+        player_context = NULL;
+    }
 }
 
 /*

+ 2 - 0
game/player.c

@@ -382,6 +382,8 @@ static void player_update(Entity *self, GameManager *manager, void *context)
         {
             if (!game_context->is_menu_open)
             {
+                save_player_context(player);
+                furi_delay_ms(100);
                 game_manager_game_stop(manager);
                 return;
             }