-
Notifications
You must be signed in to change notification settings - Fork 0
/
RaceQueue.py
64 lines (49 loc) · 2.59 KB
/
RaceQueue.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
import datetime
import time
import pickle
class RaceQueue():
def __init__(self):
self.waiting_list = []
self.ready_teams = []
self.race_history = []
self.registered_teams = []
def register_team(self, team_id):
if team_id not in self.registered_teams:
self.registered_teams.append(team_id)
def register_teams(self, team_ids):
for team_id in team_ids:
self.register_team(team_id)
def register_attempt(self, team_id):
if team_id in self.registered_teams:
if team_id in self.ready_teams:
self.waiting_list[self.ready_teams.index(team_id)]["ready_time"] = datetime.datetime.now()
waiting_info = self.waiting_list[self.ready_teams.index(team_id)]
else:
waiting_info = {"id":team_id, "ready_time":datetime.datetime.now(), "number_of_attempts":0}
self.waiting_list.append(waiting_info)
self.ready_teams.append(team_id)
print('Team {} have registered an attempt.'.format(team_id))
self.race_history.append(waiting_info)
else:
print('Can\'t find the team name, have you registered it and spell it correct?')
with open('race_history.txt', 'wb') as f:
pickle.dump(self.waiting_list, f)
def next_ready_teams(self, number=3):
sorted_waiting_list = sorted(self.waiting_list, key=lambda d: (d["number_of_attempts"], d["ready_time"]))
for waiting_teams in sorted_waiting_list[:min(number,len(sorted_waiting_list))]:
print('Team {} please wait to start race.'.format(waiting_teams['id']))
def finish_attempt(self, team_id):
if team_id in self.registered_teams:
if team_id in self.ready_teams:
number_of_attempts = self.waiting_list[self.ready_teams.index(team_id)]["number_of_attempts"]
self.race_history.append({"id":team_id,
"finish_time":datetime.datetime.now(),
"number_of_attempts":number_of_attempts})
self.waiting_list[self.ready_teams.index(team_id)]["number_of_attempts"] = number_of_attempts + 1
else:
print("This team haven't registered for an attempt yet!")
with open('race_history.txt', 'wb') as f:
pickle.dump(self.waiting_list, f)
if __name__ == "__main__":
rq = RaceQueue()
print('Please start to register the attempt.')