From 5fe9368c00362ff5ca82b5b1fe13016675f54e71 Mon Sep 17 00:00:00 2001
From: Adriel <NuquiA@cardiff.ac.uk>
Date: Wed, 26 Oct 2022 11:41:27 +0100
Subject: [PATCH] Leaderboard (not integrated into game.py yet)

---
 Group_game/leaderboard.py | 65 +++++++++++++++++++++++++++++++++++++++
 Times.csv                 |  1 +
 2 files changed, 66 insertions(+)
 create mode 100644 Group_game/leaderboard.py
 create mode 100644 Times.csv

diff --git a/Group_game/leaderboard.py b/Group_game/leaderboard.py
new file mode 100644
index 0000000..a723bda
--- /dev/null
+++ b/Group_game/leaderboard.py
@@ -0,0 +1,65 @@
+import csv, datetime
+from email.utils import format_datetime
+from datetime import datetime
+from csv import reader
+
+def view_leaderboard(filename):
+
+    print("________Leaderboard________\n")
+
+    with open(str(filename)) as csv_file:
+       csv_reader = csv.reader(csv_file, delimiter=',')
+       line_count = 0  #csv reader becomes a 2d list with contents of the Times.csv file
+       for row in csv_reader:
+            if line_count == 0:
+                pass
+                line_count += 1
+            else:
+                time = row[2] #00:00:00 format
+                minutes = time // 60
+                seconds = time % 60
+                time = [round(minutes), round(seconds)]
+                name = row[1]
+
+                if minutes[0] == '0':
+                    minutes = minutes[1]
+
+                print(f'{row[1]} escaped in {time[0]} minutes and {time[1]} seconds!')
+                line_count += 1
+
+def append_leaderboard(seconds_used,filename):  #time_used = 20 minutes - time remaining
+
+    minutes = seconds_used // 60
+    seconds = seconds_used % 60
+    time = [round(minutes), round(seconds)]
+
+    print("Congrats, enter your name on the leaderboard!")
+    name = input("What's your name? :")
+    print('Well done! '+name)
+    print(f'You escaped in {time[0]} minutes and {time[1]} seconds')
+
+    file = open(str(filename), 'r') 
+    csv_reader = reader(file)
+    list_of_rows = list(csv_reader)
+
+    lastrow = list_of_rows[-1]
+    old_index = lastrow[0] 
+
+    if old_index == 'index':
+        old_index = 0
+
+    newindex = int(old_index) + 1
+    file.close()
+
+    file = open(str(filename), 'w')
+    for row in list_of_rows:
+        
+        newRecord = str(row[0]) + "," + str(row[1]) + "," + str(row[2]) + "\n"
+        file.write(newRecord)
+        print(newRecord)
+
+    lastRecord = str(newindex) + "," + str(name) + "," + str(seconds_used) + "\n"
+    file.write(str(lastRecord))       #Writes data into the file
+    file.close()
+
+
diff --git a/Times.csv b/Times.csv
new file mode 100644
index 0000000..9790dd2
--- /dev/null
+++ b/Times.csv
@@ -0,0 +1 @@
+index,name,seconds
\ No newline at end of file
-- 
GitLab