385 lines
11 KiB
TypeScript
385 lines
11 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import {
|
|
type AuctionApplyTransitionScriptInput,
|
|
buildAuctionApplyTransitionScriptInvocation,
|
|
} from './auction-redis.apply-transition';
|
|
import {
|
|
type AuctionBidScriptInput,
|
|
buildAuctionBidScriptInvocation,
|
|
} from './auction-redis.bid';
|
|
import {
|
|
type AuctionExpirePrepareScriptInput,
|
|
buildAuctionExpirePrepareScriptInvocation,
|
|
} from './auction-redis.expire-prepare';
|
|
import { buildAuctionRedisKeys } from './auction-redis.keys';
|
|
import { AuctionRedisService } from './auction-redis.service';
|
|
import {
|
|
type AuctionReserveActionRequestScriptInput,
|
|
buildAuctionReserveActionRequestScriptInvocation,
|
|
} from './auction-redis.reserve-action';
|
|
import {
|
|
type HydrateAuctionStateScriptInput,
|
|
buildHydrateAuctionStateScriptInvocation,
|
|
} from './auction-redis.state';
|
|
|
|
export type AuctionBidCommandResult =
|
|
| {
|
|
auctionBidHistoryEntry: Record<string, unknown>;
|
|
bidRequestId: string;
|
|
currentBidRoundDeadlineTimestampMS: number;
|
|
currentHighestBidderTeamIndex: number;
|
|
currentHighestBidPointAmount: number;
|
|
currentServerTimestampMS: number;
|
|
isReplay?: boolean;
|
|
ok: true;
|
|
revision: number;
|
|
}
|
|
| {
|
|
bidRequestId: string;
|
|
code: string;
|
|
currentBidRoundDeadlineTimestampMS?: number;
|
|
currentServerTimestampMS: number;
|
|
isReplay?: boolean;
|
|
rejected: true;
|
|
};
|
|
|
|
export type AuctionReserveActionRequestCommandResult =
|
|
| {
|
|
actionRequestScopeKey: string;
|
|
auctionStateTransitionId: string;
|
|
currentServerTimestampMS: number;
|
|
newAuctionLifecycleId: string;
|
|
observedProcessingLeaseVersion: number;
|
|
reserved: true;
|
|
wasLeaseExpired: boolean;
|
|
}
|
|
| {
|
|
actionRequestScopeKey: string;
|
|
code: string;
|
|
currentServerTimestampMS: number;
|
|
rejected: true;
|
|
}
|
|
| {
|
|
actionRequestScopeKey: string;
|
|
currentServerTimestampMS: number;
|
|
isReplay: true;
|
|
requiresRefetch?: boolean;
|
|
replayStatus: string;
|
|
responseJson?: string;
|
|
revision?: number;
|
|
};
|
|
|
|
export type AuctionApplyTransitionCommandResult =
|
|
| {
|
|
auctionStateTransitionId: string;
|
|
currentServerTimestampMS: number;
|
|
ok: true;
|
|
revision: number;
|
|
}
|
|
| {
|
|
auctionStateTransitionId: string;
|
|
currentServerTimestampMS: number;
|
|
isReplay: true;
|
|
replayStatus: string;
|
|
}
|
|
| {
|
|
auctionStateTransitionId: string;
|
|
code: string;
|
|
currentServerTimestampMS: number;
|
|
rejected: true;
|
|
};
|
|
|
|
export type AuctionExpirePrepareCommandResult =
|
|
| {
|
|
auctionStateTransitionId: string;
|
|
bidRoundExpiryDecision: 'passed' | 'sold';
|
|
currentServerTimestampMS: number;
|
|
isReplay?: boolean;
|
|
ok: true;
|
|
revision: number;
|
|
}
|
|
| {
|
|
code: string;
|
|
currentServerTimestampMS: number;
|
|
noop: true;
|
|
};
|
|
|
|
export type AuctionHydrateStateCommandResult =
|
|
| {
|
|
currentServerTimestampMS: number;
|
|
hydrated: true;
|
|
revision: number;
|
|
}
|
|
| {
|
|
currentServerTimestampMS: number;
|
|
noop: true;
|
|
revision: number;
|
|
}
|
|
| {
|
|
code: string;
|
|
currentServerTimestampMS: number;
|
|
rejected: true;
|
|
};
|
|
|
|
@Injectable()
|
|
export class AuctionRedisCommandService {
|
|
constructor(private readonly auctionRedisService: AuctionRedisService) {}
|
|
|
|
public async placeBid(
|
|
matchId: string,
|
|
input: AuctionBidScriptInput,
|
|
): Promise<AuctionBidCommandResult> {
|
|
const invocation = buildAuctionBidScriptInvocation(
|
|
buildAuctionRedisKeys(matchId),
|
|
input,
|
|
);
|
|
const reply = await this.auctionRedisService.evalScript<unknown[]>(
|
|
invocation.script,
|
|
invocation.keys,
|
|
invocation.values,
|
|
);
|
|
return parseBidCommandReply(reply);
|
|
}
|
|
|
|
public async reserveActionRequest(
|
|
matchId: string,
|
|
input: AuctionReserveActionRequestScriptInput,
|
|
): Promise<AuctionReserveActionRequestCommandResult> {
|
|
const invocation = buildAuctionReserveActionRequestScriptInvocation(
|
|
buildAuctionRedisKeys(matchId),
|
|
input,
|
|
);
|
|
const reply = await this.auctionRedisService.evalScript<unknown[]>(
|
|
invocation.script,
|
|
invocation.keys,
|
|
invocation.values,
|
|
);
|
|
return parseReserveActionRequestReply(reply);
|
|
}
|
|
|
|
public async applyTransition(
|
|
matchId: string,
|
|
input: AuctionApplyTransitionScriptInput,
|
|
): Promise<AuctionApplyTransitionCommandResult> {
|
|
const invocation = buildAuctionApplyTransitionScriptInvocation(
|
|
buildAuctionRedisKeys(matchId),
|
|
input,
|
|
);
|
|
const reply = await this.auctionRedisService.evalScript<unknown[]>(
|
|
invocation.script,
|
|
invocation.keys,
|
|
invocation.values,
|
|
);
|
|
return parseApplyTransitionReply(reply);
|
|
}
|
|
|
|
public async expirePrepare(
|
|
matchId: string,
|
|
input: AuctionExpirePrepareScriptInput,
|
|
): Promise<AuctionExpirePrepareCommandResult> {
|
|
const invocation = buildAuctionExpirePrepareScriptInvocation(
|
|
buildAuctionRedisKeys(matchId),
|
|
input,
|
|
);
|
|
const reply = await this.auctionRedisService.evalScript<unknown[]>(
|
|
invocation.script,
|
|
invocation.keys,
|
|
invocation.values,
|
|
);
|
|
return parseExpirePrepareReply(reply);
|
|
}
|
|
|
|
public async hydrateState(
|
|
matchId: string,
|
|
input: HydrateAuctionStateScriptInput,
|
|
): Promise<AuctionHydrateStateCommandResult> {
|
|
const invocation = buildHydrateAuctionStateScriptInvocation(
|
|
buildAuctionRedisKeys(matchId),
|
|
input,
|
|
);
|
|
const reply = await this.auctionRedisService.evalScript<unknown[]>(
|
|
invocation.script,
|
|
invocation.keys,
|
|
invocation.values,
|
|
);
|
|
return parseHydrateStateReply(reply);
|
|
}
|
|
}
|
|
|
|
function parseBidCommandReply(reply: unknown[]): AuctionBidCommandResult {
|
|
const values = reply.map(String);
|
|
const kind = values[0];
|
|
if (kind === 'OK') {
|
|
return {
|
|
auctionBidHistoryEntry: JSON.parse(values[7] || '{}') as Record<
|
|
string,
|
|
unknown
|
|
>,
|
|
bidRequestId: values[1],
|
|
currentBidRoundDeadlineTimestampMS: numberValue(values[6]),
|
|
currentHighestBidderTeamIndex: numberValue(values[5]),
|
|
currentHighestBidPointAmount: numberValue(values[4]),
|
|
currentServerTimestampMS: numberValue(values[3]),
|
|
ok: true,
|
|
revision: numberValue(values[2]),
|
|
};
|
|
}
|
|
if (kind === 'REPLAY') {
|
|
const status = values[1];
|
|
if (status === 'ok') {
|
|
return {
|
|
auctionBidHistoryEntry: JSON.parse(values[8] || '{}') as Record<
|
|
string,
|
|
unknown
|
|
>,
|
|
bidRequestId: values[2],
|
|
currentBidRoundDeadlineTimestampMS: numberValue(values[7]),
|
|
currentHighestBidderTeamIndex: numberValue(values[6]),
|
|
currentHighestBidPointAmount: numberValue(values[5]),
|
|
currentServerTimestampMS: numberValue(values[9]),
|
|
isReplay: true,
|
|
ok: true,
|
|
revision: numberValue(values[3]),
|
|
};
|
|
}
|
|
return {
|
|
bidRequestId: values[2],
|
|
code: values[4] || status,
|
|
currentBidRoundDeadlineTimestampMS: optionalNumberValue(values[7]),
|
|
currentServerTimestampMS: numberValue(values[9]),
|
|
isReplay: true,
|
|
rejected: true,
|
|
};
|
|
}
|
|
return {
|
|
bidRequestId: values[2] ?? '',
|
|
code: values[1] ?? 'UNKNOWN',
|
|
currentBidRoundDeadlineTimestampMS: optionalNumberValue(values[4]),
|
|
currentServerTimestampMS: numberValue(values[3]),
|
|
rejected: true,
|
|
};
|
|
}
|
|
|
|
function parseReserveActionRequestReply(
|
|
reply: unknown[],
|
|
): AuctionReserveActionRequestCommandResult {
|
|
const values = reply.map(String);
|
|
const kind = values[0];
|
|
if (kind === 'RESERVED') {
|
|
return {
|
|
actionRequestScopeKey: values[1],
|
|
auctionStateTransitionId: values[2],
|
|
currentServerTimestampMS: numberValue(values[6]),
|
|
newAuctionLifecycleId: values[3],
|
|
observedProcessingLeaseVersion: numberValue(values[4]),
|
|
reserved: true,
|
|
wasLeaseExpired: values[5] === '1',
|
|
};
|
|
}
|
|
if (kind === 'REPLAY') {
|
|
return {
|
|
actionRequestScopeKey: values[2],
|
|
currentServerTimestampMS: numberValue(values[8]),
|
|
isReplay: true,
|
|
requiresRefetch: values[10] === '1' || undefined,
|
|
replayStatus: values[1],
|
|
responseJson: values[9] || undefined,
|
|
revision: values[3] ? numberValue(values[3]) : undefined,
|
|
};
|
|
}
|
|
return {
|
|
actionRequestScopeKey: values[2] ?? '',
|
|
code: values[1] ?? 'UNKNOWN',
|
|
currentServerTimestampMS: numberValue(values[3]),
|
|
rejected: true,
|
|
};
|
|
}
|
|
|
|
function parseApplyTransitionReply(
|
|
reply: unknown[],
|
|
): AuctionApplyTransitionCommandResult {
|
|
const values = reply.map(String);
|
|
const kind = values[0];
|
|
if (kind === 'OK') {
|
|
return {
|
|
auctionStateTransitionId: values[1],
|
|
currentServerTimestampMS: numberValue(values[3]),
|
|
ok: true,
|
|
revision: numberValue(values[2]),
|
|
};
|
|
}
|
|
if (kind === 'REPLAY') {
|
|
return {
|
|
auctionStateTransitionId: values[2],
|
|
currentServerTimestampMS: numberValue(values[3]),
|
|
isReplay: true,
|
|
replayStatus: values[1],
|
|
};
|
|
}
|
|
return {
|
|
auctionStateTransitionId: values[2] ?? '',
|
|
code: values[1] ?? 'UNKNOWN',
|
|
currentServerTimestampMS: numberValue(values[3]),
|
|
rejected: true,
|
|
};
|
|
}
|
|
|
|
function parseExpirePrepareReply(
|
|
reply: unknown[],
|
|
): AuctionExpirePrepareCommandResult {
|
|
const values = reply.map(String);
|
|
const kind = values[0];
|
|
if (kind === 'OK' || kind === 'REPLAY') {
|
|
return {
|
|
auctionStateTransitionId: values[2],
|
|
bidRoundExpiryDecision:
|
|
values[1] === 'sold' || values[1] === 'passed' ? values[1] : 'passed',
|
|
currentServerTimestampMS: numberValue(values[4]),
|
|
isReplay: kind === 'REPLAY' || undefined,
|
|
ok: true,
|
|
revision: numberValue(values[3]),
|
|
};
|
|
}
|
|
return {
|
|
code: values[1] ?? 'UNKNOWN',
|
|
currentServerTimestampMS: numberValue(values[2]),
|
|
noop: true,
|
|
};
|
|
}
|
|
|
|
function parseHydrateStateReply(
|
|
reply: unknown[],
|
|
): AuctionHydrateStateCommandResult {
|
|
const values = reply.map(String);
|
|
if (values[0] === 'OK') {
|
|
return {
|
|
currentServerTimestampMS: numberValue(values[2]),
|
|
hydrated: true,
|
|
revision: numberValue(values[1]),
|
|
};
|
|
}
|
|
if (values[0] === 'REJECTED') {
|
|
return {
|
|
code: values[1] ?? 'UNKNOWN',
|
|
currentServerTimestampMS: numberValue(values[2]),
|
|
rejected: true,
|
|
};
|
|
}
|
|
return {
|
|
currentServerTimestampMS: numberValue(values[2]),
|
|
noop: true,
|
|
revision: numberValue(values[1]),
|
|
};
|
|
}
|
|
|
|
function numberValue(value: string | undefined): number {
|
|
const parsed = Number(value);
|
|
return Number.isFinite(parsed) ? parsed : 0;
|
|
}
|
|
|
|
function optionalNumberValue(value: string | undefined): number | undefined {
|
|
if (value === undefined || value === '') return undefined;
|
|
const parsed = Number(value);
|
|
return Number.isFinite(parsed) ? parsed : undefined;
|
|
}
|