feat: redesign auction engine with redis sot
This commit is contained in:
1 parent
737702d78c
commit
e8bb20d988
75 files changed
+12737
-542
No files matched your search
@@ -0,0 +1,155 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { buildAuctionRedisKeys } from './auction-redis.keys';
|
||||
import { AuctionRedisService } from './auction-redis.service';
|
||||
import { assembleAuctionViewFromRedisState } from './auction-redis.state';
|
||||
|
||||
const NUMERIC_LIVE_STATE_FIELDS = new Set([
|
||||
'bidRoundExpiryPlayerOrderIndex',
|
||||
'bidRoundExpiryRevision',
|
||||
'bidRoundExpiryWinningBidPointAmount',
|
||||
'bidRoundExpiryWinningBidderTeamIndex',
|
||||
'currentAuctionHistorySequence',
|
||||
'currentAuctionPlayerOrderIndex',
|
||||
'currentBidRoundDeadlineTimestampMS',
|
||||
'currentHighestBidPointAmount',
|
||||
'currentHighestBidderTeamIndex',
|
||||
'matchSideEffectRevision',
|
||||
'revision',
|
||||
]);
|
||||
|
||||
export interface AuctionRedisLiveStateIdentity {
|
||||
auctionLifecycleId?: string;
|
||||
revision?: number;
|
||||
}
|
||||
|
||||
export interface AuctionRedisStateViewReadOptions {
|
||||
includeInternalLiveStateFields?: boolean;
|
||||
shouldForceAuctionStateResync?: boolean;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AuctionRedisStateReaderService {
|
||||
constructor(private readonly auctionRedisService: AuctionRedisService) {}
|
||||
|
||||
public async readLiveStateIdentity(
|
||||
matchId: string,
|
||||
): Promise<AuctionRedisLiveStateIdentity | null> {
|
||||
const keys = buildAuctionRedisKeys(matchId);
|
||||
const reply = await this.auctionRedisService.raw().sendCommand([
|
||||
'HMGET',
|
||||
keys.liveStateKey,
|
||||
'auctionLifecycleId',
|
||||
'revision',
|
||||
]);
|
||||
if (!Array.isArray(reply)) return null;
|
||||
const auctionLifecycleId =
|
||||
typeof reply[0] === 'string' && reply[0] ? reply[0] : undefined;
|
||||
const revision =
|
||||
typeof reply[1] === 'string' || typeof reply[1] === 'number'
|
||||
? Number(reply[1])
|
||||
: Number.NaN;
|
||||
if (!auctionLifecycleId && !Number.isFinite(revision)) return null;
|
||||
return {
|
||||
auctionLifecycleId,
|
||||
revision: Number.isFinite(revision) ? revision : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
public async readAuctionStateView(
|
||||
matchId: string,
|
||||
options: AuctionRedisStateViewReadOptions = {},
|
||||
): Promise<Record<string, unknown> | null> {
|
||||
const keys = buildAuctionRedisKeys(matchId);
|
||||
const [currentServerTimestampMS, liveReply, structureStateJson, bidEntries] =
|
||||
await Promise.all([
|
||||
this.auctionRedisService.getCurrentRedisTimestampMS(),
|
||||
this.auctionRedisService.raw().sendCommand(['HGETALL', keys.liveStateKey]),
|
||||
this.auctionRedisService.raw().sendCommand(['GET', keys.structureStateKey]),
|
||||
this.auctionRedisService.raw().sendCommand([
|
||||
'LRANGE',
|
||||
keys.bidHistoryKey,
|
||||
'0',
|
||||
'-1',
|
||||
]),
|
||||
]);
|
||||
|
||||
const liveState = parseHashReply(liveReply, {
|
||||
includeInternalLiveStateFields:
|
||||
options.includeInternalLiveStateFields ?? false,
|
||||
});
|
||||
if (!liveState) return null;
|
||||
if (
|
||||
typeof liveState.auctionLifecycleId !== 'string' ||
|
||||
typeof liveState.phase !== 'string' ||
|
||||
typeof liveState.revision !== 'number'
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
if (typeof structureStateJson !== 'string' || !structureStateJson) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const view = assembleAuctionViewFromRedisState({
|
||||
auctionBidHistoryEntries: Array.isArray(bidEntries)
|
||||
? bidEntries.map(String)
|
||||
: [],
|
||||
currentServerTimestampMS,
|
||||
liveState: liveState as Record<string, unknown> & {
|
||||
auctionLifecycleId: string;
|
||||
phase: string;
|
||||
revision: number;
|
||||
},
|
||||
shouldForceAuctionStateResync: options.shouldForceAuctionStateResync,
|
||||
structureState: JSON.parse(structureStateJson) as Record<string, unknown>,
|
||||
});
|
||||
if (options.includeInternalLiveStateFields) {
|
||||
view.redisInternalPhase = liveState.phase;
|
||||
}
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
function parseHashReply(
|
||||
reply: unknown,
|
||||
options: { includeInternalLiveStateFields?: boolean } = {},
|
||||
): Record<string, unknown> | null {
|
||||
if (!reply) return null;
|
||||
if (Array.isArray(reply)) {
|
||||
if (reply.length === 0) return null;
|
||||
const entries: [string, unknown][] = [];
|
||||
for (let index = 0; index < reply.length; index += 2) {
|
||||
entries.push([String(reply[index]), reply[index + 1]]);
|
||||
}
|
||||
return parseHashEntries(entries, options);
|
||||
}
|
||||
if (typeof reply === 'object') {
|
||||
const entries = Object.entries(reply as Record<string, unknown>);
|
||||
if (entries.length === 0) return null;
|
||||
return parseHashEntries(entries, options);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseHashEntries(
|
||||
entries: [string, unknown][],
|
||||
options: { includeInternalLiveStateFields?: boolean } = {},
|
||||
): Record<string, unknown> {
|
||||
const result: Record<string, unknown> = {};
|
||||
for (const [field, rawValue] of entries) {
|
||||
if (!options.includeInternalLiveStateFields && isInternalLiveStateField(field)) {
|
||||
continue;
|
||||
}
|
||||
const value = String(rawValue);
|
||||
if (NUMERIC_LIVE_STATE_FIELDS.has(field)) {
|
||||
const parsed = Number(value);
|
||||
result[field] = Number.isFinite(parsed) ? parsed : 0;
|
||||
continue;
|
||||
}
|
||||
result[field] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function isInternalLiveStateField(field: string): boolean {
|
||||
return field.startsWith('team:') || field.startsWith('bidRoundExpiry');
|
||||
}
|
||||
Reference in new issue
Block a user