]> jfr.im git - erebus.git/blame - modules/trivia.py
beginning to write a trivia-bot module.
[erebus.git] / modules / trivia.py
CommitLineData
80d02bd8 1# Erebus IRC bot - Author: Erebus Team
2# simple module example
3# This file is released into the public domain; see http://unlicense.org/
4
5# module info
6modinfo = {
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
14import modlib
15lib = modlib.modlib(__name__)
16def modstart(parent, *args, **kwargs):
17 global state
18 state = TriviaState("/home/jrunyon/erebus/modules/trivia.json", parent) #TODO
19 return lib.modstart(parent, *args, **kwargs)
20def modstop(*args, **kwargs):
21 global state
22 del state
23 return lib.modstop(*args, **kwargs)
24
25# module code
26import json, random
27
28class TriviaState(object):
29 def __init__(self, questionfile, parent):
30 self.parent = parent
31 self.questionfile = questionfile
32 self.db = json.load(open(questionfile, "r"))
33
34 def __del__(self):
35 json.dump(self.db, open(self.questionfile, "w"), indent=4, separators=(',',': '))
36
37 def nextquestion(self):
38 nextq = random.choice(self.db['questions'])
39 self.curq = nextq
40 self.parent.msg(self.chan, "Next up: %s" % (nextq['question']))
41
42 def checkanswer(self, answer):
43 if isinstance(self.curq['answer'], basestring):
44 return answer.lower() == self.curq['answer']
45 else: # assume it's a list or something.
46 return answer.lower() in self.curq['answer']
47
48 def addpoint(self, _user, count=1):
49 user = _user.lower()
50 if user in self.db['users']:
51 self.db['users'][user]['points'] += count
52 else:
53 self.db['users'][user] = {'points': count, 'realnick': _user, 'rank': len(self.db['ranks'])}
54 self.db['ranks']append(user)
55
56 oldrank = self.db['users'][user]['rank']
57 while oldrank != 0:
58 nextperson = self.db['ranks'][oldrank-1]
59 if self.db['users'][user]['points'] > self.db['users'][nextperson]['points']:
60 self.db['ranks'][oldrank-1] = user
61 self.db['ranks'][oldrank] = nextperson
62 oldrank = oldrank-1
63 else:
64 break
65 return self.db['users'][user]['points']
66
67 def points(self, user):
68 user = user.lower()
69 if user in self.db['users']:
70 return self.db['users'][user]['points']
71 else:
72 return 0
73
74 def rank(self, user):
75 return self.db['users'][user]['rank']
76
77 def targetuser(self, user): return "TODO" #TODO
78 def targetpoints(self, user): return 0 #TODO
79
80@lib.hookchan(state.db['chan'])
81def trivia_checkanswer(bot, user, chan, *args):
82 global state
83 line = ' '.join([str(arg) for arg in args])
84 if state.checkanswer(line):
85 bot.msg(chan, "\00308%s\003 has it! The answer was \00308%s\003. Current points: %d. Rank: %d. Target: %s (%d)." % (user, line, state.addpoint(user), state.rank(user), state.targetuser(user), state.targetpoints(user)))
86 state.nextquestion()
87
88@lib.hook('points')
89def cmd_points(bot, user, chan, realtarget, *args):
90 global state
91 if chan is not None: replyto = chan
92 else: replyto = user
93
94 if len(args) != 0: who = args[0]
95 else: who = user
96
97 bot.msg(replyto, "%s has %d points." % (who, state.points(who)))
98
99@lib.hook('give', clevel=lib.OP)
100@lib.argsGE(1)
101def cmd_give(bot, user, chan, realtarget, *args):
102 global state
103 if len(args) > 1 and args[1] != 1:
104 bot.msg(user, "Giving more than one point is not yet implemented.")
105 return NotImplemented
106
107 whoto = args[0]
108 balance = state.addpoint(whoto)
109 bot.msg(chan, "%s gave %s %d points. New balance: %d" % (user, whoto, 1, balance))
110
111@lib.hook('rank')
112@lib.argsEQ(1)
113def cmd_rank(bot, user, chan, realtarget, *args):
114 global state
115 if chan is not None: replyto = chan
116 else: replyto = user
117
118 bot.msg(replyto, "%s is in %d place." % (args[0], state.rank(args[0]))