]>
Commit | Line | Data |
---|---|---|
80d02bd8 | 1 | # Erebus IRC bot - Author: Erebus Team |
c695f740 | 2 | # trivia module |
80d02bd8 | 3 | # This file is released into the public domain; see http://unlicense.org/ |
4 | ||
5 | # module info | |
6 | modinfo = { | |
7 | 'author': 'Erebus Team', | |
8 | 'license': 'public domain', | |
9 | 'compatible': [1], # compatible module API versions | |
10 | 'depends': [], # other modules required to work properly? | |
11 | } | |
12 | ||
13 | # preamble | |
14 | import modlib | |
15 | lib = modlib.modlib(__name__) | |
16 | def modstart(parent, *args, **kwargs): | |
5871567f | 17 | state.gotParent(parent) |
18 | lib.hookchan(state.db['chan'])(trivia_checkanswer) # we need parent for this. so it goes here. | |
80d02bd8 | 19 | return lib.modstart(parent, *args, **kwargs) |
20 | def modstop(*args, **kwargs): | |
c0eee1b4 | 21 | global state |
fadbf980 | 22 | stop() |
77c61775 | 23 | state.closeshop() |
80d02bd8 | 24 | del state |
25 | return lib.modstop(*args, **kwargs) | |
26 | ||
27 | # module code | |
7bd5e7d5 | 28 | import json, random, threading, re, time, datetime |
b16b8c05 | 29 | |
c0eee1b4 | 30 | try: |
c53f6514 | 31 | import twitter |
32 | except: pass # doesn't matter if we don't have twitter, updating the status just will fall through the try-except if so... | |
c0eee1b4 | 33 | |
b16b8c05 | 34 | def findnth(haystack, needle, n): #http://stackoverflow.com/a/1884151 |
35 | parts = haystack.split(needle, n+1) | |
36 | if len(parts)<=n+1: | |
37 | return -1 | |
38 | return len(haystack)-len(parts[-1])-len(needle) | |
80d02bd8 | 39 | |
40 | class TriviaState(object): | |
5871567f | 41 | def __init__(self, parent=None, pointvote=False): |
42 | if parent is not None: | |
43 | self.gotParent(parent, pointvote) | |
44 | ||
45 | def gotParent(self, parent, pointvote=False): | |
77c61775 | 46 | self.parent = parent |
5871567f | 47 | self.questionfile = self.parent.cfg.get('trivia', 'jsonpath') |
48 | self.db = json.load(open(self.questionfile, "r")) | |
9557ee54 | 49 | self.chan = self.db['chan'] |
50 | self.curq = None | |
c695f740 | 51 | self.nextq = None |
442ed923 | 52 | self.nextquestiontimer = None |
b16b8c05 | 53 | self.steptimer = None |
54 | self.hintstr = None | |
55 | self.hintanswer = None | |
77c61775 | 56 | self.hintsgiven = 0 |
b16b8c05 | 57 | self.revealpossibilities = None |
77c61775 | 58 | self.gameover = False |
c0eee1b4 | 59 | self.missedquestions = 0 |
80d02bd8 | 60 | |
7bd5e7d5 | 61 | if 'starttime' not in self.db or self.db['starttime'] is None: |
62 | self.db['starttime'] = time.time() | |
63 | ||
c53f6514 | 64 | if pointvote: |
65 | self.getchan().msg("Vote for the next round target points! Options: %s. Vote using !vote <choice>" % (', '.join([str(x) for x in self.db['targetoptions']]))) | |
66 | self.getchan().msg("You have %s seconds." % (self.db['votetimer'])) | |
67 | self.voteamounts = dict([(x, 0) for x in self.db['targetoptions']]) # make a dict {pointsoptionA: 0, pointsoptionB: 0, ...} | |
68 | self.pointvote = threading.Timer(self.db['votetimer'], self.endPointVote) | |
69 | self.pointvote.start() | |
70 | else: | |
71 | self.pointvote = None | |
72 | ||
80d02bd8 | 73 | def __del__(self): |
77c61775 | 74 | self.closeshop() |
75 | def closeshop(self): | |
442ed923 | 76 | if threading is not None and threading._Timer is not None: |
77 | if isinstance(self.steptimer, threading._Timer): | |
78 | self.steptimer.cancel() | |
79 | if isinstance(self.nextquestiontimer, threading._Timer): | |
80 | self.nextquestiontimer.cancel() | |
81 | self.nextquestiontimer = None | |
c695f740 | 82 | if json is not None and json.dump is not None: |
6374d61f | 83 | json.dump(self.db, open(self.questionfile, "w"))#, indent=4, separators=(',', ': ')) |
80d02bd8 | 84 | |
fadbf980 | 85 | def getchan(self): |
86 | return self.parent.channel(self.chan) | |
87 | def getbot(self): | |
88 | return self.getchan().bot | |
89 | ||
b16b8c05 | 90 | def nexthint(self, hintnum): |
b16b8c05 | 91 | answer = self.hintanswer |
92 | ||
b5c89dfb | 93 | if self.hintstr is None or self.revealpossibilities is None or self.reveal is None: |
b16b8c05 | 94 | self.hintstr = list(re.sub(r'[a-zA-Z0-9]', '*', answer)) |
95 | self.revealpossibilities = range(''.join(self.hintstr).count('*')) | |
b5c89dfb | 96 | self.reveal = int(''.join(self.hintstr).count('*') * (7/24.0)) |
b16b8c05 | 97 | |
b5c89dfb | 98 | for i in range(self.reveal): |
b16b8c05 | 99 | revealcount = random.choice(self.revealpossibilities) |
100 | revealloc = findnth(''.join(self.hintstr), '*', revealcount) | |
101 | self.revealpossibilities.remove(revealcount) | |
102 | self.hintstr[revealloc] = answer[revealloc] | |
6374d61f | 103 | self.parent.channel(self.chan).bot.msg(self.chan, "\00304,01Here's a hint: %s" % (''.join(self.hintstr))) |
b16b8c05 | 104 | |
77c61775 | 105 | self.hintsgiven += 1 |
106 | ||
c4763e66 | 107 | if hintnum < self.db['hintnum']: |
108 | self.steptimer = threading.Timer(self.db['hinttimer'], self.nexthint, args=[hintnum+1]) | |
b16b8c05 | 109 | self.steptimer.start() |
110 | else: | |
c4763e66 | 111 | self.steptimer = threading.Timer(self.db['hinttimer'], self.nextquestion, args=[True]) |
b16b8c05 | 112 | self.steptimer.start() |
113 | ||
77c61775 | 114 | def doGameOver(self): |
7bd5e7d5 | 115 | msg = self.getchan().msg |
77c61775 | 116 | def person(num): return self.db['users'][self.db['ranks'][num]]['realnick'] |
7bd5e7d5 | 117 | def pts(num): return str(self.db['users'][self.db['ranks'][num]]['points']) |
c0eee1b4 | 118 | winner = person(0) |
77c61775 | 119 | try: |
120 | msg("\00312THE GAME IS OVER!!!") | |
121 | msg("THE WINNER IS: %s (%s)" % (person(0), pts(0))) | |
122 | msg("2ND PLACE: %s (%s)" % (person(1), pts(1))) | |
123 | msg("3RD PLACE: %s (%s)" % (person(2), pts(2))) | |
124 | [msg("%dth place: %s (%s)" % (i+1, person(i), pts(i))) for i in range(3,10)] | |
125 | except IndexError: pass | |
c0eee1b4 | 126 | except Exception as e: msg("DERP! %r" % (e)) |
127 | ||
7bd5e7d5 | 128 | if self.db['hofpath'] is not None and self.db['hofpath'] != '': |
129 | self.writeHof() | |
130 | ||
77c61775 | 131 | self.db['users'] = {} |
132 | self.db['ranks'] = [] | |
133 | stop() | |
134 | self.closeshop() | |
c0eee1b4 | 135 | |
c53f6514 | 136 | try: |
c0eee1b4 | 137 | t = twitter.Twitter(auth=twitter.OAuth(self.getbot().parent.cfg.get('trivia', 'token'), |
138 | self.getbot().parent.cfg.get('trivia', 'token_sec'), | |
139 | self.getbot().parent.cfg.get('trivia', 'con'), | |
140 | self.getbot().parent.cfg.get('trivia', 'con_sec'))) | |
141 | t.statuses.update(status="Round is over! The winner was %s" % (winner)) | |
c53f6514 | 142 | except: pass #don't care if errors happen updating twitter. |
143 | ||
5871567f | 144 | self.__init__(self.parent, True) |
c53f6514 | 145 | |
7bd5e7d5 | 146 | def writeHof(self): |
147 | def person(num): | |
148 | try: return self.db['users'][self.db['ranks'][num]]['realnick'] | |
149 | except: return "none" | |
150 | def pts(num): | |
151 | try: return str(self.db['users'][self.db['ranks'][num]]['points']) | |
152 | except: return 0 | |
153 | ||
154 | try: | |
155 | f = open(self.db['hofpath'], 'rb+') | |
156 | for i in range(self.db['hoflines']): #skip this many lines | |
157 | f.readline() | |
158 | insertpos = f.tell() | |
159 | fcontents = f.read() | |
160 | f.seek(insertpos) | |
161 | f.write((self.db['hofformat']+"\n") % { | |
162 | 'date': time.strftime("%F", time.gmtime()), | |
163 | 'duration': str(datetime.timedelta(seconds=time.time()-self.db['starttime'])), | |
164 | 'targetscore': self.db['target'], | |
165 | 'firstperson': person(0), | |
166 | 'firstscore': pts(0), | |
167 | 'secondperson': person(1), | |
168 | 'secondscore': pts(1), | |
169 | 'thirdperson': person(2), | |
170 | 'thirdscore': pts(2), | |
171 | }) | |
172 | f.write(fcontents) | |
173 | return True | |
174 | except Exception as e: | |
175 | raise e | |
176 | return False | |
177 | finally: | |
178 | f.close() | |
179 | ||
c53f6514 | 180 | def endPointVote(self): |
181 | self.getchan().msg("Voting has ended!") | |
182 | votelist = sorted(self.voteamounts.items(), key=lambda item: item[1]) #sort into list of tuples: [(option, number_of_votes), ...] | |
183 | for i in range(len(votelist)-1): | |
184 | item = votelist[i] | |
185 | self.getchan().msg("%s place: %s (%s votes)" % (len(votelist)-i, item[0], item[1])) | |
186 | self.getchan().msg("Aaaaand! The next round will be to \002%s\002 points! (%s votes)" % (votelist[-1][0], votelist[-1][1])) | |
c0eee1b4 | 187 | |
c53f6514 | 188 | self.db['target'] = votelist[-1][0] |
189 | self.pointvote = None | |
77c61775 | 190 | |
38b29993 | 191 | self.nextquestion() #start the game! |
192 | ||
442ed923 | 193 | def nextquestion(self, qskipped=False, iteration=0, skipwait=False): |
77c61775 | 194 | if self.gameover == True: |
195 | return self.doGameOver() | |
fadbf980 | 196 | if qskipped: |
c53f6514 | 197 | self.getchan().msg("\00304Fail! The correct answer was: %s" % (self.hintanswer)) |
c0eee1b4 | 198 | self.missedquestions += 1 |
199 | else: | |
200 | self.missedquestions = 0 | |
f8cc0124 | 201 | if 'topicformat' in self.db and self.db['topicformat'] is not None: |
202 | self.getbot().conn.send("TOPIC %s" % (self.db['chan'])) | |
fadbf980 | 203 | |
b16b8c05 | 204 | if isinstance(self.steptimer, threading._Timer): |
205 | self.steptimer.cancel() | |
442ed923 | 206 | if isinstance(self.nextquestiontimer, threading._Timer): |
207 | self.nextquestiontimer.cancel() | |
208 | self.nextquestiontimer = None | |
c0eee1b4 | 209 | |
b16b8c05 | 210 | self.hintstr = None |
77c61775 | 211 | self.hintsgiven = 0 |
b16b8c05 | 212 | self.revealpossibilities = None |
b5c89dfb | 213 | self.reveal = None |
b16b8c05 | 214 | |
c4763e66 | 215 | if self.missedquestions > self.db['maxmissedquestions']: |
c0eee1b4 | 216 | stop() |
217 | self.getbot().msg(self.getchan(), "%d questions unanswered! Stopping the game.") | |
b16b8c05 | 218 | |
442ed923 | 219 | if skipwait: |
220 | self._nextquestion(qskipped, iteration) | |
221 | else: | |
222 | print "making timer" | |
223 | self.nextquestiontimer = threading.Timer(self.db['questionpause'], self._nextquestion, args=[qskipped, iteration]) | |
224 | self.nextquestiontimer.start() | |
225 | ||
226 | def _nextquestion(self, qskipped, iteration): | |
227 | print "_" | |
e5a3970b | 228 | if self.nextq is not None: |
229 | nextq = self.nextq | |
230 | self.nextq = None | |
c695f740 | 231 | else: |
232 | nextq = random.choice(self.db['questions']) | |
b5c89dfb | 233 | |
ebee6edb | 234 | if nextq[0][0] == "!": |
b5c89dfb | 235 | nextq = specialQuestion(nextq) |
236 | ||
ebee6edb | 237 | if len(nextq) > 2 and nextq[2] - time.time() < 7*24*60*60 and iteration < 10: |
442ed923 | 238 | return self._nextquestion(iteration=iteration+1) #short-circuit to pick another question |
ebee6edb | 239 | if len(nextq) > 2: |
240 | nextq[2] = time.time() | |
241 | else: | |
242 | nextq.append(time.time()) | |
b5c89dfb | 243 | |
ebee6edb | 244 | nextq[1] = nextq[1].lower() |
c695f740 | 245 | |
6374d61f | 246 | qtext = "\00304,01Next up: " |
ebee6edb | 247 | qary = nextq[0].split(None) |
c695f740 | 248 | for qword in qary: |
6374d61f | 249 | qtext += "\00304,01"+qword+"\00301,01"+chr(random.randrange(0x61,0x7A)) #a-z |
fadbf980 | 250 | self.getbot().msg(self.chan, qtext) |
80d02bd8 | 251 | |
b5c89dfb | 252 | self.curq = nextq |
253 | ||
ebee6edb | 254 | if isinstance(self.curq[1], basestring): self.hintanswer = self.curq[1] |
255 | else: self.hintanswer = random.choice(self.curq[1]) | |
c0eee1b4 | 256 | |
c4763e66 | 257 | self.steptimer = threading.Timer(self.db['hinttimer'], self.nexthint, args=[1]) |
b16b8c05 | 258 | self.steptimer.start() |
259 | ||
80d02bd8 | 260 | def checkanswer(self, answer): |
9557ee54 | 261 | if self.curq is None: |
262 | return False | |
ebee6edb | 263 | elif isinstance(self.curq[1], basestring): |
264 | return answer.lower() == self.curq[1] | |
80d02bd8 | 265 | else: # assume it's a list or something. |
ebee6edb | 266 | return answer.lower() in self.curq[1] |
77c61775 | 267 | |
c53f6514 | 268 | def addpoint(self, user_obj, count=1): |
269 | user_nick = str(user_obj) | |
270 | user = user_nick.lower() # save this separately as we use both | |
80d02bd8 | 271 | if user in self.db['users']: |
272 | self.db['users'][user]['points'] += count | |
273 | else: | |
c53f6514 | 274 | self.db['users'][user] = {'points': count, 'realnick': user_nick, 'rank': len(self.db['ranks'])} |
9557ee54 | 275 | self.db['ranks'].append(user) |
80d02bd8 | 276 | |
e5a3970b | 277 | self.db['ranks'].sort(key=lambda nick: self.db['users'][nick]['points'], reverse=True) #re-sort ranks, rather than dealing with anything more efficient |
6374d61f | 278 | for i in range(0, len(self.db['ranks'])): |
279 | nick = self.db['ranks'][i] | |
280 | self.db['users'][nick]['rank'] = i | |
77c61775 | 281 | |
e5a3970b | 282 | if self.db['users'][user]['points'] >= self.db['target']: |
77c61775 | 283 | self.gameover = True |
284 | ||
80d02bd8 | 285 | return self.db['users'][user]['points'] |
286 | ||
287 | def points(self, user): | |
9557ee54 | 288 | user = str(user).lower() |
80d02bd8 | 289 | if user in self.db['users']: |
290 | return self.db['users'][user]['points'] | |
291 | else: | |
292 | return 0 | |
293 | ||
294 | def rank(self, user): | |
c695f740 | 295 | user = str(user).lower() |
fadbf980 | 296 | if user in self.db['users']: |
297 | return self.db['users'][user]['rank']+1 | |
298 | else: | |
299 | return len(self.db['users'])+1 | |
77c61775 | 300 | |
c695f740 | 301 | def targetuser(self, user): |
77c61775 | 302 | if len(self.db['ranks']) == 0: return "no one is ranked!" |
303 | ||
c695f740 | 304 | user = str(user).lower() |
fadbf980 | 305 | if user in self.db['users']: |
306 | rank = self.db['users'][user]['rank'] | |
307 | if rank == 0: | |
308 | return "you're in the lead!" | |
309 | else: | |
310 | return self.db['ranks'][rank-1] | |
c695f740 | 311 | else: |
fadbf980 | 312 | return self.db['ranks'][-1] |
c695f740 | 313 | def targetpoints(self, user): |
77c61775 | 314 | if len(self.db['ranks']) == 0: return 0 |
315 | ||
c695f740 | 316 | user = str(user).lower() |
fadbf980 | 317 | if user in self.db['users']: |
318 | rank = self.db['users'][user]['rank'] | |
319 | if rank == 0: | |
320 | return "N/A" | |
321 | else: | |
322 | return self.db['users'][self.db['ranks'][rank-1]]['points'] | |
c695f740 | 323 | else: |
fadbf980 | 324 | return self.db['users'][self.db['ranks'][-1]]['points'] |
80d02bd8 | 325 | |
5871567f | 326 | state = TriviaState() |
9557ee54 | 327 | |
5871567f | 328 | # we have to hook this in modstart, since we don't know the channel until then. |
80d02bd8 | 329 | def trivia_checkanswer(bot, user, chan, *args): |
80d02bd8 | 330 | line = ' '.join([str(arg) for arg in args]) |
331 | if state.checkanswer(line): | |
6374d61f | 332 | bot.msg(chan, "\00312%s\003 has it! The answer was \00312%s\003. New score: %d. Rank: %d. Target: %s (%s)." % (user, line, state.addpoint(user), state.rank(user), state.targetuser(user), state.targetpoints(user))) |
77c61775 | 333 | if state.hintsgiven == 0: |
6374d61f | 334 | bot.msg(chan, "\00312%s\003 got an extra point for getting it before the hints! New score: %d." % (user, state.addpoint(user))) |
9557ee54 | 335 | state.nextquestion() |
80d02bd8 | 336 | |
f6252f1c | 337 | @lib.hook('points', needchan=False) |
80d02bd8 | 338 | def cmd_points(bot, user, chan, realtarget, *args): |
c695f740 | 339 | if chan == realtarget: replyto = chan |
80d02bd8 | 340 | else: replyto = user |
341 | ||
342 | if len(args) != 0: who = args[0] | |
343 | else: who = user | |
344 | ||
345 | bot.msg(replyto, "%s has %d points." % (who, state.points(who))) | |
346 | ||
9306587e | 347 | @lib.hook('give', glevel=lib.STAFF, needchan=False) |
80d02bd8 | 348 | @lib.argsGE(1) |
349 | def cmd_give(bot, user, chan, realtarget, *args): | |
80d02bd8 | 350 | whoto = args[0] |
c695f740 | 351 | if len(args) > 1: |
352 | numpoints = int(args[1]) | |
353 | else: | |
354 | numpoints = 1 | |
355 | balance = state.addpoint(whoto, numpoints) | |
fadbf980 | 356 | |
c695f740 | 357 | bot.msg(chan, "%s gave %s %d points. New balance: %d" % (user, whoto, numpoints, balance)) |
358 | ||
9306587e | 359 | @lib.hook('setnextid', glevel=1, needchan=False) |
360 | def cmd_setnextid(bot, user, chan, realtarget, *args): | |
361 | try: | |
362 | qid = int(args[0]) | |
363 | state.nextq = state.db['questions'][qid] | |
ebee6edb | 364 | bot.msg(user, "Done. Next question is: %s" % (state.nextq[0])) |
9306587e | 365 | except Exception as e: |
366 | bot.msg(user, "Error: %s" % (e)) | |
367 | ||
368 | @lib.hook('setnext', glevel=lib.STAFF, needchan=False) | |
c695f740 | 369 | @lib.argsGE(1) |
370 | def cmd_setnext(bot, user, chan, realtarget, *args): | |
371 | line = ' '.join([str(arg) for arg in args]) | |
372 | linepieces = line.split('*') | |
b5c89dfb | 373 | if len(linepieces) < 2: |
374 | bot.msg(user, "Error: need <question>*<answer>") | |
375 | return | |
c695f740 | 376 | question = linepieces[0].strip() |
377 | answer = linepieces[1].strip() | |
ebee6edb | 378 | state.nextq = [question, answer] |
c695f740 | 379 | bot.msg(user, "Done.") |
380 | ||
9306587e | 381 | @lib.hook('skip', glevel=1, needchan=False) |
c695f740 | 382 | def cmd_skip(bot, user, chan, realtarget, *args): |
442ed923 | 383 | state.nextquestion(qskipped=True, skipwait=True) |
c695f740 | 384 | |
f6252f1c | 385 | @lib.hook('start', needchan=False) |
c695f740 | 386 | def cmd_start(bot, user, chan, realtarget, *args): |
387 | if chan == realtarget: replyto = chan | |
388 | else: replyto = user | |
389 | ||
442ed923 | 390 | if state.curq is None and state.pointvote is None and state.nextquestiontimer is None: |
391 | state.nextquestion(skipwait=True) | |
c53f6514 | 392 | elif state.pointvote is not None: |
393 | bot.msg(replyto, "There's a vote in progress!") | |
c695f740 | 394 | else: |
395 | bot.msg(replyto, "Game is already started!") | |
396 | ||
9306587e | 397 | @lib.hook('stop', glevel=1, needchan=False) |
c695f740 | 398 | def cmd_stop(bot, user, chan, realtarget, *args): |
fadbf980 | 399 | if stop(): |
400 | bot.msg(state.chan, "Game stopped by %s" % (user)) | |
401 | else: | |
402 | bot.msg(user, "Game isn't running.") | |
c695f740 | 403 | |
fadbf980 | 404 | def stop(): |
9306587e | 405 | try: |
406 | if state.curq is not None: | |
407 | state.curq = None | |
408 | try: | |
409 | state.steptimer.cancel() | |
410 | except Exception as e: | |
411 | print "!!! steptimer.cancel(): %s %r" % (e,e) | |
442ed923 | 412 | try: |
413 | state.nextquestiontimer.cancel() | |
414 | state.nextquestiontimer = None | |
415 | except Exception as e: | |
416 | print "!!! nextquestiontimer.cancel(): %s %r" % (e,e) | |
9306587e | 417 | return True |
418 | else: | |
419 | return False | |
420 | except NameError: | |
421 | pass | |
80d02bd8 | 422 | |
f6252f1c | 423 | @lib.hook('rank', needchan=False) |
80d02bd8 | 424 | def cmd_rank(bot, user, chan, realtarget, *args): |
c695f740 | 425 | if chan == realtarget: replyto = chan |
80d02bd8 | 426 | else: replyto = user |
427 | ||
c695f740 | 428 | if len(args) != 0: who = args[0] |
429 | else: who = user | |
430 | ||
77c61775 | 431 | bot.msg(replyto, "%s is in %d place (%s points). Target is: %s (%s points)." % (who, state.rank(who), state.points(who), state.targetuser(who), state.targetpoints(who))) |
fadbf980 | 432 | |
f6252f1c | 433 | @lib.hook('top10', needchan=False) |
fadbf980 | 434 | def cmd_top10(bot, user, chan, realtarget, *args): |
77c61775 | 435 | if len(state.db['ranks']) == 0: |
436 | return bot.msg(state.db['chan'], "No one is ranked!") | |
fadbf980 | 437 | |
438 | replylist = [] | |
439 | for nick in state.db['ranks'][0:10]: | |
440 | user = state.db['users'][nick] | |
77c61775 | 441 | replylist.append("%s (%s)" % (user['realnick'], user['points'])) |
442 | bot.msg(state.db['chan'], ', '.join(replylist)) | |
443 | ||
9306587e | 444 | @lib.hook('settarget', glevel=lib.ADMIN, needchan=False) |
77c61775 | 445 | def cmd_settarget(bot, user, chan, realtarget, *args): |
446 | try: | |
447 | state.db['target'] = int(args[0]) | |
448 | bot.msg(state.db['chan'], "Target has been changed to %s points!" % (state.db['target'])) | |
38b29993 | 449 | |
7bd5e7d5 | 450 | if state.pointvote is not None: |
451 | state.pointvote.cancel() | |
452 | state.pointvote = None | |
38b29993 | 453 | bot.msg(state.db['chan'], "Vote has been cancelled!") |
7bd5e7d5 | 454 | except Exception as e: |
455 | print e | |
77c61775 | 456 | bot.msg(user, "Failed to set target.") |
457 | ||
38b29993 | 458 | @lib.hook('vote', needchan=False) |
459 | def cmd_vote(bot, user, chan, realtarget, *args): | |
7bd5e7d5 | 460 | if state.pointvote is not None: |
38b29993 | 461 | if int(args[0]) in state.voteamounts: |
462 | state.voteamounts[int(args[0])] += 1 | |
463 | bot.msg(user, "Your vote has been recorded.") | |
464 | else: | |
465 | bot.msg(user, "Sorry - that's not an option!") | |
466 | else: | |
467 | bot.msg(user, "There's no vote in progress.") | |
468 | ||
9306587e | 469 | @lib.hook('maxmissed', glevel=lib.ADMIN, needchan=False) |
c4763e66 | 470 | def cmd_maxmissed(bot, user, chan, realtarget, *args): |
471 | try: | |
472 | state.db['maxmissedquestions'] = int(args[0]) | |
473 | bot.msg(state.db['chan'], "Max missed questions before round ends has been changed to %s." % (state.db['maxmissedquestions'])) | |
474 | except: | |
475 | bot.msg(user, "Failed to set maxmissed.") | |
476 | ||
9306587e | 477 | @lib.hook('hinttimer', glevel=lib.ADMIN, needchan=False) |
c4763e66 | 478 | def cmd_hinttimer(bot, user, chan, realtarget, *args): |
479 | try: | |
480 | state.db['hinttimer'] = float(args[0]) | |
481 | bot.msg(state.db['chan'], "Time between hints has been changed to %s." % (state.db['hinttimer'])) | |
482 | except: | |
483 | bot.msg(user, "Failed to set hint timer.") | |
484 | ||
9306587e | 485 | @lib.hook('hintnum', glevel=lib.ADMIN, needchan=False) |
c4763e66 | 486 | def cmd_hintnum(bot, user, chan, realtarget, *args): |
487 | try: | |
488 | state.db['hintnum'] = int(args[0]) | |
489 | bot.msg(state.db['chan'], "Max number of hints has been changed to %s." % (state.db['hintnum'])) | |
490 | except: | |
491 | bot.msg(user, "Failed to set hintnum.") | |
492 | ||
442ed923 | 493 | @lib.hook('questionpause', glevel=lib.ADMIN, needchan=False) |
494 | def cmd_questionpause(bot, user, chan, realtarget, *args): | |
495 | try: | |
496 | state.db['questionpause'] = float(args[0]) | |
497 | bot.msg(state.db['chan'], "Pause between questions has been changed to %s." % (state.db['questionpause'])) | |
498 | except: | |
499 | bot.msg(user, "Failed to set questionpause.") | |
500 | ||
9306587e | 501 | @lib.hook('findq', glevel=1, needchan=False) |
c4763e66 | 502 | def cmd_findquestion(bot, user, chan, realtarget, *args): |
ebee6edb | 503 | matches = [str(i) for i in range(len(state.db['questions'])) if state.db['questions'][i][0] == ' '.join(args)] #TODO looser equality check |
c4763e66 | 504 | if len(matches) > 1: |
505 | bot.msg(user, "Multiple matches: %s" % (', '.join(matches))) | |
506 | elif len(matches) == 1: | |
507 | bot.msg(user, "One match: %s" % (matches[0])) | |
508 | else: | |
509 | bot.msg(user, "No match.") | |
510 | ||
9306587e | 511 | @lib.hook('delq', glevel=lib.STAFF, needchan=False) |
512 | @lib.hook('deleteq', glevel=lib.STAFF, needchan=False) | |
c4763e66 | 513 | def cmd_deletequestion(bot, user, chan, realtarget, *args): |
514 | try: | |
515 | backup = state.db['questions'][int(args[0])] | |
516 | del state.db['questions'][int(args[0])] | |
ebee6edb | 517 | bot.msg(user, "Deleted %s*%s" % (backup[0], backup[1])) |
c4763e66 | 518 | except: |
519 | bot.msg(user, "Couldn't delete that question.") | |
520 | ||
9306587e | 521 | @lib.hook('addq', glevel=lib.STAFF, needchan=False) |
c4763e66 | 522 | def cmd_addquestion(bot, user, chan, realtarget, *args): |
523 | line = ' '.join([str(arg) for arg in args]) | |
524 | linepieces = line.split('*') | |
525 | if len(linepieces) < 2: | |
526 | bot.msg(user, "Error: need <question>*<answer>") | |
527 | return | |
528 | question = linepieces[0].strip() | |
529 | answer = linepieces[1].strip() | |
ebee6edb | 530 | state.db['questions'].append([question, answer]) |
c4763e66 | 531 | bot.msg(user, "Done. Question is #%s" % (len(state.db['questions'])-1)) |
532 | ||
533 | ||
f6252f1c | 534 | @lib.hook('triviahelp', needchan=False) |
77c61775 | 535 | def cmd_triviahelp(bot, user, chan, realtarget, *args): |
9306587e | 536 | if user.glevel == 0: |
537 | bot.msg(user, "START") | |
538 | bot.msg(user, "TOP10") | |
442ed923 | 539 | bot.msg(user, "POINTS [<user>]") |
540 | bot.msg(user, "RANK [<user>]") | |
9306587e | 541 | else: |
442ed923 | 542 | bot.msg(user, "START (ANYONE )") |
543 | bot.msg(user, "TOP10 (ANYONE )") | |
544 | bot.msg(user, "POINTS [<user>] (ANYONE )") | |
545 | bot.msg(user, "RANK [<user>] (ANYONE )") | |
9306587e | 546 | if user.glevel >= 1: |
442ed923 | 547 | bot.msg(user, "SKIP (>=KNOWN)") |
548 | bot.msg(user, "STOP (>=KNOWN)") | |
549 | bot.msg(user, "FINDQ <question> (>=KNOWN)") | |
9306587e | 550 | if user.glevel >= lib.STAFF: |
442ed923 | 551 | bot.msg(user, "GIVE <user> [<points>] (>=STAFF)") |
552 | bot.msg(user, "SETNEXT <q>*<a> (>=STAFF)") | |
553 | bot.msg(user, "ADDQ <q>*<a> (>=STAFF)") | |
554 | bot.msg(user, "DELETEQ <q>*<a> (>=STAFF) [aka DELQ]") | |
9306587e | 555 | if user.glevel >= lib.ADMIN: |
442ed923 | 556 | bot.msg(user, "SETTARGET <points> (>=ADMIN)") |
557 | bot.msg(user, "MAXMISSED <questions> (>=ADMIN)") | |
558 | bot.msg(user, "HINTTIMER <float seconds> (>=ADMIN)") | |
559 | bot.msg(user, "HINTNUM <hints> (>=ADMIN)") | |
560 | bot.msg(user, "QUESTIONPAUSE <float seconds> (>=ADMIN)") | |
b5c89dfb | 561 | |
6374d61f | 562 | @lib.hooknum(417) |
563 | def num_417(bot, textline): | |
564 | bot.msg(state.db['chan'], "Whoops, it looks like that question didn't quite go through! (E:417). Let's try another...") | |
442ed923 | 565 | state.nextquestion(qskipped=False, skipwait=True) |
6374d61f | 566 | |
f8cc0124 | 567 | @lib.hooknum(332) |
568 | def num_TOPIC(bot, textline): | |
569 | pieces = textline.split(None, 4) | |
570 | chan = pieces[3] | |
571 | if chan != state.db['chan']: | |
572 | return | |
573 | gottopic = pieces[4][1:] | |
574 | ||
575 | formatted = state.db['topicformat'] % { | |
576 | 'chan': state.db['chan'], | |
577 | 'top': state.db['users'][state.db['ranks'][0]]['realnick'], | |
578 | 'top3': '/'.join([state.db['users'][state.db['ranks'][x]]['realnick'] for x in range(3) if x < len(state.db['ranks'])]), | |
579 | 'topscore': state.db['users'][state.db['ranks'][0]]['points'], | |
580 | 'target': state.db['target'], | |
581 | } | |
582 | if gottopic != formatted: | |
583 | state.getbot().conn.send("TOPIC %s :%s" % (state.db['chan'], formatted)) | |
584 | ||
b5c89dfb | 585 | |
586 | def specialQuestion(oldq): | |
ebee6edb | 587 | newq = [oldq[0], oldq[1]] |
588 | qtype = oldq[0].upper() | |
b5c89dfb | 589 | |
590 | if qtype == "!MONTH": | |
ebee6edb | 591 | newq[0] = "What month is it currently (in UTC)?" |
592 | newq[1] = time.strftime("%B", time.gmtime()).lower() | |
b5c89dfb | 593 | elif qtype == "!MATH+": |
594 | randnum1 = random.randrange(0, 11) | |
595 | randnum2 = random.randrange(0, 11) | |
ebee6edb | 596 | newq[0] = "What is %d + %d?" % (randnum1, randnum2) |
597 | newq[1] = spellout(randnum1+randnum2) | |
b5c89dfb | 598 | return newq |
599 | ||
600 | def spellout(num): | |
601 | return [ | |
602 | "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", | |
603 | "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", | |
604 | "sixteen", "seventeen", "eighteen", "nineteen", "twenty" | |
605 | ][num] |