-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cakes.py
346 lines (268 loc) · 12.7 KB
/
Cakes.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import json
import os
import time
import Utils
from TablePrint import TablePrint
from CakeAuction import CakeAuction
from Responder import Responder
from utils.LogController import LogController
class Cakes:
def __init__(self):
self.ah_last_updated = time.time() - 99999
self.cakes = None
self.cakes = self.extract_cake_auctions_from_json()
self.ah_incorrect_pages = 0
self.ah_pages_total = 0
self.utils = Utils.Utils()
self.logger = LogController().get_logger()
def get_latest_cake_year(self):
latest_year = -1
for key, cake in self.cakes.items():
latest_year = max(cake.year, latest_year)
self.latest_year = latest_year
def extract_cake_auctions_from_json(self):
cake_auction_dict = {}
self.ah_pages_total = 0
self.ah_incorrect_pages = 0
for filename in os.listdir('auction'):
self.ah_pages_total += 1
with open('auction/' + filename, 'rb') as f:
try:
ah_dict = json.load(f)
except:
self.ah_incorrect_pages += 1
self.logger.error(f"Invalid file: {filename}")
continue
if ah_dict['success']:
for auction in ah_dict['auctions']:
if 'New Year Cake' in auction['item_name'].strip() and "bag" not in auction['item_name'] \
.strip().lower():
cake_auction_dict[auction['uuid']] = CakeAuction(auction)
else:
self.ah_incorrect_pages += 1
return cake_auction_dict
def get_current_cake_year(self) -> int:
current_year = -1
for key, cake in self.cakes.items():
current_year = max(current_year, cake.year)
return current_year
async def try_to_update_ah(self, responder: Responder):
if self.ah_last_updated + 120 < time.time():
self.ah_last_updated = time.time()
await responder.append_header("Updating AH")
await self.utils.download_auctions(responder)
self.cakes = self.extract_cake_auctions_from_json()
return True
else:
await responder.append_header("AH updating skipped")
return False
async def incorrect_download_warning(self, responder: Responder):
if self.ah_incorrect_pages != 0:
await responder.append(f"hypixel returned incorrect data, some cakes may be missing\ninvalid pages: {self.ah_incorrect_pages} out of {self.ah_pages_total} total")
elif self.ah_pages_total == 0:
await responder.append("no AH pages - hypixel api is probably down. Try again later")
# TODO: unused?
def rarest_cakes_in_ah(self):
cake_counts = {}
current_year = self.get_current_cake_year()
for year in range(current_year + 1):
cake_counts[year] = 0
for key, cake in self.cakes.items():
cake_counts[cake.year] += 1
print("Year count_in_AH")
for year, count in cake_counts.items():
print(str(year).ljust(4), count)
print("\n")
cake_counts = {k: cake_counts[k] for k in sorted(cake_counts, key=cake_counts.get)} # sort dictionary
print("Year count_in_AH")
for year, count in cake_counts.items():
print(str(year).ljust(4), count)
async def bin_prices_to_var(self, responder: Responder, ignore_name=None):
await self.try_to_update_ah(responder)
if ignore_name is not None:
ignore_name_uuid = self.utils.get_uuid_from_mc_name(ignore_name)
else:
ignore_name_uuid = None
self.bin_cake_years = {}
# cake_counts = {}
for key, cake in self.cakes.items():
if cake.bin:
if ignore_name_uuid is None or cake.auctioneer_uuid != ignore_name_uuid:
if cake.year not in self.bin_cake_years:
self.bin_cake_years[cake.year] = [cake]
else:
self.bin_cake_years[cake.year].append(cake)
self.max_year_found_in_dict = max(self.bin_cake_years, key=int)
self.bin_cheapest_cakes = {}
for year in range(self.max_year_found_in_dict + 1):
self.bin_cheapest_cakes[year] = []
year_cake_list = []
if year in self.bin_cake_years:
# add cakes to temp list
for cake in self.bin_cake_years[year]:
year_cake_list.append((cake.price, cake.id, cake))
# sort cakes by price and get 5 cheapest
year_cake_list_5_cheapest = sorted(year_cake_list)[:5]
self.bin_cheapest_cakes[year] = year_cake_list_5_cheapest
async def analyze_bin_prices(self, responder, ignore_name):
await self.bin_prices_to_var(responder, ignore_name)
ret_str = ""
await self.incorrect_download_warning(responder)
table = TablePrint((5, 5, 30, 8, 20))
ret_str += table.print_row(["Year", "BINs", "5 cheapest bins", "Cheapest auctioneer"])
for year in range(self.max_year_found_in_dict + 1):
if year in self.bin_cake_years:
year_cake_list_5_cheapest_str = ""
for cake_price, cake_id, cake in self.bin_cheapest_cakes[year]:
year_cake_list_5_cheapest_str += str(cake_price) + " "
ret_str += table.print_row([year, str(len(self.bin_cake_years[year])), year_cake_list_5_cheapest_str,
self.utils.get_mc_name_from_uuid(
self.bin_cheapest_cakes[year][0][2].auctioneer_uuid)])
return ret_str
async def analyze_undercuts(self, responder, name):
name_uuid = self.utils.get_uuid_from_mc_name(name)
if name_uuid is None:
return f"Could not get player from name {name}"
ret_str = ""
await self.bin_prices_to_var(responder, name)
name_owned_cakes = {}
name_best_offers = {}
for uuid, cake in self.cakes.items():
if cake.auctioneer_uuid == name_uuid:
if cake.bin:
if cake.year in name_best_offers:
name_owned_cakes[cake.year].append(cake.year)
name_best_offers[cake.year] = max(name_best_offers[cake.year], cake.price)
else:
name_owned_cakes[cake.year] = []
name_owned_cakes[cake.year].append(cake.year)
name_best_offers[cake.year] = cake.price
# for each cake year in ah
found_undercuts = False
for year in range(999):
if year in name_owned_cakes:
name_offer_in_milions = name_best_offers[year]
# loop all cakes in ah
found_undercut = False
for uuid, cake in self.cakes.items():
if cake.bin:
if cake.year == year:
if cake.price < name_offer_in_milions:
if not found_undercuts:
ret_str += f"Undercut results for {name}:\n\n"
found_undercuts = True
if not found_undercut:
ret_str += f"--- {year} - {str(name_offer_in_milions)} ---\n"
ret_str += f"Undercut - {str(cake.price).ljust(5)} - {self.utils.get_mc_name_from_uuid(cake.auctioneer_uuid)}\n"
found_undercut = True
if found_undercut:
ret_str += "\n"
if not found_undercuts:
ret_str += f"No undercuts found for {name}\n"
return ret_str
async def auctions_ending_soon(self, responder: Responder, auctioneer=None, top_bidder=None):
auctioneer_uuid = ""
top_bidder_uuid = ""
if auctioneer is not None:
auctioneer_uuid = self.utils.get_uuid_from_mc_name(auctioneer)
if top_bidder is not None:
top_bidder_uuid = self.utils.get_uuid_from_mc_name(top_bidder)
ret_str = ""
await self.bin_prices_to_var(responder, auctioneer)
await self.incorrect_download_warning(responder)
table = TablePrint((2, 10, 5, 10, 10, 22, 18, 10))
list_not_sorted = []
list_to_print = []
rows_to_print = 500
if top_bidder is not None or top_bidder is not None:
rows_to_print = 1000
for uuid, cake in self.cakes.items():
ends_in = cake.ends_in()
if ends_in < -60:
continue
if (auctioneer is None and top_bidder is None) and cake.bin:
continue
if auctioneer is not None and auctioneer_uuid != cake.auctioneer_uuid:
continue
if top_bidder is not None and top_bidder_uuid != cake.top_bidder_uuid:
continue
list_not_sorted.append((ends_in, cake.year, cake.id, cake))
list_sorted = sorted(list_not_sorted)
counter = 0
for ends_in, cake_year, cake_id, cake in list_sorted:
if counter == rows_to_print:
break
ends_in = cake.ends_in()
auctioneer = self.utils.get_mc_name_from_uuid(cake.auctioneer_uuid)
if cake.top_bidder_uuid:
top_bidder = self.utils.get_mc_name_from_uuid(cake.top_bidder_uuid)
else:
top_bidder = ""
if cake.bin:
time_e = "BIN"
else:
time_e = str(Utils.seconds_to_hours_minutes_seconds(ends_in))
cheapest_bin = "---"
if cake.year in self.bin_cheapest_cakes:
try:
cheapest_bin = self.bin_cheapest_cakes[cake.year][0][2].price
except:
pass
if cheapest_bin == "---" or cake.price <= cheapest_bin:
is_cheapest = '+'
else:
is_cheapest = '-'
list_to_print.append(
[is_cheapest, time_e, cake.year, cake.price, cheapest_bin, str(auctioneer), top_bidder])
counter += 1
if len(list_to_print) == 0:
return "No auctions found"
ret_str += table.print_row([".", "Time", "year", "price", "ch. bin", "auctioneer", "top bidder"])
for row_to_print in list_to_print:
ret_str += table.print_row(row_to_print)
ret_str += "\n"
return ret_str
async def top(self, responder: Responder):
ret_str = ""
await self.bin_prices_to_var(responder)
top_bidders_uuid = {}
top_auctioneers_uuid = {}
for uuid, cake in self.cakes.items():
temp_cake_bidder_uuid = cake.top_bidder_uuid
if temp_cake_bidder_uuid is not None:
if temp_cake_bidder_uuid in top_bidders_uuid:
top_bidders_uuid[temp_cake_bidder_uuid] = top_bidders_uuid[temp_cake_bidder_uuid] + 1
else:
top_bidders_uuid[temp_cake_bidder_uuid] = 1
for uuid, cake in self.cakes.items():
temp_cake_auctioneer_uuid = cake.auctioneer_uuid
if temp_cake_auctioneer_uuid in top_auctioneers_uuid:
top_auctioneers_uuid[temp_cake_auctioneer_uuid] += 1
else:
top_auctioneers_uuid[temp_cake_auctioneer_uuid] = 1
# sort by value
top_bidders_uuid = {k: v for k, v in sorted(top_bidders_uuid.items(), key=lambda item: item[1], reverse=True)}
top_auctioneers_uuid = {k: v for k, v in
sorted(top_auctioneers_uuid.items(), key=lambda item: item[1], reverse=True)}
table = TablePrint((9, 18, 15))
ret_str += "\nMost top bids:\n"
ret_str += table.print_row(["top bids", "name", "online/offline"])
i = 0
for uuid, count in top_bidders_uuid.items():
mc_name = self.utils.get_mc_name_from_uuid(uuid)
online_status = self.utils.is_player_online(mc_name)
ret_str += table.print_row([count, mc_name, online_status])
if i == 15:
break
i += 1
ret_str += "\nTop auctioneers:\n"
ret_str += table.print_row(["auctions", "name", "online/offline"])
i = 0
for uuid, count in top_auctioneers_uuid.items():
mc_name = self.utils.get_mc_name_from_uuid(uuid)
online_status = self.utils.is_player_online(mc_name)
ret_str += table.print_row([count, mc_name, online_status])
if i == 15:
break
i += 1
return f"```{ret_str}```"