users.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include "utils.h"
  6. User *head;
  7. User *current;
  8. User *array_end;
  9. int total_users;
  10. int read_users() {
  11. FILE *users_file;
  12. int error;
  13. users_file = fopen("user_data", "rb");
  14. if (users_file == NULL) {
  15. fprintf(stderr, "Error: could not open file\n");
  16. return 1;
  17. }
  18. error = fread(&total_users, sizeof(int), 1, users_file);
  19. if (error) {
  20. User* buffer = malloc(total_users*sizeof(User));
  21. fread(buffer, sizeof(User), total_users, users_file);
  22. head = buffer;
  23. User *temp = head;
  24. for (int i = 1; i < total_users; i++) {
  25. temp->next = buffer+i;
  26. temp = buffer+i;
  27. }
  28. array_end = buffer + total_users - 1;
  29. }
  30. else
  31. {
  32. total_users = 0;
  33. array_end = NULL;
  34. }
  35. error = fclose(users_file);
  36. if (error != 0) {
  37. fprintf(stderr, "Error: fclose failed\n");
  38. return 1;
  39. }
  40. return 0;
  41. }
  42. int write_users() {
  43. User *temp = head;
  44. User buffer[total_users];
  45. FILE *users_file;
  46. int error;
  47. users_file = fopen("user_data", "wb");
  48. if (users_file == NULL) {
  49. fprintf(stderr, "Error: could not open file\n");
  50. return 1;
  51. }
  52. fwrite(&total_users, sizeof(int), 1, users_file);
  53. for (int i = 0; i < total_users; i++) {
  54. buffer[i] = *temp;
  55. temp = temp->next;
  56. }
  57. error = fwrite(buffer, sizeof(User), total_users, users_file);
  58. if (error != total_users) {
  59. fprintf(stderr, "Error: fwrite failed\n");
  60. return 1;
  61. }
  62. error = fclose(users_file);
  63. if (error != 0) {
  64. fprintf(stderr, "Error: fclose failed\n");
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. int login() {
  70. char name[9];
  71. char c;
  72. int invalid_input = 1;
  73. system("clear");
  74. while(invalid_input) {
  75. printf("Username: ");
  76. scanf("%8s", name);
  77. invalid_input = 0;
  78. for (int i = 0; i < strlen(name); i++) {
  79. c = name[i];
  80. if ((!isalnum(c)) && (c != '-') && (c != '_')) {
  81. printf("Invlaid character: %c\n", c);
  82. invalid_input = 1;
  83. }
  84. }
  85. }
  86. if (head == NULL) {
  87. head = malloc(sizeof(User));
  88. strcpy(head->name, name);
  89. head->max_score = 0;
  90. head->total_games = 0;
  91. head->total_score = 0;
  92. head->next = NULL;
  93. current = head;
  94. total_users++;
  95. return -4;
  96. }
  97. else {
  98. User *temp = head;
  99. while(temp->next != NULL && strcmp(name, temp->name)) {
  100. temp = temp->next;
  101. }
  102. if (strcmp(name, temp->name)) {
  103. User* new_user = malloc(sizeof(User));
  104. strcpy(new_user->name, name);
  105. new_user->max_score = 0;
  106. new_user->total_games = 0;
  107. new_user->total_score = 0;
  108. new_user->next = NULL;
  109. temp->next = new_user;
  110. current = new_user;
  111. total_users++;
  112. return -4;
  113. }
  114. else {
  115. current = temp;
  116. return -5;
  117. }
  118. }
  119. }
  120. void update_scores(int new_score, int old_score) {
  121. if (new_score > current->max_score) {
  122. current->max_score = new_score;
  123. }
  124. if (!old_score) {
  125. current->total_games++;
  126. }
  127. current->total_score += new_score - old_score;
  128. }
  129. void scoreboard() {
  130. system("clear");
  131. User *temp = head;
  132. printf("Name | Max Score | Total Games | Total Score\n");
  133. while (temp) {
  134. printf("%s, %d, %d, %d\n", temp->name, temp->max_score, temp->total_games, temp->total_score);
  135. temp = temp->next;
  136. }
  137. }
  138. void free_players() {
  139. if (array_end) {
  140. User *curr = array_end->next;
  141. User *temp;
  142. while(curr) {
  143. temp = curr->next;
  144. free(curr);
  145. curr = temp;
  146. }
  147. }
  148. if (head) {
  149. free(head);
  150. }
  151. }