314 lines
12 KiB
TypeScript
314 lines
12 KiB
TypeScript
import type { AuctionRedisKeys } from './auction-redis.keys';
|
|
import { defineAuctionRedisScript } from './auction-redis.scripts';
|
|
|
|
export interface AuctionBidScriptInput {
|
|
bidderTeamIndex: number;
|
|
bidPointAmount: number;
|
|
bidRequestId: string;
|
|
bidderUserId: string;
|
|
clientObservedAuctionLifecycleId: string;
|
|
clientObservedCurrentBidRoundPlayerUserId: string;
|
|
maximumAuctionTeamMemberCount: number;
|
|
maximumStoredAuctionBidHistoryCount: number;
|
|
maximumStoredBidRequestResultCount: number;
|
|
matchId: string;
|
|
minimumBidPointReservePerRemainingAuctionTeamMemberSlot: number;
|
|
minimumBidRequestResultReplayWindowMS: number;
|
|
}
|
|
|
|
export interface AuctionBidScriptInvocation {
|
|
keys: string[];
|
|
script: typeof AUCTION_BID_SCRIPT;
|
|
values: string[];
|
|
}
|
|
|
|
const BID_RESULT_HASH_SUFFIXES = [
|
|
'fingerprint',
|
|
'status',
|
|
'revision',
|
|
'rejectCode',
|
|
'currentHighestBidPointAmount',
|
|
'currentHighestBidderTeamIndex',
|
|
'currentBidRoundDeadlineTimestampMS',
|
|
'auctionBidHistoryEntryJson',
|
|
'responseServerTimestampMS',
|
|
'createdAtTimestampMS',
|
|
] as const;
|
|
|
|
export function buildBidRequestPayloadFingerprint(params: {
|
|
bidderTeamIndex: number;
|
|
bidPointAmount: number;
|
|
clientObservedAuctionLifecycleId: string;
|
|
clientObservedCurrentBidRoundPlayerUserId: string;
|
|
matchId: string;
|
|
}): string {
|
|
return [
|
|
params.matchId,
|
|
params.clientObservedAuctionLifecycleId,
|
|
params.clientObservedCurrentBidRoundPlayerUserId,
|
|
params.bidderTeamIndex,
|
|
params.bidPointAmount,
|
|
].join('|');
|
|
}
|
|
|
|
export function buildAuctionBidScriptInvocation(
|
|
keys: AuctionRedisKeys,
|
|
input: AuctionBidScriptInput,
|
|
): AuctionBidScriptInvocation {
|
|
return {
|
|
keys: [
|
|
keys.liveStateKey,
|
|
keys.bidHistoryKey,
|
|
keys.bidRequestResultsKey,
|
|
keys.bidRequestResultOrderKey,
|
|
keys.deadlineQueueKey,
|
|
],
|
|
script: AUCTION_BID_SCRIPT,
|
|
values: [
|
|
input.matchId,
|
|
String(input.bidderTeamIndex),
|
|
String(input.bidPointAmount),
|
|
String(input.maximumAuctionTeamMemberCount),
|
|
String(input.minimumBidPointReservePerRemainingAuctionTeamMemberSlot),
|
|
input.bidRequestId,
|
|
input.bidderUserId,
|
|
input.clientObservedAuctionLifecycleId,
|
|
input.clientObservedCurrentBidRoundPlayerUserId,
|
|
buildBidRequestPayloadFingerprint(input),
|
|
String(input.maximumStoredBidRequestResultCount),
|
|
String(input.minimumBidRequestResultReplayWindowMS),
|
|
String(input.maximumStoredAuctionBidHistoryCount),
|
|
],
|
|
};
|
|
}
|
|
|
|
export const AUCTION_BID_SCRIPT = defineAuctionRedisScript(
|
|
'auction.bid',
|
|
`
|
|
local liveAuctionStateKey = KEYS[1]
|
|
local auctionBidHistoryKey = KEYS[2]
|
|
local bidRequestResultsKey = KEYS[3]
|
|
local bidRequestResultOrderKey = KEYS[4]
|
|
local bidRoundDeadlineQueueKey = KEYS[5]
|
|
|
|
local matchId = ARGV[1]
|
|
local bidderTeamIndex = ARGV[2]
|
|
local bidPointAmount = tonumber(ARGV[3])
|
|
local maximumAuctionTeamMemberCount = tonumber(ARGV[4])
|
|
local minimumBidPointReservePerRemainingAuctionTeamMemberSlot = tonumber(ARGV[5])
|
|
local bidRequestId = ARGV[6]
|
|
local bidderUserId = ARGV[7]
|
|
local clientObservedAuctionLifecycleId = ARGV[8]
|
|
local clientObservedCurrentBidRoundPlayerUserId = ARGV[9]
|
|
local bidRequestPayloadFingerprint = ARGV[10]
|
|
local maximumStoredBidRequestResultCount = tonumber(ARGV[11])
|
|
local minimumBidRequestResultReplayWindowMS = tonumber(ARGV[12])
|
|
local maximumStoredAuctionBidHistoryCount = tonumber(ARGV[13])
|
|
|
|
local redisTime = redis.call('TIME')
|
|
local currentRedisTimestampMS = (tonumber(redisTime[1]) * 1000) + math.floor(tonumber(redisTime[2]) / 1000)
|
|
|
|
local function requestField(suffix)
|
|
return bidRequestId .. ':' .. suffix
|
|
end
|
|
|
|
local function hdelBidRequest(requestId)
|
|
redis.call(
|
|
'HDEL',
|
|
bidRequestResultsKey,
|
|
requestId .. ':fingerprint',
|
|
requestId .. ':status',
|
|
requestId .. ':revision',
|
|
requestId .. ':rejectCode',
|
|
requestId .. ':currentHighestBidPointAmount',
|
|
requestId .. ':currentHighestBidderTeamIndex',
|
|
requestId .. ':currentBidRoundDeadlineTimestampMS',
|
|
requestId .. ':auctionBidHistoryEntryJson',
|
|
requestId .. ':responseServerTimestampMS',
|
|
requestId .. ':createdAtTimestampMS'
|
|
)
|
|
end
|
|
|
|
local function trimOldBidRequestResults()
|
|
while redis.call('LLEN', bidRequestResultOrderKey) >= maximumStoredBidRequestResultCount do
|
|
local oldestBidRequestId = redis.call('LINDEX', bidRequestResultOrderKey, 0)
|
|
if not oldestBidRequestId then
|
|
return
|
|
end
|
|
local createdAtTimestampMS = tonumber(redis.call('HGET', bidRequestResultsKey, oldestBidRequestId .. ':createdAtTimestampMS') or '0')
|
|
if createdAtTimestampMS > currentRedisTimestampMS - minimumBidRequestResultReplayWindowMS then
|
|
return
|
|
end
|
|
redis.call('LPOP', bidRequestResultOrderKey)
|
|
hdelBidRequest(oldestBidRequestId)
|
|
end
|
|
end
|
|
|
|
local function replayExistingRequest()
|
|
local status = redis.call('HGET', bidRequestResultsKey, requestField('status')) or ''
|
|
return {
|
|
'REPLAY',
|
|
status,
|
|
bidRequestId,
|
|
redis.call('HGET', bidRequestResultsKey, requestField('revision')) or '',
|
|
redis.call('HGET', bidRequestResultsKey, requestField('rejectCode')) or '',
|
|
redis.call('HGET', bidRequestResultsKey, requestField('currentHighestBidPointAmount')) or '',
|
|
redis.call('HGET', bidRequestResultsKey, requestField('currentHighestBidderTeamIndex')) or '',
|
|
redis.call('HGET', bidRequestResultsKey, requestField('currentBidRoundDeadlineTimestampMS')) or '',
|
|
redis.call('HGET', bidRequestResultsKey, requestField('auctionBidHistoryEntryJson')) or '',
|
|
redis.call('HGET', bidRequestResultsKey, requestField('responseServerTimestampMS')) or tostring(currentRedisTimestampMS)
|
|
}
|
|
end
|
|
|
|
local existingFingerprint = redis.call('HGET', bidRequestResultsKey, requestField('fingerprint'))
|
|
if existingFingerprint then
|
|
if existingFingerprint ~= bidRequestPayloadFingerprint then
|
|
return {'REJECTED', 'DUPLICATE_REQUEST_CONFLICT', bidRequestId, tostring(currentRedisTimestampMS)}
|
|
end
|
|
return replayExistingRequest()
|
|
end
|
|
|
|
trimOldBidRequestResults()
|
|
if redis.call('LLEN', bidRequestResultOrderKey) >= maximumStoredBidRequestResultCount then
|
|
return {'REJECTED', 'BID_REQUEST_RESULT_LIMIT', bidRequestId, tostring(currentRedisTimestampMS)}
|
|
end
|
|
|
|
local function storeRejectedRequest(rejectCode)
|
|
local rejectedCurrentBidRoundDeadlineTimestampMS = redis.call('HGET', liveAuctionStateKey, 'currentBidRoundDeadlineTimestampMS') or ''
|
|
redis.call(
|
|
'HSET',
|
|
bidRequestResultsKey,
|
|
requestField('fingerprint'),
|
|
bidRequestPayloadFingerprint,
|
|
requestField('status'),
|
|
'rejected',
|
|
requestField('rejectCode'),
|
|
rejectCode,
|
|
requestField('currentBidRoundDeadlineTimestampMS'),
|
|
rejectedCurrentBidRoundDeadlineTimestampMS,
|
|
requestField('responseServerTimestampMS'),
|
|
tostring(currentRedisTimestampMS),
|
|
requestField('createdAtTimestampMS'),
|
|
tostring(currentRedisTimestampMS)
|
|
)
|
|
redis.call('RPUSH', bidRequestResultOrderKey, bidRequestId)
|
|
return {'REJECTED', rejectCode, bidRequestId, tostring(currentRedisTimestampMS), rejectedCurrentBidRoundDeadlineTimestampMS}
|
|
end
|
|
|
|
local phase = redis.call('HGET', liveAuctionStateKey, 'phase')
|
|
if phase ~= 'bidding' then
|
|
return storeRejectedRequest('PHASE')
|
|
end
|
|
|
|
local auctionLifecycleId = redis.call('HGET', liveAuctionStateKey, 'auctionLifecycleId')
|
|
local currentBidRoundPlayerUserId = redis.call('HGET', liveAuctionStateKey, 'currentBidRoundPlayerUserId')
|
|
if auctionLifecycleId ~= clientObservedAuctionLifecycleId or currentBidRoundPlayerUserId ~= clientObservedCurrentBidRoundPlayerUserId then
|
|
return storeRejectedRequest('STALE_BID_ROUND')
|
|
end
|
|
|
|
local currentBidRoundDeadlineTimestampMS = tonumber(redis.call('HGET', liveAuctionStateKey, 'currentBidRoundDeadlineTimestampMS') or '0')
|
|
if currentBidRoundDeadlineTimestampMS <= currentRedisTimestampMS then
|
|
return storeRejectedRequest('EXPIRED')
|
|
end
|
|
|
|
local currentHighestBidPointAmount = tonumber(redis.call('HGET', liveAuctionStateKey, 'currentHighestBidPointAmount') or '0')
|
|
if bidPointAmount <= currentHighestBidPointAmount then
|
|
return storeRejectedRequest('TOO_LOW')
|
|
end
|
|
|
|
local remainingBidPointAmountField = 'team:' .. bidderTeamIndex .. ':remainingBidPointAmount'
|
|
local assignedAuctionTeamMemberCountField = 'team:' .. bidderTeamIndex .. ':assignedAuctionTeamMemberCount'
|
|
local remainingBidPointAmountRaw = redis.call('HGET', liveAuctionStateKey, remainingBidPointAmountField)
|
|
if not remainingBidPointAmountRaw then
|
|
return storeRejectedRequest('UNKNOWN_TEAM')
|
|
end
|
|
|
|
local assignedAuctionTeamMemberCount = tonumber(redis.call('HGET', liveAuctionStateKey, assignedAuctionTeamMemberCountField) or '0')
|
|
if assignedAuctionTeamMemberCount >= maximumAuctionTeamMemberCount then
|
|
return storeRejectedRequest('FULL')
|
|
end
|
|
|
|
local remainingBidPointAmount = tonumber(remainingBidPointAmountRaw)
|
|
local remainingSlotsAfterBid = maximumAuctionTeamMemberCount - assignedAuctionTeamMemberCount - 1
|
|
local requiredReserveAfterBid = remainingSlotsAfterBid * minimumBidPointReservePerRemainingAuctionTeamMemberSlot
|
|
if bidPointAmount + requiredReserveAfterBid > remainingBidPointAmount then
|
|
return storeRejectedRequest('INSUFFICIENT')
|
|
end
|
|
|
|
if redis.call('LLEN', auctionBidHistoryKey) >= maximumStoredAuctionBidHistoryCount then
|
|
return storeRejectedRequest('BID_HISTORY_LIMIT')
|
|
end
|
|
|
|
local maximumBidRoundDurationSeconds = tonumber(redis.call('HGET', liveAuctionStateKey, 'maximumBidRoundDurationSeconds') or '0')
|
|
local bidRoundExtensionSeconds = tonumber(redis.call('HGET', liveAuctionStateKey, 'bidRoundExtensionSeconds') or '0')
|
|
local remainingBidRoundTimeMS = currentBidRoundDeadlineTimestampMS - currentRedisTimestampMS
|
|
local nextRemainingBidRoundTimeMS = remainingBidRoundTimeMS
|
|
local bidRoundExtensionMS = bidRoundExtensionSeconds * 1000
|
|
local maximumBidRoundDurationMS = maximumBidRoundDurationSeconds * 1000
|
|
if nextRemainingBidRoundTimeMS < bidRoundExtensionMS then
|
|
nextRemainingBidRoundTimeMS = bidRoundExtensionMS
|
|
end
|
|
if nextRemainingBidRoundTimeMS > maximumBidRoundDurationMS then
|
|
nextRemainingBidRoundTimeMS = maximumBidRoundDurationMS
|
|
end
|
|
local nextBidRoundDeadlineTimestampMS = currentRedisTimestampMS + nextRemainingBidRoundTimeMS
|
|
|
|
local revision = redis.call('HINCRBY', liveAuctionStateKey, 'revision', 1)
|
|
local auctionBidHistoryEntryJson =
|
|
'{"bidderTeamIndex":' .. bidderTeamIndex ..
|
|
',"bidPointAmount":' .. tostring(bidPointAmount) ..
|
|
',"bidderUserId":"' .. bidderUserId ..
|
|
'","bidPlacedAtTimestampMS":' .. tostring(currentRedisTimestampMS) ..
|
|
'}'
|
|
|
|
redis.call('RPUSH', auctionBidHistoryKey, auctionBidHistoryEntryJson)
|
|
redis.call(
|
|
'HSET',
|
|
liveAuctionStateKey,
|
|
'currentHighestBidPointAmount',
|
|
tostring(bidPointAmount),
|
|
'currentHighestBidderTeamIndex',
|
|
bidderTeamIndex,
|
|
'currentBidRoundDeadlineTimestampMS',
|
|
tostring(nextBidRoundDeadlineTimestampMS)
|
|
)
|
|
redis.call('ZADD', bidRoundDeadlineQueueKey, nextBidRoundDeadlineTimestampMS, matchId)
|
|
redis.call(
|
|
'HSET',
|
|
bidRequestResultsKey,
|
|
requestField('fingerprint'),
|
|
bidRequestPayloadFingerprint,
|
|
requestField('status'),
|
|
'ok',
|
|
requestField('revision'),
|
|
tostring(revision),
|
|
requestField('currentHighestBidPointAmount'),
|
|
tostring(bidPointAmount),
|
|
requestField('currentHighestBidderTeamIndex'),
|
|
bidderTeamIndex,
|
|
requestField('currentBidRoundDeadlineTimestampMS'),
|
|
tostring(nextBidRoundDeadlineTimestampMS),
|
|
requestField('auctionBidHistoryEntryJson'),
|
|
auctionBidHistoryEntryJson,
|
|
requestField('responseServerTimestampMS'),
|
|
tostring(currentRedisTimestampMS),
|
|
requestField('createdAtTimestampMS'),
|
|
tostring(currentRedisTimestampMS)
|
|
)
|
|
redis.call('RPUSH', bidRequestResultOrderKey, bidRequestId)
|
|
|
|
return {
|
|
'OK',
|
|
bidRequestId,
|
|
tostring(revision),
|
|
tostring(currentRedisTimestampMS),
|
|
tostring(bidPointAmount),
|
|
bidderTeamIndex,
|
|
tostring(nextBidRoundDeadlineTimestampMS),
|
|
auctionBidHistoryEntryJson
|
|
}
|
|
`,
|
|
);
|
|
|
|
export const BID_RESULT_HASH_FIELD_SUFFIXES = BID_RESULT_HASH_SUFFIXES;
|