fix: 지난 플레이 전투 자료를 보존한다

통일·장수 삭제·게임 취소 archive에 전투 집계와 숙련도, 전투 결과를 저장한다. 개인 기록과 전투 상세는 보존 대상에서 제외하고 기존 Core snapshot도 정규화한다.
This commit is contained in:
2026-08-22 09:15:45 +00:00
parent d74a8e48bb
commit d3d89cf003
12 changed files with 484 additions and 78 deletions
@@ -1,6 +1,8 @@
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { normalizeArchivedGeneral, type ArchivedJsonValue } from '@sammo-ts/common';
import { createGamePostgresConnector, type GamePrismaClient } from '@sammo-ts/infra';
import { LogCategory, LogScope } from '@sammo-ts/logic';
import { cancelGame } from '../src/scenario/gameCancellation.js';
@@ -62,13 +64,46 @@ integration('game cancellation transaction', () => {
userId,
name: '취소장수',
turnTime: openedAt,
meta: { inherit_spent_dyn: 4_500 },
meta: { inherit_spent_dyn: 4_500, dex1: 1, dex2: 1, dex3: 1, dex4: 1, dex5: 1 },
},
});
await db.rankData.createMany({
data: [
{ generalId, nationId: 0, type: 'inherit_spent_dyn', value: 4_500 },
{ generalId, nationId: 0, type: 'warnum', value: 10 },
{ generalId, nationId: 0, type: 'killnum', value: 6 },
{ generalId, nationId: 0, type: 'deathnum', value: 4 },
{ generalId, nationId: 0, type: 'firenum', value: 2 },
{ generalId, nationId: 0, type: 'killcrew', value: 1_000 },
{ generalId, nationId: 0, type: 'deathcrew', value: 500 },
],
});
await db.logEntry.createMany({
data: [
{
scope: LogScope.GENERAL,
category: LogCategory.ACTION,
generalId,
year: 190,
month: 7,
text: '보존하지 않을 개인 기록',
},
{
scope: LogScope.GENERAL,
category: LogCategory.BATTLE_DETAIL,
generalId,
year: 190,
month: 7,
text: '보존하지 않을 전투 기록',
},
{
scope: LogScope.GENERAL,
category: LogCategory.BATTLE_BRIEF,
generalId,
year: 190,
month: 7,
text: '보존할 전투 결과',
},
],
});
await db.inheritancePoint.createMany({
@@ -180,7 +215,7 @@ integration('game cancellation transaction', () => {
[userId]: {
openingPoint: 10_000,
currentPoint: 7_000,
earnedPoint: 1_750,
earnedPoint: 1_750.005,
retainedEarnedPoint: 700,
finalPoint: 10_700,
baselineSource: 'OPENING',
@@ -197,6 +232,23 @@ integration('game cancellation transaction', () => {
const archived = await db.oldGeneral.findMany({ where: { serverId }, orderBy: { generalNo: 'asc' } });
expect(archived).toHaveLength(2);
expect(archived.every((row) => JSON.stringify(row.data).includes(request.cancellationId))).toBe(true);
const activeArchive = archived.find((row) => row.generalNo === generalId)!;
const snapshot = normalizeArchivedGeneral(activeArchive.data as ArchivedJsonValue, activeArchive.name).snapshot;
expect(snapshot).toMatchObject({
mastery: { infantry: 1, archery: 1, cavalry: 1, special: 1, siege: 1 },
battle: {
battles: 10,
wins: 6,
losses: 4,
fireSuccesses: 2,
killedCrew: 1_000,
lostCrew: 500,
},
records: { battleResult: ['보존할 전투 결과'] },
availability: { battleResultLogs: true, battleDetailLogs: false },
});
expect(JSON.stringify(activeArchive.data)).not.toContain('보존하지 않을 개인 기록');
expect(JSON.stringify(activeArchive.data)).not.toContain('보존하지 않을 전투 기록');
await expect(db.hallOfFame.count({ where: { serverId } })).resolves.toBe(0);
await expect(db.oldNation.count({ where: { serverId } })).resolves.toBe(0);
await expect(db.emperor.count({ where: { serverId } })).resolves.toBe(0);
@@ -1,5 +1,5 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { asRecord } from '@sammo-ts/common';
import { asRecord, normalizeArchivedGeneral, type ArchivedJsonValue } from '@sammo-ts/common';
import { createGamePostgresConnector, type GamePrismaClient } from '@sammo-ts/infra';
import { LogCategory, LogScope } from '@sammo-ts/logic';
@@ -98,6 +98,10 @@ integration('general turn lifecycle persistence', () => {
inherit_active_action: 2,
inheritRandomUnique: true,
dex1: 1_000,
dex2: 1,
dex3: 1,
dex4: 1,
dex5: 1,
},
});
await db.generalAccessLog.create({
@@ -110,6 +114,10 @@ integration('general turn lifecycle persistence', () => {
data: [
{ generalId: general.id, nationId: 0, type: 'warnum', value: 2 },
{ generalId: general.id, nationId: 0, type: 'firenum', value: 1 },
{ generalId: general.id, nationId: 0, type: 'killnum', value: 1 },
{ generalId: general.id, nationId: 0, type: 'deathnum', value: 1 },
{ generalId: general.id, nationId: 0, type: 'killcrew', value: 400 },
{ generalId: general.id, nationId: 0, type: 'deathcrew', value: 300 },
],
});
await db.logEntry.createMany({
@@ -130,6 +138,30 @@ integration('general turn lifecycle persistence', () => {
generalId: general.id,
text: '<Y>●</>둘째 기록',
},
{
scope: LogScope.GENERAL,
category: LogCategory.ACTION,
year: 200,
month: 1,
generalId: general.id,
text: '보존하지 않을 개인 기록',
},
{
scope: LogScope.GENERAL,
category: LogCategory.BATTLE_DETAIL,
year: 200,
month: 1,
generalId: general.id,
text: '보존하지 않을 전투 기록',
},
{
scope: LogScope.GENERAL,
category: LogCategory.BATTLE_BRIEF,
year: 200,
month: 1,
generalId: general.id,
text: '보존할 전투 결과',
},
],
});
@@ -150,6 +182,22 @@ integration('general turn lifecycle persistence', () => {
expect(archivedData.history).toEqual(['<Y>●</>둘째 기록', '<C>●</>첫 기록']);
expect(asRecord(archivedData.meta)).not.toHaveProperty('inheritRandomUnique');
expect(asRecord(archivedData.meta)).not.toHaveProperty('inheritSpecificSpecialWar');
const snapshot = normalizeArchivedGeneral(archived.data as ArchivedJsonValue, archived.name).snapshot;
expect(snapshot).toMatchObject({
mastery: { infantry: 1_000, archery: 1, cavalry: 1, special: 1, siege: 1 },
battle: {
battles: 2,
wins: 1,
losses: 1,
fireSuccesses: 1,
killedCrew: 400,
lostCrew: 300,
},
records: { battleResult: ['보존할 전투 결과'] },
availability: { battleResultLogs: true, battleDetailLogs: false },
});
expect(JSON.stringify(archived.data)).not.toContain('보존하지 않을 개인 기록');
expect(JSON.stringify(archived.data)).not.toContain('보존하지 않을 전투 기록');
expect(
await db.inheritancePoint.findUnique({
where: { userId_key: { userId: general.userId!, key: 'previous' } },
@@ -39,6 +39,7 @@ const archivedGeneral = (): TurnGeneral => ({
triggerState: { flags: {}, counters: {}, modifiers: {}, meta: {} },
meta: {
killturn: 0,
dex1: 1_000,
inheritRandomUnique: true,
inheritSpecificSpecialWar: true,
},
@@ -55,7 +56,18 @@ describe('general lifecycle archive history', () => {
deleteMany: vi.fn(async () => ({ count: 1 })),
},
logEntry: {
findMany: vi.fn(async () => [{ text: '<Y>●</>둘째 기록' }, { text: '<C>●</>첫 기록' }]),
findMany: vi.fn(async () => [
{ category: LogCategory.BATTLE_BRIEF, text: '둘째 전투 결과' },
{ category: LogCategory.HISTORY, text: '<Y>●</>둘째 기록' },
{ category: LogCategory.BATTLE_BRIEF, text: '첫째 전투 결과' },
{ category: LogCategory.HISTORY, text: '<C>●</>첫 기록' },
]),
},
rankData: {
findMany: vi.fn(async () => [
{ type: 'warnum', value: 2 },
{ type: 'killnum', value: 1 },
]),
},
oldGeneral: { upsert },
} as unknown as GamePrisma.TransactionClient;
@@ -73,17 +85,19 @@ describe('general lifecycle archive history', () => {
where: {
generalId: general.id,
scope: LogScope.GENERAL,
category: LogCategory.HISTORY,
category: { in: [LogCategory.HISTORY, LogCategory.BATTLE_BRIEF] },
},
orderBy: { id: 'desc' },
select: { text: true },
select: { category: true, text: true },
});
expect(upsert).toHaveBeenCalledWith(
expect.objectContaining({
create: expect.objectContaining({
data: expect.objectContaining({
history: ['<Y>●</>둘째 기록', '<C>●</>첫 기록'],
meta: { killturn: 0 },
records: { battleResult: ['둘째 전투 결과', '첫째 전투 결과'] },
availability: { battleResultLogs: true },
meta: { killturn: 0, dex1: 1_000, rank_warnum: 2, rank_killnum: 1 },
}),
}),
})
@@ -1,6 +1,8 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { normalizeArchivedGeneral, type ArchivedJsonValue } from '@sammo-ts/common';
import { createGamePostgresConnector, type GamePrismaClient } from '@sammo-ts/infra';
import { LogCategory, LogScope } from '@sammo-ts/logic';
import { createAuctionBidder } from '../src/auction/bidder.js';
import { createDatabaseTurnHooks } from '../src/turn/databaseHooks.js';
@@ -39,7 +41,9 @@ integration('unification finalization transaction', () => {
await db.inheritanceLog.deleteMany({ where: { userId } });
await db.inheritancePoint.deleteMany({ where: { userId } });
await db.gameHistory.deleteMany({ where: { serverId } });
await db.logEntry.deleteMany({ where: { year: 190, month: 7 } });
await db.logEntry.deleteMany({
where: { OR: [{ generalId: fixtureId }, { year: 190, month: 7 }] },
});
await db.rankData.deleteMany({ where: { generalId: fixtureId } });
await db.general.deleteMany({ where: { id: fixtureId } });
await db.city.deleteMany({ where: { id: fixtureId } });
@@ -139,6 +143,52 @@ integration('unification finalization transaction', () => {
},
},
});
await db.rankData.createMany({
data: [
{ generalId: fixtureId, nationId: fixtureId, type: 'warnum', value: 4 },
{ generalId: fixtureId, nationId: fixtureId, type: 'killnum', value: 3 },
{ generalId: fixtureId, nationId: fixtureId, type: 'deathnum', value: 1 },
{ generalId: fixtureId, nationId: fixtureId, type: 'firenum', value: 2 },
{ generalId: fixtureId, nationId: fixtureId, type: 'killcrew', value: 1_200 },
{ generalId: fixtureId, nationId: fixtureId, type: 'deathcrew', value: 800 },
],
});
await db.logEntry.createMany({
data: [
{
scope: LogScope.GENERAL,
category: LogCategory.ACTION,
generalId: fixtureId,
year: 190,
month: 6,
text: '보존하지 않을 개인 기록',
},
{
scope: LogScope.GENERAL,
category: LogCategory.BATTLE_DETAIL,
generalId: fixtureId,
year: 190,
month: 6,
text: '보존하지 않을 전투 기록',
},
{
scope: LogScope.GENERAL,
category: LogCategory.BATTLE_BRIEF,
generalId: fixtureId,
year: 190,
month: 5,
text: '먼저 보존할 전투 결과',
},
{
scope: LogScope.GENERAL,
category: LogCategory.BATTLE_BRIEF,
generalId: fixtureId,
year: 190,
month: 6,
text: '보존할 전투 결과',
},
],
});
await db.inheritancePoint.createMany({
data: [
{ userId, key: 'previous', value: 100 },
@@ -413,6 +463,28 @@ integration('unification finalization transaction', () => {
where: { generalId_type: { generalId: fixtureId, type: 'inherit_earned' } },
})
).resolves.toMatchObject({ value: 2_160 });
const archivedGeneral = await db.oldGeneral.findUniqueOrThrow({
where: { by_no: { serverId, generalNo: fixtureId } },
});
const archivedSnapshot = normalizeArchivedGeneral(
archivedGeneral.data as ArchivedJsonValue,
archivedGeneral.name
).snapshot;
expect(archivedSnapshot).toMatchObject({
mastery: { infantry: 100 },
battle: {
battles: 4,
wins: 3,
losses: 1,
fireSuccesses: 2,
killedCrew: 1_200,
lostCrew: 800,
},
records: { battleResult: ['보존할 전투 결과', '먼저 보존할 전투 결과'] },
availability: { battleResultLogs: true, battleDetailLogs: false },
});
expect(JSON.stringify(archivedGeneral.data)).not.toContain('보존하지 않을 개인 기록');
expect(JSON.stringify(archivedGeneral.data)).not.toContain('보존하지 않을 전투 기록');
expect((await db.gameHistory.findUniqueOrThrow({ where: { serverId } })).winnerNation).toBe(fixtureId);
const yearbook = await db.yearbookHistory.findUniqueOrThrow({
where: {
@@ -1,7 +1,8 @@
import { describe, expect, it, vi } from 'vitest';
import { normalizeArchivedGeneral, type ArchivedJsonValue } from '@sammo-ts/common';
import type { GamePrisma } from '@sammo-ts/infra';
import type { City, Nation } from '@sammo-ts/logic';
import { LogCategory, LogScope, type City, type Nation } from '@sammo-ts/logic';
import { InMemoryTurnWorld } from '../src/turn/inMemoryWorld.js';
import { persistUnificationFinalization, resolveStoredInheritancePoint } from '../src/turn/unificationPersistence.js';
@@ -187,6 +188,7 @@ describe('persistUnificationFinalization', () => {
const hallCreate = vi.fn().mockResolvedValue({});
const gameHistoryUpdate = vi.fn().mockResolvedValue({});
const emperorCreate = vi.fn().mockResolvedValue({});
const oldGeneralUpsert = vi.fn().mockResolvedValue({});
const transaction = Object.assign({} as GamePrisma.TransactionClient, {
$executeRaw: vi.fn().mockResolvedValue(1),
$queryRaw: vi.fn().mockResolvedValue([]),
@@ -204,19 +206,41 @@ describe('persistUnificationFinalization', () => {
},
inheritanceResult: { create: inheritanceResultCreate },
inheritanceLog: { create: inheritanceLogCreate },
rankData: { findMany: vi.fn().mockResolvedValue([]) },
rankData: {
findMany: vi.fn().mockResolvedValue([
{ generalId: 1, type: 'warnum', value: 15 },
{ generalId: 1, type: 'killnum', value: 9 },
{ generalId: 1, type: 'deathnum', value: 6 },
{ generalId: 1, type: 'firenum', value: 4 },
{ generalId: 1, type: 'killcrew', value: 1_200 },
{ generalId: 1, type: 'deathcrew', value: 800 },
{ generalId: 1, type: 'ttw', value: 3 },
{ generalId: 1, type: 'ttd', value: 2 },
{ generalId: 1, type: 'ttl', value: 1 },
]),
},
gameHistory: { count: vi.fn().mockResolvedValue(1), update: gameHistoryUpdate },
hallOfFame: {
findFirst: vi.fn().mockResolvedValue(null),
create: hallCreate,
update: vi.fn().mockResolvedValue({}),
},
logEntry: { findMany: vi.fn().mockResolvedValue([]) },
logEntry: {
findMany: vi.fn().mockImplementation(({ where }: { where: { scope: string } }) =>
where.scope === LogScope.GENERAL
? [
{ generalId: 1, category: LogCategory.BATTLE_BRIEF, text: '이전 전투 결과' },
{ generalId: 1, category: LogCategory.HISTORY, text: '통일 장수 열전' },
{ generalId: 1, category: LogCategory.BATTLE_BRIEF, text: '최신 전투 결과' },
]
: []
),
},
oldNation: {
upsert: vi.fn().mockResolvedValue({}),
findMany: vi.fn().mockResolvedValue([]),
},
oldGeneral: { upsert: vi.fn().mockResolvedValue({}) },
oldGeneral: { upsert: oldGeneralUpsert },
emperor: { create: emperorCreate },
});
await expect(persistUnificationFinalization(transaction, input, buildWorld())).resolves.toEqual({
@@ -252,5 +276,28 @@ describe('persistUnificationFinalization', () => {
data: expect.objectContaining({ aux: { winnerNationId: 1, generationKey: input.generationKey } }),
})
);
const archiveWrite = oldGeneralUpsert.mock.calls[0]?.[0] as {
create: { data: ArchivedJsonValue };
};
const snapshot = normalizeArchivedGeneral(archiveWrite.create.data, '통일장수').snapshot;
expect(snapshot).toMatchObject({
mastery: { infantry: 100 },
battle: {
battles: 15,
wins: 9,
losses: 6,
fireSuccesses: 4,
killedCrew: 1_200,
lostCrew: 800,
tactics: { total: { wins: 3, draws: 2, losses: 1 } },
},
records: { battleResult: ['최신 전투 결과', '이전 전투 결과'] },
availability: {
mastery: true,
battleAggregates: true,
battleResultLogs: true,
battleDetailLogs: false,
},
});
});
});