📄 salesdb.lua
字号:
elseif (salesForItemTable and table.getn(salesForItemTable) == 0 and not create) then salesTable[item] = nil; end return salesForItemTable;end--------------------------------------------------------------------------------- Gets the list of sold items.-------------------------------------------------------------------------------function getSoldItems(item) local items = {}; if (BeanCounterRealmDB.sales) then for item in BeanCounterRealmDB.sales do local salesTable = getSalesTableForItem(item); if (salesTable and table.getn(salesTable) > 0) then table.insert(items, item); end end end return items;end--------------------------------------------------------------------------------- Gets the sales (unpacked) for the specified item-------------------------------------------------------------------------------function getSalesForItem(item) local sales = {}; local salesTable = getSalesTableForItem(item); if (salesTable) then for index in salesTable do local sale = unpackSale(salesTable[index]); table.insert(sales, sale); end end return sales;end--------------------------------------------------------------------------------- Prints the sales.-------------------------------------------------------------------------------function printSales() chatPrint("Sales:"); if (BeanCounterRealmDB.sales) then for item in BeanCounterRealmDB.sales do local salesTable = BeanCounterRealmDB.sales[item]; for index = 1, table.getn(salesTable) do local sale = unpackSale(salesTable[index]); printSale(chatPrint, nil, item, sale); end end endend--------------------------------------------------------------------------------- Calls the specified print function for the sale.-------------------------------------------------------------------------------function printSale(printFunc, prefix, item, sale) if (prefix == nil) then prefix = ""; end printFunc( prefix.. date("%c", sale.time)..", ".. stringFromNumber(sale.result)..", ".. item..", ".. stringFromNumber(sale.quantity)..", ".. stringFromNumber(sale.bid)..", ".. stringFromNumber(sale.buyout)..", ".. stringFromNumber(sale.net)..", ".. stringFromNumber(sale.price)..", ".. stringFromBoolean(sale.isBuyout)..", ".. nilSafeStringFromString(sale.buyer)..", ".. nilSafeStringFromString(getPlayerName(sale.sellerId)));end--=============================================================================-- Auction reconcilation functions--=============================================================================--------------------------------------------------------------------------------- Reconcile all auctions that should be completed by the specified time.-------------------------------------------------------------------------------function reconcileAuctions(reconcileTime, excludeItems) local totalReconciled = 0; local totalDiscarded = 0; local items = getPendingAuctionItems(); for index in items do local item = items[index]; if (excludeItems == nil or excludeItems[item] == nil) then local reconciled, discarded = reconcileAuctionsByTime(item, reconcileTime); totalReconciled = totalReconciled + reconciled; totalDiscarded = totalDiscarded + discarded; end end return totalReconciled, totalDiscarded;end--------------------------------------------------------------------------------- Reconcile auctions for the item that should be completed by the specified-- time.-------------------------------------------------------------------------------function reconcileAuctionsByTime(item, reconcileTime) debugPrint("reconcileBidsByTime("..item..", "..date("%c", reconcileTime)..")"); -- Get the list of pending auctions that should have completed before the -- specified time. local pendingAuctions = getPendingAuctionsForItem( item, function(pendingAuction) return (pendingAuction.sellerId == getCurrentPlayerId() and pendingAuction.time + (pendingAuction.runTime * 60) + AUCTION_OVERRUN_LIMIT < reconcileTime); end); debugPrint(table.getn(pendingAuctions).." matching pending auctions"); -- Get the list of completed auctions that completed before the specified -- time. local completedAuctions = getCompletedAuctionsForItem( item, function(completedAuction) return (completedAuction.sellerId == getCurrentPlayerId() and completedAuction.time < reconcileTime); end); debugPrint(table.getn(completedAuctions).." matching completed auctions"); -- Reconcile the lists. local reconciledAuctions = reconcileAuctionList(item, pendingAuctions, completedAuctions, true); -- Cleanup the unmatch pending auctions that are too old to match any -- completed auctions. local discardedPendingAuctions = 0; for index = table.getn(pendingAuctions), 1, -1 do local pendingAuction = pendingAuctions[index]; deletePendingAuction(item, pendingAuction.index, "no match", pendingAuctions); discardedPendingAuctions = discardedPendingAuctions + 1; end -- Cleanup the unmatched completed auctions that are too old to match any -- pending auctions. local discardedCompletedAuctions = 0; for index = table.getn(completedAuctions), 1, -1 do local completedAuction = completedAuctions[index]; if (completedAuction.time + AUCTION_DURATION_LIMIT < reconcileTime) then deleteCompletedAuction(item, completedAuction.index, "no match", completedAuctions); discardedCompletedAuctions = discardedCompletedAuctions + 1; end end return reconciledAuctions, discardedPendingAuctions, discardedCompletedAuctions;end--------------------------------------------------------------------------------- Attempts to reconcile auctions by either time or proceeds.-------------------------------------------------------------------------------function reconcileAuctionsByQuantityOrProceeds(item, quantityHint, proceedsHint) -- If a hint is supplied, try them first. If no hints are supplied or the -- hints cause auctions to be reconciled, then widen the reconcilation -- hunt. if ((proceedsHint == nil and quantityHint == nil) or (proceedsHint ~= nil and reconcileAuctionsByProceeds(item, proceedsHint) > 0) or (quantityHint ~= nil and reconcileAuctionsByQuantity(item, quantityHint) > 0)) then local index = 1; local quantitiesAttempted = {}; local proceedsAttempted = {}; local completedAuctions = getCompletedAuctionsTableForItem(item); if (completedAuctions) then while (index <= table.getn(completedAuctions)) do local completedAuction = unpackCompletedAuction(completedAuctions[index]); if (completedAuction.quantity and not quantitiesAttempted[completedAuction.quantity]) then quantitiesAttempted[completedAuction.quantity] = true; if (reconcileAuctionsByQuantity(item, completedAuction.quantity) > 0) then index = 1; proceedsAttempted = {}; else index = index + 1; end elseif (completedAuction.proceeds and not proceedsAttempted[completedAuction.proceeds]) then proceedsAttempted[completedAuction.proceeds] = true; if (reconcileAuctionsByProceeds(item, completedAuction.proceeds) > 0) then index = 1; quantitiesAttempted = {}; proceedsAttempted = {}; else index = index + 1; end else index = index + 1; end end end endend--------------------------------------------------------------------------------- Attempts to reconcile auctions by quantity-------------------------------------------------------------------------------function reconcileAuctionsByQuantity(item, quantity) debugPrint("reconcileAuctionsByQuantity("..item..", "..quantity..")"); -- Get all the pending auctions match the quantity. local pendingAuctions = getPendingAuctionsForItem( item, function(pendingAuction) return (pendingAuction.sellerId == getCurrentPlayerId() and pendingAuction.quantity == quantity); end); debugPrint(table.getn(pendingAuctions).." matching pending auctions"); -- Get all the completed auctions matching the quantity. local completedAuctions = getCompletedAuctionsForItem( item, function(completedAuction) return (completedAuction.sellerId == getCurrentPlayerId() and completedAuction.quantity == quantity); end); debugPrint(table.getn(completedAuctions).." matching completed auctions"); -- Attempt to reconcile the lists. if (table.getn(pendingAuctions) == table.getn(completedAuctions)) then return reconcileAuctionList(item, pendingAuctions, completedAuctions, false); elseif (doesPendingAuctionListMatch(pendingAuctions)) then return reconcileAuctionList(item, pendingAuctions, completedAuctions, false); else debugPrint("Cannot reconcile by quantity"); end return 0;end--------------------------------------------------------------------------------- Attempts to reconcile auctions by proceeds-------------------------------------------------------------------------------function reconcileAuctionsByProceeds(item, proceeds) debugPrint("reconcileAuctionsByProceeds("..item..", "..proceeds..")"); -- Get all the pending auctions match the proceeds. local pendingAuctions = getPendingAuctionsForItem( item, function(pendingAuction) local minProceeds = math.floor(pendingAuction.bid * (1 - pendingAuction.consignmentPercent)) + pendingAuction.deposit; local maxProceeds = math.floor(pendingAuction.buyout * (1 - pendingAuction.consignmentPercent)) + pendingAuction.deposit; return (pendingAuction.sellerId == getCurrentPlayerId() and minProceeds <= proceeds and proceeds <= maxProceeds); end); debugPrint(table.getn(pendingAuctions).." matching pending auctions"); -- Get all the completed auctions matching the quantity. local completedAuctions = getCompletedAuctionsForItem( item, function(completedAuction) return (completedAuction.sellerId == getCurrentPlayerId() and completedAuction.proceeds == proceeds); end); debugPrint(table.getn(completedAuctions).." matching completed auctions"); -- Attempt to reconcile the lists. if (table.getn(pendingAuctions) == table.getn(completedAuctions)) then return reconcileAuctionList(item, pendingAuctions, completedAuctions, false); elseif (doesPendingAuctionListMatch(pendingAuctions)) then return reconcileAuctionList(item, pendingAuctions, completedAuctions, false); else debugPrint("Cannot reconcile by proceeds"); end return 0;end--------------------------------------------------------------------------------- Takes lists of pending and completed items and attempts to reconcile them.-- A sale is added for each pending and completed auction match. If-- discrepencies are not allowed and there are discrepencies, then no auctions-- are reconciled.-------------------------------------------------------------------------------function reconcileAuctionList(item, pendingAuctions, completedAuctions, discrepenciesAllowed) -- If we have some auctions, reconcile them! local auctionsReconciled = 0; if (table.getn(pendingAuctions) > 0 or table.getn(completedAuctions) > 0) then -- For each pending auction, get the list of potential completed auctions. -- Afterwards, sort the pending auction list by match count. for pendingIndex = 1, table.getn(pendingAuctions) do local pendingAuction = pendingAuctions[pendingIndex]; pendingAuction.matches = {}; for completedIndex = 1, table.getn(completedAuctions) do local completedAuction = completedAuctions[completedIndex]; if (doesPendingAuctionMatchCompletedAuction(pendingAuction, completedAuction)) then table.insert(pendingAuction.matches, completedAuction); end end table.sort(pendingAuction.matches, compareTime); end table.sort(pendingAuctions, compareMatchCount); -- For each pending auction, pick a suitable completed auction match. -- This algorithm could be much better, but it works for most sane -- cases. for pendingIndex = 1, table.getn(pendingAuctions) do local pendingAuction = pendingAuctions[pendingIndex]; -- First check if this auction likely expired. We consider it likely -- if a matching auction expired within 1 minute of the run length. for completedIndex = 1, table.getn(completedAuctions) do local completedAuction = completedAuctions[completedIndex]; if (completedAuction.match == nil and completedAuction.result == AUCTION_EXPIRED) then local expectedExpiredTime = pendingAuction.time + (pendingAuction.runTime * 60); if (expectedExpiredTime < completedAuction.time and completedAuction.time < expectedExpiredTime + 60) then
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -