-
Notifications
You must be signed in to change notification settings - Fork 4
/
kevinbot.py
809 lines (751 loc) · 31.1 KB
/
kevinbot.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
from constants import *
import roles
import random
class KevinBotArgumentException(Exception):
pass
class KevinBotSettingException(Exception):
pass
class KevinBotPermissionException(Exception):
pass
class KevinBotGM:
def __init__(self, commandHandler, channel):
self.commandHandler = commandHandler
self.channel = channel
self.state = ST_INACTIVE
self.users = {}
self.players = {}
self.my_nick = self.commandHandler.my_nick
self.busy = 1
self.send('JOIN', self.channel)
def assignRoles(self):
nPlayers = len(self.players)
nDoctors = self.settings[OPT_NUM_DOCTORS]
nInspectors = self.settings[OPT_NUM_INSPECTORS]
nIdiots = self.settings[OPT_NUM_IDIOTS]
nMafia = self.settings[OPT_NUM_MAFIA]
auto = False
if nMafia == NUM_MAFIA_AUTO1:
nMafia = max(int((nPlayers - 2.0)/4.0), 1)
auto = True
nVillagers = nPlayers - nDoctors - nInspectors - nIdiots - nMafia
if nVillagers < 0:
self.echo('Cannot assign roles because there are more special ' +
'characters than there are players. For reference, ' +
'there are currently %d mafia and %d players' %
(nMafia, nPlayers))
return False
groups = self.random_partition(self.players.keys(),
[nDoctors, nInspectors, nIdiots, nMafia,
nVillagers])
self.players.clear()
for nick in groups[0]:
self.players[nick] = roles.Doctor(nick, self)
for nick in groups[1]:
self.players[nick] = roles.Inspector(nick, self)
for nick in groups[2]:
self.players[nick] = roles.Idiot(nick, self)
for nick in groups[3]:
self.players[nick] = roles.Mafia(nick, self)
for nick in groups[4]:
self.players[nick] = roles.Villager(nick, self)
for nick, player in self.players.items():
self.privmsg(nick, 'Your role is %s' % player.getRole())
self.echo('Roles have been assigned; check your private messages.')
if auto:
self.echo('Number of mafiosi: %d' % nMafia)
return True
def canStartGame(self):
if not self.muted:
return False
for nick, mode in self.users.items():
if nick == self.my_nick:
if mode != '@':
return False
else:
if mode != ' ':
return False
return True
def checkDoctorsOver(self):
for p in self.players.values():
if p.__class__ == roles.Doctor and p.vote == None:
return False
self.startDay()
return True
def checkGameOver(self):
# When there are as many mafia as villagers, the mafia win.
bad = 0
for player in self.players.values():
if player.__class__ == roles.Mafia:
bad += 1
if bad == 0:
self.echo('Game over---village wins.')
self.endGame()
return True
else:
good = len(self.players) - bad
if bad >= good:
self.echo('Game over---Mafia win.')
self.endGame()
return True
else:
return False
def checkInspectorsOver(self):
for p in self.players.values():
if p.__class__ == roles.Inspector and p.vote == None:
return False
self.startNightDoctor()
return True
def checkMafiaOver(self):
# We can assume there is at least one mafioso.
target = None
for p in self.players.values():
if p.__class__ == roles.Mafia:
if p.vote == None:
return False
elif target == None:
target = p.vote
elif target != p.vote:
return False
self.victim = target
self.mafiaBroadcast(None, '%s will be taken care of.' % target)
self.startNightInspector()
return True
def checkVoteOver(self):
'''
If some or all votes have been counted and the difference between the
leader and the runner-up is greater than the number of missing votes,
the vote is over.
If all votes have been counted and there is a tie, indicate this.
'''
N = len(self.players)
votes = 0
# a[i] is a set of all players who have received i votes.
# This includes the no-lynch option.
a = [set(self.players.keys() + [ENT_NONE])]
for i in range(N):
a.append(set())
# b[i] is how many votes player i has received.
b = dict(zip(self.players.keys() + [ENT_NONE], [0]*(N + 1)))
for p in self.players.values():
if p.vote != None:
a[b[p.vote]].discard(p.vote)
b[p.vote] += 1
a[b[p.vote]].add(p.vote)
votes += 1
target = None
for i in range(N+1)[::-1]:
if len(a[i]) == 0:
continue
if target == None:
if len(a[i]) >= 2:
if votes == N:
self.echo('The vote is tied with the following ' +
'leaders: ' + ', '.join(a[i]))
return False
else: # exactly 1
target = a[i].pop()
else:
if b[target] - i > N - votes:
break
else:
return False
if target == ENT_NONE:
self.echo('Nobody was lynched.')
self.startNightMafia()
else:
idiot = self.players[target].__class__ == roles.Idiot
self.echo('%s (%s) has been lynched.' % (target,
self.players[target].getRole()))
del self.players[target]
self.mode('-v', target)
if idiot:
self.echo('Game over---Village Idiot wins.')
self.endGame()
elif not self.checkGameOver():
self.startNightMafia()
return True
def echo(self, msg):
'''Sends a notice to the channel'''
self.send('NOTICE %s :%s' % (self.channel, msg))
def endGame(self):
self.mode('-m')
self.state = ST_INACTIVE
for p in self.players.values():
self.echo('%s was %s!' % (p.nick, p.getRole()))
def get(self, level, setting):
setting = setting.lower()
if setting in self.settings:
return self.settings[setting]
else:
raise KevinBotSettingException()
def getMode(self):
self.busy += 1
self.send('MODE', self.channel)
def mafiaBroadcast(self, nick, msg):
for p in self.players.values():
if p.nick != nick and p.__class__ == roles.Mafia:
self.privmsg(p.nick, msg)
def mode(self, *args):
self.send('MODE', self.channel, ' '.join(args))
def privmsg(self, nick, msg):
'''Sends a private message'''
self.send('PRIVMSG %s :%s' % (nick, msg))
def random_partition(self, values, counts):
random.shuffle(values)
ret = []
for i in counts:
ret.append(values[:i])
values = values[i:]
return ret
def replyto(self, nick, msg, private = False):
'''example: "brian: You can't do that!"'''
if private:
self.send('PRIVMSG %s :%s' % (nick, msg))
else:
self.send('PRIVMSG %s :%s: %s' % (self.channel, nick, msg))
def resendNames(self):
self.busy += 1
self.users.clear()
self.send('NAMES', self.channel)
def send(self, *args):
'''Sends a command directly through the IRC socket'''
self.commandHandler.client.send(*args)
def set(self, level, setting, value):
setting = setting.lower()
if not (setting in self.settings):
raise KevinBotSettingException()
elif level < LVL_OP:
raise KevinBotPermissionException()
# TODO (v1.1): refactor this
elif setting == OPT_NUM_MAFIA and value == NUM_MAFIA_AUTO1:
self.settings[setting] = value
else:
try:
num = int(value)
if num < 0 or num == 0 and setting == OPT_NUM_MAFIA:
raise KevinBotArgumentException()
if num > 1 and setting in [OPT_SELF_PROTECTING_DOCTOR,
OPT_NO_LYNCH,
OPT_SELF_LYNCH,
OPT_SEE_PATIENT]:
raise KevinBotArgumentException()
self.settings[setting] = num
except ValueError:
raise KevinBotArgumentException()
def startDay(self):
self.echo('It is now daytime.')
self.state = ST_DAY
self.voiceAll('+')
saved = False
for p in self.players.values():
if p.__class__ == roles.Doctor and p.vote == self.victim:
saved = True
if saved:
self.echo('Nobody was killed by the Mafia last night.')
if self.settings[OPT_SEE_PATIENT]:
self.echo('%s was saved!' % self.victim)
else:
self.echo('%s (%s) was killed by the Mafia last night.' %
(self.victim, self.players[self.victim].getRole()))
del self.players[self.victim]
self.mode('-v', self.victim)
if self.checkGameOver():
return
for p in self.players.values():
p.vote = None
def startGame(self):
self.echo('Game started.')
self.victim = None
self.startNightMafia()
def startNightDoctor(self):
doctor_exists = False
for p in self.players.values():
if p.__class__ == roles.Doctor:
p.vote = None
doctor_exists = True
self.privmsg(p.nick, 'Choose someone to save.')
self.privmsg(p.nick, 'Type "SAVE <nick>" when you have ' +
'chosen.')
if doctor_exists:
self.state = ST_NIGHT_DOCTOR
self.echo('Still nighttime. Doctor(s) at work.')
else:
self.startDay()
def startNightInspector(self):
inspector_exists = False
for p in self.players.values():
if p.__class__ == roles.Inspector:
p.vote = None
inspector_exists = True
self.privmsg(p.nick, 'Choose someone to inspect.')
self.privmsg(p.nick, 'Type "INSPECT <nick>" when you have ' +
'chosen.')
if inspector_exists:
self.state = ST_NIGHT_INSPECTOR
self.echo('Still nighttime. Detective(s) at work.')
else:
self.startNightDoctor()
def startNightMafia(self):
self.echo('It is now nighttime, and the Mafia are afoot.')
self.voiceAll('-')
self.mafiaBroadcast(None, 'Tonight the Mafia will liquidate ' +
'someone. Discuss whom.')
self.mafiaBroadcast(None, 'Use "KILL <nick>" to select someone to ' +
'kill. All messages will be CCed to all ' +
'other mafiosi (if any). All mafiosi ' +
'must select the same victim.')
self.state = ST_NIGHT_MAFIA
for p in self.players.values():
if p.__class__ == roles.Mafia:
p.vote = None
def unexpectedDeathTriggers(self, nick):
self.mode('-v', nick)
# This is called AFTER the player has been destroyed.
if self.checkGameOver():
return
# If it's the day phase, check whether everyone has voted.
if self.state == ST_DAY:
for p in self.players.values():
if p.vote == nick:
self.replyto(p.nick, 'Your vote is no longer valid.')
p.vote = None
self.checkVoteOver()
elif self.state == ST_NIGHT_MAFIA:
for p in self.players.values():
if p.__class__ == roles.Mafia and p.vote == nick:
self.mafiaBroadcast(None, "%s' vote is no longer valid"\
% p.nick)
p.vote = None
self.checkMafiaOver()
elif self.state == ST_NIGHT_DOCTOR:
self.checkDoctorsOver()
elif self.state == ST_NIGHT_INSPECTOR:
self.checkInspectorsOver()
def userlevel(self, nick):
'''Gets the privilege level of the user.'''
if nick in self.commandHandler.settings['admins']:
return LVL_ADMIN
elif nick == self.creator:
return LVL_OP
else:
return LVL_LUSER
def voiceAll(self, sign):
buf = []
bufSize = 0
for nick in self.players:
if bufSize + len(nick) + 2 > 400 or len(buf) >= 3:
self.voiceAllHelper(buf, sign)
bufSize = 0
buf = []
bufSize += len(nick) + 2
buf.append(nick)
self.voiceAllHelper(buf, sign)
def voiceAllHelper(self, buf, sign):
self.mode(sign + len(buf)*'v', *buf)
def _chanmsg(self, nick, msg):
'''Called when a message is posted to this channel.'''
if self.busy:
return
if not msg.startswith('!'):
return
words = msg.split()
# words must have at least one entry, since msg starts with !
# and that first entry must have at least one character
cmd = words[0][1:]
if (not cmd.startswith('_') and
callable(getattr(self, 'c_' + cmd, None))):
self.is_private_command = False
try:
getattr(self, 'c_' + cmd)(nick, *words[1:])
except KevinBotArgumentException:
self.replyto(nick, 'Invalid arguments to !%s' % cmd)
else:
self.replyto(nick, 'Invalid command !%s' % cmd)
def _cmode(self, modestr):
if 'm' in modestr:
self.muted = True
else:
self.muted = False
self.busy -= 1
if self.state == ST_PENDING_MODES and self.canStartGame():
self.startGame()
def _endofnames(self):
self.busy -= 1
if self.state == ST_PENDING_MODES and self.canStartGame():
self.startGame()
def _join(self, nick):
if nick == self.my_nick:
self.echo(VERSION)
self.echo('To create a game, type !create')
self.getMode()
else:
self.users[nick] = ' '
if self.state == ST_DAY and nick in self.players:
self.mode('+v', nick)
def _kick(self, nick):
del self.users[nick]
def _mode(self, modestr, *args):
# The information I found online is unclear on whether modern IRCds
# always send 005 RPL_ISUPPORT, which we ideally want in order to
# be able to properly parse mode strings. So I will just re-issue
# NAMES every time anything changes. This is less performant but
# also much simpler.
if 'o' in modestr or 'h' in modestr or 'v' in modestr:
self.resendNames()
if 'm' in modestr:
self.getMode()
def _namreply(self, users):
self.users.update(users) # Python awwww yeahhhh
def _nick(self, oldnick, newnick):
if oldnick == self.my_nick:
self.my_nick = newnick
self.users[newnick] = self.users[oldnick]
del self.users[oldnick]
if self.state == ST_DAY:
if newnick in self.players and self.users[newnick] == ' ':
self.mode('+v', newnick)
if newnick not in self.players and self.users[newnick] != ' ':
self.mode('-v', newnick)
def _part(self, nick):
del self.users[nick]
def _quit(self, nick):
del self.users[nick]
def _usermsg(self, nick, msg):
'''
There are only three in-game commands that need to be accepted in
private messages as well as channel messages. The reason for this is
that a player might want to perform them at any time, even if it is
night and they have no voice:
!abort: ends the game
!kick: removes another player from the game
!quit: removes yourself from the game
Also, the command !role should always be private-messaged, because the
reply will always be a private message.
The command !list was added due to beta test feedback.
'''
if self.busy:
return
words = msg.split()
if len(words) == 0:
return
if words[0] in ['!abort', '!kick', '!quit', '!role', '!list']:
self.is_private_command = True
try:
getattr(self, 'c_' + words[0][1:])(nick, *words[1:])
except KevinBotArgumentException():
self.privmsg(nick, 'Invalid arguments to !%s' % words[0][1:])
elif self.state == ST_NIGHT_MAFIA:
# Already know player is in game, don't check again
if self.players[nick].__class__ == roles.Mafia:
self.mafiaBroadcast(nick, '<%s> %s' % (nick, msg))
else:
return
# Check whether this is a vote.
if len(words) == 2 and words[0] == 'KILL':
if words[1] in self.players:
self.players[nick].vote = words[1]
self.checkMafiaOver()
else:
self.mafiaBroadcast(None, 'No such player %s' % words[1])
elif self.state == ST_NIGHT_INSPECTOR:
if self.players[nick].__class__ != roles.Inspector:
return
if len(words) == 2 and words[0] == 'INSPECT':
if self.players[nick].vote:
self.privmsg(nick, 'You can only inspect one player per ' +
'night.')
return
target = words[1]
if target in self.players:
self.players[nick].vote = target
if self.players[target].__class__ == roles.Mafia:
self.privmsg(nick, 'Mafia')
else:
self.privmsg(nick, 'Innocent')
self.checkInspectorsOver()
else:
self.privmsg(nick, 'No such player.')
elif self.state == ST_NIGHT_DOCTOR:
if self.players[nick].__class__ != roles.Doctor:
return
if len(words) == 2 and words[0] == 'SAVE':
target = words[1]
if (target == nick and
not self.settings[OPT_SELF_PROTECTING_DOCTOR]):
self.privmsg(nick, 'You cannot save yourself.')
elif target in self.players:
self.players[nick].vote = target
self.privmsg(nick, 'You have chosen to save %s'\
% target)
self.checkDoctorsOver()
else:
self.privmsg(nick, 'No such player.')
def c_abort(self, nick, *args):
'''Abort the game in progress'''
if len(args) > 0:
raise KevinBotArgumentException()
if self.state == ST_INACTIVE:
self.replyto(nick, 'No active game.', self.is_private_command)
elif self.userlevel(nick) < LVL_OP:
self.replyto(nick, 'Not authorized.', self.is_private_command)
elif self.state == ST_WAITING_FOR_PLAYERS:
self.replyto(nick, 'The game has not yet started.',
self.is_private_command)
else:
self.echo('Game aborted by %s' % nick)
self.endGame()
def c_create(self, nick, *args):
'''Create a new game'''
if len(args) > 0:
raise KevinBotArgumentException()
if self.state == ST_INACTIVE:
self.state = ST_WAITING_FOR_PLAYERS
self.players = {}
self.settings = {OPT_NUM_DOCTORS: 1,
OPT_NUM_INSPECTORS: 1,
OPT_NUM_IDIOTS: 0,
OPT_NUM_MAFIA: NUM_MAFIA_AUTO1,
OPT_SELF_PROTECTING_DOCTOR: 1,
OPT_NO_LYNCH: 0,
OPT_SELF_LYNCH: 1,
OPT_SEE_PATIENT: 0}
self.creator = nick
self.echo('Game created by %s' % nick)
else:
self.replyto(nick, 'A game has already been created.')
def c_cancel(self, nick, *args):
if len(args) > 0:
raise KevinBotArgumentException()
if self.state == ST_INACTIVE:
self.replyto(nick, 'No active game.')
elif self.state == ST_WAITING_FOR_PLAYERS:
if self.userlevel(nick) >= LVL_OP:
self.state = ST_INACTIVE
self.echo('Game cancelled.')
else:
self.replyto(nick, 'You are not the creator of this game, ' +
'so you cannot cancel it.')
else:
self.replyto(nick, 'Cannot cancel a game that has already ' +
'started. Maybe you meant to !abort.')
def c_get(self, nick, *args):
if len(args) != 1:
raise KevinBotArgumentException()
if self.state == ST_INACTIVE:
self.replyto(nick, 'No active game.')
return
setting = args[0]
try:
val = self.get(self.userlevel(nick), setting)
self.replyto(nick, '%s = %s' % (setting, str(val)))
except KevinBotSettingException:
self.replyto(nick, 'Nonexistent setting %s' % setting)
def c_join(self, nick, *args):
if len(args) > 0:
raise KevinBotArgumentException()
if self.state == ST_WAITING_FOR_PLAYERS:
if nick in self.players:
self.replyto(nick, 'You are already in this game.')
else:
can_join = True
for chan, game in self.commandHandler.channels.items():
if chan != self.channel and nick in game.players:
can_join = False
if can_join:
self.players[nick] = None
self.echo('%s has joined the game.' % nick)
else:
self.replyto(nick, 'You can only be in one game at a ' +
'time.')
elif self.state == ST_INACTIVE:
self.replyto(nick, 'No active game to join.')
else:
self.replyto(nick, 'The game has already started.')
def c_list(self, nick, *args):
if len(args) > 0:
raise KevinBotArgumentException()
if self.state == ST_INACTIVE:
self.replyto(nick, 'No active game.', self.is_private_command)
else:
self.replyto(nick, ', '.join(self.players.keys()),
self.is_private_command)
def c_kick(self, nick, *args):
if len(args) != 1:
raise KevinBotArgumentException()
target = args[0]
if self.state == ST_INACTIVE:
self.replyto(nick, 'No active game.', self.is_private_command)
elif self.userlevel(nick) < LVL_OP:
self.replyto(nick, 'Only the game creator can kick players.',
self.is_private_command)
elif self.state == ST_WAITING_FOR_PLAYERS:
self.replyto(nick, 'Use !remove to remove a player from the ' +
'tentative game.', self.is_private_command)
elif target in self.players:
self.echo('%s (%s) was kicked by %s' % (target,
self.players[target].getRole(), nick))
del self.players[target]
self.unexpectedDeathTriggers(target)
else:
self.replyto(nick, '%s is not in this game' % target,
self.is_private_command)
def c_quit(self, nick, *args):
if len(args) > 0:
raise KevinBotArgumentException()
if self.state == ST_INACTIVE:
self.replyto(nick, 'No active game.', self.is_private_command)
elif self.state == ST_WAITING_FOR_PLAYERS:
self.replyto(nick, 'The game has not yet started.',
self.is_private_command)
elif not (nick in self.players):
self.replyto(nick, 'You are not in this game.',
self.is_private_command)
else:
self.echo('%s (%s) has quit.' %
(nick, self.players[nick].getRole()))
del self.players[nick]
self.unexpectedDeathTriggers(nick)
def c_remove(self, nick, *args):
if len(args) != 1:
raise KevinBotArgumentException()
if self.state == ST_INACTIVE:
self.replyto(nick, 'No active game.')
elif self.userlevel(nick) < LVL_OP:
self.replyto(nick, 'Not authorized!')
elif self.state == ST_WAITING_FOR_PLAYERS:
if args[0] in self.players:
del self.players[args[0]]
self.echo('%s removed %s from the game.' % (nick, args[0]))
else:
self.replyto(nick, '%s is not in the game.' % args[0])
else:
self.replyto(nick, 'Use !kick to remove a player after the game ' +
'has already started.')
def c_role(self, nick, *args):
if not self.is_private_command:
return
if len(args) > 0:
raise KevinBotArgumentException()
if self.state == ST_INACTIVE:
self.privmsg(nick, 'No active game.')
elif self.state == ST_WAITING_FOR_PLAYERS:
self.privmsg(nick, 'The game has not yet started.')
elif nick in self.players:
self.privmsg(nick, self.players[nick].getDescription())
else:
self.privmsg(nick, 'You are not in the game.')
def c_set(self, nick, *args):
if len(args) != 2:
raise KevinBotArgumentException()
if self.state == ST_INACTIVE:
self.replyto(nick, 'No active game.')
return
setting = args[0]
val = args[1]
try:
self.set(self.userlevel(nick), setting, val)
self.echo('%s set %s = %s' % (nick, setting, val))
except KevinBotArgumentException:
self.replyto(nick, 'Invalid value %s for %s' % (val, setting))
except KevinBotPermissionException:
self.replyto(nick, "You don't have permission to set %s = %s" %
(setting, val))
except KevinBotSettingException:
self.replyto(nick, 'Nonexistent setting %s' % setting)
def c_start(self, nick, *args):
if len(args) > 0:
raise KevinBotArgumentException()
if self.state == ST_INACTIVE:
self.replyto(nick, 'You must create a game first.')
elif self.state == ST_WAITING_FOR_PLAYERS:
if self.userlevel(nick) >= LVL_OP:
if self.users[self.my_nick] != '@':
self.replyto(nick,
'%s needs operator status in order to run.' %
self.my_nick)
else:
if self.assignRoles():
if self.canStartGame():
self.startGame()
else:
self.state = ST_PENDING_MODES
self.echo('The game will start as soon as the ' +
'appropriate modes have been set.')
if not self.muted:
self.mode('+m')
for nick, mode in self.users.items():
if nick != self.my_nick and mode != ' ':
self.mode('-ohv', nick, nick, nick)
else:
self.replyto(nick, 'Only the game creator can start the game.')
else:
self.replyto(nick, 'The game has already started.')
def c_unjoin(self, nick, *args):
if len(args) > 0:
raise KevinBotArgumentException()
if self.state == ST_WAITING_FOR_PLAYERS:
if nick in self.players:
del self.players[nick]
self.replyto(nick, 'You are no longer in the game.')
else:
self.replyto(nick, 'You are not in this game.')
elif self.state == ST_INACTIVE:
self.replyto(nick, 'No current game.')
else:
self.replyto(nick,
'You cannot unjoin a game that has already started. ' +
'Perhaps you meant to !quit the game that is in ' +
'progress.')
def c_vlist(self, nick, *args):
if len(args) > 0:
raise KevinBotArgumentException()
if self.state == ST_DAY:
# The coder still thinks he's writing C++...
a = []
b = {}
c = []
for p in self.players:
b[p] = len(a)
a.append((p, []))
b[ENT_NONE] = len(a)
a.append((ENT_NONE, []))
for n, p in self.players.items():
if p.vote:
a[b[p.vote]][1].append(n)
else:
c.append(n)
a.sort(lambda x, y: -cmp(len(x[1]), len(y[1])))
for e in a:
if len(e[1]) == 0:
break
self.echo('Votes for %s by %s' % (e[0], ', '.join(e[1])))
if len(c):
self.echo('No votes from ' + ', '.join(c))
else:
self.replyto(nick, 'This command is only valid during the day.')
def c_vote(self, nick, *args):
if len(args) != 1:
raise KevinBotArgumentException()
if self.state == ST_DAY:
if nick in self.players:
if args[0] in self.players:
if args[0] == nick and not self.settings[OPT_SELF_LYNCH]:
self.replyto(nick, 'You cannot vote to lynch ' +
'yourself.')
else:
self.replyto(nick, 'Your vote has been registered.')
self.players[nick].vote = args[0]
self.checkVoteOver()
elif args[0] == ENT_NONE:
if self.settings[OPT_NO_LYNCH]:
self.replyto(nick, 'Your vote has been registered.')
self.players[nick].vote = ENT_NONE
self.checkVoteOver()
else:
self.replyto(nick, 'You cannot vote for no lynch.')
else:
self.replyto(nick, 'No such player %s' % args[0])
else:
self.replyto(nick, 'You are not in the game.')
else:
self.replyto(nick, 'You can only vote during the day.')