-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
45 lines (37 loc) · 1.38 KB
/
state.py
File metadata and controls
45 lines (37 loc) · 1.38 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
#!/usr/bin/env python
import re
import time
class State():
'State of a game of UNO™, like if there\'s a game and so on…'
def __init__(self):
# Assume that there isn't a game unless we see it
self.running = False
# Timestamp of last known message… or now.
self.last_msg = time.time()
def parse_message(self, msg):
'''Reads IRC-messages from UNO-bot and adjusts the running-state.
To ensure a working time-foo please only call this function for
UNO-bot's messages!'''
def chk(regs, desired_state):
for r in regs:
if r.search(msg):
self.running = desired_state
print('"{}" matched; state is {}'.format(msg, desired_state))
break
pos_regs = map(re.compile, [
r'Current discard:',
r'has UNO!!',
r'joins this game of UNO!',
r' plays ',
r'There is already an UNO! game running here, managed by pim. say \'jo\' to join in',
r'game will start in 20 seconds',
r'it\'s (.*)\'s turn',
r'it\'s your turn, sleepyhead'
])
neg_regs = map(re.compile, [
r'UNO! game finished after (.*)! The winner is',
r' still had (.*)'
])
chk(pos_regs, True)
chk(neg_regs, False)
self.last_msg = time.time()