Add loading of stats from file

This commit is contained in:
Aidan Haas 2022-12-19 21:33:56 -05:00 committed by GitHub
parent 3d72f4a7dd
commit e096bf71a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 33 deletions

View File

@ -7,8 +7,7 @@
#include "utilities.h"
#include "typing_test.h"
int main() {
void debug_words() {
FILE *words_file;
words_file = fopen("words.txt", "r");
Word_array word_array, return_words;
@ -29,5 +28,24 @@ int main() {
for (i = 0; i < words_to_generate; i++) {
printf("Generated Word: %s %d\n", return_words.words[i].text, return_words.words[i].length);
}
}
void debug_stats() {
FILE *stats_file;
Stat_struct x;
char buf[256], temp[256];
int temp_stat;
stats_file = fopen("stats", "r");
load_stats(stats_file, &x);
for (int i = 0; i < 9; i++) {
printf("%d\n", x.data[i]);
}
}
int main() {
debug_stats();
}
}

View File

@ -269,17 +269,44 @@ void settings_ui(WINDOW *win) {
/* Draws statistics UI to console */
/* Placeholder, will work on next */
void stat_ui(WINDOW *win) {
char ch;
void stat_ui(WINDOW *win, Stat_struct *stats) {
int row = 0;
char ch, temp_str[MAX_STRING], temp_num[MAX_STRING];
clear();
print_centered_text(win, 0, "Statistics");
print_centered_text(win, 3, "Best WPM:");
print_centered_text(win, 4, "Average WPM:");
print_centered_text(win, 5, "Average Accuracy:");
temp_str[0] = '\0';
print_centered_text(win, row, "Statistics");
row += 3;
append_line("Best WPM: ", temp_str);
gcvt(stats->data[BEST_WPM], 5, temp_num);
append_line(temp_num, temp_str);
print_centered_text(win, row, temp_str);
row++;
temp_str[0] = '\0';
append_line("Average WPM: ", temp_str);
/* Calculate here */
append_line(temp_num, temp_str);
print_centered_text(win, row, temp_str);
row++;
temp_str[0] = '\0';
append_line("Tests Completed: ", temp_str);
gcvt(stats->data[TESTS_COMPLETE], 5, temp_num);
append_line(temp_num, temp_str);
print_centered_text(win, row, temp_str);
row++;
temp_str[0] = '\0';
append_line("Time Spent Typing: ", temp_str);
gcvt(stats->data[TIME_TYPED], 5, temp_num);
append_line(temp_num, temp_str);
print_centered_text(win, row, temp_str);
row++;
temp_str[0] = '\0';
print_centered_text(win, 7, "Tests Completed:");
print_centered_text(win, 8, "Time Spent Typing: ");
print_centered_text(win, 10, "10 Word Test: ");
print_centered_text(win, 11, "25 Word Test: ");
@ -346,7 +373,7 @@ int main() {
/* Load stats from stats file */
/* These will be modified as the program runs and the stats file will be updated
upon program exit */
upon program exit. */
load_stats(stats_file, &stats);
clear();
@ -403,7 +430,7 @@ int main() {
run = 0;
} else if (cursor_x == 1 && cursor_y == 2) {
/* Stats */
stat_ui(stdscr);
stat_ui(stdscr, &stats);
} else if (cursor_x == 2 && cursor_y == 2) {
/* Settings */
settings_ui(stdscr);
@ -418,9 +445,10 @@ int main() {
/* Exiting */
refresh();
endwin();
/* Saves stats and exits file */
save_stats(stats_file, &stats);
/* Saves stats and closes file */
/* Open stats first */
/* save_stats(stats_file, &stats); */
return 0;
}

View File

@ -75,25 +75,30 @@ int parse_words_file(FILE *words_file, Word_array *words) {
/* Creates empty stats file */
int create_stats_file(FILE *stats_file) {
/* Numerical Stats */
fputs("Best WPM: 0\n", stats_file);
fputs("10w: 0\n", stats_file);
fputs("25w: 0\n", stats_file);
fputs("50w: 0\n", stats_file);
fputs("100w: 0\n", stats_file);
fputs("Tests completed: 0\n", stats_file);
int i;
for (i = 0; i < 9; i++) {
fputs("0\n", stats_file);
}
/* Dynamic Stats (Used for calculating average WPM, Accuracy, etc) */
fputs("Characters typed: 0\n", stats_file);
fputs("Characters correct: 0\n", stats_file);
fputs("Time typed: 0\n", stats_file);
fclose(stats_file);
return SUCCESS;
}
int load_stats(FILE *stats_file, Stat_struct *stats) {
/* Placeholder */
int i = 0;
int temp_stat;
char buf[256];
while (fgets(buf, sizeof(buf), stats_file) != NULL) {
sscanf(buf, "%d", &temp_stat);
stats->data[i] = temp_stat;
i++;
}
fclose(stats_file);
return SUCCESS;
}
int save_stats(FILE *stats_file, Stat_struct *stats) {
@ -114,5 +119,5 @@ void append_line(char *source, char *target) {
cursor++;
}
target[i] = '\0'; /* End string */
}

View File

@ -7,6 +7,15 @@
#define FAILURE 0
#define TYPING_PROMPT_START_Y 3
#define TYPING_PROMPT_END_Y 6
#define BEST_WPM 0
#define W_10 1
#define W_25 2
#define W_50 3
#define W_100 4
#define TESTS_COMPLETE 5
#define CHARS_TYPED 6
#define CHARS_CORRECT 7
#define TIME_TYPED 8
/* Word Structure
length - length of word
@ -26,9 +35,7 @@ typedef struct {
} Word_array;
typedef struct {
int best_wpm, w_10, w_25, w_50, w_100;
int tests_complete, chars_typed, chars_correct;
double time_typed; /* In seconds */
double data[9];
} Stat_struct;
void clear_word_array(Word_array *array);