📄 salesdb.lua
字号:
-- Adds a successful auction result to the database.-------------------------------------------------------------------------------function addSuccessfulAuction(timestamp, item, proceeds, price, isBuyout, deposit, consignment, buyer) addCompletedAuction(timestamp, item, AUCTION_SOLD, nil, proceeds, price, isBuyout, deposit, consignment, buyer);end--------------------------------------------------------------------------------- Adds an expired auction result to the database.-------------------------------------------------------------------------------function addExpiredAuction(timestamp, item, quantity) addCompletedAuction(timestamp, item, AUCTION_EXPIRED, quantity);end--------------------------------------------------------------------------------- Adds a canceled auction result to the database.-------------------------------------------------------------------------------function addCanceledAuction(timestamp, item, quantity) addCompletedAuction(timestamp, item, AUCTION_CANCELED, quantity);end--------------------------------------------------------------------------------- Adds a auction completed (success/expired/canceled) to the database.-------------------------------------------------------------------------------function addCompletedAuction(timestamp, item, result, quantity, proceeds, price, isBuyout, deposit, consignment, buyer) if (item and (quantity or proceeds)) then -- Create a packed record. local completedAuction = {}; completedAuction.time = timestamp; completedAuction.result = result; completedAuction.quantity = quantity; completedAuction.proceeds = proceeds; completedAuction.price = price; completedAuction.isBuyout = isBuyout; completedAuction.deposit = deposit; completedAuction.consignment = consignment; completedAuction.buyer = buyer; completedAuction.sellerId = getCurrentPlayerId(); local packedCompletedAuction = packCompletedAuction(completedAuction); -- Only add the completed auction to the database if there is a -- matching pending auction. if (getPendingAuctionMatchCount(item, completedAuction) > 0) then -- Add to the completed auctions table. local completedAuctionsTable = getCompletedAuctionsTableForItem(item, true); table.insert(completedAuctionsTable, packedCompletedAuction); -- Debugging noise. printCompletedAuction(debugPrint, "Added completed auction: ", item, completedAuction); -- Attmept to immediately reconcile this item. reconcileAuctionsByQuantityOrProceeds(item, completedAuction.quantity, completedAuction.proceeds); else printCompletedAuction(debugPrint, "Ignoring completed auction (no match): ", item, completedAuction); end else debugPrint("Invalid call to addCompletedAuction()"); endend--------------------------------------------------------------------------------- Deletes a completed auction from the database.---- If completedAuctions is provided, the completed auction will be removed-- from it.-------------------------------------------------------------------------------function deleteCompletedAuction(item, index, reason, completedAuctions) -- Update the unpacked completed auction list, if provided local completedAuction = nil; if (completedAuctions) then -- Iterate in reverse since we will be removing the completed auction -- from the list when we find it. for completedIndex = table.getn(completedAuctions), 1, -1 do local auction = completedAuctions[completedIndex]; if (index == auction.index) then completedAuction = auction; table.remove(completedAuctions, completedIndex); elseif (index < auction.index) then auction.index = auction.index - 1; end end end -- Grab the completed auctions table for the item. local completedAuctionsTable = getCompletedAuctionsTableForItem(item); if (completedAuctionsTable) then -- Get the completed auction if we don't already have it. if (completedAuction == nil) then completedAuction = unpackCompletedAuction(completedAuctionsTable[index]); end -- Remove the completed auction from the table. table.remove(completedAuctionsTable, index); if (table.getn(completedAuctionsTable)) then getCompletedAuctionsTableForItem(item); -- Deletes the table end -- Debug noise. printCompletedAuction( debugPrint, "Deleted completed auction ("..nilSafeStringFromString(reason).."): ", item, completedAuction); endend--------------------------------------------------------------------------------- Converts a completed auction into a ';' delimited string.-------------------------------------------------------------------------------function packCompletedAuction(completedAuction) return stringFromNumber(completedAuction.time)..";".. stringFromNumber(completedAuction.result)..";".. stringFromNumber(completedAuction.quantity)..";".. stringFromNumber(completedAuction.proceeds)..";".. stringFromNumber(completedAuction.price)..";".. stringFromBoolean(completedAuction.isBuyout)..";".. stringFromNumber(completedAuction.deposit)..";".. stringFromNumber(completedAuction.consignment)..";".. nilSafeStringFromString(completedAuction.buyer)..";".. stringFromNumber(completedAuction.sellerId);end--------------------------------------------------------------------------------- Converts a ';' delimited string into a completed auction.-------------------------------------------------------------------------------function unpackCompletedAuction(packedCompletedAuction) local completedAuction = {}; _, _, completedAuction.time, completedAuction.result, completedAuction.quantity, completedAuction.proceeds, completedAuction.price, completedAuction.isBuyout, completedAuction.deposit, completedAuction.consignment, completedAuction.buyer, completedAuction.sellerId = string.find(packedCompletedAuction, "(.+);(.+);(.+);(.+);(.+);(.+);(.+);(.+);(.+);(.+)"); completedAuction.time = numberFromString(completedAuction.time); completedAuction.result = numberFromString(completedAuction.result); completedAuction.quantity = numberFromString(completedAuction.quantity); completedAuction.proceeds = numberFromString(completedAuction.proceeds); completedAuction.price = numberFromString(completedAuction.price); completedAuction.isBuyout = numberFromString(completedAuction.isBuyout); completedAuction.deposit = numberFromString(completedAuction.deposit); completedAuction.consignment = numberFromString(completedAuction.consignment); completedAuction.buyer = stringFromNilSafeString(completedAuction.buyer); completedAuction.sellerId = numberFromString(completedAuction.sellerId); return completedAuction;end--------------------------------------------------------------------------------- Gets the completed auctions table for the specified item. The table contains-- packed records.-------------------------------------------------------------------------------function getCompletedAuctionsTableForItem(item, create) local completedAuctionsTable = BeanCounterRealmDB.completedAuctions; if (completedAuctionsTable == nil) then completedAuctionsTable = {}; BeanCounterRealmDB.completedAuctions = completedAuctionsTable; end -- Get the table for the item. Create or delete if appropriate. local completedAuctionsForItemTable = completedAuctionsTable[item]; if (completedAuctionsForItemTable == nil and create) then completedAuctionsForItemTable = {}; completedAuctionsTable[item] = completedAuctionsForItemTable; elseif (completedAuctionsForItemTable and table.getn(completedAuctionsForItemTable) == 0 and not create) then completedAuctionsTable[item] = nil; end return completedAuctionsForItemTable;end--------------------------------------------------------------------------------- Gets the completed auctions (unpacked) for the specified item-------------------------------------------------------------------------------function getCompletedAuctionsForItem(item, filterFunc) local completedAuctions = {}; local completedAuctionsTable = getCompletedAuctionsTableForItem(item); if (completedAuctionsTable) then for index in completedAuctionsTable do local completedAuction = unpackCompletedAuction(completedAuctionsTable[index]); completedAuction.index = index; if (filterFunc == nil or filterFunc(completedAuction)) then table.insert(completedAuctions, completedAuction); end end end return completedAuctions;end--------------------------------------------------------------------------------- Prints the completed auctions.-------------------------------------------------------------------------------function printCompletedAuctions() chatPrint("Completed Auctions:"); if (BeanCounterRealmDB.completedAuctions) then for item in BeanCounterRealmDB.completedAuctions do local completedAuctionsTable = BeanCounterRealmDB.completedAuctions[item]; for index = 1, table.getn(completedAuctionsTable) do local completedAuction = unpackCompletedAuction(completedAuctionsTable[index]); printCompletedAuction(chatPrint, nil, item, completedAuction); end end endend--------------------------------------------------------------------------------- Calls the specified print function for the completed auction.-------------------------------------------------------------------------------function printCompletedAuction(printFunc, prefix, item, completedAuction) if (prefix == nil) then prefix = ""; end printFunc( prefix.. date("%c", completedAuction.time)..", ".. item..", ".. stringFromNumber(completedAuction.quantity)..", ".. stringFromNumber(completedAuction.proceeds)..", ".. stringFromNumber(completedAuction.price)..", ".. stringFromBoolean(completedAuction.isBuyout)..", ".. stringFromNumber(completedAuction.deposit)..", ".. stringFromNumber(completedAuction.consignment)..", ".. nilSafeStringFromString(completedAuction.buyer)..", ".. nilSafeStringFromString(getPlayerName(completedAuction.sellerId)));end--=============================================================================-- Sales functions--=============================================================================--------------------------------------------------------------------------------- Adds a sale to the database.-------------------------------------------------------------------------------function addSale(timestamp, result, item, quantity, bid, buyout, net, price, isBuyout, buyer) if (item and quantity and net) then -- Create a packed record. local sale = {}; sale.time = timestamp; sale.result = result; sale.quantity = quantity; sale.bid = bid; sale.buyout = buyout; sale.net = net; sale.price = price; sale.isBuyout = isBuyout; sale.buyer = buyer; sale.sellerId = getCurrentPlayerId(); local packedSale = packSale(sale); -- Add the sale to the table. local salesTable = getSalesTableForItem(item, true); table.insert(salesTable, packedSale); -- Debugging noise. printSale(debugPrint, "Added sale: ", item, sale); else debugPrint("Invalid call to addSale()"); endend--------------------------------------------------------------------------------- Converts a sale into a ';' delimited string.-------------------------------------------------------------------------------function packSale(sale) return sale.time..";".. stringFromNumber(sale.result)..";".. stringFromNumber(sale.quantity)..";".. stringFromNumber(sale.bid)..";".. stringFromNumber(sale.buyout)..";".. stringFromNumber(sale.net)..";".. stringFromNumber(sale.price)..";".. stringFromBoolean(sale.isBuyout)..";".. nilSafeStringFromString(sale.buyer)..";".. stringFromNumber(sale.sellerId);end--------------------------------------------------------------------------------- Converts a ';' delimited string into a sale.-------------------------------------------------------------------------------function unpackSale(packedSale) local sale = {}; _, _, sale.time, sale.result, sale.quantity, sale.bid, sale.buyout, sale.net, sale.price, sale.isBuyout, sale.buyer, sale.sellerId = string.find(packedSale, "(.+);(.+);(.+);(.+);(.+);(.+);(.+);(.+);(.+);(.+)"); sale.time = numberFromString(sale.time); sale.result = numberFromString(sale.result); sale.quantity = numberFromString(sale.quantity); sale.bid = numberFromString(sale.bid); sale.buyout = numberFromString(sale.buyout); sale.net = numberFromString(sale.net); sale.price = numberFromString(sale.price); sale.isBuyout = booleanFromString(sale.isBuyout); sale.buyer = stringFromNilSafeString(sale.buyer); sale.sellerId = numberFromString(sale.sellerId); return sale;end--------------------------------------------------------------------------------- Gets the sales table for the specified item. The table contains packed-- records.-------------------------------------------------------------------------------function getSalesTableForItem(item, create) local salesTable = BeanCounterRealmDB.sales; if (salesTable == nil) then salesTable = {}; BeanCounterRealmDB.sales = salesTable; end -- Get the table for the item. Create or delete if appropriate. local salesForItemTable = salesTable[item]; if (salesForItemTable == nil and create) then salesForItemTable = {}; salesTable[item] = salesForItemTable;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -