Add loading of stats from file
This commit is contained in:
parent
3d72f4a7dd
commit
e096bf71a7
24
debugger.c
24
debugger.c
@ -7,8 +7,7 @@
|
|||||||
#include "utilities.h"
|
#include "utilities.h"
|
||||||
#include "typing_test.h"
|
#include "typing_test.h"
|
||||||
|
|
||||||
|
void debug_words() {
|
||||||
int main() {
|
|
||||||
FILE *words_file;
|
FILE *words_file;
|
||||||
words_file = fopen("words.txt", "r");
|
words_file = fopen("words.txt", "r");
|
||||||
Word_array word_array, return_words;
|
Word_array word_array, return_words;
|
||||||
@ -29,5 +28,24 @@ int main() {
|
|||||||
for (i = 0; i < words_to_generate; i++) {
|
for (i = 0; i < words_to_generate; i++) {
|
||||||
printf("Generated Word: %s %d\n", return_words.words[i].text, return_words.words[i].length);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
@ -269,17 +269,44 @@ void settings_ui(WINDOW *win) {
|
|||||||
|
|
||||||
/* Draws statistics UI to console */
|
/* Draws statistics UI to console */
|
||||||
/* Placeholder, will work on next */
|
/* Placeholder, will work on next */
|
||||||
void stat_ui(WINDOW *win) {
|
void stat_ui(WINDOW *win, Stat_struct *stats) {
|
||||||
char ch;
|
int row = 0;
|
||||||
|
char ch, temp_str[MAX_STRING], temp_num[MAX_STRING];
|
||||||
clear();
|
clear();
|
||||||
print_centered_text(win, 0, "Statistics");
|
|
||||||
|
|
||||||
print_centered_text(win, 3, "Best WPM:");
|
temp_str[0] = '\0';
|
||||||
print_centered_text(win, 4, "Average WPM:");
|
|
||||||
print_centered_text(win, 5, "Average Accuracy:");
|
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, 10, "10 Word Test: ");
|
||||||
print_centered_text(win, 11, "25 Word Test: ");
|
print_centered_text(win, 11, "25 Word Test: ");
|
||||||
@ -346,7 +373,7 @@ int main() {
|
|||||||
|
|
||||||
/* Load stats from stats file */
|
/* Load stats from stats file */
|
||||||
/* These will be modified as the program runs and the stats file will be updated
|
/* 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);
|
load_stats(stats_file, &stats);
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
@ -403,7 +430,7 @@ int main() {
|
|||||||
run = 0;
|
run = 0;
|
||||||
} else if (cursor_x == 1 && cursor_y == 2) {
|
} else if (cursor_x == 1 && cursor_y == 2) {
|
||||||
/* Stats */
|
/* Stats */
|
||||||
stat_ui(stdscr);
|
stat_ui(stdscr, &stats);
|
||||||
} else if (cursor_x == 2 && cursor_y == 2) {
|
} else if (cursor_x == 2 && cursor_y == 2) {
|
||||||
/* Settings */
|
/* Settings */
|
||||||
settings_ui(stdscr);
|
settings_ui(stdscr);
|
||||||
@ -418,9 +445,10 @@ int main() {
|
|||||||
/* Exiting */
|
/* Exiting */
|
||||||
refresh();
|
refresh();
|
||||||
endwin();
|
endwin();
|
||||||
|
|
||||||
/* Saves stats and exits file */
|
/* Saves stats and closes file */
|
||||||
save_stats(stats_file, &stats);
|
/* Open stats first */
|
||||||
|
/* save_stats(stats_file, &stats); */
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
33
utilities.c
33
utilities.c
@ -75,25 +75,30 @@ int parse_words_file(FILE *words_file, Word_array *words) {
|
|||||||
|
|
||||||
/* Creates empty stats file */
|
/* Creates empty stats file */
|
||||||
int create_stats_file(FILE *stats_file) {
|
int create_stats_file(FILE *stats_file) {
|
||||||
/* Numerical Stats */
|
int i;
|
||||||
fputs("Best WPM: 0\n", stats_file);
|
|
||||||
fputs("10w: 0\n", stats_file);
|
for (i = 0; i < 9; i++) {
|
||||||
fputs("25w: 0\n", stats_file);
|
fputs("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);
|
fclose(stats_file);
|
||||||
|
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_stats(FILE *stats_file, Stat_struct *stats) {
|
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) {
|
int save_stats(FILE *stats_file, Stat_struct *stats) {
|
||||||
@ -114,5 +119,5 @@ void append_line(char *source, char *target) {
|
|||||||
cursor++;
|
cursor++;
|
||||||
}
|
}
|
||||||
target[i] = '\0'; /* End string */
|
target[i] = '\0'; /* End string */
|
||||||
|
|
||||||
}
|
}
|
||||||
13
utilities.h
13
utilities.h
@ -7,6 +7,15 @@
|
|||||||
#define FAILURE 0
|
#define FAILURE 0
|
||||||
#define TYPING_PROMPT_START_Y 3
|
#define TYPING_PROMPT_START_Y 3
|
||||||
#define TYPING_PROMPT_END_Y 6
|
#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
|
/* Word Structure
|
||||||
length - length of word
|
length - length of word
|
||||||
@ -26,9 +35,7 @@ typedef struct {
|
|||||||
} Word_array;
|
} Word_array;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int best_wpm, w_10, w_25, w_50, w_100;
|
double data[9];
|
||||||
int tests_complete, chars_typed, chars_correct;
|
|
||||||
double time_typed; /* In seconds */
|
|
||||||
} Stat_struct;
|
} Stat_struct;
|
||||||
|
|
||||||
void clear_word_array(Word_array *array);
|
void clear_word_array(Word_array *array);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user