-
Notifications
You must be signed in to change notification settings - Fork 1
/
rubiks_database.py
163 lines (127 loc) · 4.94 KB
/
rubiks_database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import pyrebase
from datetime import datetime
# Configure Firebase
config = {
"apiKey": "AIzaSyDyTulUCX5swJauYm8YM55Vr5a5vv8ipPQ",
"authDomain": "rubiksscanner.firebaseapp.com",
"databaseURL": "https://rubiksscanner.firebaseio.com",
"storageBucket": "rubiksscanner.appspot.com"
}
# Get database
firebase = pyrebase.initialize_app(config)
db = firebase.database()
# Used to compute the average number of seconds to complete each round
def computeAvgSeconds(comp_id, times):
all_times = []
for r, time in enumerate(times):
# add each round's time to database
round = r + 1
db.child("EventName").child("Competitors").child(comp_id).child(round).set(time)
# get time in total seconds
time = time.replace(".", ":")
divided = time.split(":")
minutes = divided[0]
seconds = divided[1]
miliseconds = divided[2]
total_seconds = float(minutes) * 60 + float(seconds) + float(miliseconds) * 0.001
# print(total_seconds)
# add each time to list
all_times.append(total_seconds)
# remove max and min time from list
all_times.remove(max(all_times))
all_times.remove(min(all_times))
# compute average of remaining times
average_time = sum(all_times) / 3
return average_time
# Used to convert average time back to string format
def convertTimeFormat(average_time):
int_time = int(average_time)
miliseconds = "%.3f" % (average_time - int_time)
minutes = int(average_time / 60)
seconds = int_time - minutes * 60
# add 0 to front of minutes if <10
minutes = str(minutes)
if len(minutes) == 1:
minutes = "0" + minutes
# same with seconds
seconds = str(seconds)
if len(seconds) == 1:
seconds = "0" + seconds
avg_string = minutes + ":" + seconds + "." + str(miliseconds[2:])
return avg_string
# Add info to database given id of competitor and array of 5 strings representing
# the times it took to complete each round
def addInfoToDatabase(comp_id, times, flagged):
seconds = computeAvgSeconds(comp_id, times)
print(seconds)
avg_time = convertTimeFormat(seconds)
print(avg_time)
# create strings to add to database for flagged solve times
flagged_id = ""
for f in flagged[0]:
flagged_id += f + ","
flagged_1 = ""
for f in flagged[1]:
flagged_1 += f + ","
flagged_2 = ""
for f in flagged[2]:
flagged_2 += f + ","
flagged_3 = ""
for f in flagged[3]:
flagged_3 += f + ","
flagged_4 = ""
for f in flagged[4]:
flagged_4 += f + ","
flagged_5 = ""
for f in flagged[5]:
flagged_5 += f + ","
# add the average time and average seconds to database
db.child("EventName").child("Competitors").child(comp_id).child("seconds").set(seconds)
db.child("EventName").child("Competitors").child(comp_id).child("avg").set(avg_time)
db.child("EventName").child("Competitors").child(comp_id).child("Flagged").child("ID").set(flagged_id)
db.child("EventName").child("Competitors").child(comp_id).child("Flagged").child(1).set(flagged_1)
db.child("EventName").child("Competitors").child(comp_id).child("Flagged").child(2).set(flagged_2)
db.child("EventName").child("Competitors").child(comp_id).child("Flagged").child(3).set(flagged_3)
db.child("EventName").child("Competitors").child(comp_id).child("Flagged").child(4).set(flagged_4)
db.child("EventName").child("Competitors").child(comp_id).child("Flagged").child(5).set(flagged_5)
# Return Dictionary of competitors organized in order of average completion time
def getWinners():
# dictionary of competitors
winners_dict = {}
competitors = db.child("EventName").child("Competitors").get()
# add competitor id/times to dictionary and check if times are correct
for c in competitors.each():
if type(c.val()) is dict:
comp_id = str(c.key())
seconds = c.val()['seconds']
round_1 = c.val()['1']
round_2 = c.val()['2']
round_3 = c.val()['3']
round_4 = c.val()['4']
round_5 = c.val()['5']
times = [round_1, round_2, round_3, round_4, round_5]
new_seconds = computeAvgSeconds(comp_id, times)
# check to see if round data has been changed
if (new_seconds == seconds):
winners_dict[comp_id] = seconds
else:
# if data changed, update average times in database
print("in else for id" + comp_id)
winners_dict[comp_id] = new_seconds
new_average = convertTimeFormat(new_seconds)
db.child("EventName").child("Competitors").child(comp_id).child("seconds").set(new_seconds)
db.child("EventName").child("Competitors").child(comp_id).child("avg").set(new_average)
# sort dictionary by average seconds
ordered_winners = sorted(winners_dict.items(), key=lambda x: x[1])
print("Winners") # Print winners
place = 1
for key, value in ordered_winners:
# get average time for each competitor from database
time = db.child("EventName").child("Competitors").child(str(key)).child('avg').get()
print(str(place) + ". Competitor id " + str(key) + ": " + time.val())
place += 1
return ordered_winners
# times = ["01:23:456", "02:04:818", "05:04:321", "03:14:888", "01:52:582"]
# flagged = [[], [], ["6"], [], [], ["1"]]
# addInfoToDatabase("880", times, flagged)
# getWinners()