4 import time
, os
, random
, json
5 from threading
import Timer
6 from traceback
import print_exc
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
20 global questions
, points
21 cache
.currmod
= __name__
25 qfile
= open(cache
.moduledata
+"/trivia/questions.txt", 'r')
27 print_exc(None, cache
.excfile
)
29 questions
= qfile
.readlines()
33 ptsfile
= open(cache
.moduledata
+"/trivia/points.json", 'r')
35 print_exc(None, cache
.excfile
)
37 points
= json
.load(ptsfile
)
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__
46 ptsfile
= open(cache
.moduledata
+"/trivia/points.json", 'w')
47 json
.dump(points
, ptsfile
, indent
=4)
53 cache
.unhookcmd('TRIVIA')
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'] = []
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(' ')
75 if piece
== '': continue
77 reveal
= int(round(plen
*0.40))
78 if reveal
< 2: reveal
= 2
79 if reveal
> plen
: reveal
= plen
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)
90 hiddenpieces
.append(piece
[i
])
91 elif not piece
[i
].isalnum():
92 hiddenpieces
.append(piece
[i
])
94 hiddenpieces
.append('*')
95 hintpieces
.append(''.join(hiddenpieces
))
96 hint
= ' '.join(hintpieces
)
97 cache
.chans
[chan
].bot
.msg(chan
, "Hint: %s" % (hint
))
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")
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.']