jblanked 1 год назад
Родитель
Сommit
6b94ef9b23
3 измененных файлов с 153 добавлено и 5 удалено
  1. 3 5
      callback/callback.c
  2. 149 0
      game/storage.c
  3. 1 0
      game/storage.h

+ 3 - 5
callback/callback.c

@@ -5,6 +5,7 @@
 #include "engine/game_manager_i.h"
 #include "engine/level_i.h"
 #include "engine/entity_i.h"
+#include "game/storage.h"
 
 // Below added by Derek Jamison
 // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
@@ -688,6 +689,7 @@ static bool fetch_player_stats(FlipperHTTP *fhttp)
     fhttp->save_received_data = true;
     return flipper_http_get_request_with_headers(fhttp, url, "{\"Content-Type\":\"application/json\"}");
 }
+
 static bool start_game_thread(void *context)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
@@ -1041,10 +1043,6 @@ void callback_submenu_choices(void *context, uint32_t index)
             {
                 return fetch_player_stats(fhttp);
             }
-            bool parse_player_stats_i()
-            {
-                return fhttp->state != ISSUE;
-            }
 
             Loading *loading;
             int32_t loading_view_id = 987654321; // Random ID
@@ -1065,7 +1063,7 @@ void callback_submenu_choices(void *context, uint32_t index)
 
             // Make the request
             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, parse_player_stats_i))
+                !flipper_http_process_response_async(fhttp, fetch_player_stats_i, set_player_context))
             {
                 FURI_LOG_E(HTTP_TAG, "Failed to make request");
                 view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSubmenu);

+ 149 - 0
game/storage.c

@@ -549,6 +549,155 @@ bool load_player_context(PlayerContext *player_context)
 
     return true;
 }
+// loads from STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json
+// then gets each key-value pair and saves it as it's own file so it can be loaded separately using
+// load_player_context
+bool set_player_context()
+{
+    char file_path[256];
+    snprintf(file_path, sizeof(file_path),
+             STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/player/player_stats.json");
+
+    FuriString *player_stats = flipper_http_load_from_file(file_path);
+    if (!player_stats)
+    {
+        FURI_LOG_E(TAG, "Failed to load player stats from file: %s", file_path);
+        return false;
+    }
+
+    // Get the key one-by-one and save it to a separate file
+
+    // 1. Username (String)
+    FuriString *username = get_json_value_furi("username", player_stats);
+    if (username)
+    {
+        save_char("player/username", furi_string_get_cstr(username));
+        furi_string_free(username);
+    }
+
+    // 2. Level (uint32_t)
+    FuriString *level = get_json_value_furi("level", player_stats);
+    if (level)
+    {
+        save_uint32("player/level", atoi(furi_string_get_cstr(level)));
+        furi_string_free(level);
+    }
+
+    // 3. XP (uint32_t)
+    FuriString *xp = get_json_value_furi("xp", player_stats);
+    if (xp)
+    {
+        save_uint32("player/xp", atoi(furi_string_get_cstr(xp)));
+        furi_string_free(xp);
+    }
+
+    // 4. Health (uint32_t)
+    FuriString *health = get_json_value_furi("health", player_stats);
+    if (health)
+    {
+        save_uint32("player/health", atoi(furi_string_get_cstr(health)));
+        furi_string_free(health);
+    }
+
+    // 5. Strength (uint32_t)
+    FuriString *strength = get_json_value_furi("strength", player_stats);
+    if (strength)
+    {
+        save_uint32("player/strength", atoi(furi_string_get_cstr(strength)));
+        furi_string_free(strength);
+    }
+
+    // 6. Max Health (uint32_t)
+    FuriString *max_health = get_json_value_furi("max_health", player_stats);
+    if (max_health)
+    {
+        save_uint32("player/max_health", atoi(furi_string_get_cstr(max_health)));
+        furi_string_free(max_health);
+    }
+
+    // 7. Health Regen (uint32_t)
+    FuriString *health_regen = get_json_value_furi("health_regen", player_stats);
+    if (health_regen)
+    {
+        save_uint32("player/health_regen", atoi(furi_string_get_cstr(health_regen)));
+        furi_string_free(health_regen);
+    }
+
+    // 8. Elapsed Health Regen (float)
+    FuriString *elapsed_health_regen = get_json_value_furi("elapsed_health_regen", player_stats);
+    if (elapsed_health_regen)
+    {
+        save_float("player/elapsed_health_regen", strtof(furi_string_get_cstr(elapsed_health_regen), NULL));
+        furi_string_free(elapsed_health_regen);
+    }
+
+    // 9. Attack Timer (float)
+    FuriString *attack_timer = get_json_value_furi("attack_timer", player_stats);
+    if (attack_timer)
+    {
+        save_float("player/attack_timer", strtof(furi_string_get_cstr(attack_timer), NULL));
+        furi_string_free(attack_timer);
+    }
+
+    // 10. Elapsed Attack Timer (float)
+    FuriString *elapsed_attack_timer = get_json_value_furi("elapsed_attack_timer", player_stats);
+    if (elapsed_attack_timer)
+    {
+        save_float("player/elapsed_attack_timer", strtof(furi_string_get_cstr(elapsed_attack_timer), NULL));
+        furi_string_free(elapsed_attack_timer);
+    }
+
+    // 11. Direction (enum PlayerDirection)
+    FuriString *direction = get_json_value_furi("direction", player_stats);
+    if (direction)
+    {
+        save_char("player/direction", furi_string_get_cstr(direction));
+        furi_string_free(direction);
+    }
+
+    // 12. State (enum PlayerState)
+    FuriString *state = get_json_value_furi("state", player_stats);
+    if (state)
+    {
+        save_char("player/state", furi_string_get_cstr(state));
+        furi_string_free(state);
+    }
+
+    // 13. Start Position X (float)
+    FuriString *start_position_x = get_json_value_furi("start_position_x", player_stats);
+    if (start_position_x)
+    {
+        save_float("player/start_position_x", strtof(furi_string_get_cstr(start_position_x), NULL));
+        furi_string_free(start_position_x);
+    }
+
+    // 14. Start Position Y (float)
+    FuriString *start_position_y = get_json_value_furi("start_position_y", player_stats);
+    if (start_position_y)
+    {
+        save_float("player/start_position_y", strtof(furi_string_get_cstr(start_position_y), NULL));
+        furi_string_free(start_position_y);
+    }
+
+    // 15. dx (int8_t)
+    FuriString *dx = get_json_value_furi("dx", player_stats);
+    if (dx)
+    {
+        save_int8("player/dx", atoi(furi_string_get_cstr(dx)));
+        furi_string_free(dx);
+    }
+
+    // 16. dy (int8_t)
+    FuriString *dy = get_json_value_furi("dy", player_stats);
+    if (dy)
+    {
+        save_int8("player/dy", atoi(furi_string_get_cstr(dy)));
+        furi_string_free(dy);
+    }
+
+    furi_string_free(player_stats);
+    return true;
+}
 
 static inline void furi_string_remove_str(FuriString *string, const char *needle)
 {

+ 1 - 0
game/storage.h

@@ -6,6 +6,7 @@
 
 bool save_player_context(PlayerContext *player_context);
 bool load_player_context(PlayerContext *player_context);
+bool set_player_context();
 
 // save the json_data and enemy_data to separate files
 bool separate_world_data(char *id, FuriString *world_data);