-
Notifications
You must be signed in to change notification settings - Fork 13
/
eve.py
285 lines (251 loc) · 8.45 KB
/
eve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import operator
import sqlite3
import time
from math import sqrt
import requests
import config
rs = requests.Session()
rs.headers.update({'User-Agent': 'sbot'})
if config.bot.eve_db is not None:
db = sqlite3.connect(config.bot.eve_db)
esi_price_cache = {'last_update': 0, 'items': {}}
def price_check(cmd):
def __item_info(query):
curs = db.execute('''
SELECT "typeID", "typeName" FROM "invTypes"
WHERE LOWER("typeName") LIKE ? AND "marketGroupID" IS NOT NULL
''', (query.lower(),))
results = curs.fetchmany(3)
if len(results) == 1:
return results[0]
if len(results) == 2 and \
results[0][1].endswith('Blueprint') ^ results[1][1].endswith('Blueprint'):
# an item and its blueprint; show the item
if results[0][1].endswith('Blueprint'):
return results[1]
else:
return results[0]
if len(results) >= 2:
return results
return None
def item_info(item_name):
# exact match
curs = db.execute(
'SELECT "typeID", "typeName" FROM "invTypes" WHERE LOWER("typeName") LIKE ?',
(item_name.lower(),))
result = curs.fetchone()
if result:
return result
# start of string match
results = __item_info(item_name + '%')
if isinstance(results, tuple):
return results
if results:
names = map(lambda r: r[1], results)
cmd.reply('Found items: ' + ', '.join(names))
return None
# substring match
results = __item_info('%' + item_name + '%')
if isinstance(results, tuple):
return results
if results:
names = map(lambda r: r[1], results)
cmd.reply('Found items: ' + ', '.join(names))
return None
cmd.reply('Item not found')
return None
def get_esi_price(typeid):
now = time.time()
if esi_price_cache['last_update'] < now - 60 * 60 * 2:
res = rs.get(
'https://esi.evetech.net/latest/markets/prices/?datasource=tranquility')
if res.status_code == 200:
esi_price_cache['items'].clear()
for item in res.json():
esi_price_cache['items'][item['type_id']] = item
prices = esi_price_cache['items'][typeid]
if prices and 'average_price' in prices:
if prices['average_price'] < 1000.0:
return 'avg {average_price:g} adj {adjusted_price:g}'.format(**prices)
for k, v in prices.items():
prices[k] = int(v)
return 'avg {average_price:,d} adj {adjusted_price:,d}'.format(**prices)
else:
return 'n/a'
if not cmd.args:
return
result = item_info(cmd.args)
if not result:
return
typeid, item_name = result
try:
esi = get_esi_price(typeid)
cmd.reply('%s: %s' % (item_name, esi))
except KeyError:
cmd.reply('error: could not find %r in ESI market prices' % item_name)
def jumps(cmd):
split = cmd.args.split()
if not 2 <= len(split) <= 3:
cmd.reply('usage: `!jumps [from] [to] (safe|shortest)`')
return
results = []
for i in range(2):
curs = db.execute('''
SELECT "solarSystemID" FROM "mapSolarSystems"
WHERE "solarSystemName" LIKE ? LIMIT 3
''', (split[i],))
matches = list(map(operator.itemgetter(0), curs.fetchall()))
if len(matches) == 0:
cmd.reply('no systems found for {}'.format(split[i]))
return
assert len(matches) == 1, f'more than 1 system found for {split[i]}'
results.append(matches[0])
if len(split) == 3 and split[2] in ['safe', 'secure']:
flag = 'secure'
else:
flag = 'shortest'
r = rs.get('https://esi.evetech.net/latest/route/{}/{}/?datasource=tranquility&flag={}'.format(
results[0], results[1], flag))
try:
data = r.json()
except ValueError:
cmd.reply('error getting jumps')
return
jumps_split = []
for j in data:
curs.execute('''SELECT "solarSystemName", "security"
FROM "mapSolarSystems"
WHERE "solarSystemID" = ?''', (j,))
system_name, security = curs.fetchone()
if security >= 0.45:
sec_emoji = 'green_apple'
elif security > 0.0:
sec_emoji = 'yellow_heart'
else:
sec_emoji = 'red_circle'
jumps_split.append(':%s: %s' % (sec_emoji, system_name))
cmd.reply('{} jumps:\n'.format(len(jumps_split)-1) + ' \u2192 '.join(jumps_split))
def lightyears(cmd):
split = [n + '%' for n in cmd.args.lower().split()]
if len(split) != 2:
cmd.reply('usage: !ly [from] [to]')
return
curs = db.execute('''
SELECT "solarSystemName", x, y, z FROM "mapSolarSystems"
WHERE LOWER("solarSystemName") LIKE ? OR LOWER("solarSystemName") LIKE ?
LIMIT 6
''', (split[0], split[1]))
result = curs.fetchall()
if len(result) < 2:
cmd.reply('error: one or both systems not found')
return
elif len(result) > 2:
cmd.reply('error: found too many systems: ' + ' '.join(map(operator.itemgetter(0), result)))
return
dist = 0
for d1, d2 in zip(result[0][1:], result[1][1:]):
dist += (d1 - d2)**2
dist = sqrt(dist) / 9.4605284e15 # meters to lightyears
ship_ranges = [
('other:\t ', 3.5),
('blops:\t ', 4.0),
('JF:\t\t', 5.0),
('super:\t ', 3.0),
]
jdc = []
for ship, jump_range in ship_ranges:
for level in range(6):
if dist <= jump_range * (1 + level * 0.2):
jdc.append('%s%d' % (ship, level))
break
else:
jdc.append(ship + 'N/A')
cmd.reply('```%s ⟷ %s: %.3f ly\n%s```' %
(result[0][0], result[1][0], dist, '\n'.join(jdc)))
def who(cmd):
if len(cmd.args) == 0:
cmd.reply('usage: !who [name]')
return
char_info, corp_info, alliance_info = None, None, None
entity_type_map = {
0: 'characterID',
1: 'corporationID',
2: 'allianceID',
}
def get_char_info(char_id):
r = rs.get('https://esi.evetech.net/latest/characters/{}/'.format(char_id),
params={'datasource': 'tranquility'})
r.raise_for_status()
char_info = r.json()
killed, lost = get_zkill_stats(char_id, 0)
return char_info, killed, lost
def get_corp_info(corp_id):
r = rs.get('https://esi.evetech.net/latest/corporations/{}/'.format(corp_id),
params={'datasource': 'tranquility'})
r.raise_for_status()
corp_info = r.json()
active_members = get_group_actives(corp_id, 1)
return corp_info, active_members
def get_alliance_info(alliance_id):
r = rs.get('https://esi.evetech.net/latest/alliances/{}/'.format(alliance_id),
params={'datasource': 'tranquility'})
r.raise_for_status()
alliance_info = r.json()
active_members = get_group_actives(alliance_id, 2)
return alliance_info, active_members
def get_zkill_stats(entity_id, entity_type):
r = rs.get('https://zkillboard.com/api/stats/{entity_type}/{entity_id}/'.format(
entity_id=entity_id, entity_type=entity_type_map[entity_type]))
r.raise_for_status()
stats = r.json()
killed = stats.get('shipsDestroyed', 0)
lost = stats.get('shipsLost', 0)
return killed, lost
def get_group_actives(entity_id, entity_type):
r = rs.get('https://zkillboard.com/api/stats/{entity_type}/{entity_id}/'.format(
entity_type=entity_type_map[entity_type], entity_id=entity_id))
r.raise_for_status()
data = r.json()
try:
return data['activepvp']['characters']['count']
except KeyError:
return 0
try:
r = rs.post('https://esi.evetech.net/latest/universe/ids/',
params={'datasource': 'tranquility', 'language': 'en-us'},
json=[cmd.args])
r.raise_for_status()
except requests.exceptions.HTTPError:
cmd.reply('{}: esi error'.format(cmd.sender['pretty_name']))
return
initial_id = r.json()
if len(initial_id) == 0:
cmd.reply("%s: couldn't find your sleazebag" % cmd.sender['pretty_name'])
return
corp_id = alliance_id = None
output = ''
if 'characters' in initial_id:
try:
char_info, killed, lost = get_char_info(initial_id['characters'][0]['id'])
except requests.exceptions.HTTPError:
cmd.reply("%s: couldn't find your sleazebag" % cmd.sender['pretty_name'])
corp_id = char_info['corporation_id']
output += '{name} ({security:.2f}) [{killed}/{lost}]\n'.format(
name=char_info['name'], security=char_info['security_status'], killed=killed, lost=lost)
try:
if 'corporations' in initial_id or char_info:
if not char_info:
corp_id = initial_id['corporations'][0]['id']
corp_info, active_members = get_corp_info(corp_id)
alliance_id = corp_info.get('alliance_id')
output += '{name} [{ticker}] {active} active members\n'.format(
name=corp_info['name'], ticker=corp_info['ticker'], active=active_members)
if 'alliances' in initial_id or alliance_id is not None:
if alliance_id is None:
alliance_id = initial_id['alliances'][0]['id']
alliance_info, active_members = get_alliance_info(alliance_id)
output += '{name} <{ticker}> {active} active members'.format(
name=alliance_info['name'], ticker=alliance_info['ticker'], active=active_members)
except requests.exceptions.HTTPError:
output += 'esi or zkb error looking up corporation/alliance'
cmd.reply('```' +output + '```')