108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
import type { AuctionRedisKeys } from './auction-redis.keys';
|
|
import { defineAuctionRedisScript } from './auction-redis.scripts';
|
|
|
|
export interface AuctionExpirePrepareScriptInput {
|
|
auctionStateTransitionId: string;
|
|
bidRoundExpiryRetryDelayMS: number;
|
|
matchId: string;
|
|
}
|
|
|
|
export interface AuctionExpirePrepareScriptInvocation {
|
|
keys: string[];
|
|
script: typeof AUCTION_EXPIRE_PREPARE_SCRIPT;
|
|
values: string[];
|
|
}
|
|
|
|
export function buildAuctionExpirePrepareScriptInvocation(
|
|
keys: AuctionRedisKeys,
|
|
input: AuctionExpirePrepareScriptInput,
|
|
): AuctionExpirePrepareScriptInvocation {
|
|
return {
|
|
keys: [keys.liveStateKey, keys.deadlineQueueKey],
|
|
script: AUCTION_EXPIRE_PREPARE_SCRIPT,
|
|
values: [
|
|
input.matchId,
|
|
input.auctionStateTransitionId,
|
|
String(input.bidRoundExpiryRetryDelayMS),
|
|
],
|
|
};
|
|
}
|
|
|
|
export const AUCTION_EXPIRE_PREPARE_SCRIPT = defineAuctionRedisScript(
|
|
'auction.expirePrepare',
|
|
`
|
|
local liveAuctionStateKey = KEYS[1]
|
|
local bidRoundDeadlineQueueKey = KEYS[2]
|
|
|
|
local matchId = ARGV[1]
|
|
local auctionStateTransitionId = ARGV[2]
|
|
local bidRoundExpiryRetryDelayMS = tonumber(ARGV[3])
|
|
|
|
local redisTime = redis.call('TIME')
|
|
local currentRedisTimestampMS = (tonumber(redisTime[1]) * 1000) + math.floor(tonumber(redisTime[2]) / 1000)
|
|
|
|
local phase = redis.call('HGET', liveAuctionStateKey, 'phase')
|
|
if phase == 'expiring' then
|
|
return {
|
|
'REPLAY',
|
|
redis.call('HGET', liveAuctionStateKey, 'bidRoundExpiryDecision') or '',
|
|
redis.call('HGET', liveAuctionStateKey, 'bidRoundExpiryAuctionStateTransitionId') or '',
|
|
redis.call('HGET', liveAuctionStateKey, 'bidRoundExpiryRevision') or '',
|
|
tostring(currentRedisTimestampMS)
|
|
}
|
|
end
|
|
if phase ~= 'bidding' then
|
|
redis.call('ZREM', bidRoundDeadlineQueueKey, matchId)
|
|
return {'NOOP', 'PHASE', tostring(currentRedisTimestampMS)}
|
|
end
|
|
|
|
local currentBidRoundDeadlineTimestampMS = tonumber(redis.call('HGET', liveAuctionStateKey, 'currentBidRoundDeadlineTimestampMS') or '0')
|
|
if currentBidRoundDeadlineTimestampMS > currentRedisTimestampMS then
|
|
return {'NOOP', 'NOT_DUE', tostring(currentRedisTimestampMS)}
|
|
end
|
|
|
|
local currentHighestBidPointAmount = tonumber(redis.call('HGET', liveAuctionStateKey, 'currentHighestBidPointAmount') or '0')
|
|
local currentHighestBidderTeamIndex = redis.call('HGET', liveAuctionStateKey, 'currentHighestBidderTeamIndex') or '-1'
|
|
local bidRoundExpiryDecision = 'passed'
|
|
if currentHighestBidPointAmount > 0 then
|
|
bidRoundExpiryDecision = 'sold'
|
|
end
|
|
|
|
local revision = redis.call('HINCRBY', liveAuctionStateKey, 'revision', 1)
|
|
redis.call(
|
|
'HSET',
|
|
liveAuctionStateKey,
|
|
'phase',
|
|
'expiring',
|
|
'bidRoundExpiryDecision',
|
|
bidRoundExpiryDecision,
|
|
'bidRoundExpiryAuctionStateTransitionId',
|
|
auctionStateTransitionId,
|
|
'bidRoundExpiryPlayerUserId',
|
|
redis.call('HGET', liveAuctionStateKey, 'currentBidRoundPlayerUserId') or '',
|
|
'bidRoundExpiryPlayerOrderIndex',
|
|
redis.call('HGET', liveAuctionStateKey, 'currentAuctionPlayerOrderIndex') or '',
|
|
'bidRoundExpiryWinningBidPointAmount',
|
|
tostring(currentHighestBidPointAmount),
|
|
'bidRoundExpiryWinningBidderTeamIndex',
|
|
currentHighestBidderTeamIndex,
|
|
'bidRoundExpiryRevision',
|
|
tostring(revision)
|
|
)
|
|
redis.call(
|
|
'ZADD',
|
|
bidRoundDeadlineQueueKey,
|
|
currentRedisTimestampMS + bidRoundExpiryRetryDelayMS,
|
|
matchId
|
|
)
|
|
|
|
return {
|
|
'OK',
|
|
bidRoundExpiryDecision,
|
|
auctionStateTransitionId,
|
|
tostring(revision),
|
|
tostring(currentRedisTimestampMS)
|
|
}
|
|
`,
|
|
);
|