Index: proj/src/data/scoreboard.txt
===================================================================
--- proj/src/data/scoreboard.txt	(nonexistent)
+++ proj/src/data/scoreboard.txt	(revision 375)
@@ -0,0 +1,3 @@
+06/01/20 15:01:27 - 6 - 38
+06/01/20 13:01:24 - 0 - 0
+06/01/20 16:01:54 - 0 - 0

Property changes on: proj/src/data/scoreboard.txt
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: proj/src/project/include/scoreboards.h
===================================================================
--- proj/src/project/include/scoreboards.h	(revision 374)
+++ proj/src/project/include/scoreboards.h	(revision 375)
@@ -48,7 +48,15 @@
  * @param   path    Path to file to be written
  */
 void (highscores_save)(const highscores_t *p, const char *path);
+
 /**
+ * @brief Checks if score is higher than any of the top 3, and if so replaces the highscores.
+ * @param   p               Pointer to highscores.
+ * @param   score           New score obtained.
+ * @param   time_played     Time played
+ */
+void (check_new_score)(highscores_t *p, int score, int time_played);
+/**
  * @brief Destruct menu.
  * @param   p   Pointer to menu to destruct
  */
Index: proj/src/project/include/zombies.h
===================================================================
--- proj/src/project/include/zombies.h	(revision 374)
+++ proj/src/project/include/zombies.h	(revision 375)
@@ -10,13 +10,21 @@
  * @{
  */
 
+#include "scoreboards.h"
+
 /**
 * @brief Zombies.
 * @return  SUCCESS if operation was successful, other value otherwise
 */
-int (zombies)(void);
+int (zombies)(highscores_t *zombies_highscore);
 
 /**
+ * @brief Zombies ranking.
+ * @param zombies_highscore Highscores.
+ */
+void (zombies_ranking)(highscores_t *zombie_highscore);
+
+/**
  * @}
  */
 
Index: proj/src/project/src/proj.c
===================================================================
--- proj/src/project/src/proj.c	(revision 374)
+++ proj/src/project/src/proj.c	(revision 375)
@@ -41,6 +41,8 @@
 #include "singleplayer.h"
 static int (multiplayer)(void);
 #include "chat.h"
+
+#include "scoreboards.h"
 int(proj_main_loop)(int argc, char *argv[]) {
     (void)argc; (void)argv;
 
Index: proj/src/project/src/proj_func.c
===================================================================
--- proj/src/project/src/proj_func.c	(revision 374)
+++ proj/src/project/src/proj_func.c	(revision 375)
@@ -245,6 +245,7 @@
     text_set_string(p->text, buffer);
 }
 void (text_timer_reset)(text_timer_t *p){
+    p->time = 0;
     text_set_string(p->text, "000s");
 }
 void (text_timer_dtor)(text_timer_t *p){
Index: proj/src/project/src/scoreboards.c
===================================================================
--- proj/src/project/src/scoreboards.c	(revision 374)
+++ proj/src/project/src/scoreboards.c	(revision 375)
@@ -9,8 +9,6 @@
 #include "rtc.h"
 #include "errors.h"
 
-#include <stdio.h>
-
 #define MAX_HIGHSCORES   3 /** @brief Number of scores saved */
 
 struct  score_info {
@@ -37,12 +35,12 @@
     score_info_t *ret = (score_info_t*)malloc(sizeof(score_info_t));
     if (ret == NULL) return NULL;
     uint8_t score_date[4];
-    if (rtc_read_date(score_date)) return NULL;
+    if (rtc_read_time(score_date)) return NULL;
     ret->sec = score_date[0];
     ret->min = score_date[1];
     ret->hour = score_date[2];
 
-    if (rtc_read_time(score_date)) return NULL;
+    if (rtc_read_date(score_date)) return NULL;
 
     ret->day = score_date[1];
     ret->month = score_date[2];
@@ -62,17 +60,16 @@
     if(fnt == NULL || path == NULL) return NULL;
 
     FILE *f = fopen(path, "r");
-    if (f == NULL) return NULL;
+    if (f == NULL) { printf("NULL\n");return NULL;}
     highscores_t *ret = (highscores_t*)malloc(sizeof(highscores_t));
     if (ret == NULL) return NULL;
-    char line[30];
     for (int i = 0; i < MAX_HIGHSCORES; i++) {
-        fgets(line, 30, f);
         score_info_t *score = (score_info_t*)malloc(sizeof(score_info_t));
+        memset(score, 0, sizeof(score_info_t));
 
-        sscanf(line, "%d/%d/%d %d:%d:%d - %d - %d\n",
+        fscanf(f, "%02d/%02d/%02d %02d:%02d:%02d - %d - %d\n",
                     &(score->day), &(score->month), &(score->year),
-                    &(score->hour), &(score->month), &(score->sec),
+                    &(score->hour), &(score->min), &(score->sec),
                     &(score->score), &(score->time_played));
         ret->highscores[i] = score;
     }
@@ -81,6 +78,35 @@
     return ret;
 }
 
+void (check_new_score)(highscores_t *p, int score, int time_played){
+    score_info_t *new_score = score_ctor(score, time_played);
+    if (new_score == NULL) return;
+    if (score > (p->highscores[0])->score) { // rank 1
+        score_info_t *s = p->highscores[0];
+        p->highscores[0] = new_score;
+        new_score = s;
+        s = p->highscores[1];
+        p->highscores[1] = new_score;
+        new_score = s;
+        s = p->highscores[2];
+        p->highscores[2] = new_score;
+        score_dtor(s);
+    } else if (score > (p->highscores[1])->score) { // rank 2
+        score_info_t *s = p->highscores[1];
+        p->highscores[1] = new_score;
+        new_score = s;
+        s = p->highscores[2];
+        p->highscores[2] = new_score;
+        score_dtor(s);
+    } else if (score > (p->highscores[2])->score) { // rank 3
+        score_info_t *s = p->highscores[2];
+        p->highscores[2] = new_score;
+        score_dtor(s);
+    } else {
+        score_dtor(new_score);
+    }
+}
+
 void (highscores_save)(const highscores_t *p, const char *path) {
     if(p == NULL || path == NULL) return;
 
@@ -88,7 +114,7 @@
     if (f == NULL) return;
     for (int i = 0; i < MAX_HIGHSCORES; i++) {
         score_info_t *score = p->highscores[i];
-        fprintf(f, "%d/%d/%d %d:%d:%d - %d - %d\n",
+        fprintf(f, "%02d/%02d/%02d %02d:%02d:%02d - %d - %d\n",
                     (score->day), (score->month), (score->year),
                     (score->hour), (score->month), (score->sec),
                     (score->score), (score->time_played));
@@ -97,7 +123,9 @@
 }
 
 void (highscores_dtor)(highscores_t *p) {
+    if (p == NULL) return;
     for (int i = 0; i < MAX_HIGHSCORES; i++) {
         score_dtor(p->highscores[i]);
     }
+    free(p);
 }
Index: proj/src/project/src/singleplayer.c
===================================================================
--- proj/src/project/src/singleplayer.c	(revision 374)
+++ proj/src/project/src/singleplayer.c	(revision 375)
@@ -21,8 +21,11 @@
     menu_t *main_menu = menu_ctor(font_get_default());
     menu_add_item(main_menu, "Campaign");
     menu_add_item(main_menu, "Zombies");
+    menu_add_item(main_menu, "Zombies Ranking");
     menu_add_item(main_menu, "Back");
 
+    highscores_t *zombies_highscore = highscores_ctor(font_get_default(), "/home/lcom/labs/proj/src/data/scoreboard.txt");
+
     //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
     uint8_t last_lb = 0;
     struct packet pp;
@@ -46,8 +49,9 @@
                     switch(menu_update_state(main_menu, click)){
                         case -1: break;
                         case  0: campaign(); break;
-                        case  1: zombies(); break;
-                        case  2: good = false; break;
+                        case  1: zombies(zombies_highscore); break;
+                        case  2: zombies_ranking(zombies_highscore); break;
+                        case  3: good = false; break;
                     }
                     menu_draw(main_menu);
 
@@ -75,5 +79,10 @@
         }
     }
 
+    highscores_save(zombies_highscore, "/home/lcom/labs/proj/src/data/scoreboard.txt");
+    highscores_dtor(zombies_highscore);
+
+    menu_dtor(main_menu);
+
     return 0;
 }
Index: proj/src/project/src/zombies.c
===================================================================
--- proj/src/project/src/zombies.c	(revision 374)
+++ proj/src/project/src/zombies.c	(revision 375)
@@ -14,7 +14,7 @@
 
 #define ZOMBIES_NUM             5
 #define ZOMBIE_HEALTH_FACTOR    1.1
-int (zombies)(void){
+int (zombies)(highscores_t *zombies_highscore){
 
     int r;
 
@@ -43,6 +43,11 @@
     uint64_t int_vector = 0;
     int good = true;
     int dead = false;
+    /// Kills
+    int kills = -ZOMBIES_NUM;
+    text_t *text_kills = text_ctor(font_get_default(), "Kills: 0");
+    text_set_color(text_kills, TEXT_COLOR);
+    text_set_pos(text_kills, 200, 0);
 
     int health = 50;
 
@@ -86,6 +91,10 @@
                         health *= ZOMBIE_HEALTH_FACTOR;
                         get_random_spawn(map1, zombie, shooter_list);
                         list_push_back(shooter_list, zombie);
+                        kills++;
+                        char buffer[20];
+                        sprintf(buffer, "Kills: %d", kills);
+                        text_set_string(text_kills, buffer);
                     }
 
                     graph_clear_screen();
@@ -92,8 +101,8 @@
                     map_draw   (map1);
                     bullet_draw_list(bullet_list);
                     gunner_draw_list(shooter_list);
-
                     text_draw(in_game_timer->text);
+                    text_draw(text_kills);
 
                     sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
                     sprite_draw(sp_crosshair);
@@ -135,11 +144,15 @@
     }
     if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
 
-    if(dead){
-        printf("YOU DIED\n");
-    }
+    check_new_score(zombies_highscore, kills, in_game_timer->time);
 
+    text_dtor(text_kills);
+
     text_timer_dtor(in_game_timer); in_game_timer = NULL;
 
     return SUCCESS;
 }
+
+void (zombies_ranking)(highscores_t *zombies_highscore) {
+
+}
Index: proj/src/Makefile
===================================================================
--- proj/src/Makefile	(revision 374)
+++ proj/src/Makefile	(revision 375)
@@ -12,7 +12,7 @@
 SRCS= list.c queue.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c fast_math.c xpm_utils.c rtc.c uart.c makecode_map.c menu.c proj_func.c rectangle.c font.c ent.c proj.c hltp.c zombies.c campaign.c singleplayer.c chat.c scoreboards.c
 IPATHS=-I./project/include -I./libs -I./libs/graph/include -I./libs/kbc/include -I./libs/rtc/include -I./libs/timer/include -I./libs/uart/include -I./libs/classes/include -I./libs/utils/include -I./maps -I./media/xpm
 
-CPPFLAGS += -pedantic ${IPATHS} -D LCOM_MACRO -D __LCOM_OPTIMIZED__ -Wall -Wextra -Wshadow -Wunreachable-code #-Weverything -Wno-padded -Wno-unused-macros
+CPPFLAGS += -pedantic ${IPATHS} -D LCOM_MACRO #-D __LCOM_OPTIMIZED__ -Wall -Wextra -Wshadow -Wunreachable-code #-Weverything -Wno-padded -Wno-unused-macros
 
 DPADD += ${LIBLCF}
 LDADD += -llcf
