1 #include <string.h>
2 #include <shadowdive/shadowdive.h>
3 #include "utils/log.h"
4 #include "resources/pathmanager.h"
5 #include "resources/scores.h"
6 
scores_clear(scoreboard * sb)7 void scores_clear(scoreboard *sb) {
8     for(int i = 0; i < 4; i++) {
9         for(int m = 0; m < 20; m++) {
10             sb->entries[i][m].score = 0;
11             sb->entries[i][m].har_id = 0;
12             sb->entries[i][m].pilot_id = 0;
13             strcpy(sb->entries[i][m].name, "");
14         }
15     }
16 }
17 
scores_read(scoreboard * sb)18 int scores_read(scoreboard *sb) {
19     sd_score score_file;
20     if(sd_score_create(&score_file) != SD_SUCCESS) {
21         goto error_0;
22     }
23     if(sd_score_load(&score_file, pm_get_local_path(SCORE_PATH)) != SD_SUCCESS) {
24         PERROR("Failure while attempting to open scores file!");
25         goto error_1;
26     }
27     DEBUG("Loaded scores file successfully!");
28 
29     // Fetch data
30     for(int i = 0; i < 4; i++) {
31         for(int m = 0; m < 20; m++) {
32             sb->entries[i][m].score = score_file.scores[i][m].score;
33             sb->entries[i][m].har_id = score_file.scores[i][m].har_id;
34             sb->entries[i][m].pilot_id = score_file.scores[i][m].pilot_id;
35             strcpy(sb->entries[i][m].name, score_file.scores[i][m].name);
36         }
37     }
38 
39     // All done, free raw data
40     sd_score_free(&score_file);
41     return 0;
42 
43 error_1:
44     sd_score_free(&score_file);
45 error_0:
46     return 1;
47 }
48 
scores_write(scoreboard * sb)49 int scores_write(scoreboard *sb) {
50     sd_score score_file;
51     if(sd_score_create(&score_file) != SD_SUCCESS) {
52         return 1;
53     }
54 
55     // Convert data
56     for(int i = 0; i < 4; i++) {
57         for(int m = 0; m < 20; m++) {
58             score_file.scores[i][m].score = sb->entries[i][m].score;
59             score_file.scores[i][m].har_id = sb->entries[i][m].har_id;
60             score_file.scores[i][m].pilot_id = sb->entries[i][m].pilot_id;
61             strcpy(score_file.scores[i][m].name, sb->entries[i][m].name);
62         }
63     }
64 
65     // Save
66     sd_score_save(&score_file, pm_get_local_path(SCORE_PATH));
67 
68     // All done
69     sd_score_free(&score_file);
70     return 0;
71 }
72