-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeManager.py
More file actions
70 lines (49 loc) · 2.03 KB
/
TimeManager.py
File metadata and controls
70 lines (49 loc) · 2.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
import time, traceback
from threading import Thread
BADELEMENTAMOUNT = "ERROR: Object {0} of event\
loop is containing bad amount of elements({1}): {2}"
class TimeManager:
# Everything related to time
# (e.g. Idling players slaying, coin tossing, game phase switch, etc.)
# is computed here.
def __init__(self, serv, GameContainer):
self.serv, self.GameContainer = serv, GameContainer
Thread(target=self.infiniteloop).start() # Start eet
self.kill = False
# Format: (timestamp, function, args)
# Function's first arg is a TimeManager instance.
event_bank = []
def addfunc(self, func, delay, *args):
event = [time.time()+delay, func]+list(args)
self.event_bank.append(event)
return event
def call(self, func, args):
try:
func(self, *args)
except:
print traceback.print_exc()
def infiniteloop(self):
while 1:
if self.kill:
return
time.sleep(1)
timestamp = time.time()
self.GameContainer.save_config()
for game in list(self.GameContainer):
if hasattr(game, "PlayerList"):
game.on_tick()
for obj in self.event_bank:
if len(obj) < 2:
# We won't make the bot crash because of this
print BADELEMENTAMOUNT.format(
str(self.event_bank.index(obj)),
str(len(obj)),
str(obj))
else:
if obj[0] <= timestamp:
self.call(obj[1], obj[2:])
# Object expires after call.
# That's why self.call provides a TimeManager instance.
self.event_bank.remove(obj)
def asdf(self):
pass