-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiidp.py
248 lines (178 loc) · 7.88 KB
/
iidp.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
"""
A Simple bit of output code, just dump some HTML.
"""
import json
def sellProfit(itm):
if itm['value']:
return itm['yieldValue'] - itm['value']
return itm['yieldValue']
def sellString(itm):
template = "<b><span style='color:{}'>{}</span></b>"
if not itm['value']:
return template.format('blue', 'Refine')
if itm['value'] < 0.90 * itm['yieldValue']:
return template.format('blue', 'Refine')
if itm['value'] < itm['yieldValue']:
return template.format('green', 'Refine')
if itm['value'] < 1.10 * itm['yieldValue']:
return template.format('yellow', 'Sell')
else:
return template.format('red', 'Sell')
__document = """
<!DOCTYPE html>
<html>
<body>
<h1>{name}</h1>
<p>{location}</p>
{summary}
{groups}
<div>
<h3>Item Breakdown</h3>
<table border=1>
{headers}
{rows}
</table>
</div>
</body>
</html>
"""
def wrap(s, tag):
return '<' + tag + '>' + str(s) + '</' + tag + '>'
def td(s):
return wrap(s, 'td')
def th(s):
return wrap(s, 'th')
def totalSellValue(container):
return sum(map(lambda itm: itm['value'] * itm['quantity']
if itm['value'] else 0.0, container.contents))
def totalYieldValue(container):
return sum(map(lambda itm: itm['value'] * itm['quantity']
if itm['value'] else 0.0, container.attainableYield))
def buildSummary(can, grouppayouts):
def totalYieldTable():
hrow = "";
trow = "";
prow = "";
totalYield = sorted(can.attainableYield, key=(lambda itm: itm['typeID']))
for itm in totalYield:
hrow += th(itm['name'])
trow += td("{:,.2f}".format(itm['quantity']))
prow += td("{:,.2f}".format(itm['value']))
return ("<table border=1>\n" +
"<tr><th></th>" + hrow + "</tr>\n" +
"<tr><td>Quantity</td>" + trow + "</tr>\n" +
"<tr><td>Valued at</td>" + prow + "</tr>\n" +
"</table>\n")
top = ("<p>" +
"Total Sell Value: {:,.2f} ISK <br>" +
"Total Reprocess Value: {:,.2f} ISK <br>" +
"</p>\n").format(totalSellValue(can),
totalYieldValue(can))
groups = ''.join(map(lambda group: "{group}: {:,.2f} ISK <br>".format(grouppayouts[group], group = group)
if grouppayouts[group] > 0
else '',
grouppayouts))
breakdown = ("<p>" +
"<h4>LBP Payouts by group:</h4>" +
"Refining Yield: {:,.2f} ISK <br>" +
"{groups}"
"</p>\n").format(0.95 * totalYieldValue(can), groups = groups)
payout = ("<p><b>" +
"Total LBP Payout: {:,.2f} ISK <br>" +
"</b></p>").format(int(0.95 * totalYieldValue(can) +
sum([grouppayouts[group] for group in grouppayouts])))
mid = ("<div>" +
"Total Reprocessing Yield: <br>\n" +
totalYieldTable() +
"</div>\n")
return top + breakdown + payout + mid
def buildGroups(grouprates, groupitems):
# Do we actually have any groups to output?
if all([groupitems[group] == [] for group in groupitems]):
return ''
def buildGroup(group):
# No items, no output.
if not groupitems[group]: return ''
__format = "<div><h3>{group}</h3><div>{summary}</div><div>{table}</div>"
__table = "<table border=1>{headers}{rows}</table>"
# Item - Quantity - Unit Sell Value - Total Sell Value
__headers = ("<tr>" +
th("Item") +
th("Quantity") +
th("Unit Sell Value") +
th("Total Sell Value") +
"</tr>")
total = sum(map(lambda itm: itm['quantity'] * itm['value'], groupitems[group]))
summary = ('<p>Total Value: {:,.2f} ISK</p>' +
'<p>LBP Payout: {:,.2f} ISK</p')
summary = summary.format(total, float(grouprates[group]) * total)
__row = "<tr><td>{}</td><td>{}</td><td>{:,.2f}</td><td>{:,.2f}</td></tr>"
__rows = '\n'.join(map(lambda itm: __row.format(itm['name'],
itm['quantity'],
itm['value'],
itm['quantity'] * itm['value']),
sorted(groupitems[group],
key = lambda itm: -itm['quantity'] * itm['value'])))
return __format.format(group = group,
summary = summary,
table = __table.format(headers = __headers,
rows = __rows))
__format = '<div>{groups}</div>'
return __format.format(groups = ''.join(map(buildGroup, groupitems)))
def buildHeaders(can):
header = (th("Group") +
th("Item") +
th("Quantity") +
th("Unit Mineral Value") +
th("Unit Sell Value") +
th("<b>Unit Sell Profit</b>") +
th("Liquidate") +
th("Total Mineral Value") +
th("Total Sell Value"))
return "<tr>" + header + "</tr>"
def buildRows(can, ignored_marketgroups):
def buildRow(itm):
if itm['groupName'] in ignored_marketgroups:
return ""
row = "<td>{group}</td><td>{name}</td><td>{quantity}</td>"
row = row.format(group = itm['groupName'],
name = itm['name'],
quantity = itm['quantity'])
row += td("{:,.2f}".format(itm['yieldValue']))
row += td("{:,.2f}".format(itm['value']) if itm['value'] else str(None))
row += td("<b>{:,.2f}</b>".format(-sellProfit(itm)))
row += td(sellString(itm))
row += td("{:,.2f}".format(itm['quantity'] * itm['yieldValue']))
row += td("{:,.2f}".format(itm['quantity'] * itm['value']) if itm['value'] else str(None))
return "<tr>" + row + "</tr>"
return '\n'.join(map(buildRow, can.contents))
def output(cans, args):
prefix = args['outputprefix'] if args['outputprefix'] else ''
# The JSON roundabout is - well - I prefer not to use eval. This seems like a better hack.
grouprates = json.loads(args['marketgroups'].replace("'", "\""))
groupitems = {}
for can in cans:
location = '[' + can.locationName.split()[0] + ']'
out = None
if args['uniquefiles'] in ['true', 'yes', 'True', 'Yes', '1']:
out = open(prefix + "{}.{}.{}.html".format(location, can.name, can.itemID), 'w')
else:
out = open(prefix + "{}.{}.html".format(location, can.name), 'w')
# We need to collect the items for the special marketgroups.
groupitems = { group:
[itm for itm in can.contents if itm['groupName'] == group]
for group in grouprates.keys() }
grouppayouts = { group:
float(grouprates[group]) *
sum(map(lambda itm:
itm['quantity'] * itm['value'], groupitems[group]))
for group in grouprates.keys() }
# Sort the contents, makes a nicer dump.
can.contents = sorted(can.contents, key=sellProfit)
out.write(__document.format(name = can.name,
location = can.locationName,
summary = buildSummary(can, grouppayouts),
groups = buildGroups(grouprates, groupitems),
headers = buildHeaders(can),
rows = buildRows(can, grouprates.keys())))
out.close()