⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 purchasesdb.lua

📁 时间太紧了
💻 LUA
📖 第 1 页 / 共 4 页
字号:
			table.sort(pendingBid.matches, compareTime);		end		table.sort(pendingBids, compareMatchCount);		-- For each pending bid, pick a suitable completed bid match.		-- This algorithm could be much better, but it works for most sane		-- cases.		for pendingIndex = 1, table.getn(pendingBids) do			local pendingBid = pendingBids[pendingIndex];			if (pendingBid.match == nil) then				for completedIndex = 1, table.getn(completedBids) do					local completedBid = completedBids[completedIndex];					if (completedBid.match == nil) then						completedBid.match = pendingBid;						pendingBid.match = completedBid;						break;					end				end			end		end		-- Check for unmatched pending bids.		local unmatchedPendingBidCount = 0;		for pendingIndex = 1, table.getn(pendingBids) do			local pendingBid = pendingBids[pendingIndex];			if (pendingBid.match == nil) then				unmatchedPendingBidCount = unmatchedPendingBidCount + 1;			end		end		-- Check for unmatched completed bids.		local unmatchedCompletedBidCount = 0;		for completedIndex = 1, table.getn(completedBids) do			local completedBid = completedBids[completedIndex];			if (completedBid.match == nil) then				unmatchedCompletedBidCount = unmatchedCompletedBidCount + 1;			end		end		-- Reconcile the lists if all bids match or discrepencies are		-- allowed!		if (discrepencesAllowed or unmatchedPendingBidCount == 0 or unmatchedCompletedBidCount == 0) then			-- Time to log some purchases! We iterate through the pending			-- list in reverse since we will be deleting items from			-- it.			debugPrint("Begin reconciling bid list for "..item);			for pendingIndex = table.getn(pendingBids), 1, -1 do				local pendingBid = pendingBids[pendingIndex];				if (pendingBid.match ~= nil) then					-- Reconcile the matching bids.					reconcileMatchingBids(item, pendingBids, pendingBid, completedBids, pendingBid.match);					bidsReconciled = bidsReconciled + 1;				end			end			debugPrint("End reconciling bid list for "..item.." ("..bidsReconciled.." reconciled)");		else			debugPrint("Not reconciling bids for "..item.." due to discrepencies");		end	else		debugPrint("No reconcilable bids for "..item);	end	return bidsReconciled;end--------------------------------------------------------------------------------- Performs bid reconcilation by removing the pending and completed bids-- and adding a purchase.---- WARNING: The pendingBid and completedBid will be removed from their lists.-------------------------------------------------------------------------------function reconcileMatchingBids(item, pendingBids, pendingBid, completedBids, completedBid)	-- Remove the pending and completed bids	deletePendingBidByIndex(item, pendingBid.index, "reconciled", pendingBids);	deleteCompletedBidByIndex(item, completedBid.index, "reconciled", completedBids);	-- Add a sale (success or failure).	if (not completedBid.isSuccessful) then		debugPrint("Bid not successful");	elseif (pendingBid.isBuyout or completedBid.isPurchaseRecorded) then		debugPrint("Purchase already recorded");	else		addPurchase(completedBid.time, item, pendingBid.quantity, pendingBid.bid, pendingBid.seller, pendingBid.isBuyout);	endend--------------------------------------------------------------------------------- Checks if all pending bids in the list match each other. All pending-- bids must be within a 5 minute period of time to be considered matches.-------------------------------------------------------------------------------function doesPendingBidListMatch(pendingBids)	local match = (table.getn(pendingBids) > 0);	if (match) then		local pendingBid = pendingBids[1];		local firstBidTime = pendingBid.time;		local lastBidTime = pendingBid.time;		local quantity = pendingBid.quantity;		local bid = pendingBid.bid;		local buyout = pendingBid.buyout;		local deposit = pendingBid.deposit;		local buyerId = pendingBid.buyerId;		for index = 2, table.getn(pendingBids) do			local pendingBid = pendingBids[index];			if (quantity ~= pendingBid.quantity or				bid ~= pendingBid.bid or				isBuyout ~= pendingBid.isBuyout or				buyerId ~= pendingBid.buyerId) then				match = false;				break;			elseif (pendingBid.time < firstBidTime) then				firstBidTime = pendingBid.time;			elseif (pendingBid.time > lastBidTime) then				lastBidTime = pendingBid.time;			end		end		if (match) then			local bidTimeSpread = lastBidTime - firstBidTime;			match = (bidTimeSpread < (5 * 60));		end	end	return match;	end--------------------------------------------------------------------------------- Checks if the completed bid could be the pending bid.-------------------------------------------------------------------------------function doesPendingBidMatchCompletedBid(pendingBid, completedBid)	-- Fudge time that we add to the duration of auctions. This accounts for	-- mail delivery lag and additional auction duration due to bidding.	local AUCTION_DURATION_CUSHION = (12 * 60 * 60); -- 12 hours	-- Check if buyer ids match.	if (pendingBid.buyerId ~= nil and 		completedBid.buyerId ~= nil and		completedBid.buyerId ~= pendingBid.buyerId) then		return false;	end	-- Check if auction completed in a time frame that makes sense for the	-- the time left.	if (pendingBid.time + MINIMUM_TIME_LEFT[pendingBid.timeLeft] > completedBid.time or 		pendingBid.time + MAXIMUM_TIME_LEFT[pendingBid.timeLeft] + AUCTION_DURATION_CUSHION < completedBid.time) then		return false;	end		-- Check if the quantities match.	if (pendingBid.quantity ~= nil and 		completedBid.quantity ~= nil and		completedBid.quantity ~= pendingBid.quantity) then		return false;	end	-- Check if the sellers match.	if (pendingBid.seller ~= nil and 		completedBid.seller ~= nil and		completedBid.seller ~= pendingBid.seller) then		return false;	end	-- Check if the buyout flags match.	if (pendingBid.isBuyout ~= nil and 		completedBid.isBuyout ~= nil and		completedBid.isBuyout ~= pendingBid.isBuyout) then		return false;	end	-- If we made it this far, its a possible match!	return true;end--------------------------------------------------------------------------------- Compare two auctions based on match count.-------------------------------------------------------------------------------function compareMatchCount(auction1, auction2)	local count1 = table.getn(auction1.matches);	local count2 = table.getn(auction2.matches);	if (count1 == count2) then		return (auction1.time > auction2.time);	end	return (count1 > count2);end--------------------------------------------------------------------------------- Compares two auctions based on time.-------------------------------------------------------------------------------function compareTime(auction1, auction2)	return (auction1.time > auction2.time);end--=============================================================================-- Utility functions--=============================================================================--------------------------------------------------------------------------------- Gets the number of non-nil fields in a bid.-------------------------------------------------------------------------------function getBidNonNilFieldCount(bid)	local count = 0;	if (bid.quantity) then count = count + 1 end;	if (bid.bid) then count = count + 1 end;	if (bid.seller) then count = count + 1 end;	if (bid.isBuyout) then count = count + 1 end;	return count;end--------------------------------------------------------------------------------- Compares the bids by number of non-nil fields.-------------------------------------------------------------------------------function compareBidsByNonNilFieldCount(bid1, bid2)	local count1 = getBidNonNilFieldCount(bid1);	local count2 = getBidNonNilFieldCount(bid2);	if (count1 > count2) then		return true;	end	return false;end--------------------------------------------------------------------------------------------------------------------------------------------------------------function debugPrint(message)	BeanCounter.DebugPrint("[BeanCounter.PurchasesDB] "..stringFromNilSafeString(message));end--[[--------------------------------------------------------------------------------------------------------------------------------------------------------------function testme()	addPurchase(time(), "Silver Bar", 10, 1000, "Stupid", false);	printPurchases();end--------------------------------------------------------------------------------------------------------------------------------------------------------------function testme1()	resetDatabase();	addPendingBid("Silver Bar", 1, 3500, "Sucker1", false);	addPendingBid("Silver Bar", 1, 3000, "Sucker1", true);	addPendingBid("Silver Bar", 1, 4000, "Sucker1", false);	addPendingBid("Silver Bar", 2, 3500, "Sucker2", false);	addPendingBid("Silver Bar", 2, 3000, "Sucker2", false);	addPendingBid("Silver Bar", 2, 4000, "Sucker2", false);	printPendingBids();	printCompletedBids();	printPurchases();	addSuccessfulBid("Silver Bar", 1, 4000, "Sucker1", false);	addSuccessfulBid("Silver Bar", 1);	addSuccessfulBid("Silver Bar", 1);	addFailedBid("Silver Bar", 4000);	addFailedBid("Silver Bar", 3000);	addFailedBid("Silver Bar", 3500);	printPendingBids();	printCompletedBids();	printPurchases();end--------------------------------------------------------------------------------------------------------------------------------------------------------------function testme2()	resetDatabase();	addPendingBid("Silver Bar", 1, 3500, "Sucker", false);	addPendingBid("Silver Bar", 1, 3500, "Sucker", false);	addPendingBid("Silver Bar", 1, 3500, "Sucker", false);	printPendingBids();	printCompletedBids();	printPurchases();	addSuccessfulBid("Silver Bar", 1);	printPendingBids();	printCompletedBids();	printPurchases();	addFailedBid("Silver Bar", 3500);	printPendingBids();	printCompletedBids();	printPurchases();	addSuccessfulBid("Silver Bar", 1);	printPendingBids();	printCompletedBids();	printPurchases();end--]]--------------------------------------------------------------------------------- Public API-------------------------------------------------------------------------------BeanCounter.Purchases = {	AddPendingBid = addPendingBid;	DeletePendingBid = deletePendingBidBySignature;	GetPendingBidItems = getPendingBidItems;	GetPendingBidsForItem = getPendingBidsForItem;	AddSuccessfulBid = addSuccessfulBid;	AddFailedBid = addFailedBid;	GetPurchasedItems = getPurchasedItems;	GetPurchasesForItem = getPurchasesForItem;	ReconcileBids = reconcileBids;	PrintPendingBids = printPendingBids;	PrintCompletedBids = printCompletedBids;	PrintPurchases = printPurchases;};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -