-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
215 lines (171 loc) · 5.85 KB
/
game.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from logger import log, error
from categories import Categories
from alphabet import Alphabet
from random import seed, randint
import re
seed()
class Round():
def __init__(self):
self.categories = Categories()
self.categories.load()
self.alphabet = Alphabet()
self.alphabet.load()
self.responses = []
self.nextRound()
def allResponses(self):
return [d['response'] for d in self.responses]
def getResponse(self, ptn):
log( 'getResponse for ' + ptn )
try:
pr = [d for d in self.responses if d['tn'] == ptn]
return pr[0]
except Exception as e:
return { 'tn': ptn, 'valid': False, 'response': 'UNK' }
def nextRound(self):
self.cat_index = randint( 0, len(self.categories.data)-1)
log( self.cat_index)
self.alpha_index = randint( 0, len(self.alphabet.data)-1)
log( self.alpha_index )
self.responses = []
def describe(self):
alpha = self.alphabet.data[self.alpha_index]
return self.categories.data[self.cat_index]['category'] + " that " + alpha['position'].lower() + " " + alpha['letter']
round = Round()
def usage(players, tn, args):
return [tn], "Usage: \nTo join the current game\njoin: YourName\n\nTo start the game once people have joined\nnew:\n\nTo reset scores for all players to 0\nreset:\n\nTo see this help\nhelp:\n\nOtherwise, just type your answer"
def validate( args ):
r = True
alpha = round.alphabet.data[round.alpha_index]
v = args[0]
if alpha['position'] == 'Begins with':
r = v.lower().startswith( alpha['letter'].lower() )
elif alpha['position'] == 'Ends with':
r = v.lower().endswith( alpha['letter'].lower() )
return r
def guess(players, tn, args):
v = validate(args)
round.responses.append( { 'tn': tn, 'valid': v, 'response': args[0] })
# the end of the round occurs when # responses equals # players
# in which case we need to score the round
if len( round.responses) >= players.numPlayers() :
# make a list of the responses
allresp = [d['response'] for d in round.responses ]
tnlist = players.allPlayerTNs()
answers = ""
try:
for p in players.getPlayers():
pr = round.getResponse(p.tn)
c = allresp.count( pr['response'])
if pr['valid'] is False:
p.increment(-1)
answers += 'Invalid response ' + pr['response'] + ' from ' + p.name + '\n'
elif c == 1:
p.increment(1)
answers += 'Unique response ' + pr['response'] + ' from ' + p.name + '\n'
else:
answers += 'Duplicate response ' + pr['response'] + ' from ' + p.name + '\n'
except Exception as e:
log( e )
return tnlist, "Ugh, Scoring Error"
round.nextRound()
return tnlist, answers + players.currentScore() + "\n\nNext category!\n" + round.describe()
return [tn], "Noted"
def join(players, tn, args):
log("join")
try:
p = Player(tn, args[1])
log(p)
players.append(p)
log(players)
tnlist = players.allPlayerTNs()
return tnlist, p.name + " joined"
except Exception as e:
error("join", e)
return [tn], "We had a join problem"
def drop(players, tn, args):
log("drop")
try:
p = players.remove(tn)
log(players)
tnlist = players.allPlayerTNs()
return tnlist, p.name + " left"
except Exception as e:
error("join", e)
return [tn], "We had a drop problem"
def reset(players, tn, args):
log("reset")
plist = players.allPlayerTNs()
players.reset()
round.responses = {}
round.nextRound()
return plist, "New game! " + round.describe()
commands = {'help': usage,
'join': join,
'new': reset,
'reset': reset,
'drop': drop
}
class Player:
def __init__(self, tn, name):
self.tn = tn
self.name = name
self.score = 0
def reset(self):
self.score = 0
def increment(self, amt):
self.score += amt
class Players():
def __init__(self):
self.players = []
def numPlayers(self):
return len( self.players )
def allPlayerTNs(self):
tnlist = []
for p in self.players:
tnlist.append( p.tn )
return tnlist
def append(self, p):
return self.players.append(p)
def remove(self, tn):
for p in self.players:
if p.tn == tn:
self.players.remove(p)
return p
return Player(tn, "Unknown")
def reset(self):
for p in self.players:
p.reset()
def getPlayers(self):
return self.players
def currentScore(self):
res = "Current Score: "
sep = ""
for p in self.players:
res += sep + p.name + " " + str(p.score)
sep = ", "
return res
class Chategory(object):
def __init__(self, parent):
self.players = Players()
self.parent = parent
def allPlayerTNs(self):
tnlist = []
for p in self.players:
tnlist.append( p.tn )
return tnlist
def command(self, tn, msg):
log('command')
# lookup first arg in the msg, and map it to a function
words = msg.split(':')
func = commands.get(words[0].lower(), guess)
log(func)
# remove all special chars from words before invoking func
fargs=[]
for w in words:
fargs.append(re.sub( '[^A-Za-z0-9 ]+', '', w ))
plist, result = func(self.players, tn, fargs )
log(result)
return plist, result
def sendAll(self, msg ):
for p in self.players:
self.parent.sendMsg( p.tn, msg )