-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
119 lines (95 loc) · 5.03 KB
/
server.py
File metadata and controls
119 lines (95 loc) · 5.03 KB
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
from calculations import calculateTIMD, calculateTeam
import export
import pyrebase
import time
import slack
import ast
import os
def reset_timds(database):
for timd in database.child('decompedTIMDs').get().each():
database.child("rawTIMDs").child(timd.key()).set(timd.val())
database.child("decompedTIMDs").child(timd.key()).remove()
def get_num_timds_for_match(match_number, database):
timds = database.child("TIMDs").get().each()
return len([timd for timd in timds if timd.val()['match_number'] == match_number])
def run_server_testing():
firebase = pyrebase.initialize_app(ast.literal_eval(os.environ['firebase_info']))
database = firebase.database()
rawTIMDs = database.child('rawTIMDs').get()
if rawTIMDs.val() is None:
reset_timds(database)
rawTIMDs = database.child('rawTIMDs').get()
for temp_timd in rawTIMDs.each():
timd = calculateTIMD.calculate_timd(temp_timd.val(), temp_timd.key())
database.child("rawTIMDs").child(temp_timd.key()).remove()
team_num = temp_timd.key().split("-")[1]
calculateTeam.calculate_team(team_num, timd)
print("calced, exporting test")
export.export_spreadsheet()
def run_server():
firebase = pyrebase.initialize_app(ast.literal_eval(os.environ['firebase_info']))
database = firebase.database()
slack_token = os.environ['slack_api_key']
slack_client = slack.WebClient(token=slack_token)
slack_channel = os.environ['head_scout_slack_id']
timds_for_current_match = 0
while True:
rawTIMDs = database.child('rawTIMDs').get()
if rawTIMDs.val() is None:
time.sleep(10)
else:
try:
current_unscouted_match = database.child('config').child('currentMatch').get().val()
for temp_timd in rawTIMDs.each():
timd = calculateTIMD.calculate_timd(temp_timd.val(), temp_timd.key())
database.child("rawTIMDs").child(temp_timd.key()).remove()
team_num = temp_timd.key().split("-")[1]
calculateTeam.calculate_team(team_num, timd)
match_num = timd['match_number']
if match_num == current_unscouted_match:
timds_for_current_match += 1
if timds_for_current_match == 6:
print("\nAll TIMDs for QM " + str(current_unscouted_match) + " synced\n")
timds_for_current_match = 0
export.export_spreadsheet()
print("Data exported")
slack_client.chat_postMessage(
channel=slack_channel,
text="All TIMDs for Match " + str(current_unscouted_match) + " processed and data exported"
)
database.child('config').child('currentMatch').set(current_unscouted_match + 1)
elif match_num > current_unscouted_match:
print("WARNING: MISSING TIMD FOR MATCH " + str(current_unscouted_match))
slack_client.chat_postMessage(
channel=slack_channel,
text="WARNING: TIMD for Match " + str(match_num) + " uploaded before Match " +
str(current_unscouted_match) + " had 6 TIMDs!"
)
database.child('config').child('currentMatch').set(match_num)
timds_for_current_match = 1
elif match_num < current_unscouted_match:
print("WARNING: CALCULATING TIMD FROM PAST MATCH " + str(match_num))
if get_num_timds_for_match(match_num, database) == 6:
print("\nAll TIMDs for QM " + str(match_num) + " synced\n")
timds_for_current_match = 0
export.export_spreadsheet()
print("Data exported")
slack_client.chat_postMessage(
channel=slack_channel,
text="All TIMDs for Match " + str(match_num) + "processed and data exported"
)
elif get_num_timds_for_match(match_num, database) > 6:
slack_client.chat_postMessage(
channel=slack_channel,
text="More than 6 TIMDs processed for Match " + str(match_num)
)
else:
print("WARNING: Still missing " + str(6 - get_num_timds_for_match(match_num, database)) + " TIMDs for Match " + str(match_num))
except Exception as e:
slack_client.chat_postMessage(
channel=slack_channel,
text="Exception occurred: " + str(e)
)
exit()
if __name__ == "__main__":
run_server()