Skip to content

Commit

Permalink
clean up trade logic to reduce the number of unnecessary requests and…
Browse files Browse the repository at this point in the history
… consolidate api calls... update credit display more frequently
  • Loading branch information
hunterjm committed Nov 25, 2015
1 parent 6512269 commit 4e7cd21
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 55 deletions.
113 changes: 65 additions & 48 deletions core/bid.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ def roundBid(bid):
return int(increment(bid) * round(float(bid)/increment(bid)))


def bid(q, api, playerList, settings, trades={}):
def bid(q, api, playerList, settings):
pileFull = False
auctionsWon = 0
bidDetails = {}
trades = {}

api.resetSession()

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

for item in api.watchlist():
trades[item['tradeId']] = item['resourceId']

# Grab all items from tradepile
tradepile = api.tradepile()

for defId in bidDetails.keys():

if bidDetails[defId]['maxBid'] < 100:
continue

try:
# Grab all items from tradepile
tradepile = api.tradepile()

# How many of this item do we already have listed?
listed = sum([str(api.baseId(item['resourceId'])) == defId for item in tradepile])

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

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

q.put((card, EventType.BIN))
q.put((card, EventType.BIN, api.credits))
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']))
trades[item['tradeId']] = item['resourceId']
binWon = True
listed += 1
else:
q.put('%s Bid Error: You are not allowed to bid on this trade\n' % (time.strftime('%Y-%m-%d %H:%M:%S')))
Expand Down Expand Up @@ -107,25 +115,26 @@ def bid(q, api, playerList, settings, trades={}):
card = PlayerCard(item, displayName)

card.currentBid = bid
q.put((card, EventType.NEWBID))
q.put((card, EventType.NEWBID, api.credits))
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']))
trades[item['tradeId']] = item['resourceId']
bidon += 1
else:
q.put('%s Bid Error: You are not allowed to bid on this trade\n' % (time.strftime('%Y-%m-%d %H:%M:%S')))

if not settings['snipeOnly']:
if not settings['snipeOnly'] and trades:
# Update watched items
q.put('%s Updating watched items...\n' % (time.strftime('%Y-%m-%d %H:%M:%S')))
for item in api.watchlist():
for item in api.tradeStatus([tradeId for tradeId in trades]):
item['resourceId'] = trades[item['tradeId']]
baseId = str(api.baseId(item['resourceId']))
if baseId not in bidDetails:
continue
maxBid = bidDetails[baseId]['maxBid']
sell = bidDetails[baseId]['sell']
binPrice = bidDetails[baseId]['binPrice']
# How many of this item do we already have listed?
listed = sum([str(api.baseId(item['resourceId'])) == baseId for item in tradepile])
listed = sum([str(api.baseId(trade['resourceId'])) == baseId for trade in tradepile])

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

# We won! Send to Pile!
q.put((card, EventType.BIDWON))
q.put((card, EventType.BIDWON, api.credits))
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']))
if api.sendToTradepile(tradeId, item['id'], safe=True):
# List on market
if api.sell(item['id'], sell, binPrice):
auctionsWon += 1
listed += 1
# No need to keep track of expired bids
del trades[tradeId]
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))
pileFull = False

else:
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']))
pileFull = True

else:

if api.watchlistDelete(tradeId):

if item['currentBid'] < maxBid:
q.put((card, EventType.LOST))
q.put((card, EventType.LOST, api.credits))
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']))
else:
q.put((card, EventType.LOST))
q.put((card, EventType.LOST, api.credits))
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']))

# No need to keep track of expired bids
del trades[tradeId]
# No need to keep track of expired bids
del trades[tradeId]

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

Expand All @@ -182,51 +194,52 @@ def bid(q, api, playerList, settings, trades={}):
newBid = item['currentBid'] + increment(item['currentBid'])
if newBid > maxBid:
if api.watchlistDelete(tradeId):
q.put((card, EventType.OUTBID))
q.put((card, EventType.OUTBID, api.credits))
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']))
del trades[tradeId]

else:
if api.bid(tradeId, newBid):
card.currentBid = newBid
q.put((card, EventType.BIDWAR))
q.put((card, EventType.BIDWAR, api.credits))
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']))
else:
q.put('%s Bid Error: You are not allowed to bid on this trade\n' % (time.strftime('%Y-%m-%d %H:%M:%S')))

else:
q.put((card, EventType.UPDATE))
q.put((card, EventType.UPDATE, api.credits))

# buy now goes directly to unassigned now
for item in api.unassigned():
baseId = str(api.baseId(item['resourceId']))
if baseId not in bidDetails:
continue
maxBid = bidDetails[baseId]['maxBid']
sell = bidDetails[baseId]['sell']
binPrice = bidDetails[baseId]['binPrice']

tradeId = item['tradeId'] if item['tradeId'] is not None else -1
asset = api.cardInfo(item['resourceId'])
displayName = asset['Item']['CommonName'] if asset['Item']['CommonName'] else asset['Item']['LastName']
card = PlayerCard(item, displayName)

# We won! Send to Pile!
q.put((card, EventType.BIDWON))
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']))
if api.sendToTradepile(tradeId, item['id'], safe=True):
# List on market
if api.sell(item['id'], sell, binPrice):
auctionsWon += 1
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))
pileFull = False

else:
pileFull = True

# No need to keep track of expired bids
if tradeId > 0:
del trades[tradeId]
if binWon:
for item in api.unassigned():
baseId = str(api.baseId(item['resourceId']))
if baseId not in bidDetails:
continue
maxBid = bidDetails[baseId]['maxBid']
sell = bidDetails[baseId]['sell']
binPrice = bidDetails[baseId]['binPrice']

tradeId = item['tradeId'] if item['tradeId'] is not None else -1
asset = api.cardInfo(item['resourceId'])
displayName = asset['Item']['CommonName'] if asset['Item']['CommonName'] else asset['Item']['LastName']
card = PlayerCard(item, displayName)

# We won! Send to Pile!
q.put((card, EventType.BIDWON, api.credits))
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']))
if api.sendToTradepile(tradeId, item['id'], safe=True):
# List on market
if api.sell(item['id'], sell, binPrice):
auctionsWon += 1
# No need to keep track of expired bids
if tradeId > 0:
del trades[tradeId]
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))
pileFull = False

else:
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']))
pileFull = True

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

q.put((auctionsWon, sold))
auctionsWon = 0
q.put((auctionsWon, sold, api.credits))

# re-sync tradepile if we won something
if auctionsWon or sold:
tradepile = api.tradepile()
auctionsWon = 0

except (FutError, RequestException) as e:
q.put(e)
Expand Down
11 changes: 4 additions & 7 deletions frames/bid.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(self, master, controller):
auctions = tk.Frame(self)

self.auctionStatus = Auctions(auctions)
self.auctionStatus.get_view().grid(column=0, row=0, sticky='nsew', rowspan=2)
self.auctionStatus.get_view().grid(column=0, row=0, sticky='nsew')

self.logView = tk.Text(auctions, bg='#1d93ab', fg='#ffeb7e', bd=0)
self.logView.grid(column=0, row=1, sticky='nsew')
Expand Down Expand Up @@ -179,18 +179,13 @@ def bid(self):
if self.settings['autoUpdate'] and time.time() - self._lastUpdate > 3600:
self.updatePrice()
return
# Populate trades with what I am already watching
trades = {}
try:
for item in self.controller.api.watchlist():
trades[item['tradeId']] = item['resourceId']
self._bidCycle += 1
self.p = mp.Process(target=bid, args=(
self.q,
self.controller.api,
self.args['playerList'],
self.settings,
trades
self.settings
))
self.p.start()
self.controller.status.set_credits(self.controller.api.credits)
Expand Down Expand Up @@ -328,10 +323,12 @@ def checkQueue(self):
self.auctionStatus.update_status(msg[0], time.strftime('%Y-%m-%d %H:%M:%S'), msg[0].currentBid, tag='won')
elif msg[1] == EventType.UPDATE:
self.auctionStatus.update_status(msg[0], time.strftime('%Y-%m-%d %H:%M:%S'), msg[0].currentBid)
self.controller.status.set_credits(msg[2])
else:
# Auction Results
self.auctionsWon += msg[0]
self.sold += msg[1]
self.controller.status.set_credits(msg[2])
self.controller.status.set_stats((self.auctionsWon, self.sold))
self.controller.status.set_status('Bidding Cycle: %d' % (self._bidCycle))
if time.time() - self._startTime > 18000:
Expand Down

0 comments on commit 4e7cd21

Please sign in to comment.