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
@@ -153,14 +153,42 @@ export const AUCTION_TOP_TIER_THRESHOLDS = [
|
||||
] as const;
|
||||
export type AuctionTopTierThreshold =
|
||||
(typeof AUCTION_TOP_TIER_THRESHOLDS)[number];
|
||||
export type AuctionActionType =
|
||||
| 'start'
|
||||
| 'open-bidding'
|
||||
| 'bid'
|
||||
| 'sold'
|
||||
| 'passed'
|
||||
| 'next'
|
||||
| 'complete';
|
||||
export const AUCTION_ACTION_TYPES = [
|
||||
'start',
|
||||
'open-bidding',
|
||||
'bid',
|
||||
'sold',
|
||||
'passed',
|
||||
'next',
|
||||
'complete',
|
||||
'reset',
|
||||
'undo',
|
||||
] as const;
|
||||
export type AuctionActionType = (typeof AUCTION_ACTION_TYPES)[number];
|
||||
export const AUCTION_ENGINE_VERSIONS = ['redis-v1', 'legacy-renamed'] as const;
|
||||
export type AuctionEngineVersion = (typeof AUCTION_ENGINE_VERSIONS)[number];
|
||||
export const AUCTION_MATCH_SIDE_EFFECT_STATUSES = [
|
||||
'none',
|
||||
'pending',
|
||||
'processing',
|
||||
'completed',
|
||||
'failed',
|
||||
] as const;
|
||||
export type AuctionMatchSideEffectStatus =
|
||||
(typeof AUCTION_MATCH_SIDE_EFFECT_STATUSES)[number];
|
||||
export const AUCTION_SIDE_EFFECT_APPLICATION_STATES = [
|
||||
'pending',
|
||||
'processing',
|
||||
'completed',
|
||||
'failed',
|
||||
] as const;
|
||||
export type AuctionSideEffectApplicationState =
|
||||
(typeof AUCTION_SIDE_EFFECT_APPLICATION_STATES)[number];
|
||||
|
||||
export type AuctionHistoryMutation =
|
||||
| { type: 'append' }
|
||||
| { targetHistorySequence: number; type: 'deleteAfterSequenceAndAppendUndo' }
|
||||
| { resetMode: 'clear-history' | 'preserve-audit'; type: 'reset' };
|
||||
|
||||
export interface MongooseTimestamps {
|
||||
createdAt: Date;
|
||||
@@ -616,6 +644,20 @@ export interface AuctionTeam {
|
||||
members: AuctionTeamMember[];
|
||||
}
|
||||
|
||||
export interface RedisAuctionTeamMember {
|
||||
userId: mongoose.Types.ObjectId;
|
||||
assignedPosition: Position;
|
||||
memberAcquisitionBidPointAmount: number;
|
||||
}
|
||||
|
||||
export interface RedisAuctionTeam {
|
||||
teamIndex: number;
|
||||
name?: string;
|
||||
captainUserId: mongoose.Types.ObjectId;
|
||||
remainingBidPointAmount: number;
|
||||
members: RedisAuctionTeamMember[];
|
||||
}
|
||||
|
||||
export interface AuctionBid {
|
||||
teamIndex: number;
|
||||
amount: number;
|
||||
@@ -623,6 +665,13 @@ export interface AuctionBid {
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
export interface AuctionBidHistoryEntry {
|
||||
bidderTeamIndex: number;
|
||||
bidPointAmount: number;
|
||||
bidderUserId: mongoose.Types.ObjectId;
|
||||
bidPlacedAtTimestampMS: number;
|
||||
}
|
||||
|
||||
export interface AuctionChatMessage {
|
||||
id: string;
|
||||
userId?: mongoose.Types.ObjectId;
|
||||
@@ -634,6 +683,17 @@ export interface AuctionChatMessage {
|
||||
export interface Auction extends MongooseTimestamps {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
phase: AuctionPhase;
|
||||
revision: number;
|
||||
auctionEngineVersion?: AuctionEngineVersion;
|
||||
auctionLifecycleId?: string;
|
||||
lastAppliedAuctionStateTransitionId?: string;
|
||||
currentAuctionHistorySequence: number;
|
||||
startAuctionRequestId?: string;
|
||||
startAuctionStateTransitionId?: string;
|
||||
auctionStartedByUserId?: mongoose.Types.ObjectId;
|
||||
matchSideEffectStatus: AuctionMatchSideEffectStatus;
|
||||
matchSideEffectRevision: number;
|
||||
matchSideEffectError?: string;
|
||||
teams: AuctionTeam[];
|
||||
order: AuctionPlayerSnapshot[];
|
||||
passedQueue: AuctionPlayerSnapshot[];
|
||||
@@ -651,6 +711,22 @@ export interface Auction extends MongooseTimestamps {
|
||||
chatMessages: AuctionChatMessage[];
|
||||
startedAt?: Date;
|
||||
completedAt?: Date;
|
||||
auctionTeams?: RedisAuctionTeam[];
|
||||
auctionPlayerOrder?: AuctionPlayerSnapshot[];
|
||||
passedAuctionPlayerQueue?: AuctionPlayerSnapshot[];
|
||||
currentAuctionRound?: number;
|
||||
currentAuctionPlayerOrderIndex?: number;
|
||||
auctionBidHistory?: AuctionBidHistoryEntry[];
|
||||
currentHighestBidPointAmount?: number;
|
||||
currentHighestBidderTeamIndex?: number;
|
||||
currentBidRoundDeadlineTimestampMS?: number;
|
||||
maximumBidRoundDurationSeconds?: number;
|
||||
bidRoundExtensionSeconds?: number;
|
||||
initialTeamBidPointAmount?: number;
|
||||
topTierAuctionPriorityThreshold?: AuctionTopTierThreshold;
|
||||
manualTopTierAuctionPriorityUserIds?: mongoose.Types.ObjectId[];
|
||||
auctionStartedAtTimestampMS?: number;
|
||||
auctionCompletedAtTimestampMS?: number;
|
||||
}
|
||||
|
||||
export type AuctionDocument = HydratedDocument<Auction>;
|
||||
@@ -660,13 +736,38 @@ export interface AuctionHistoryEntry extends MongooseTimestamps {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
sequence: number;
|
||||
action: AuctionActionType;
|
||||
auctionStateTransitionId?: string;
|
||||
auctionLifecycleId?: string;
|
||||
revision?: number;
|
||||
actorUserId?: mongoose.Types.ObjectId;
|
||||
snapshotBefore: Record<string, unknown>;
|
||||
historyMutation?: AuctionHistoryMutation;
|
||||
matchSideEffectMutation?: Record<string, unknown>;
|
||||
takenAt: Date;
|
||||
}
|
||||
|
||||
export type AuctionHistoryEntryDocument = HydratedDocument<AuctionHistoryEntry>;
|
||||
|
||||
export interface AuctionSideEffectApplication extends MongooseTimestamps {
|
||||
auctionId: mongoose.Types.ObjectId;
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
auctionStateTransitionId: string;
|
||||
auctionLifecycleId: string;
|
||||
sideEffectType: string;
|
||||
state: AuctionSideEffectApplicationState;
|
||||
payload: Record<string, unknown>;
|
||||
payloadChecksum: string;
|
||||
attempts: number;
|
||||
processingStartedAtTimestampMS?: number;
|
||||
processingLeaseExpiresAtTimestampMS?: number;
|
||||
processingOwnerToken?: string;
|
||||
completedAtTimestampMS?: number;
|
||||
lastError?: string;
|
||||
}
|
||||
|
||||
export type AuctionSideEffectApplicationDocument =
|
||||
HydratedDocument<AuctionSideEffectApplication>;
|
||||
|
||||
// ===== MatchStats (precomputed internal stats fragment) =====
|
||||
export interface MatchStatsRuneStyleSelection {
|
||||
styleId: number;
|
||||
@@ -1646,6 +1747,34 @@ const AuctionTeamSubSchema = new Schema<AuctionTeam>(
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
const RedisAuctionTeamMemberSubSchema = new Schema<RedisAuctionTeamMember>(
|
||||
{
|
||||
userId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
||||
assignedPosition: { type: String, enum: POSITIONS, required: true },
|
||||
memberAcquisitionBidPointAmount: {
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 0,
|
||||
},
|
||||
},
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
const RedisAuctionTeamSubSchema = new Schema<RedisAuctionTeam>(
|
||||
{
|
||||
teamIndex: { type: Number, required: true, min: 1 },
|
||||
name: { type: String, required: false },
|
||||
captainUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
||||
remainingBidPointAmount: { type: Number, required: true, default: 0 },
|
||||
members: {
|
||||
type: [RedisAuctionTeamMemberSubSchema],
|
||||
required: true,
|
||||
default: [],
|
||||
},
|
||||
},
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
const AuctionBidSubSchema = new Schema<AuctionBid>(
|
||||
{
|
||||
teamIndex: { type: Number, required: true, min: 1 },
|
||||
@@ -1656,6 +1785,16 @@ const AuctionBidSubSchema = new Schema<AuctionBid>(
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
const AuctionBidHistoryEntrySubSchema = new Schema<AuctionBidHistoryEntry>(
|
||||
{
|
||||
bidderTeamIndex: { type: Number, required: true, min: 1 },
|
||||
bidPointAmount: { type: Number, required: true },
|
||||
bidderUserId: { type: Schema.Types.ObjectId, ref: 'user', required: true },
|
||||
bidPlacedAtTimestampMS: { type: Number, required: true },
|
||||
},
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
const AuctionChatMessageSubSchema = new Schema<AuctionChatMessage>(
|
||||
{
|
||||
id: { type: String, required: true },
|
||||
@@ -1681,6 +1820,40 @@ export const AuctionSchema = new Schema<Auction>(
|
||||
required: true,
|
||||
default: 'waiting',
|
||||
},
|
||||
revision: { type: Number, required: true, default: 0, min: 0 },
|
||||
auctionEngineVersion: {
|
||||
type: String,
|
||||
enum: AUCTION_ENGINE_VERSIONS,
|
||||
required: false,
|
||||
},
|
||||
auctionLifecycleId: { type: String, required: false },
|
||||
lastAppliedAuctionStateTransitionId: { type: String, required: false },
|
||||
currentAuctionHistorySequence: {
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 0,
|
||||
min: 0,
|
||||
},
|
||||
startAuctionRequestId: { type: String, required: false },
|
||||
startAuctionStateTransitionId: { type: String, required: false },
|
||||
auctionStartedByUserId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user',
|
||||
required: false,
|
||||
},
|
||||
matchSideEffectStatus: {
|
||||
type: String,
|
||||
enum: AUCTION_MATCH_SIDE_EFFECT_STATUSES,
|
||||
required: true,
|
||||
default: 'none',
|
||||
},
|
||||
matchSideEffectRevision: {
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 0,
|
||||
min: 0,
|
||||
},
|
||||
matchSideEffectError: { type: String, required: false },
|
||||
teams: { type: [AuctionTeamSubSchema], required: true, default: [] },
|
||||
order: {
|
||||
type: [AuctionPlayerSnapshotSubSchema],
|
||||
@@ -1720,6 +1893,55 @@ export const AuctionSchema = new Schema<Auction>(
|
||||
},
|
||||
startedAt: { type: Date, required: false },
|
||||
completedAt: { type: Date, required: false },
|
||||
auctionTeams: {
|
||||
type: [RedisAuctionTeamSubSchema],
|
||||
required: false,
|
||||
default: undefined,
|
||||
},
|
||||
auctionPlayerOrder: {
|
||||
type: [AuctionPlayerSnapshotSubSchema],
|
||||
required: false,
|
||||
default: undefined,
|
||||
},
|
||||
passedAuctionPlayerQueue: {
|
||||
type: [AuctionPlayerSnapshotSubSchema],
|
||||
required: false,
|
||||
default: undefined,
|
||||
},
|
||||
currentAuctionRound: { type: Number, required: false, min: 1 },
|
||||
currentAuctionPlayerOrderIndex: { type: Number, required: false, min: 0 },
|
||||
auctionBidHistory: {
|
||||
type: [AuctionBidHistoryEntrySubSchema],
|
||||
required: false,
|
||||
default: undefined,
|
||||
},
|
||||
currentHighestBidPointAmount: { type: Number, required: false, min: 0 },
|
||||
currentHighestBidderTeamIndex: { type: Number, required: false, min: 1 },
|
||||
currentBidRoundDeadlineTimestampMS: {
|
||||
type: Number,
|
||||
required: false,
|
||||
min: 0,
|
||||
},
|
||||
maximumBidRoundDurationSeconds: {
|
||||
type: Number,
|
||||
required: false,
|
||||
min: 1,
|
||||
},
|
||||
bidRoundExtensionSeconds: { type: Number, required: false, min: 0 },
|
||||
initialTeamBidPointAmount: { type: Number, required: false, min: 0 },
|
||||
topTierAuctionPriorityThreshold: {
|
||||
type: String,
|
||||
enum: AUCTION_TOP_TIER_THRESHOLDS,
|
||||
required: false,
|
||||
},
|
||||
manualTopTierAuctionPriorityUserIds: {
|
||||
type: [Schema.Types.ObjectId],
|
||||
ref: 'user',
|
||||
required: false,
|
||||
default: undefined,
|
||||
},
|
||||
auctionStartedAtTimestampMS: { type: Number, required: false, min: 0 },
|
||||
auctionCompletedAtTimestampMS: { type: Number, required: false, min: 0 },
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
@@ -1727,7 +1949,7 @@ export const AuctionSchema = new Schema<Auction>(
|
||||
},
|
||||
);
|
||||
|
||||
const AuctionHistoryEntrySchema = new Schema<AuctionHistoryEntry>(
|
||||
export const AuctionHistoryEntrySchema = new Schema<AuctionHistoryEntry>(
|
||||
{
|
||||
auctionId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
@@ -1744,19 +1966,16 @@ const AuctionHistoryEntrySchema = new Schema<AuctionHistoryEntry>(
|
||||
sequence: { type: Number, required: true, min: 1 },
|
||||
action: {
|
||||
type: String,
|
||||
enum: [
|
||||
'start',
|
||||
'open-bidding',
|
||||
'bid',
|
||||
'sold',
|
||||
'passed',
|
||||
'next',
|
||||
'complete',
|
||||
],
|
||||
enum: AUCTION_ACTION_TYPES,
|
||||
required: true,
|
||||
},
|
||||
auctionStateTransitionId: { type: String, required: false },
|
||||
auctionLifecycleId: { type: String, required: false },
|
||||
revision: { type: Number, required: false, min: 0 },
|
||||
actorUserId: { type: Schema.Types.ObjectId, ref: 'user', required: false },
|
||||
snapshotBefore: { type: Schema.Types.Mixed, required: true },
|
||||
historyMutation: { type: Schema.Types.Mixed, required: false },
|
||||
matchSideEffectMutation: { type: Schema.Types.Mixed, required: false },
|
||||
takenAt: { type: Date, required: true, default: Date.now },
|
||||
},
|
||||
{
|
||||
@@ -1766,6 +1985,74 @@ const AuctionHistoryEntrySchema = new Schema<AuctionHistoryEntry>(
|
||||
);
|
||||
|
||||
AuctionHistoryEntrySchema.index({ auctionId: 1, sequence: -1 });
|
||||
AuctionHistoryEntrySchema.index(
|
||||
{ auctionId: 1, sequence: 1 },
|
||||
{ unique: true },
|
||||
);
|
||||
AuctionHistoryEntrySchema.index(
|
||||
{ auctionId: 1, auctionStateTransitionId: 1 },
|
||||
{ sparse: true, unique: true },
|
||||
);
|
||||
|
||||
export const AuctionSideEffectApplicationSchema =
|
||||
new Schema<AuctionSideEffectApplication>(
|
||||
{
|
||||
auctionId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'auction',
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
matchId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'match',
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
auctionStateTransitionId: { type: String, required: true },
|
||||
auctionLifecycleId: { type: String, required: true },
|
||||
sideEffectType: { type: String, required: true },
|
||||
state: {
|
||||
type: String,
|
||||
enum: AUCTION_SIDE_EFFECT_APPLICATION_STATES,
|
||||
required: true,
|
||||
default: 'pending',
|
||||
},
|
||||
payload: { type: Schema.Types.Mixed, required: true, default: {} },
|
||||
payloadChecksum: { type: String, required: true },
|
||||
attempts: { type: Number, required: true, default: 0, min: 0 },
|
||||
processingStartedAtTimestampMS: {
|
||||
type: Number,
|
||||
required: false,
|
||||
min: 0,
|
||||
},
|
||||
processingLeaseExpiresAtTimestampMS: {
|
||||
type: Number,
|
||||
required: false,
|
||||
min: 0,
|
||||
},
|
||||
processingOwnerToken: { type: String, required: false },
|
||||
completedAtTimestampMS: { type: Number, required: false, min: 0 },
|
||||
lastError: { type: String, required: false },
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
collection: 'auctionSideEffectApplications',
|
||||
},
|
||||
);
|
||||
|
||||
AuctionSideEffectApplicationSchema.index(
|
||||
{
|
||||
auctionId: 1,
|
||||
auctionStateTransitionId: 1,
|
||||
sideEffectType: 1,
|
||||
},
|
||||
{ unique: true },
|
||||
);
|
||||
AuctionSideEffectApplicationSchema.index({
|
||||
state: 1,
|
||||
processingLeaseExpiresAtTimestampMS: 1,
|
||||
});
|
||||
|
||||
export const MatchStatsSchema: mongoose.Schema<MatchStats> = new Schema(
|
||||
{
|
||||
@@ -1840,6 +2127,11 @@ export const tournamentModelDefinitions: ModelDefinition[] = [
|
||||
schema: AuctionHistoryEntrySchema,
|
||||
schemaName: 'auctionHistory',
|
||||
},
|
||||
{
|
||||
name: 'auctionSideEffectApplication',
|
||||
schema: AuctionSideEffectApplicationSchema,
|
||||
schemaName: 'auctionSideEffectApplication',
|
||||
},
|
||||
{
|
||||
name: 'matchStats',
|
||||
schema: MatchStatsSchema,
|
||||
@@ -1860,5 +2152,6 @@ export interface TournamentModelTypes {
|
||||
match: Match;
|
||||
auction: Auction;
|
||||
auctionHistory: AuctionHistoryEntry;
|
||||
auctionSideEffectApplication: AuctionSideEffectApplication;
|
||||
matchStats: MatchStats;
|
||||
}
|
||||
Reference in new issue
Block a user