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