시간 도메인과 정지 중 메시지·베팅 경계 정리
This commit is contained in:
@@ -60,27 +60,27 @@ export const hasAuctionClosePassed = (
|
||||
auction: { closeAt: Date; closeTick: bigint | null },
|
||||
now: Date,
|
||||
nowTick: number | null
|
||||
): boolean =>
|
||||
auction.closeTick !== null && nowTick !== null
|
||||
? auction.closeTick < BigInt(nowTick)
|
||||
: auction.closeAt.getTime() < now.getTime();
|
||||
): boolean => {
|
||||
void now;
|
||||
return auction.closeTick === null || nowTick === null || auction.closeTick < BigInt(nowTick);
|
||||
};
|
||||
|
||||
export const resolveAuctionBidTiming = (
|
||||
world: Pick<InMemoryTurnWorld, 'dateToGameTick' | 'gameTickToDate'>,
|
||||
processingNow: Date,
|
||||
acceptedGameTick?: number
|
||||
): { bidAt: Date; bidTick: number } =>
|
||||
acceptedGameTick === undefined
|
||||
? { bidAt: processingNow, bidTick: world.dateToGameTick(processingNow) }
|
||||
: { bidAt: world.gameTickToDate(acceptedGameTick), bidTick: acceptedGameTick };
|
||||
world: Pick<InMemoryTurnWorld, 'gameTickToDate'>,
|
||||
processingGameTick: number
|
||||
): { bidAt: Date; bidTick: number } => {
|
||||
if (!Number.isSafeInteger(processingGameTick)) {
|
||||
throw new Error('Auction bid requires an authoritative daemon processing game tick.');
|
||||
}
|
||||
return { bidAt: world.gameTickToDate(processingGameTick), bidTick: processingGameTick };
|
||||
};
|
||||
|
||||
export const hasAuctionBidClosePassed = (
|
||||
auction: { closeAt: Date; closeTick: bigint | null },
|
||||
world: Pick<InMemoryTurnWorld, 'dateToGameTick' | 'gameTickToDate'>,
|
||||
processingNow: Date,
|
||||
acceptedGameTick?: number
|
||||
world: Pick<InMemoryTurnWorld, 'gameTickToDate'>,
|
||||
processingGameTick: number
|
||||
): boolean => {
|
||||
const { bidAt, bidTick } = resolveAuctionBidTiming(world, processingNow, acceptedGameTick);
|
||||
const { bidAt, bidTick } = resolveAuctionBidTiming(world, processingGameTick);
|
||||
return hasAuctionClosePassed(auction, bidAt, bidTick);
|
||||
};
|
||||
|
||||
@@ -268,15 +268,15 @@ export const createAuctionBidder = async (options: {
|
||||
reason: '경매가 종료되었습니다.',
|
||||
};
|
||||
}
|
||||
const processingNow = world.getGameNow(new Date());
|
||||
const convertedProcessingTick = Reflect.get(command, 'processingGameTick');
|
||||
const { bidAt, bidTick } = resolveAuctionBidTiming(
|
||||
world,
|
||||
processingNow,
|
||||
typeof convertedProcessingTick === 'number' && Number.isSafeInteger(convertedProcessingTick)
|
||||
? convertedProcessingTick
|
||||
: command.acceptedGameTick
|
||||
);
|
||||
if (typeof convertedProcessingTick !== 'number' || !Number.isSafeInteger(convertedProcessingTick)) {
|
||||
throw new Error('auctionBid requires an authoritative daemon processing game tick.');
|
||||
}
|
||||
const requestedAtWall = Reflect.get(command, 'requestedAtWall');
|
||||
if (!(requestedAtWall instanceof Date) || Number.isNaN(requestedAtWall.getTime())) {
|
||||
throw new Error('auctionBid requires its durable input-event wall occurrence.');
|
||||
}
|
||||
const { bidAt, bidTick } = resolveAuctionBidTiming(world, convertedProcessingTick);
|
||||
if (hasAuctionClosePassed(auction, bidAt, bidTick)) {
|
||||
return {
|
||||
type: 'auctionBid',
|
||||
@@ -510,13 +510,24 @@ export const createAuctionBidder = async (options: {
|
||||
const persistBid = async (tx: GamePrisma.TransactionClient): Promise<void> => {
|
||||
await tx.$executeRaw(
|
||||
GamePrisma.sql`
|
||||
INSERT INTO auction_bid (auction_id, general_id, amount, event_id, event_at, meta)
|
||||
INSERT INTO auction_bid (
|
||||
auction_id,
|
||||
general_id,
|
||||
amount,
|
||||
event_id,
|
||||
event_at,
|
||||
occurred_game_tick,
|
||||
requested_at_wall,
|
||||
meta
|
||||
)
|
||||
VALUES (
|
||||
${command.auctionId},
|
||||
${command.generalId},
|
||||
${command.amount},
|
||||
${eventId},
|
||||
${eventAt},
|
||||
${BigInt(bidTick)},
|
||||
${requestedAtWall},
|
||||
${JSON.stringify({
|
||||
tryExtendCloseDate: command.tryExtendCloseDate ?? true,
|
||||
...(auction.type === 'UNIQUE_ITEM'
|
||||
@@ -536,7 +547,7 @@ export const createAuctionBidder = async (options: {
|
||||
close_tick = ${BigInt(world.dateToGameTick(nextCloseAt))},
|
||||
latest_event_id = ${eventId},
|
||||
latest_event_at = ${eventAt},
|
||||
updated_at = ${eventAt}
|
||||
updated_at = CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
|
||||
WHERE id = ${command.auctionId}
|
||||
AND status = 'OPEN'
|
||||
AND latest_event_id = ${auction.latestEventId}
|
||||
@@ -556,7 +567,7 @@ export const createAuctionBidder = async (options: {
|
||||
GamePrisma.sql`
|
||||
UPDATE inheritance_point
|
||||
SET value = value - ${morePoint},
|
||||
updated_at = ${eventAt}
|
||||
updated_at = CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
|
||||
WHERE user_id = ${userId}
|
||||
AND key = 'previous'
|
||||
AND value >= ${morePoint}
|
||||
@@ -587,11 +598,16 @@ export const createAuctionBidder = async (options: {
|
||||
await tx.$executeRaw(
|
||||
GamePrisma.sql`
|
||||
INSERT INTO inheritance_point (user_id, key, value, updated_at)
|
||||
VALUES (${prevUserId}, 'previous', ${highestBid.amount}, ${eventAt})
|
||||
VALUES (
|
||||
${prevUserId},
|
||||
'previous',
|
||||
${highestBid.amount},
|
||||
CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
|
||||
)
|
||||
ON CONFLICT (user_id, key)
|
||||
DO UPDATE SET
|
||||
value = inheritance_point.value + EXCLUDED.value,
|
||||
updated_at = EXCLUDED.updated_at
|
||||
updated_at = CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
|
||||
`
|
||||
);
|
||||
await tx.$executeRaw(
|
||||
@@ -711,6 +727,7 @@ export const createAuctionBidder = async (options: {
|
||||
ok: true,
|
||||
auctionId: command.auctionId,
|
||||
closeAt: nextCloseAt.toISOString(),
|
||||
closeTick: world.dateToGameTick(nextCloseAt),
|
||||
};
|
||||
},
|
||||
close: async (): Promise<void> => {
|
||||
|
||||
@@ -91,21 +91,17 @@ export const isAuctionFinalizeGenerationCurrent = (
|
||||
auction: Pick<AuctionRow, 'closeAt' | 'closeTick'>,
|
||||
command: Pick<Extract<TurnDaemonCommand, { type: 'auctionFinalize' }>, 'expectedCloseAt' | 'expectedCloseTick'>
|
||||
): boolean => {
|
||||
if (command.expectedCloseTick !== undefined) {
|
||||
return auction.closeTick !== null && auction.closeTick === BigInt(command.expectedCloseTick);
|
||||
}
|
||||
if (command.expectedCloseAt !== undefined) {
|
||||
return auction.closeAt.getTime() === new Date(command.expectedCloseAt).getTime();
|
||||
}
|
||||
return true;
|
||||
return auction.closeTick !== null && auction.closeTick === BigInt(command.expectedCloseTick);
|
||||
};
|
||||
|
||||
export const hasAuctionFinalizeDeadlineArrived = (
|
||||
auction: Pick<AuctionRow, 'closeAt' | 'closeTick'>,
|
||||
now: Date,
|
||||
nowTick: number
|
||||
): boolean =>
|
||||
auction.closeTick === null ? auction.closeAt.getTime() <= now.getTime() : auction.closeTick <= BigInt(nowTick);
|
||||
): boolean => {
|
||||
void now;
|
||||
return auction.closeTick !== null && auction.closeTick <= BigInt(nowTick);
|
||||
};
|
||||
|
||||
export const buildAuctionBidderSystemMessage = (options: {
|
||||
bidder: TurnGeneral;
|
||||
@@ -293,7 +289,11 @@ export const createAuctionFinalizer = async (options: {
|
||||
return { type: 'auctionFinalize', ok: true, auctionId };
|
||||
}
|
||||
|
||||
const now = world.getGameNow(new Date());
|
||||
const processingGameTick = Reflect.get(command, 'processingGameTick');
|
||||
if (typeof processingGameTick !== 'number' || !Number.isSafeInteger(processingGameTick)) {
|
||||
throw new Error('auctionFinalize requires an authoritative daemon processing game tick.');
|
||||
}
|
||||
const now = world.gameTickToDate(processingGameTick);
|
||||
if (auction.status === 'OPEN') {
|
||||
if (!isAuctionFinalizeGenerationCurrent(auction, command)) {
|
||||
return {
|
||||
@@ -303,8 +303,7 @@ export const createAuctionFinalizer = async (options: {
|
||||
reason: '경매 마감 세대가 변경되었습니다.',
|
||||
};
|
||||
}
|
||||
const nowTick = world.dateToGameTick(now);
|
||||
if (!hasAuctionFinalizeDeadlineArrived(auction, now, nowTick)) {
|
||||
if (!hasAuctionFinalizeDeadlineArrived(auction, now, processingGameTick)) {
|
||||
return {
|
||||
type: 'auctionFinalize',
|
||||
ok: false,
|
||||
@@ -316,8 +315,8 @@ export const createAuctionFinalizer = async (options: {
|
||||
GamePrisma.sql`
|
||||
UPDATE auction
|
||||
SET status = 'FINALIZING',
|
||||
finalizing_at = ${now},
|
||||
updated_at = ${now}
|
||||
finalizing_at = CURRENT_TIMESTAMP AT TIME ZONE 'UTC',
|
||||
updated_at = CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
|
||||
WHERE id = ${auctionId}
|
||||
AND status = 'OPEN'
|
||||
`
|
||||
@@ -364,8 +363,8 @@ export const createAuctionFinalizer = async (options: {
|
||||
GamePrisma.sql`
|
||||
UPDATE auction
|
||||
SET status = ${status},
|
||||
finished_at = ${now},
|
||||
updated_at = ${now}
|
||||
finished_at = CURRENT_TIMESTAMP AT TIME ZONE 'UTC',
|
||||
updated_at = CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
|
||||
WHERE id = ${auctionId}
|
||||
`
|
||||
);
|
||||
|
||||
@@ -94,7 +94,11 @@ const openResourceAuction = async (
|
||||
return fail(`기본 ${hostResource === 'rice' ? '쌀' : '금'} ${minimumResource}은 거래할 수 없습니다.`);
|
||||
}
|
||||
|
||||
const now = world.getGameNow(new Date());
|
||||
const processingGameTick = Reflect.get(command, 'processingGameTick');
|
||||
if (typeof processingGameTick !== 'number' || !Number.isSafeInteger(processingGameTick)) {
|
||||
throw new Error('auctionOpen requires an authoritative daemon processing game tick.');
|
||||
}
|
||||
const now = world.gameTickToDate(processingGameTick);
|
||||
const turnMinutes = Math.max(1, Math.round(world.getState().tickSeconds / 60));
|
||||
const closeAt = new Date(now.getTime() + closeTurnCnt * turnMinutes * 60_000);
|
||||
const auction = await db.auction.create({
|
||||
@@ -113,7 +117,7 @@ const openResourceAuction = async (
|
||||
},
|
||||
status: 'OPEN',
|
||||
closeAt,
|
||||
openTick: BigInt(world.dateToGameTick(now)),
|
||||
openTick: BigInt(processingGameTick),
|
||||
closeTick: BigInt(world.dateToGameTick(closeAt)),
|
||||
},
|
||||
});
|
||||
@@ -125,6 +129,7 @@ const openResourceAuction = async (
|
||||
ok: true,
|
||||
auctionId: auction.id,
|
||||
closeAt: closeAt.toISOString(),
|
||||
closeTick: world.dateToGameTick(closeAt),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -220,8 +225,16 @@ const openUniqueAuction = async (
|
||||
}
|
||||
|
||||
const state = world.getState();
|
||||
const processingGameTick = Reflect.get(command, 'processingGameTick');
|
||||
if (typeof processingGameTick !== 'number' || !Number.isSafeInteger(processingGameTick)) {
|
||||
throw new Error('auctionOpen requires an authoritative daemon processing game tick.');
|
||||
}
|
||||
const requestedAtWall = Reflect.get(command, 'requestedAtWall');
|
||||
if (!(requestedAtWall instanceof Date) || Number.isNaN(requestedAtWall.getTime())) {
|
||||
throw new Error('auctionOpen requires its durable input-event wall occurrence.');
|
||||
}
|
||||
const turnMinutes = Math.max(1, Math.round(state.tickSeconds / 60));
|
||||
const now = world.getGameNow(new Date());
|
||||
const now = world.gameTickToDate(processingGameTick);
|
||||
const closeMinutes = Math.max(MIN_AUCTION_CLOSE_MINUTES, turnMinutes * COEFF_AUCTION_CLOSE_MINUTES);
|
||||
const closeAt = new Date(now.getTime() + closeMinutes * 60_000);
|
||||
const extensionLimitMinutes = Math.max(
|
||||
@@ -253,7 +266,7 @@ const openUniqueAuction = async (
|
||||
},
|
||||
status: 'OPEN',
|
||||
closeAt,
|
||||
openTick: BigInt(world.dateToGameTick(now)),
|
||||
openTick: BigInt(processingGameTick),
|
||||
closeTick: BigInt(world.dateToGameTick(closeAt)),
|
||||
latestEventId: eventId,
|
||||
latestEventAt: now,
|
||||
@@ -263,6 +276,8 @@ const openUniqueAuction = async (
|
||||
amount: command.amount,
|
||||
eventId,
|
||||
eventAt: now,
|
||||
occurredGameTick: BigInt(processingGameTick),
|
||||
requestedAtWall,
|
||||
meta: buildInitialUniqueAuctionBidMeta(alias, command.amount),
|
||||
},
|
||||
},
|
||||
@@ -308,6 +323,7 @@ const openUniqueAuction = async (
|
||||
ok: true,
|
||||
auctionId: auction.id,
|
||||
closeAt: closeAt.toISOString(),
|
||||
closeTick: world.dateToGameTick(closeAt),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user