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