4 from datetime
import datetime
5 from decimal
import Decimal
, InvalidOperation
7 from operator
import itemgetter
8 from api
import citizen
, feed
, region
, utils
9 from pseudoclient
.cmd_manager
import *
11 from esim_utils
import *
13 def command_citizen_register(self
, manager
, opts
, arg
, channel
, sender
):
15 c
= citizen
.from_id(int(arg
)) if 'id' in opts
else citizen
.from_name(arg
)
17 self
.errormsg(channel
, '%s is not a valid id.' % arg
)
19 except feed
.FeedError
, e
:
20 self
.errormsg(channel
, e
.msg
)
24 self
.errormsg(channel
, "%s, @b%s@b is an organization, you cannot register an organization under your nick." % (sender
, c
.name
))
27 self
.users
.set(sender
, 'citizen', c
.id)
28 self
.msg(channel
, '%s: registered e-Sim citizen @b%s@b' % (sender
, c
.name
))
30 def command_citizen_info(self
, manager
, opts
, arg
, channel
, sender
):
31 c
= self
.get_citizen(opts
, arg
, channel
, sender
)
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(
41 dmgToday
= format(c
.dmgToday
, ',d'),
42 dmgTotal
= format(c
.dmgTotal
, ',d')
45 message
= "@b%s@b [%d] @sep @bOrganization@b @sep @bLocation@b: %s" % (c
.name
, c
.id, c
.citizenship
)
47 self
.msg(channel
, format_citizen_message(c
, message
))
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
)
55 if 'rank' in opts
and 'strength' in opts
:
58 c
= self
.get_citizen(opts
, arg
, channel
, sender
)
64 self
.errormsg(channel
, '%s is an organization.' % c
.name
)
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']
71 if 'influence' in opts
:
73 self
.errormsg(channel
, 'option -I needs a citizen')
76 if 'objective' in opts
:
77 objective
= opts
['objective']
78 if objective
>= 10000000000:
79 self
.errormsg(channel
, 'option -o out of range')
82 obj_str
= 'fights required for {inf:,} '.format(inf
=objective
)
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
92 damage
= [utils
.fight_calc(q
, rank
, strength
, region_bonus
, ds_bonus
, mu_bonus
, surrounded
) for q
in range(6)]
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
))]
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
,
102 'fights' : ' in %d fights' % fights
if fights
!= 1 else '',
103 'damage' : ' '.join(damagestr
)}
105 self
.msg(channel
, format_citizen_message(c
, message
))
107 def command_citizen_link(self
, manager
, opts
, arg
, channel
, sender
):
108 c
= self
.get_citizen(opts
, arg
, channel
, sender
)
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' % {
117 'name' : format_name(c
.name
),
120 self
.msg(channel
, format_citizen_message(c
, message
))
122 def command_citizen_donate(self
, manager
, opts
, arg
, channel
, sender
):
123 c
= self
.get_citizen(opts
, arg
, channel
, sender
)
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""") % {
133 'name' : format_name(c
.name
),
136 def command_citizen_companies(self
, manager
, opts
, arg
, channel
, sender
):
137 c
= self
.get_citizen(opts
, arg
, channel
, sender
)
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' % {
146 'name' : format_name(c
.name
),
149 self
.msg(channel
, format_citizen_message(c
, message
))
151 def command_citizen_message(self
, manager
, opts
, arg
, channel
, sender
):
152 c
= self
.get_citizen(opts
, arg
, channel
, sender
)
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') % {
161 'name' : format_name(c
.name
),
164 def command_region_info(self
, manager
, opts
, arg
, channel
, sender
):
165 r
= self
.get_region(opts
, arg
, channel
, sender
)
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' % {
172 'country' : r
.country
['shortName'],
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'])
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
)
186 chan
= self
.inter
.findChannel(channel
)
187 senderinfo
= self
.inter
.findUser(sender
)
188 if not chan
or not senderinfo
:
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.')
197 members
= chan
.getUsers() # Set<String>
198 it
= members
.iterator()
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']:
207 userlist
.append(user
)
211 while len(userlist
) > 0:
213 out
+= ' ' + userlist
.pop()
215 out
+= userlist
.pop()
217 self
.msg(channel
, '@sep @bListen up!@b @sep %s @sep' % out
)
219 self
.msg(channel
, '@sep @bListen up!@b @sep %s @sep' % out
)
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'})
225 def command_esim_help(self
, manager
, opts
, arg
, channel
, sender
):
226 command
= arg
.lower()
229 message
= ['e-Sim: .help e-Sim - for e-Sim commands']
230 elif command
== 'e-sim':
231 message
= manager
.get_help()
233 message
= manager
.get_help(command
)
236 message
= ['%s is not a valid command.' % arg
]
239 self
.notice(sender
, line
)
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
)
245 class UserCommandManager(CommandManager
):
246 def get_prefix(self
):
249 def get_commands(self
):
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', [
258 'lookup': (command_citizen_info
, ARG_OPT
, 'shows useful data about the citizen', [
265 'fightcalc': (command_citizen_fightcalc
, ARG_OPT
, 'calculates influence done with different weapon qualities', [
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
),
279 'link': (command_citizen_link
, ARG_OPT
, "return a link to the target citizen's profile", [
285 'donate': (command_citizen_donate
, ARG_OPT
, 'link to the donation page of a citizen', [
291 'companies': (command_citizen_companies
, ARG_OPT
, 'link to the citizen company page', [
298 'message': (command_citizen_message
, ARG_OPT
, 'link to send a message to a citizen', [
304 'region': (command_region_info
, ARG_YES
, 'information about a region', [
309 'highlight': (command_highlight
, ARG_OPT
, 'highlights all the nicks in the channel (limited to channel ops, admins and founders)', [], 'optional_message'),
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', []),