Skip to content

Commit 4e7cd21

Browse files
committed
clean up trade logic to reduce the number of unnecessary requests and consolidate api calls... update credit display more frequently
1 parent 6512269 commit 4e7cd21

File tree

2 files changed

+69
-55
lines changed

2 files changed

+69
-55
lines changed

core/bid.py

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ def roundBid(bid):
2121
return int(increment(bid) * round(float(bid)/increment(bid)))
2222

2323

24-
def bid(q, api, playerList, settings, trades={}):
24+
def bid(q, api, playerList, settings):
2525
pileFull = False
2626
auctionsWon = 0
2727
bidDetails = {}
28+
trades = {}
2829

2930
api.resetSession()
3031

@@ -35,18 +36,24 @@ def bid(q, api, playerList, settings, trades={}):
3536
'binPrice': item['bin']
3637
}
3738

39+
for item in api.watchlist():
40+
trades[item['tradeId']] = item['resourceId']
41+
42+
# Grab all items from tradepile
43+
tradepile = api.tradepile()
44+
3845
for defId in bidDetails.keys():
3946

4047
if bidDetails[defId]['maxBid'] < 100:
4148
continue
4249

4350
try:
44-
# Grab all items from tradepile
45-
tradepile = api.tradepile()
51+
4652
# How many of this item do we already have listed?
4753
listed = sum([str(api.baseId(item['resourceId'])) == defId for item in tradepile])
4854

4955
# Only bid if we don't already have a full trade pile and don't own too many of this player
56+
binWon = False
5057
if not pileFull and api.credits > settings['minCredits'] and listed < settings['maxPlayer']:
5158

5259
# Look for any BIN less than the BIN price
@@ -69,9 +76,10 @@ def bid(q, api, playerList, settings, trades={}):
6976
displayName = asset['Item']['CommonName'] if asset['Item']['CommonName'] else asset['Item']['LastName']
7077
card = PlayerCard(item, displayName)
7178

72-
q.put((card, EventType.BIN))
79+
q.put((card, EventType.BIN, api.credits))
7380
q.put('%s Card Purchased: BIN %d on %s %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), item['buyNowPrice'], asset['Item']['FirstName'], asset['Item']['LastName']))
7481
trades[item['tradeId']] = item['resourceId']
82+
binWon = True
7583
listed += 1
7684
else:
7785
q.put('%s Bid Error: You are not allowed to bid on this trade\n' % (time.strftime('%Y-%m-%d %H:%M:%S')))
@@ -107,25 +115,26 @@ def bid(q, api, playerList, settings, trades={}):
107115
card = PlayerCard(item, displayName)
108116

109117
card.currentBid = bid
110-
q.put((card, EventType.NEWBID))
118+
q.put((card, EventType.NEWBID, api.credits))
111119
q.put('%s New Bid: %d on %s %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), bid, asset['Item']['FirstName'], asset['Item']['LastName']))
112120
trades[item['tradeId']] = item['resourceId']
113121
bidon += 1
114122
else:
115123
q.put('%s Bid Error: You are not allowed to bid on this trade\n' % (time.strftime('%Y-%m-%d %H:%M:%S')))
116124

117-
if not settings['snipeOnly']:
125+
if not settings['snipeOnly'] and trades:
118126
# Update watched items
119127
q.put('%s Updating watched items...\n' % (time.strftime('%Y-%m-%d %H:%M:%S')))
120-
for item in api.watchlist():
128+
for item in api.tradeStatus([tradeId for tradeId in trades]):
129+
item['resourceId'] = trades[item['tradeId']]
121130
baseId = str(api.baseId(item['resourceId']))
122131
if baseId not in bidDetails:
123132
continue
124133
maxBid = bidDetails[baseId]['maxBid']
125134
sell = bidDetails[baseId]['sell']
126135
binPrice = bidDetails[baseId]['binPrice']
127136
# How many of this item do we already have listed?
128-
listed = sum([str(api.baseId(item['resourceId'])) == baseId for item in tradepile])
137+
listed = sum([str(api.baseId(trade['resourceId'])) == baseId for trade in tradepile])
129138

130139
# Break if we don't have enough credits
131140
if api.credits < settings['minCredits']:
@@ -145,32 +154,35 @@ def bid(q, api, playerList, settings, trades={}):
145154
if (item['bidState'] == 'highest' or (item['tradeState'] == 'closed' and item['bidState'] == 'buyNow')):
146155

147156
# We won! Send to Pile!
148-
q.put((card, EventType.BIDWON))
157+
q.put((card, EventType.BIDWON, api.credits))
149158
q.put('%s Auction Won: %d on %s %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), item['currentBid'], asset['Item']['FirstName'], asset['Item']['LastName']))
150159
if api.sendToTradepile(tradeId, item['id'], safe=True):
151160
# List on market
152161
if api.sell(item['id'], sell, binPrice):
153162
auctionsWon += 1
154163
listed += 1
164+
# No need to keep track of expired bids
165+
del trades[tradeId]
155166
q.put('%s Item Listed: %s %s for %d (%d BIN)\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), asset['Item']['FirstName'], asset['Item']['LastName'], sell, binPrice))
156167
pileFull = False
157168

158169
else:
170+
q.put('%s Error: %s %s could not be placed in the tradepile...\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), asset['Item']['FirstName'], asset['Item']['LastName']))
159171
pileFull = True
160172

161173
else:
162174

163175
if api.watchlistDelete(tradeId):
164176

165177
if item['currentBid'] < maxBid:
166-
q.put((card, EventType.LOST))
178+
q.put((card, EventType.LOST, api.credits))
167179
q.put('%s TOO SLOW: %s %s went for %d\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), asset['Item']['FirstName'], asset['Item']['LastName'], item['currentBid']))
168180
else:
169-
q.put((card, EventType.LOST))
181+
q.put((card, EventType.LOST, api.credits))
170182
q.put('%s Auction Lost: %s %s went for %d\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), asset['Item']['FirstName'], asset['Item']['LastName'], item['currentBid']))
171183

172-
# No need to keep track of expired bids
173-
del trades[tradeId]
184+
# No need to keep track of expired bids
185+
del trades[tradeId]
174186

175187
elif item['bidState'] != 'highest':
176188

@@ -182,51 +194,52 @@ def bid(q, api, playerList, settings, trades={}):
182194
newBid = item['currentBid'] + increment(item['currentBid'])
183195
if newBid > maxBid:
184196
if api.watchlistDelete(tradeId):
185-
q.put((card, EventType.OUTBID))
197+
q.put((card, EventType.OUTBID, api.credits))
186198
q.put('%s Outbid: Won\'t pay %d for %s %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), newBid, asset['Item']['FirstName'], asset['Item']['LastName']))
187199
del trades[tradeId]
188200

189201
else:
190202
if api.bid(tradeId, newBid):
191203
card.currentBid = newBid
192-
q.put((card, EventType.BIDWAR))
204+
q.put((card, EventType.BIDWAR, api.credits))
193205
q.put('%s Bidding War: %d on %s %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), newBid, asset['Item']['FirstName'], asset['Item']['LastName']))
194206
else:
195207
q.put('%s Bid Error: You are not allowed to bid on this trade\n' % (time.strftime('%Y-%m-%d %H:%M:%S')))
196208

197209
else:
198-
q.put((card, EventType.UPDATE))
210+
q.put((card, EventType.UPDATE, api.credits))
199211

200212
# buy now goes directly to unassigned now
201-
for item in api.unassigned():
202-
baseId = str(api.baseId(item['resourceId']))
203-
if baseId not in bidDetails:
204-
continue
205-
maxBid = bidDetails[baseId]['maxBid']
206-
sell = bidDetails[baseId]['sell']
207-
binPrice = bidDetails[baseId]['binPrice']
208-
209-
tradeId = item['tradeId'] if item['tradeId'] is not None else -1
210-
asset = api.cardInfo(item['resourceId'])
211-
displayName = asset['Item']['CommonName'] if asset['Item']['CommonName'] else asset['Item']['LastName']
212-
card = PlayerCard(item, displayName)
213-
214-
# We won! Send to Pile!
215-
q.put((card, EventType.BIDWON))
216-
q.put('%s Auction Won: %d on %s %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), item['lastSalePrice'], asset['Item']['FirstName'], asset['Item']['LastName']))
217-
if api.sendToTradepile(tradeId, item['id'], safe=True):
218-
# List on market
219-
if api.sell(item['id'], sell, binPrice):
220-
auctionsWon += 1
221-
q.put('%s Item Listed: %s %s for %d (%d BIN)\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), asset['Item']['FirstName'], asset['Item']['LastName'], sell, binPrice))
222-
pileFull = False
223-
224-
else:
225-
pileFull = True
226-
227-
# No need to keep track of expired bids
228-
if tradeId > 0:
229-
del trades[tradeId]
213+
if binWon:
214+
for item in api.unassigned():
215+
baseId = str(api.baseId(item['resourceId']))
216+
if baseId not in bidDetails:
217+
continue
218+
maxBid = bidDetails[baseId]['maxBid']
219+
sell = bidDetails[baseId]['sell']
220+
binPrice = bidDetails[baseId]['binPrice']
221+
222+
tradeId = item['tradeId'] if item['tradeId'] is not None else -1
223+
asset = api.cardInfo(item['resourceId'])
224+
displayName = asset['Item']['CommonName'] if asset['Item']['CommonName'] else asset['Item']['LastName']
225+
card = PlayerCard(item, displayName)
226+
227+
# We won! Send to Pile!
228+
q.put((card, EventType.BIDWON, api.credits))
229+
q.put('%s Auction Won: %d on %s %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), item['lastSalePrice'], asset['Item']['FirstName'], asset['Item']['LastName']))
230+
if api.sendToTradepile(tradeId, item['id'], safe=True):
231+
# List on market
232+
if api.sell(item['id'], sell, binPrice):
233+
auctionsWon += 1
234+
# No need to keep track of expired bids
235+
if tradeId > 0:
236+
del trades[tradeId]
237+
q.put('%s Item Listed: %s %s for %d (%d BIN)\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), asset['Item']['FirstName'], asset['Item']['LastName'], sell, binPrice))
238+
pileFull = False
239+
240+
else:
241+
q.put('%s Error: %s %s could not be placed in the tradepile...\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), asset['Item']['FirstName'], asset['Item']['LastName']))
242+
pileFull = True
230243

231244
completedTrades = sum([i['tradeState'] in ('expired', 'closed') for i in tradepile])
232245
sold = 0
@@ -249,7 +262,7 @@ def bid(q, api, playerList, settings, trades={}):
249262
asset = api.cardInfo(item['resourceId'])
250263
displayName = asset['Item']['CommonName'] if asset['Item']['CommonName'] else asset['Item']['LastName']
251264
card = PlayerCard(item, displayName)
252-
q.put((card, EventType.SOLD))
265+
q.put((card, EventType.SOLD, api.credits))
253266
api.tradepileDelete(i['tradeId'])
254267
sold += 1
255268
if i['tradeState'] == 'expired' and sell and binPrice:
@@ -265,8 +278,12 @@ def bid(q, api, playerList, settings, trades={}):
265278
q.put('%s Trade Pile Full! Resume bidding in %d seconds\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), selling[0]['expires']))
266279
time.sleep(selling[0]['expires'])
267280

268-
q.put((auctionsWon, sold))
269-
auctionsWon = 0
281+
q.put((auctionsWon, sold, api.credits))
282+
283+
# re-sync tradepile if we won something
284+
if auctionsWon or sold:
285+
tradepile = api.tradepile()
286+
auctionsWon = 0
270287

271288
except (FutError, RequestException) as e:
272289
q.put(e)

frames/bid.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def __init__(self, master, controller):
8585
auctions = tk.Frame(self)
8686

8787
self.auctionStatus = Auctions(auctions)
88-
self.auctionStatus.get_view().grid(column=0, row=0, sticky='nsew', rowspan=2)
88+
self.auctionStatus.get_view().grid(column=0, row=0, sticky='nsew')
8989

9090
self.logView = tk.Text(auctions, bg='#1d93ab', fg='#ffeb7e', bd=0)
9191
self.logView.grid(column=0, row=1, sticky='nsew')
@@ -179,18 +179,13 @@ def bid(self):
179179
if self.settings['autoUpdate'] and time.time() - self._lastUpdate > 3600:
180180
self.updatePrice()
181181
return
182-
# Populate trades with what I am already watching
183-
trades = {}
184182
try:
185-
for item in self.controller.api.watchlist():
186-
trades[item['tradeId']] = item['resourceId']
187183
self._bidCycle += 1
188184
self.p = mp.Process(target=bid, args=(
189185
self.q,
190186
self.controller.api,
191187
self.args['playerList'],
192-
self.settings,
193-
trades
188+
self.settings
194189
))
195190
self.p.start()
196191
self.controller.status.set_credits(self.controller.api.credits)
@@ -328,10 +323,12 @@ def checkQueue(self):
328323
self.auctionStatus.update_status(msg[0], time.strftime('%Y-%m-%d %H:%M:%S'), msg[0].currentBid, tag='won')
329324
elif msg[1] == EventType.UPDATE:
330325
self.auctionStatus.update_status(msg[0], time.strftime('%Y-%m-%d %H:%M:%S'), msg[0].currentBid)
326+
self.controller.status.set_credits(msg[2])
331327
else:
332328
# Auction Results
333329
self.auctionsWon += msg[0]
334330
self.sold += msg[1]
331+
self.controller.status.set_credits(msg[2])
335332
self.controller.status.set_stats((self.auctionsWon, self.sold))
336333
self.controller.status.set_status('Bidding Cycle: %d' % (self._bidCycle))
337334
if time.time() - self._startTime > 18000:

0 commit comments

Comments
 (0)