Browse Source

still not working, but more stuff has been added

jgabor 1 year ago
parent
commit
632bb8ec48
3 changed files with 66 additions and 10 deletions
  1. 1 1
      Makefile
  2. 40 2
      game_server.c
  3. 25 7
      players.c

+ 1 - 1
Makefile

@@ -7,7 +7,7 @@ boggle_server: game_server.o players.o
 	$(CC) $(CFLAGS) -o boggle_server game_server.o players.o
 
 game_server.o: game_server.c players.o game_server.h
-	$(CC) $(CFLAGS) -c game_server.c players.o
+	$(CC) $(CFLAGS) -c game_server.c
 players.o: players.c game_server.h
 	$(CC) $(CFLAGS) -c players.c
 

+ 40 - 2
game_server.c

@@ -28,7 +28,7 @@ char dice[16][6] = {
 };
 
 int main(int argc, char* argv[]) {
-    struct client *p;
+  //  struct client *p;
 	if (argc > 1)
 		port = (short)(atoi(argv[1]));
 	else
@@ -294,7 +294,45 @@ int do_command(struct client * p, int cmd_argc, char **cmd_argv) {
       }
       sendclient(p, str);
     } else if (strcmp(cmd_argv[0], "top_3") == 0 && cmd_argc == 1) {
-		//TODO produce list of top 3 players with their total score and sentclient as a text
+		    Player* first = NULL;
+        Player* second = NULL;
+        Player* third = NULL;
+
+        Player* temp = player_list;
+        int s;
+        while (temp) {
+          s = temp->total_score;
+          if (!first || s > first->total_score) {
+            third=second;
+            second=first;
+            first=temp;
+          }
+          else if (!second || s > second->total_score) {
+              third=second;
+              second=temp;
+          }
+          else if (!third || s > third->total_score) {
+              third=temp;
+          }
+          temp = temp->next;
+        }
+        char str [BUFFER_SIZE - 10];
+        char str2 [BUFFER_SIZE - 10];
+        if (first) {
+          sprintf(str, "1: %s, %d points\r\n", first->name, first->total_score);
+          if (second) {
+            sprintf(str2, "2: %s, %d points\r\n", second->name, second->total_score);
+            strcat(str, str2);
+            if (third) {
+              sprintf(str2, "3: %s, %d points\r\n", third->name, third->total_score);
+              strcat(str, str2);
+            }
+          }
+        }
+        else {
+          sprintf(str, "No players ever\r\n");
+        }
+        sendclient(p, str);
 
     } else if (strcmp(cmd_argv[0], "add_score") == 0 && cmd_argc == 2) {
         char str [BUFFER_SIZE - 10];

+ 25 - 7
players.c

@@ -1,4 +1,4 @@
-#define _GNU_SOURCE 
+#define _GNU_SOURCE
 #include "game_server.h"
 
 
@@ -8,16 +8,34 @@
  * of players whose head is pointed to by *player_ptr_add.
  */
 int create_player(const char *name, Player **player_ptr_add) {
+  Player * new = malloc(sizeof(Player));
+  strcpy(new->name, name);
+  new->max_score = 0;
+  new->total_games = 0;
+  new->total_score = 0;
+  new->next = NULL;
+  Player * temp = *player_ptr_add;
+  if (temp) {
+    while (temp->next) {
+      temp = temp->next;
+    }
+    temp->next = new;
+  }
+  else {
+    player_ptr_add = &new;
+  }
    return 0;
 }
 
 //TODO - implement this
-/* 
+/*
  * Return a pointer to the player with this name in
- * the list starting with head. Return NULL if no such player exists. 
+ * the list starting with head. Return NULL if no such player exists.
  */
-Player *find_player(const char *name, const Player *head) {    
-    return NULL;
+Player *find_player(const char *name, const Player *head) {
+  Player * temp = (Player *)head;
+  while (temp && !strcmp(temp->name, name)) {
+    temp = temp->next;
+  }
+  return temp;
 }
-
-