This commit is contained in:
RogueMaster
2022-10-25 00:20:29 -04:00
parent 9691650616
commit a21df918d6
3 changed files with 65 additions and 46 deletions

View File

@@ -4,7 +4,7 @@
#include <flipper_format/flipper_format.h>
static void snake_game_close_file(FlipperFormat* file) {
if(file == NULL){
if(file == NULL) {
furi_record_close(RECORD_STORAGE);
return;
}
@@ -27,7 +27,8 @@ static FlipperFormat* snake_game_open_file(Storage* storage) {
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;
@@ -39,19 +40,19 @@ int16_t snake_game_save_score_to_file(int32_t len) {
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)){
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){
} else {
if(len > temp) {
flipper_format_rewind(file);
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)){
if(!flipper_format_insert_or_update_int32(file, SNAKE_GAME_CONFIG_HIGHSCORE, &len, 1)) {
snake_game_close_file(file);
return -1;
}
}else{
} else {
len = temp;
}
}
@@ -64,41 +65,45 @@ void snake_game_save_game_to_file(SnakeState* const snake_state) {
FlipperFormat* file = snake_game_open_file(storage);
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);
@@ -118,7 +123,7 @@ bool snake_game_init_game_from_file(SnakeState* const snake_state) {
furi_string_free(file_type);
uint32_t temp;
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;
}
@@ -127,35 +132,35 @@ 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;
}
snake_state->nextMovement = temp;
flipper_format_delete_key(file, SNAKE_GAME_CONFIG_KEY_NEXT_MOVEMENT);
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;
}