📄 purchasesdb.lua
字号:
purchase.cost = numberFromString(purchase.cost); purchase.seller = stringFromNilSafeString(purchase.seller); purchase.isBuyout = booleanFromString(purchase.isBuyout); purchase.buyerId = numberFromString(purchase.buyerId); return purchase;end--------------------------------------------------------------------------------- Gets the purchases table for the specified item. The table contains packed-- records.-------------------------------------------------------------------------------function getPurchasesTableForItem(item, create) local purchasesTable = BeanCounterRealmDB.purchases; if (purchasesTable == nil) then purchasesTable = {}; BeanCounterRealmDB.purchases = purchasesTable; end -- Get the table for the item. Create or delete if appropriate. local purchasesForItemTable = purchasesTable[item]; if (purchasesForItemTable == nil and create) then purchasesForItemTable = {}; purchasesTable[item] = purchasesForItemTable; elseif (purchasesForItemTable and table.getn(purchasesForItemTable) == 0 and not create) then purchasesTable[item] = nil; end return purchasesForItemTable;end--------------------------------------------------------------------------------- Gets the list of puchased items.-------------------------------------------------------------------------------function getPurchasedItems(item) local items = {}; if (BeanCounterRealmDB.purchases) then for item in BeanCounterRealmDB.purchases do local purchasesTable = getPurchasesTableForItem(item); if (purchasesTable and table.getn(purchasesTable) > 0) then table.insert(items, item); end end end return items;end--------------------------------------------------------------------------------- Gets the purchases (unpacked) for the specified item-------------------------------------------------------------------------------function getPurchasesForItem(item, filterFunc) local purchases = {}; local purchasesTable = getPurchasesTableForItem(item); if (purchasesTable) then for index in purchasesTable do local purchase = unpackPurchase(purchasesTable[index]); purchase.index = index; if (filterFunc == nil or filterFunc(purchase)) then table.insert(purchases, purchase); end end end return purchases;end--------------------------------------------------------------------------------- Prints the purchases.-------------------------------------------------------------------------------function printPurchases() chatPrint("Purchases:"); if (BeanCounterRealmDB.purchases) then for item in BeanCounterRealmDB.purchases do local purchasesTable = BeanCounterRealmDB.purchases[item]; for index = 1, table.getn(purchasesTable) do local purchase = unpackPurchase(purchasesTable[index]); printPurchase(chatPrint, nil, item, purchase); end end endend--------------------------------------------------------------------------------- Calls the specified print function for the completed bid.-------------------------------------------------------------------------------function printPurchase(printFunc, prefix, item, purchase) if (prefix == nil) then prefix = ""; end printFunc( prefix.. date("%c", purchase.time)..", ".. item..", ".. purchase.quantity..", ".. purchase.cost..", ".. nilSafeStringFromString(purchase.seller)..", ".. stringFromBoolean(purchase.isBuyout)..", ".. nilSafeStringFromString(getPlayerName(purchase.buyerId)));end--=============================================================================-- Bid reconcilation functions--=============================================================================--------------------------------------------------------------------------------- Reconcile all bids that should be completed by the specified time.-------------------------------------------------------------------------------function reconcileBids(reconcileTime, excludeItems) local totalReconciled = 0; local totalDiscarded = 0; local items = getPendingBidItems(); for index in items do local item = items[index]; if (excludeItems == nil or excludeItems[item] == nil) then local reconciled, discarded = reconcileBidsByTime(item, reconcileTime); totalReconciled = totalReconciled + reconciled; totalDiscarded = totalDiscarded + discarded; end end return totalReconciled, totalDiscarded;end--------------------------------------------------------------------------------- Reconcile bids for the item that should be completed by the specified-- time.-------------------------------------------------------------------------------function reconcileBidsByTime(item, reconcileTime) debugPrint("reconcileBidsByTime("..item..", "..date("%c", reconcileTime)..")"); -- Get the list of pending bids that should have completed before the -- specified time. local pendingBids = getPendingBidsForItem( item, function(pendingBid) return (pendingBid.buyerId == getCurrentPlayerId() and pendingBid.time + MAXIMUM_TIME_LEFT[pendingBid.timeLeft] + AUCTION_OVERRUN_LIMIT < reconcileTime); end); debugPrint(table.getn(pendingBids).." matching pending bids"); -- Get the list of completed bids that completed before the specified -- time. local completedBids = getCompletedBidsForItem( item, function(completedBid) return (completedBid.buyerId == getCurrentPlayerId() and completedBid.time < reconcileTime); end); debugPrint(table.getn(completedBids).." matching completed bids"); -- Reconcile the lists. local reconciledBids = reconcileBidList(item, pendingBids, completedBids, true); -- Cleanup the unmatch pending bids that are too old to match any -- completed bids. local discardedPendingBids = 0; for index = table.getn(pendingBids), 1, -1 do local pendingBid = pendingBids[index]; deletePendingBidByIndex(item, pendingBid.index, "no match", pendingBids); discardedPendingBids = discardedPendingBids + 1; end -- Cleanup the unmatched completed bids that are too old to match any -- pending bids. local discardedCompletedBids = 0; for index = table.getn(completedBids), 1, -1 do local completedBid = completedBids[index]; if (completedBid.time + AUCTION_DURATION_LIMIT < reconcileTime) then deleteCompletedBidByIndex(item, completedBid.index, "no match", completedBids); discardedCompletedBids = discardedCompletedBids + 1; end end return reconciledBids, discardedPendingBids, discardedCompletedBids;end--------------------------------------------------------------------------------- Attempts to reconcile bids by either quantity or bid amount.-------------------------------------------------------------------------------function reconcileBidsByQuantityOrBids(item, quantityHint, bidHint) -- If a hint is supplied, try them first. If no hints are supplied or the -- hints cause bids to be reconciled, then widen the reconcilation -- hunt. if ((bidHint == nil and quantityHint == nil) or (bidHint ~= nil and reconcileBidsByBid(item, bidHint) > 0) or (quantityHint ~= nil and reconcileBidsByQuantity(item, quantityHint) > 0)) then local index = 1; local quantitiesAttempted = {}; local bidsAttempted = {}; local completedBids = getCompletedBidsTableForItem(item); if (completedBids) then while (index <= table.getn(completedBids)) do local completedBid = unpackCompletedBid(completedBids[index]); if (completedBid.quantity and not quantitiesAttempted[completedBid.quantity]) then quantitiesAttempted[completedBid.quantity] = true; if (reconcileBidsByQuantity(item, completedBid.quantity) > 0) then index = 1; bidsAttempted = {}; else index = index + 1; end elseif (completedBid.bid and not bidsAttempted[completedBid.bid]) then bidsAttempted[completedBid.bid] = true; if (reconcileBidsByBid(item, completedBid.bid) > 0) then index = 1; quantitiesAttempted = {}; else index = index + 1; end else index = index + 1; end end end endend--------------------------------------------------------------------------------- Attempts to reconcile bids by quantity-------------------------------------------------------------------------------function reconcileBidsByQuantity(item, quantity) debugPrint("reconcileBidsByQuantity("..item..", "..quantity..")"); -- Get all the pending bids matching the quantity local pendingBids = getPendingBidsForItem( item, function(pendingBid) return (pendingBid.buyerId == getCurrentPlayerId() and pendingBid.quantity == quantity); end); debugPrint(table.getn(pendingBids).." matching pending bids"); -- Get all the completed bids matching the quantity local completedBids = getCompletedBidsForItem( item, function(completedBid) return (completedBid.buyerId == getCurrentPlayerId() and completedBid.quantity == quantity); end); debugPrint(table.getn(completedBids).." matching completed bids"); -- Attempt to reconcile the lists. if (table.getn(pendingBids) == table.getn(completedBids)) then return reconcileBidList(item, pendingBids, completedBids, false); elseif (doesPendingBidListMatch(pendingBids)) then return reconcileBidList(item, pendingBids, completedBids, false); else debugPrint("Cannot reconcile by quantity"); end return 0;end--------------------------------------------------------------------------------- Attempts to reconcile bids by bid-------------------------------------------------------------------------------function reconcileBidsByBid(item, bid) debugPrint("reconcileBidsByBid("..item..", "..bid..")"); -- Get all the pending bids matching the quantity local pendingBids = getPendingBidsForItem( item, function(pendingBid) return (pendingBid.buyerId == getCurrentPlayerId() and pendingBid.bid == bid); end); debugPrint(table.getn(pendingBids).." matching pending bids"); -- Get all the completed bids matching the quantity local completedBids = getCompletedBidsForItem( item, function(completedBid) return (completedBid.buyerId == getCurrentPlayerId() and completedBid.bid == bid); end); debugPrint(table.getn(completedBids).." matching completed bids"); -- Attempt to reconcile the lists. if (table.getn(pendingBids) == table.getn(completedBids)) then return reconcileBidList(item, pendingBids, completedBids, false); elseif (doesPendingBidListMatch(pendingBids)) then return reconcileBidList(item, pendingBids, completedBids, false); else debugPrint("Cannot reconcile by quantity"); end return 0;end--------------------------------------------------------------------------------- Takes lists of pending and completed items and attempts to reconcile them.-- A purchase is added for each pending and completed bid match. If-- discrepencies are not allowed and there are discrepencies, then no bids-- are reconciled.-------------------------------------------------------------------------------function reconcileBidList(item, pendingBids, completedBids, discrepenciesAllowed) -- If we have some bids, reconcile them! local bidsReconciled = 0; if (table.getn(pendingBids) > 0 or table.getn(completedBids) > 0) then -- For each pending bid, get the list of potential completed bids. -- Afterwards, sort the pending bid list by match count. for pendingIndex = 1, table.getn(pendingBids) do local pendingBid = pendingBids[pendingIndex]; pendingBid.matches = {}; for completedIndex = 1, table.getn(completedBids) do local completedBid = completedBids[completedIndex]; if (doesPendingBidMatchCompletedBid(pendingBid, completedBid)) then table.insert(pendingBid.matches, completedBid); end end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -