snake-pp/src/files.c
2023-12-20 16:35:25 +01:00

147 lines
3.9 KiB
C

#include "game.h"
#include "menu.h"
#include "files.h"
//global struct for storing all data of the 10 best players
playerScore_t topScores[];
int recordsInFile;
//==========================
//==== savePlayerScores ====
//==========================
void savePlayerScore(const char *filename/*int score, int difficulty, const char *playerName, const char *map*/)
{
playerScore_t playerScore;
// copy data into struct
playerScore.score = game.snake.length - config.snakeDefaultLength;
playerScore.difficulty = config.difficulty;
strcpy(playerScore.playerName, ttlStorage.userName);
//strcpy(playerScore.map, "testmap");
strcpy(playerScore.map, storedMaps[ttlStorage.userSelectedMap - 1]->name);
FILE *file;
// open file
file = fopen(filename, "ab");
// write data in file
if (file != NULL)
{
fwrite(&playerScore, sizeof(playerScore_t), 1, file);
fclose(file);
LOGI("Spielergebnis wurde erfolgreich in die Binaerdatei gespeichert.\n");
}
else
{
LOGI("Fehler beim Öffnen der Datei!\n");
}
}
//==========================
//==== readTopScores ======
//==========================
// number of reads depends on 'MAX_PRINTED_SCORES'
void readTopScores(const char *filename)
{
FILE *filePtr;
playerScore_t tempPlayerScore;
int highestPlayerScore = 0;
int count = 0; // increase up to 'MAX_PRINTED_SCORES'
// determine the number of contents in the file
recordsInFile = countRecordsInFile(filename);
// if failure
if(recordsInFile == -1)
{
game.gameState = EXIT;
return;
}
filePtr = fopen(filename, "rb");
// fail with file opening
if (filePtr == NULL)
{
LOGI("Datei: Fehler beim Öffnen der Datei für die besten 10 Ergebnisse!\n");
game.gameState = EXIT;
return;
}
LOGI("Datei: Datensaetze in Datei: %d\n", recordsInFile);
//---- search for the highest score------
for (int i = 0; i < recordsInFile; i++)
{
fread(&tempPlayerScore, sizeof(playerScore_t), 1, filePtr);
if(tempPlayerScore.score > highestPlayerScore)
{
highestPlayerScore = tempPlayerScore.score;
}
}
//--- decrease highest score -----
while((count < MAX_PRINTED_SCORES) && (count < recordsInFile))
{
// set file pointer to start of the file
rewind(filePtr);
// search for the highest score and then save it in topScores
for (int i = 0; i < recordsInFile; i++)
{
// read record from the file
fread(&tempPlayerScore, sizeof(playerScore_t), 1, filePtr);
// current highscore found
if(tempPlayerScore.score == highestPlayerScore)
{
topScores[count] = tempPlayerScore;
LOGI("Datei: score: %d name: %s schwierigkeit: %d map: %s\n", topScores[count].score, topScores[count].playerName, topScores[count].difficulty, topScores[count].map);
count++;
}
// leave if limit is reached
if(count >= recordsInFile || count >= MAX_PRINTED_SCORES)
{
break;
}
}
highestPlayerScore--;
}
fclose(filePtr);
}
//==========================
//==== readTop10Scores =====
//==========================
// return the number of various scores of the file
int countRecordsInFile(const char *filename)
{
FILE *file;
file = fopen(filename, "rb");
// failure
if (file == NULL)
{
LOGI("Datei: Fehler beim Öffnen der Datei!\n");
return -1;
}
fseek(file, 0, SEEK_END); // Gehe zum Dateiende
long fileSize = ftell(file); // Hole die Größe der Datei in Bytes
fclose(file);
int recordSize = sizeof(playerScore_t);
int numberOfRecords = fileSize / recordSize; // Berechne die Anzahl der Datensätze
return numberOfRecords;
}