user.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <stdio.h>
  2. #include "boggle.h"
  3. #include <string.h>
  4. int hasBeenUsed(char ** words, char * word){
  5. for(int i =0; i < 50; i++){
  6. if(strcmp(words[i], word) == 0)
  7. return 1;
  8. }
  9. return 0;
  10. }
  11. int validUser(char * str){
  12. if(strlen(str) > 8)
  13. return 0;
  14. for(int i = 0; i < strlen(str); i++)
  15. if(((str[i] < 'a') | (str[i] > 'z')) && ((str[i] != '-') | (str[i] != '.')) && (str[i] < '0') | (str[i] > '9'))
  16. return 0;
  17. return 1;
  18. }
  19. void createLL(User * head){
  20. FILE * userList_c;
  21. userList_c = fopen("users.txt", "r");
  22. char name[9];
  23. int hs, g, ts;
  24. fscanf(userList_c, "%s %d %d %d \n", name, &hs, &g, &ts);
  25. //User * head;
  26. head->name = malloc(strlen(name) + 1);
  27. strcpy(head->name, name);
  28. head->highscore = hs;
  29. head->total_games = g;
  30. head->total_score = ts;
  31. User * curr = head;
  32. while(fscanf(userList_c, "%s %d %d %d \n", name, &hs, &g, &ts) == 4){
  33. User * temp = malloc(sizeof(User));
  34. temp->name = malloc(strlen(name) + 1);
  35. strcpy(temp->name, name);
  36. temp->highscore = hs;
  37. temp->total_games = g;
  38. temp->total_score = ts;
  39. curr->next = temp;
  40. curr = curr->next;
  41. }
  42. fclose(userList_c);
  43. }
  44. int userNumberInFile(char * str){
  45. FILE * userList_r;
  46. userList_r = fopen("users.txt", "r");
  47. char * name = malloc(9);
  48. int hs, g, ts;
  49. int c = 0;
  50. while(fscanf(userList_r, "%s %d %d %d \n", name, &hs, &g, &ts) == 4){
  51. c ++;
  52. if(strcmp(name, str) == 0)
  53. return c;
  54. }
  55. fclose(userList_r);
  56. //free(name);
  57. return -1;
  58. }
  59. int userExists(char * str){
  60. if(userNumberInFile(str) > 0)
  61. return 1;
  62. return 0;
  63. }
  64. int userLogin(){
  65. FILE * userList_a;
  66. userList_a = fopen("users.txt", "a");
  67. char * user = malloc(sizeof(char) * 9);
  68. printf("Hello! Username? \n");
  69. scanf("%s", user);
  70. while(!validUser(user)){
  71. printf("That is not a valid username. Please enter another: \n");
  72. scanf("%s", user);
  73. }
  74. if(userExists(user)){
  75. printf("Welcome back %s!\n", user);
  76. }
  77. else{
  78. printf("Welcome new user!\n");
  79. fprintf(userList_a, "%s %d %d %d\n", user, 0, 0, 0);
  80. }
  81. // free(user);
  82. fclose(userList_a);
  83. int u = userNumberInFile(user);
  84. return u;
  85. }
  86. void userUpdate(User * head, int un, int s){
  87. FILE * userList_w;
  88. userList_w = fopen("users.txt", "w");
  89. User * curr = head;
  90. int c = 0;
  91. while(curr != NULL){
  92. c++;
  93. if(c == un){
  94. if(curr->highscore < s)
  95. curr->highscore = s;
  96. curr->total_games = curr->total_games + 1;
  97. curr->total_score = curr->total_score + s;
  98. }
  99. char * str = curr->name;
  100. fprintf(userList_w, "%s %d %d %d\n", str, curr->highscore, curr->total_games, curr->total_score);
  101. curr = curr->next;
  102. }
  103. fclose(userList_w);
  104. }
  105. void freeLL(User * head){
  106. User * curr = head;
  107. while(curr != NULL){
  108. free(curr->name);
  109. User * temp = curr;
  110. curr = curr->next;
  111. free(temp);
  112. }
  113. }