This repository has been archived by the owner on Jul 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminerals_calculator.py
277 lines (248 loc) · 10.8 KB
/
minerals_calculator.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
"""usage:
minerals_calculator.py
minerals_calculator.py <location> <net_refine_yield> <refinery_tax> [--file <assets>]
minerals_calculator.py --file <assets>
--file: indicate the location on disk of a file containing your assets, copied from the EVE client."""
from __future__ import division
import evelink.api
import evelink.eve
import evelink.char
import evecentral
import sys
from database import database
from docopt import docopt
KEY_ERROR = -1
class minerals_calculator(object):
def __init__(self):
try:
charinfo = open('char_info.txt')
keyid, vcode, charname = charinfo.read().split(',')
keyid = int(keyid)
except IOError:
keyid, vcode, charname = self.get_charinfo()
self.get_character(charname,(keyid,vcode))
self.database = database()
self.get_tables()
def get_character(self, charname, apikey):
self.api = evelink.api.API(api_key=apikey)
self.eve = evelink.eve.EVE()
charid = self.eve.character_id_from_name(charname)
self.char = evelink.char.Char(char_id = charid, api= self.api)
self.assets = self.char.assets()
def get_charinfo(self):
keyid = int(raw_input('What is your api key id? \n'))
vcode = raw_input('What is your api vcode? \n')
charname = raw_input('What is your character name? \n')
out = file('char_info.txt','w')
out.write(str(keyid) + ',' + vcode + ',' + charname)
out.close()
return [keyid,vcode,charname]
def get_tables(self):
self.nullsec_stations = self.eve.conquerable_stations()
inserts = []
for key in self.nullsec_stations:
temp = self.nullsec_stations[key]
inserts.append((temp['id'],temp['system_id'],self.get_value('region_id','solar_systems','system_id',temp['system_id'])[0],temp['name']))
self.database.add_temp_values('stations','(?,?,?,?)',inserts)
print('loading complete.')
def get_charinfo(self):
keyid = int(raw_input('What is your api key id? \n'))
vcode = raw_input('What is your api vcode? \n')
charname = raw_input('What is your character name? \n')
out = file('char_info.txt','w')
out.write(str(keyid) + ',' + vcode + ',' + charname)
out.close()
return [keyid,vcode,charname]
def get_value(self,field,table,key,value):
result = self.database.query('select '+field+' from '+table+' where '+key+'=?',(value,))
if len(result) == 0:
return result
#database returns a tuple. If the tuple has only one element, flatten it.
elif len(result[0]) == 1:
result = [x[0] for x in result]
return result
def get_assets_at_station(self,name):
sysid = self.get_value('system_id','solar_systems','system_name',name)[0]
staids = self.get_value('id','stations','system_id',sysid)
system_assets = [] #list of assets at stations.
asset_ids = [] #ids of stations with assets at them
for station in staids:
try:
system_assets.append(self.assets[station]['contents'])
asset_ids.append(station)
except KeyError: #KeyError means no assets were found at that station.
pass
if len(system_assets) == 0:
print('you appear not to have anything there!')
return(KEY_ERROR)
elif len(system_assets) == 1:
return(system_assets[0])
else:
i = 0
for sta in asset_ids:
print('[' + str(i) + '] \t' + self.get_value('name','stations','id',sta)[0])
i = i+1
result = int(raw_input('Which station did you mean?'))
return(system_assets[result])
def offer_containers(self,assets):
i = 1
has_containsers = False
containers = {}
for elem in assets:
try:
contents = elem['contents']
containers[i] = elem
has_containers = True
except KeyError:
pass
if has_containers == False:
return(None)
return(containers)
def get_refine_list(self,location):
refine_assets = self.get_assets_at_station(location)
if(refine_assets == KEY_ERROR): return KEY_ERROR
containers = self.offer_containers(refine_assets)
if(containers != None):
print('Here are the available containers:')
print('[0]\tHangar')
for key in containers:
print('['+str(key)+']' + '\t' + self.get_value('type_name','inv_types','type_id',containers[key]['item_type_id'])[0])
selection = int(raw_input('Which container would you like to look in? \n'))
if(selection != 0):
refine_assets = containers[selection]['contents']
return(refine_assets)
def get_file_refine_list(self,assets_file):
refine_assets = parse_assets(assets_file)
return(refine_assets)
def get_prices(self,system):
''' reads in prices from a text file. TODO: replace w/eve-central'''
minerals = ['Isogen','Megacyte','Mexallon','Nocxium','Pyerite','Tritanium','Zydrine']
pricesraw = []
for item in minerals:
typeid = self.get_value('type_id','inv_types','type_name',item)[0]
pricesraw.append((typeid,evecentral.find_sys_price(typeid,system)))
prices = dict(pricesraw)
return(prices)
def print_refine(self,refine_assets,region):
res = []
total = 0.0;
for item in refine_assets:
itemid = item['item_type_id']
repro = self.get_value('material_id,quantity','item_materials','type_id',itemid)
if len(repro) > 0:
refine_price = addm(repro,prices,refinery*0.01,standings*0.01)*item['quantity']/self.get_value('portion_size','inv_types','type_id',itemid)[0]
else:
refine_price = 0
sell_price = evecentral.find_best_price(item['item_type_id'],region)*item['quantity']
if(refine_price > sell_price):
verdict = 'refine'
buy_price = evecentral.find_sys_sell(itemid,system)
delta = ''
if refine_price*(1-0.97-1.5)/item['quantity'] > buy_price*(1-0.97):
delta = 'Arbitrage'
total = total + refine_price*(1-0.97-1.5)
else:
verdict = 'sell'
delta = ''
total = total + sell_price*(1-0.97-1.5)
res.append([self.get_value('type_name','inv_types','type_id',itemid)[0],verdict,str(refine_price/item[1]),delta])
res.sort()
colsize = biggest_name(res)
pattern = '{0:'+str(colsize+3)+'s} {1:6s} {2:10s} {3:1s}'
for item in res:
print(pattern.format(item[0],item[1],item[2],item[3]))
print("-------------- \n Total: "+str(total))
def print_file_refine(self,refine_assets,region):
res = []
excl = []
total = 0.0;
for item in refine_assets:
itemid = self.get_value('type_id','inv_types','type_name',item[0])
if(itemid != []):
itemid = itemid[0]
repro = self.get_value('material_id,quantity','item_materials','type_id',itemid)
if len(repro) > 0:
refine_price = addm(repro,prices,refinery*0.01,standings*0.01)/self.get_value('portion_size','inv_types','type_id',itemid)[0]
else:
refine_price = 0
sell_price = evecentral.find_best_price(itemid,region)
finalprice = 0
if(refine_price > sell_price):
verdict = "refine"
finalprice = refine_price
buy_price = evecentral.find_sys_sell(itemid,system)
delta = ''
if refine_price*(1-0.97-1.5) > buy_price*(1-0.97):
delta = 'Arbitrage'
total = total + refine_price*item[1]
else:
verdict = "sell"
finalprice = sell_price
delta = ''
total = total + sell_price*item[1]
res.append([self.get_value('type_name','inv_types','type_id',itemid)[0],verdict,str(finalprice),str(item[1])])
else:
excl.append("Excluded "+item[0])
res.sort()
colsize = biggest_name(res)
pattern = '{0:'+str(colsize+3)+'s} {1:6s} {2:10s} {3:5s}'
for item in res:
print(pattern.format(item[0],item[1],item[2],item[3]))
print("-------------- \n Total: "+str(total))
for item in excl:
print(item)
def addm(data,prices,refine,tax):
''' takes a list of tuples of (minerals,amount) and adds them, taking into account tax etc '''
res = 0
for item in data:
try:
res = res + prices[item[0]]*int(item[1]*refine*(1-tax))
except KeyError: #this error for items which refine into t2 stuff
return 0
return(res)
def biggest_name(results):
biggest = 0
for item in results:
if biggest < len(item[0]):
biggest = len(item[0])
return biggest
def parse_assets(path_to_file):
f = open(path_to_file)
assets = [[y[0],get_qty(y[1])] for y in [x.split('\t') for x in f.readlines()]]
return assets
def get_qty(ins):
if ins == '':
qty = 1
else:
ins = ins.split(',')
qty = ''
for item in ins:
qty = qty + item
qty = int(qty)
return qty
if __name__ == '__main__':
arguments = docopt(__doc__, argv=sys.argv[1:])
calc = minerals_calculator()
if arguments['<location>'] == False:
location = raw_input('Where are you located?\n')
refinery = float(raw_input('What is your net refining yield in percent? \n'))
standings = float(raw_input('And what is the refinery tax in percent? \n'))
else:
location = arguments['<location>']
refinery = arguments['<net_refine_yield>']
standings = arguments['<refinery_tax>']
if(type(refinery) == str and type(standings) == str):
refinery = float(refinery)
standings = float(standings)
system = calc.get_value('system_id','solar_systems','system_name',location)[0]
region = calc.get_value('region_id','solar_systems','system_name',location)[0]
prices = calc.get_prices(system)
if arguments['--file'] == False:
refine_assets = calc.get_refine_list(location)
if(refine_assets != KEY_ERROR):
calc.print_refine(refine_assets,region)
else:
assetfile = arguments['<assets>']
refine_assets = calc.get_file_refine_list(assetfile)
if(refine_assets != KEY_ERROR):
calc.print_file_refine(refine_assets,region)