mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-07-09 23:18:10 -07:00
Snake Plugin: get Highscore on start and only write to file if new highscore
This commit is contained in:
@@ -34,30 +34,14 @@ static FlipperFormat* snake_game_open_file() {
|
||||
return file;
|
||||
}
|
||||
|
||||
int16_t snake_game_save_score_to_file(int32_t len) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
FlipperFormat* file = snake_game_open_file(storage);
|
||||
|
||||
len = len - 7;
|
||||
int32_t temp;
|
||||
if(!flipper_format_read_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)){
|
||||
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)){
|
||||
snake_game_close_file(file);
|
||||
return -1;
|
||||
}
|
||||
}else{
|
||||
if(len > temp){
|
||||
flipper_format_rewind(file);
|
||||
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)){
|
||||
snake_game_close_file(file);
|
||||
return -1;
|
||||
}
|
||||
}else{
|
||||
len = temp;
|
||||
}
|
||||
void snake_game_save_score_to_file(int16_t highscore) {
|
||||
FlipperFormat* file = snake_game_open_file();
|
||||
uint32_t temp = highscore;
|
||||
if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE,&temp, 1)){
|
||||
snake_game_close_file(file);
|
||||
return;
|
||||
}
|
||||
snake_game_close_file(file);
|
||||
return len;
|
||||
}
|
||||
|
||||
void snake_game_save_game_to_file(SnakeState* const snake_state) {
|
||||
@@ -116,7 +100,11 @@ bool snake_game_init_game_from_file(SnakeState* const snake_state) {
|
||||
}
|
||||
furi_string_free(file_type);
|
||||
|
||||
|
||||
uint32_t temp;
|
||||
snake_state->highscore = (flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) ? temp : 0;
|
||||
flipper_format_rewind(file);
|
||||
|
||||
if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)){
|
||||
snake_game_close_file(file);
|
||||
return false;
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#define SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS "FruitPoints"
|
||||
#define SNAKE_GAME_CONFIG_HIGHSCORE "Highscore"
|
||||
|
||||
int16_t snake_game_save_score_to_file(int32_t length);
|
||||
void snake_game_save_score_to_file(int16_t highscore);
|
||||
|
||||
void snake_game_save_game_to_file(SnakeState* const snake_state);
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ typedef enum {
|
||||
typedef struct {
|
||||
Point points[MAX_SNAKE_LEN];
|
||||
uint16_t len;
|
||||
bool isNewHighscore;
|
||||
int16_t highscore;
|
||||
Direction currentMovement;
|
||||
Direction nextMovement; // if backward of currentMovement, ignore
|
||||
|
||||
@@ -292,6 +292,8 @@ int32_t snake_game_app(void* p) {
|
||||
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(SnakeEvent));
|
||||
|
||||
SnakeState* snake_state = malloc(sizeof(SnakeState));
|
||||
snake_state->isNewHighscore = false;
|
||||
snake_state->highscore = 0;
|
||||
if(!snake_game_init_game_from_file(snake_state))
|
||||
snake_game_init_game(snake_state);
|
||||
|
||||
@@ -365,6 +367,8 @@ int32_t snake_game_app(void* p) {
|
||||
release_mutex(&state_mutex, snake_state);
|
||||
}
|
||||
|
||||
if(snake_state->isNewHighscore)
|
||||
snake_game_save_score_to_file(snake_state->highscore);
|
||||
// Wait for all notifications to be played and return backlight to normal state
|
||||
notification_message_block(notification, &sequence_display_backlight_enforce_auto);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user