]> jfr.im git - erebus.git/blob - modules/trivia.py
0762bed45094ff48349a0c417932c305efe49ec6
[erebus.git] / modules / trivia.py
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
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):
17 state.parent = parent
18 return lib.modstart(parent, *args, **kwargs)
19 def modstop(*args, **kwargs):
20 global state
21 del state
22 return lib.modstop(*args, **kwargs)
23
24 # module code
25 import json, random
26
27 class TriviaState(object):
28 def __init__(self, questionfile):
29 self.questionfile = questionfile
30 self.db = json.load(open(questionfile, "r"))
31 self.chan = self.db['chan']
32 self.curq = None
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.channel(self.chan).bot.msg(self.chan, "Next up: %s" % (nextq['question']))
41
42 def checkanswer(self, answer):
43 if self.curq is None:
44 return False
45 elif isinstance(self.curq['answer'], basestring):
46 return answer.lower() == self.curq['answer']
47 else: # assume it's a list or something.
48 return answer.lower() in self.curq['answer']
49
50 def addpoint(self, _user, count=1):
51 _user = str(_user)
52 user = _user.lower()
53 if user in self.db['users']:
54 self.db['users'][user]['points'] += count
55 else:
56 self.db['users'][user] = {'points': count, 'realnick': _user, 'rank': len(self.db['ranks'])}
57 self.db['ranks'].append(user)
58
59 oldrank = self.db['users'][user]['rank']
60 while oldrank != 0:
61 nextperson = self.db['ranks'][oldrank-1]
62 if self.db['users'][user]['points'] > self.db['users'][nextperson]['points']:
63 self.db['ranks'][oldrank-1] = user
64 self.db['ranks'][oldrank] = nextperson
65 oldrank = oldrank-1
66 else:
67 break
68 return self.db['users'][user]['points']
69
70 def points(self, user):
71 user = str(user).lower()
72 if user in self.db['users']:
73 return self.db['users'][user]['points']
74 else:
75 return 0
76
77 def rank(self, user):
78 user = str(user)
79 return self.db['users'][user]['rank']
80
81 def targetuser(self, user): return "TODO" #TODO
82 def targetpoints(self, user): return 0 #TODO
83
84 state = TriviaState("/home/jrunyon/erebus/modules/trivia.json") #TODO
85
86 @lib.hookchan(state.db['chan'])
87 def trivia_checkanswer(bot, user, chan, *args):
88 line = ' '.join([str(arg) for arg in args])
89 bot.msg('dimecadmium', line)
90 if state.checkanswer(line):
91 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)))
92 state.nextquestion()
93
94 @lib.hook('points')
95 def cmd_points(bot, user, chan, realtarget, *args):
96 if chan is not None: replyto = chan
97 else: replyto = user
98
99 if len(args) != 0: who = args[0]
100 else: who = user
101
102 bot.msg(replyto, "%s has %d points." % (who, state.points(who)))
103
104 @lib.hook('give', clevel=lib.OP)
105 @lib.argsGE(1)
106 def cmd_give(bot, user, chan, realtarget, *args):
107 if len(args) > 1 and args[1] != 1:
108 bot.msg(user, "Giving more than one point is not yet implemented.")
109 return NotImplemented
110
111 whoto = args[0]
112 balance = state.addpoint(whoto)
113 bot.msg(chan, "%s gave %s %d points. New balance: %d" % (user, whoto, 1, balance))
114
115 @lib.hook('rank')
116 @lib.argsEQ(1)
117 def cmd_rank(bot, user, chan, realtarget, *args):
118 if chan is not None: replyto = chan
119 else: replyto = user
120
121 bot.msg(replyto, "%s is in %d place." % (args[0], state.rank(args[0])))