]>
Commit | Line | Data |
---|---|---|
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 | |
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): | |
9557ee54 | 17 | state.parent = parent |
80d02bd8 | 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): | |
9557ee54 | 28 | def __init__(self, questionfile): |
80d02bd8 | 29 | self.questionfile = questionfile |
30 | self.db = json.load(open(questionfile, "r")) | |
9557ee54 | 31 | self.chan = self.db['chan'] |
32 | self.curq = None | |
80d02bd8 | 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 | |
9557ee54 | 40 | self.parent.channel(self.chan).bot.msg(self.chan, "Next up: %s" % (nextq['question'])) |
80d02bd8 | 41 | |
42 | def checkanswer(self, answer): | |
9557ee54 | 43 | if self.curq is None: |
44 | return False | |
45 | elif isinstance(self.curq['answer'], basestring): | |
80d02bd8 | 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): | |
9557ee54 | 51 | _user = str(_user) |
80d02bd8 | 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'])} | |
9557ee54 | 57 | self.db['ranks'].append(user) |
80d02bd8 | 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): | |
9557ee54 | 71 | user = str(user).lower() |
80d02bd8 | 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): | |
9557ee54 | 78 | user = str(user) |
80d02bd8 | 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 | ||
9557ee54 | 84 | state = TriviaState("/home/jrunyon/erebus/modules/trivia.json") #TODO |
85 | ||
80d02bd8 | 86 | @lib.hookchan(state.db['chan']) |
87 | def trivia_checkanswer(bot, user, chan, *args): | |
80d02bd8 | 88 | line = ' '.join([str(arg) for arg in args]) |
9557ee54 | 89 | bot.msg('dimecadmium', line) |
80d02bd8 | 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))) | |
9557ee54 | 92 | state.nextquestion() |
80d02bd8 | 93 | |
94 | @lib.hook('points') | |
95 | def cmd_points(bot, user, chan, realtarget, *args): | |
80d02bd8 | 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): | |
80d02bd8 | 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): | |
80d02bd8 | 118 | if chan is not None: replyto = chan |
119 | else: replyto = user | |
120 | ||
9557ee54 | 121 | bot.msg(replyto, "%s is in %d place." % (args[0], state.rank(args[0]))) |