diff --git a/applications/plugins/snake_game/helpers/snake_file_handler.c b/applications/plugins/snake_game/helpers/snake_file_handler.c index 79405cbf4..3a1ea7455 100644 --- a/applications/plugins/snake_game/helpers/snake_file_handler.c +++ b/applications/plugins/snake_game/helpers/snake_file_handler.c @@ -4,7 +4,7 @@ #include static void snake_game_close_file(FlipperFormat* file) { - if(file == NULL){ + if(file == NULL) { furi_record_close(RECORD_STORAGE); return; } @@ -28,7 +28,8 @@ static FlipperFormat* snake_game_open_file() { return NULL; } - flipper_format_write_header_cstr(file, SNAKE_GAME_FILE_HEADER, SNAKE_GAME_FILE_ACTUAL_VERSION); + flipper_format_write_header_cstr( + file, SNAKE_GAME_FILE_HEADER, SNAKE_GAME_FILE_ACTUAL_VERSION); flipper_format_rewind(file); } return file; @@ -37,7 +38,7 @@ static FlipperFormat* snake_game_open_file() { 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)){ + if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &temp, 1)) { snake_game_close_file(file); return; } @@ -48,41 +49,45 @@ void snake_game_save_game_to_file(SnakeState* const snake_state) { FlipperFormat* file = snake_game_open_file(); uint32_t temp = snake_state->len; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN,&temp, 1)){ + if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)) { snake_game_close_file(file); return; } uint16_t array_size = snake_state->len * 2; uint32_t temp_array[array_size]; - for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++){ + for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++) { temp_array[a++] = snake_state->points[i].x; temp_array[a++] = snake_state->points[i].y; } - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)){ - snake_game_close_file(file); - return; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)) { + snake_game_close_file(file); + return; } temp = snake_state->currentMovement; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT,&temp, 1)){ - snake_game_close_file(file); - return; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)) { + snake_game_close_file(file); + return; } temp = snake_state->nextMovement; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT,&temp, 1)){ - snake_game_close_file(file); - return; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)) { + snake_game_close_file(file); + return; } array_size = 2; uint32_t temp_point_array[array_size]; temp_point_array[0] = snake_state->fruit.x; temp_point_array[1] = snake_state->fruit.y; - if(!flipper_format_insert_or_update_uint32(file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)){ - snake_game_close_file(file); - return; + if(!flipper_format_insert_or_update_uint32( + file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)) { + snake_game_close_file(file); + return; } snake_game_close_file(file); @@ -100,12 +105,12 @@ 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; + 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)){ + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_LEN, &temp, 1)) { snake_game_close_file(file); return false; } @@ -114,25 +119,25 @@ bool snake_game_init_game_from_file(SnakeState* const snake_state) { uint16_t array_size = snake_state->len * 2; uint32_t temp_array[array_size]; - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)){ + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_POINTS, temp_array, array_size)) { snake_game_close_file(file); return false; } - for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++){ + for(int16_t i = 0, a = 0; a < array_size && i < snake_state->len; i++) { snake_state->points[i].x = temp_array[a++]; snake_state->points[i].y = temp_array[a++]; } flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_POINTS); - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)){ + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT, &temp, 1)) { snake_game_close_file(file); return false; } snake_state->currentMovement = temp; flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_CURRENT_MOVEMENT); - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)){ + if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT, &temp, 1)) { snake_game_close_file(file); return false; } @@ -141,7 +146,8 @@ bool snake_game_init_game_from_file(SnakeState* const snake_state) { array_size = 2; uint32_t temp_point_array[array_size]; - if(!flipper_format_read_uint32(file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)){ + if(!flipper_format_read_uint32( + file, SNAKE_GAME_CONFIG_KEY_FRUIT_POINTS, temp_point_array, array_size)) { snake_game_close_file(file); return false; } diff --git a/applications/plugins/snake_game/snake_game.c b/applications/plugins/snake_game/snake_game.c index 5658c1ac1..623ccea10 100644 --- a/applications/plugins/snake_game/snake_game.c +++ b/applications/plugins/snake_game/snake_game.c @@ -224,14 +224,13 @@ static void snake_game_move_snake(SnakeState* const snake_state, Point const nex static void snake_game_game_over(SnakeState* const snake_state, NotificationApp* notification) { snake_state->state = GameStateGameOver; - snake_state->len = snake_state->len -7; - if(snake_state->len > snake_state->highscore){ + snake_state->len = snake_state->len - 7; + if(snake_state->len > snake_state->highscore) { snake_state->isNewHighscore = true; snake_state->highscore = snake_state->len; } notification_message_block(notification, &sequence_fail); - } static void @@ -294,8 +293,9 @@ int32_t snake_game_app(void* p) { SnakeState* snake_state = malloc(sizeof(SnakeState)); snake_state->isNewHighscore = false; snake_state->highscore = 0; - if(!snake_game_init_game_from_file(snake_state)) + if(!snake_game_init_game_from_file(snake_state)) { snake_game_init_game(snake_state); + } ValueMutex state_mutex; if(!init_mutex(&state_mutex, snake_state, sizeof(SnakeState))) { @@ -350,8 +350,9 @@ int32_t snake_game_app(void* p) { } break; case InputKeyBack: - if(snake_state->state == GameStateLife) + if(snake_state->state == GameStateLife) { snake_game_save_game_to_file(snake_state); + } processing = false; break; } @@ -367,8 +368,9 @@ int32_t snake_game_app(void* p) { release_mutex(&state_mutex, snake_state); } - if(snake_state->isNewHighscore) + 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);