]> jfr.im git - irc/rizon/acid.git/blob - pyva/pyva/src/main/python/esim/cmd_user.py
Split pyva plugin into pyva.core and pyva.pyva
[irc/rizon/acid.git] / pyva / pyva / src / main / python / esim / cmd_user.py
1 import itertools
2 import random
3 import re
4 from datetime import datetime
5 from decimal import Decimal, InvalidOperation
6 from math import ceil
7 from operator import itemgetter
8 from api import citizen, feed, region, utils
9 from pseudoclient.cmd_manager import *
10 from utils import *
11 from esim_utils import *
12
13 def command_citizen_register(self, manager, opts, arg, channel, sender):
14 try:
15 c = citizen.from_id(int(arg)) if 'id' in opts else citizen.from_name(arg)
16 except ValueError, e:
17 self.errormsg(channel, '%s is not a valid id.' % arg)
18 return
19 except feed.FeedError, e:
20 self.errormsg(channel, e.msg)
21 return
22
23 if c.is_organization:
24 self.errormsg(channel, "%s, @b%s@b is an organization, you cannot register an organization under your nick." % (sender, c.name))
25 return
26
27 self.users.set(sender, 'citizen', c.id)
28 self.msg(channel, '%s: registered e-Sim citizen @b%s@b' % (sender, c.name))
29
30 def command_citizen_info(self, manager, opts, arg, channel, sender):
31 c = self.get_citizen(opts, arg, channel, sender)
32
33 if c == None:
34 return
35
36 if not c.is_organization:
37 message = u"""@b{c.name}@b [{c.id}] @sep @bLevel@b: {c.level} [{c.exp}] @sep @bStrength@b: {c.strength} @sep \
38 @bRank@b: {c.rank[name]} [{c.rank[id]}] @sep @bEconomy skill@b: {c.economySkill} @sep @bCitizenship@b: {c.citizenship} \
39 @sep @bToday's damage@b: {dmgToday} @sep @bTotal damage@b: {dmgTotal}""".format(
40 c = c,
41 dmgToday = format(c.dmgToday, ',d'),
42 dmgTotal = format(c.dmgTotal, ',d')
43 )
44 else:
45 message = "@b%s@b [%d] @sep @bOrganization@b @sep @bLocation@b: %s" % (c.name, c.id, c.citizenship)
46
47 self.msg(channel, format_citizen_message(c, message))
48
49 def command_citizen_fightcalc(self, manager, opts, arg, channel, sender):
50 fights = 1 if 'fights' not in opts or 'objective' in opts else opts['fights']
51 if fights >= 100000 or fights == 0:
52 self.errormsg(channel, 'fights: expected value (0 <= x <= 100000), got %d instead' % fights)
53 return
54
55 if 'rank' in opts and 'strength' in opts:
56 c = None
57 else:
58 c = self.get_citizen(opts, arg, channel, sender)
59
60 if c == None:
61 return
62
63 if c.is_organization:
64 self.errormsg(channel, '%s is an organization.' % c.name)
65 return
66
67 colors = [3, 12, 4, 7, 9, 10]
68 rank = c.rank if 'rank' not in opts else opts['rank']
69 strength = c.strength if 'strength' not in opts else opts['strength']
70
71 if 'influence' in opts:
72 if not c:
73 self.errormsg(channel, 'option -I needs a citizen')
74 return
75
76 if 'objective' in opts:
77 objective = opts['objective']
78 if objective >= 10000000000:
79 self.errormsg(channel, 'option -o out of range')
80 return
81
82 obj_str = 'fights required for {inf:,} '.format(inf=objective)
83 else:
84 objective = None
85 obj_str = ''
86
87 region_bonus = True if 'region_bonus' in opts else False
88 surrounded = True if 'surrounded' in opts else False
89 ds_bonus = opts['defense_system'] if 'defense_system' in opts else 0
90 mu_bonus = opts['mu_bonus'] if 'mu_bonus' in opts else 0
91
92 damage = [utils.fight_calc(q, rank, strength, region_bonus, ds_bonus, mu_bonus, surrounded) for q in range(6)]
93 if objective:
94 damage = [1 + (objective / d) if objective % d != 0 else objective / d for d in damage]
95 damagestr = ['@c%d[Q%d: @b%d@b]@c' % (z[0], q, fights * z[1]) for q, z in enumerate(zip(colors, damage))]
96
97 message = '@b%(name)s@b(%(rank)s, %(strength)s strength) %(obj)sinfluence%(fights)s @sep %(damage)s' % {
98 'name' : '%s ' % format_name(c.name) if c else '',
99 'rank' : '%s [%d]' % (rank['name'], rank['id']),
100 'strength': strength,
101 'obj' : obj_str,
102 'fights' : ' in %d fights' % fights if fights != 1 else '',
103 'damage' : ' '.join(damagestr)}
104
105 self.msg(channel, format_citizen_message(c, message))
106
107 def command_citizen_link(self, manager, opts, arg, channel, sender):
108 c = self.get_citizen(opts, arg, channel, sender)
109
110 if c == None:
111 return
112
113 # Here
114 server = 'secura' if 'secura' in opts else 'primera'
115 message = '@b%(name)s@b link @sep http://%(server)s.e-sim.org/profile.html?id=%(id)d' % {
116 'server': server,
117 'name' : format_name(c.name),
118 'id' : c.id}
119
120 self.msg(channel, format_citizen_message(c, message))
121
122 def command_citizen_donate(self, manager, opts, arg, channel, sender):
123 c = self.get_citizen(opts, arg, channel, sender)
124
125 if c == None:
126 return
127
128 # Here
129 server = 'secura' if 'secura' in opts else 'primera'
130 self.msg(channel, format_citizen_message(c, """@b%(name)s@b donations @sep @bMoney@b http://%(server)s.e-sim.org/donateMoney.html?id=%(id)d @sep \
131 @bItems@b http://%(server)s.e-sim.org/donateProducts.html?id=%(id)d""") % {
132 'server': server,
133 'name' : format_name(c.name),
134 'id' : c.id})
135
136 def command_citizen_companies(self, manager, opts, arg, channel, sender):
137 c = self.get_citizen(opts, arg, channel, sender)
138
139 if c == None:
140 return
141
142 # Here
143 server = 'secura' if 'secura' in opts else 'primera'
144 message = '@b%(name)s@b companies @sep http://%(server)s.e-sim.org/companies.html?id=%(id)d' % {
145 'server': server,
146 'name' : format_name(c.name),
147 'id' : c.id}
148
149 self.msg(channel, format_citizen_message(c, message))
150
151 def command_citizen_message(self, manager, opts, arg, channel, sender):
152 c = self.get_citizen(opts, arg, channel, sender)
153
154 if c == None:
155 return
156
157 # Here
158 server = 'secura' if 'secura' in opts else 'primera'
159 self.msg(channel, format_citizen_message(c, '@b%(name)s@b message link @sep http://%(server)s.e-sim.org/composeMessage.html?id=%(id)d') % {
160 'server': server,
161 'name' : format_name(c.name),
162 'id' : c.id})
163
164 def command_region_info(self, manager, opts, arg, channel, sender):
165 r = self.get_region(opts, arg, channel, sender)
166
167 if r == None:
168 return
169
170 self.msg(channel, '@b%(region)s, %(country)s@b [%(id)d]%(cap)s @sep @bPopulation:@b %(pop)d%(raw)s%(cons)s%(borders)s' % {
171 'region' : r.name,
172 'country' : r.country['shortName'],
173 'id' : r.id,
174 'cap' : ' @sep @bCapital city@b' if r.is_capital else '',
175 'pop' : r.population,
176 'raw' : ' @sep @b%s@b level of @b%s@b resource@b' % (r.richness.title(), r.resource.title()) if r.richness else '',
177 'cons' : ' @sep @bDefense buildings:@b %s' % r.def_buildings if r.def_buildings else '',
178 'borders' : ' @sep @bBorders with:@b %s, and %s' % (', '.join(b['name'] for b in r.borders[:-1]), r.borders[-1]['name'])
179 })
180
181 def command_highlight(self, manager, opts, arg, channel, sender):
182 if not self.channels[channel].mass:
183 self.notice(sender, 'The .hl command is disabled for this channel. To enable it, type @b/msg e-Sim highlight %s@b (founder only).' % channel)
184 return
185
186 chan = self.inter.findChannel(channel)
187 senderinfo = self.inter.findUser(sender)
188 if not chan or not senderinfo:
189 return
190
191 sender_modes = chan.getModes(senderinfo.getNick())
192 if '@' not in sender_modes and '&' not in sender_modes and '~' not in sender_modes:
193 self.notice(sender, 'This command is only available to channel ops, admins and founders.')
194 return
195
196 userlist = []
197 members = chan.getUsers() # Set<String>
198 it = members.iterator()
199 while it.hasNext():
200 name = it.next()
201
202 u = self.inter.findUser(name)
203 if not u or u['server'] in ['services.rizon.net', 'geo.rizon.net', 'py2.rizon.net', 'py3.rizon.net']:
204 continue
205
206 user = u['nick']
207 userlist.append(user)
208
209 if userlist:
210 out = ''
211 while len(userlist) > 0:
212 if out:
213 out += ' ' + userlist.pop()
214 else:
215 out += userlist.pop()
216 if len(out) > 400:
217 self.msg(channel, '@sep @bListen up!@b @sep %s @sep' % out)
218 out = ''
219 self.msg(channel, '@sep @bListen up!@b @sep %s @sep' % out)
220
221 def command_esim_info(self, manager, opts, arg, channel, sender):
222 self.notice(sender, '@bRizon e-Sim Bot@b @sep @bDeveloper@b Digital_Lemon @sep @bThanks to@b @uZeroni@u, mink, ElChE and martin @sep @bHelp/feedback@b %(channel)s' % {
223 'channel' : '#e-sim.bot'})
224
225 def command_esim_help(self, manager, opts, arg, channel, sender):
226 command = arg.lower()
227
228 if command == '':
229 message = ['e-Sim: .help e-Sim - for e-Sim commands']
230 elif command == 'e-sim':
231 message = manager.get_help()
232 else:
233 message = manager.get_help(command)
234
235 if message == None:
236 message = ['%s is not a valid command.' % arg]
237
238 for line in message:
239 self.notice(sender, line)
240
241 id_opt = ('id', '-i', 'look up by id instead of name', {'action': 'store_true'}, ARG_YES)
242 nick_opt = ('nick', '-n', 'look up by irc nick', {'action': 'store_true'}, ARG_YES)
243 secura_opt = ('secura', '-S', 'uses Secura\'s API', {'action': 'store_true'}, ARG_YES)
244
245 class UserCommandManager(CommandManager):
246 def get_prefix(self):
247 return '.'
248
249 def get_commands(self):
250 return {
251 'regcit': 'register_citizen',
252 'regnick': 'register_citizen',
253 'register_citizen': (command_citizen_register, ARG_YES, 'links an e-Sim citizen to an IRC nickname', [
254 id_opt,
255 ], 'citizen_name'),
256
257 'lp': 'lookup',
258 'lookup': (command_citizen_info, ARG_OPT, 'shows useful data about the citizen', [
259 id_opt,
260 nick_opt,
261 secura_opt,
262 ]),
263
264 'fc': 'fightcalc',
265 'fightcalc': (command_citizen_fightcalc, ARG_OPT, 'calculates influence done with different weapon qualities', [
266 id_opt,
267 nick_opt,
268 secura_opt,
269 ('strength', '-s', 'uses a custom strength', {'type': '+decimal'}, ARG_OFFLINE|ARG_OFFLINE_REQ),
270 ('rank', '-r', 'uses a custom military rank (use rank ID, not name)', {'type': 'rank'}, ARG_OFFLINE|ARG_OFFLINE_REQ),
271 ('region_bonus', '-b', 'adds region bonus influence (20%)', {'action': 'store_true'}, ARG_OFFLINE),
272 ('defense_system', '-d', 'adds defense system bonus influence', {'type': 'ds_bonus'}, ARG_OFFLINE),
273 ('mu_bonus', '-m', 'adds military unit bonus influence', {'type': 'mu_bonus'}, ARG_OFFLINE),
274 ('surrounded', '-u', 'applies the surrounded debuff', {'action': 'store_true'}, ARG_OFFLINE),
275 ('fights', '-f', 'specifies number of fights', {'type': '+integer'}, ARG_OFFLINE),
276 ('objective', '-o', 'calculates how many fights are required to make a given amount of influence', {'type': '+integer'}, ARG_OFFLINE),
277 ]),
278
279 'link': (command_citizen_link, ARG_OPT, "return a link to the target citizen's profile", [
280 id_opt,
281 nick_opt,
282 secura_opt,
283 ]),
284
285 'donate': (command_citizen_donate, ARG_OPT, 'link to the donation page of a citizen', [
286 id_opt,
287 nick_opt,
288 secura_opt,
289 ]),
290
291 'companies': (command_citizen_companies, ARG_OPT, 'link to the citizen company page', [
292 id_opt,
293 nick_opt,
294 secura_opt,
295 ]),
296
297 'igm': 'message',
298 'message': (command_citizen_message, ARG_OPT, 'link to send a message to a citizen', [
299 id_opt,
300 nick_opt,
301 secura_opt,
302 ]),
303
304 'region': (command_region_info, ARG_YES, 'information about a region', [
305 id_opt
306 ]),
307
308 'hl': 'highlight',
309 'highlight': (command_highlight, ARG_OPT, 'highlights all the nicks in the channel (limited to channel ops, admins and founders)', [], 'optional_message'),
310
311 'info': (command_esim_info, ARG_NO|ARG_OFFLINE, 'Displays version and author information', []),
312 'help': (command_esim_help, ARG_OPT|ARG_OFFLINE, 'Displays available commands and their usage', []),
313 }