]> jfr.im git - z_archive/Ophion.git/blob - modules/trivia.py
Initialize repo
[z_archive/Ophion.git] / modules / trivia.py
1 from classes import *
2 from util import *
3
4 import time, os, random, json
5 from threading import Timer
6 from traceback import print_exc
7
8 name = 'trivia'
9 author = 'John Runyon'
10 version = '1'
11
12 questions = []
13 points = {}
14
15 aqinterval = 10 # seconds from question end -> next question
16 qhinterval = 30 # seconds from question prompt -> hint
17 hainterval = 30 # seconds from hint -> question end
18
19 def init(cache):
20 global questions, points
21 cache.currmod = __name__
22 cache.trivia = {}
23
24 try:
25 qfile = open(cache.triviapath+"/questions.txt", 'r')
26 except IOError as e:
27 print_exc(None, cache.excfile)
28 return True
29 questions = qfile.readlines()
30 qfile.close()
31
32 try:
33 ptsfile = open(cache.triviapath+"/points.json", 'r')
34 except IOError as e:
35 print_exc(None, cache.excfile)
36 return True
37 points = json.load(ptsfile)
38 ptsfile.close()
39
40 cache.hookcmd('TRIVIA', 1, trivia, 1, helptrivia)
41 cache.hookcmd('T', 0, t, 1, helpt)
42 def deinit(cache, reloading=False):
43 global questions, points
44 cache.currmod = __name__
45
46 ptsfile = open(cache.triviapath+"/points.json", 'w')
47 json.dump(points, ptsfile, indent=4)
48 ptsfile.close()
49
50 del questions
51 del points
52 del cache.trivia
53 cache.unhookcmd('TRIVIA')
54
55 def _loadQuestion():
56 tdict = {}
57 line = random.choice(questions)
58 parts = line.split('~', 2)
59 tdict['question'] = parts[0]
60 tdict['answer'] = parts[1].lower()
61 if len(parts) == 3: tdict['flags'] = parts[2].split('~')
62 else: tdict['flags'] = []
63 return tdict
64 def _sendQuestion(cache, chan):
65 tdict = cache.trivia[chan]['tdict']
66 cache.chans[chan].bot.msg(chan, "Next question: %s" % (tdict['question']))
67 timer = Timer(qhinterval, _sendHint, kwargs={'cache': cache, 'chan': chan})
68 cache.trivia[chan]['timer'] = timer
69 cache.trivia[chan]['timertype'] = '_sendHint'
70 def _sendHint(cache, chan):
71 tdict = cache.trivia[chan]['tdict']
72 pieces = tdict['answer'].split(' ')
73 hintpieces = []
74 for piece in pieces:
75 if piece == '': continue
76 plen = len(piece)
77 reveal = int(round(plen*0.40))
78 if reveal < 2: reveal = 2
79 if reveal > plen: reveal = plen
80
81 revealpos = []
82 for i in range(reveal):
83 pos = random.randint(0, plen-1)
84 while pos in revealpos:
85 pos = random.randint(0, plen-1)
86 revealpos.append(pos)
87 hiddenpieces = []
88 for i in range(plen):
89 if i in revealpos:
90 hiddenpieces.append(piece[i])
91 elif not piece[i].isalnum():
92 hiddenpieces.append(piece[i])
93 else:
94 hiddenpieces.append('*')
95 hintpieces.append(''.join(hiddenpieces))
96 hint = ' '.join(hintpieces)
97 cache.chans[chan].bot.msg(chan, "Hint: %s" % (hint))
98
99 def trivia(nick, target, params, bot, cache):
100 chan = target.lower()
101 if params == 'start':
102 tdict = _loadQuestion()
103 timer = Timer(aqinterval, _sendQuestion, kwargs={'cache': cache, 'chan': chan})
104 cache.trivia[chan] = {'tdict': tdict, 'timer': timer, 'timertype': '_sendQuestion'}
105 elif params == 'stop':
106 cache.trivia[chan]['timer'].cancel()
107 del cache.trivia[chan]
108 def t(nick, target, params, bot, cache):
109 chan = target.lower()
110 guess = params.lower()
111 if chan not in cache.trivia:
112 bot.msg(nick, "Trivia isn't running in %s!" % (chan))
113 elif nick not in cache.users or cache.users[nick] == '':
114 bot.msg(nick, "You must be AUTH'ed and recognized by me! Try !AUTH")
115 else:
116 pass #TODO
117
118 def helptrivia(): return ['TRIVIA <#channel> start|stop', 'Starts or stops trivia.']
119 def helpt(): return ['T <#channel> <answer>', 'Attempt to answer a trivia question. You must be authed with the bot. If no reply, guess was incorrect.']