From 3d72f4a7dd1fdfa4875e9bdeeffa59b08cfe8fce Mon Sep 17 00:00:00 2001 From: Aidan Haas <94150901+ahaas25@users.noreply.github.com> Date: Sat, 17 Dec 2022 23:35:29 -0500 Subject: [PATCH] Begin implementation of stats --- typing_test.c | 21 +++++++++++++++++++-- utilities.c | 30 ++++++++++++++++++++++++++++++ utilities.h | 9 +++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/typing_test.c b/typing_test.c index f307cad..890f07d 100644 --- a/typing_test.c +++ b/typing_test.c @@ -295,8 +295,9 @@ void stat_ui(WINDOW *win) { /* Main function. Creates main menu */ int main() { - FILE *words_file; + FILE *words_file, *stats_file; Word_array *word_array; + Stat_struct stats; int cursor_x = 0, cursor_y = 0, run = 1; int ch; @@ -306,8 +307,9 @@ int main() { keypad(stdscr, TRUE); noecho(); - words_file = fopen("words.txt", "r"); print_centered_text(stdscr, 4, "Loading..."); + words_file = fopen("words.txt", "r"); + stats_file = fopen("stats", "r"); if (has_colors() == FALSE) { print_centered_text(stdscr, 4, "Your terminal does not support color"); @@ -335,6 +337,18 @@ int main() { parse_words_file(words_file, word_array); } + /* Creates a new stats file if one is not detected */ + if (stats_file == NULL) { + stats_file = fopen("stats", "w"); + create_stats_file(stats_file); + stats_file = fopen("stats", "r"); + } + + /* Load stats from stats file */ + /* These will be modified as the program runs and the stats file will be updated + upon program exit */ + load_stats(stats_file, &stats); + clear(); while (run) { @@ -404,6 +418,9 @@ int main() { /* Exiting */ refresh(); endwin(); + + /* Saves stats and exits file */ + save_stats(stats_file, &stats); return 0; } \ No newline at end of file diff --git a/utilities.c b/utilities.c index 478cc3e..8c4e980 100644 --- a/utilities.c +++ b/utilities.c @@ -73,6 +73,36 @@ int parse_words_file(FILE *words_file, Word_array *words) { return SUCCESS; } +/* 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); + + /* 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 save_stats(FILE *stats_file, Stat_struct *stats) { + /* Placeholder */ + + fclose(stats_file); +} + + /* Appends first parameter to the end of end of the second parameter (Does not change size of the target, assumes there is space) */ void append_line(char *source, char *target) { diff --git a/utilities.h b/utilities.h index 45cc91c..97d8982 100644 --- a/utilities.h +++ b/utilities.h @@ -25,7 +25,16 @@ typedef struct { Word *words; } 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 */ +} Stat_struct; + void clear_word_array(Word_array *array); int generate_words(int num_words, Word_array *words, Word_array *to_return); int parse_words_file(FILE *words_file, Word_array *words); +int create_stats_file(FILE *stats_file); +int load_stats(FILE *stats_file, Stat_struct *stats); +int save_stats(FILE *stats_file, Stat_struct *stats); void append_line(char *source, char *target); \ No newline at end of file