merge: 도시정보 예약 명령 요약을 main에 통합한다
This commit is contained in:
@@ -175,13 +175,14 @@ export const worldRouter = router({
|
||||
const turns = generalIds.length
|
||||
? await ctx.db.generalTurn.findMany({
|
||||
where: { generalId: { in: generalIds }, turnIdx: { lt: 5 } },
|
||||
select: { generalId: true, turnIdx: true, actionCode: true, arg: true },
|
||||
orderBy: [{ generalId: 'asc' }, { turnIdx: 'asc' }],
|
||||
})
|
||||
: [];
|
||||
const turnMap = new Map<number, string[]>();
|
||||
const turnMap = new Map<number, Array<{ action: string; args: unknown }>>();
|
||||
for (const turn of turns) {
|
||||
const list = turnMap.get(turn.generalId) ?? [];
|
||||
list[turn.turnIdx] = turn.actionCode;
|
||||
list[turn.turnIdx] = { action: turn.actionCode, args: turn.arg };
|
||||
turnMap.set(turn.generalId, list);
|
||||
}
|
||||
const nationMap = new Map(nations.map((item) => [item.id, item]));
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import type { GameSessionTokenPayload } from '@sammo-ts/common/auth/gameToken';
|
||||
import type { RedisConnector } from '@sammo-ts/infra';
|
||||
|
||||
import { RedisAccessTokenStore } from '../src/auth/accessTokenStore.js';
|
||||
import { InMemoryFlushStore } from '../src/auth/flushStore.js';
|
||||
import type { DatabaseClient, GameApiContext, GeneralRow } from '../src/context.js';
|
||||
import { appRouter } from '../src/router.js';
|
||||
|
||||
vi.mock('../src/maps/mapLayout.js', () => ({
|
||||
loadMapLayout: vi.fn(async () => ({
|
||||
mapName: 'che',
|
||||
cityList: [{ id: 1, name: '업', level: 8, region: 1, x: 0, y: 0, path: [] }],
|
||||
regionMap: { 1: '하북' },
|
||||
levelMap: { 8: '특' },
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock('@sammo-ts/game-engine/scenario/unitSetLoader.js', () => ({
|
||||
loadUnitSetDefinitionByName: vi.fn(async () => ({ crewTypes: [{ id: 1, name: '보병' }] })),
|
||||
}));
|
||||
|
||||
const now = new Date('2026-01-01T01:02:00Z');
|
||||
const general = (overrides: Partial<GeneralRow> = {}): GeneralRow => ({
|
||||
id: 1,
|
||||
userId: 'u1',
|
||||
name: '장수',
|
||||
nationId: 1,
|
||||
cityId: 1,
|
||||
troopId: 0,
|
||||
npcState: 0,
|
||||
affinity: null,
|
||||
bornYear: 180,
|
||||
deadYear: 300,
|
||||
picture: null,
|
||||
imageServer: 0,
|
||||
leadership: 70,
|
||||
strength: 60,
|
||||
intel: 50,
|
||||
injury: 0,
|
||||
experience: 900,
|
||||
dedication: 100,
|
||||
officerLevel: 1,
|
||||
gold: 1000,
|
||||
rice: 2000,
|
||||
crew: 300,
|
||||
crewTypeId: 1,
|
||||
train: 90,
|
||||
atmos: 90,
|
||||
weaponCode: 'None',
|
||||
bookCode: 'None',
|
||||
horseCode: 'None',
|
||||
itemCode: 'None',
|
||||
turnTime: now,
|
||||
recentWarTime: null,
|
||||
age: 20,
|
||||
startAge: 20,
|
||||
personalCode: 'None',
|
||||
specialCode: 'None',
|
||||
special2Code: 'None',
|
||||
lastTurn: {},
|
||||
meta: { defence_train: 80 },
|
||||
penalty: {},
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const token = (): GameSessionTokenPayload => ({
|
||||
version: 1,
|
||||
profile: 'che:default',
|
||||
issuedAt: now.toISOString(),
|
||||
expiresAt: new Date(now.getTime() + 86_400_000).toISOString(),
|
||||
sessionId: 'session-1',
|
||||
user: { id: 'u1', username: 'u1', displayName: '장수', roles: [] },
|
||||
sanctions: {},
|
||||
});
|
||||
|
||||
const fixture = (authenticated = true) => {
|
||||
const actor = general();
|
||||
const npc = general({ id: 2, userId: null, name: 'NPC', npcState: 2 });
|
||||
const city = {
|
||||
id: 1,
|
||||
name: '업',
|
||||
nationId: 1,
|
||||
level: 8,
|
||||
region: 1,
|
||||
population: 150_000,
|
||||
populationMax: 620_500,
|
||||
agriculture: 1_000,
|
||||
agricultureMax: 12_500,
|
||||
commerce: 1_000,
|
||||
commerceMax: 11_300,
|
||||
security: 1_000,
|
||||
securityMax: 10_000,
|
||||
trust: 80,
|
||||
trade: 100,
|
||||
defence: 5_000,
|
||||
defenceMax: 11_700,
|
||||
wall: 5_000,
|
||||
wallMax: 12_200,
|
||||
};
|
||||
const db = {
|
||||
general: {
|
||||
findFirst: vi.fn(async () => actor),
|
||||
findMany: vi.fn(async ({ where }: { where: Record<string, unknown> }) => {
|
||||
if ('cityId' in where) return [actor, npc];
|
||||
if ('officerLevel' in where) return [];
|
||||
if ('nationId' in where) return [actor, npc];
|
||||
return [];
|
||||
}),
|
||||
},
|
||||
nation: {
|
||||
findUnique: vi.fn(async () => ({ id: 1, name: '위', color: '#008000', level: 1, meta: {} })),
|
||||
findMany: vi.fn(async () => [{ id: 1, name: '위', color: '#008000', level: 1, meta: {} }]),
|
||||
},
|
||||
city: { findMany: vi.fn(async () => [city]) },
|
||||
worldState: {
|
||||
findFirst: vi.fn(async () => ({ config: {}, meta: { turntime: '2026-01-01 10:02:00' } })),
|
||||
},
|
||||
generalTurn: {
|
||||
findMany: vi.fn(async () => [
|
||||
{
|
||||
generalId: 1,
|
||||
turnIdx: 0,
|
||||
actionCode: 'che_징병',
|
||||
arg: { crewType: 1, amount: 300 },
|
||||
},
|
||||
]),
|
||||
},
|
||||
};
|
||||
const redis = { get: vi.fn(async () => null), set: vi.fn(async () => null) } as unknown as RedisConnector['client'];
|
||||
const context: GameApiContext = {
|
||||
db: db as unknown as DatabaseClient,
|
||||
redis,
|
||||
turnDaemon: {} as GameApiContext['turnDaemon'],
|
||||
battleSim: {} as GameApiContext['battleSim'],
|
||||
profile: { id: 'che', scenario: 'default', name: 'che:default' },
|
||||
auth: authenticated ? token() : null,
|
||||
uploadDir: 'uploads',
|
||||
uploadPath: '/uploads',
|
||||
uploadPublicUrl: null,
|
||||
accessTokenStore: new RedisAccessTokenStore(redis, 'che:default'),
|
||||
flushStore: new InMemoryFlushStore(),
|
||||
gameTokenSecret: 'secret',
|
||||
};
|
||||
return { caller: appRouter.createCaller(context), db };
|
||||
};
|
||||
|
||||
describe('world current-city command projection', () => {
|
||||
it('returns the first five own-user turns as canonical action and args while redacting NPC turns', async () => {
|
||||
const { caller, db } = fixture();
|
||||
|
||||
const result = await caller.world.getCurrentCity();
|
||||
|
||||
expect(result.generals.find((entry) => entry.id === 1)?.turns).toEqual([
|
||||
{ action: 'che_징병', args: { crewType: 1, amount: 300 } },
|
||||
]);
|
||||
expect(result.generals.find((entry) => entry.id === 2)?.turns).toEqual([]);
|
||||
expect(db.generalTurn.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { generalId: { in: [1] }, turnIdx: { lt: 5 } },
|
||||
select: { generalId: true, turnIdx: true, actionCode: true, arg: true },
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps authentication and input validation in front of the city read model', async () => {
|
||||
await expect(fixture(false).caller.world.getCurrentCity()).rejects.toMatchObject({ code: 'UNAUTHORIZED' });
|
||||
await expect(fixture().caller.world.getCurrentCity({ cityId: 0 })).rejects.toMatchObject({
|
||||
code: 'BAD_REQUEST',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -210,7 +210,44 @@ const install = async (
|
||||
}
|
||||
if (operation === 'general.me') return response(generalContext);
|
||||
if (operation === 'world.getMap') return response(mapFixture);
|
||||
if (operation === 'turns.getCommandTable') return response({ general: [], nation: [] });
|
||||
if (operation === 'turns.getCommandTable')
|
||||
return response({
|
||||
general: [
|
||||
{
|
||||
category: '군사',
|
||||
values: [
|
||||
{
|
||||
key: 'che_징병',
|
||||
name: '징병',
|
||||
reqArg: true,
|
||||
status: 'needsInput',
|
||||
possible: true,
|
||||
inputFields: [],
|
||||
},
|
||||
{
|
||||
key: 'che_화계',
|
||||
name: '화계',
|
||||
reqArg: true,
|
||||
status: 'needsInput',
|
||||
possible: true,
|
||||
inputFields: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
nation: [],
|
||||
inputOptions: {
|
||||
cities: [{ value: 1, label: '업 (아국)' }],
|
||||
nations: [],
|
||||
generals: [],
|
||||
crewTypes: [{ value: 1, label: '보병' }],
|
||||
armTypes: [],
|
||||
nationTypes: [],
|
||||
colors: [],
|
||||
items: {},
|
||||
recruitment: null,
|
||||
},
|
||||
});
|
||||
if (operation === 'turns.reserved.getGeneral' || operation === 'turns.reserved.getNation') {
|
||||
return response({ turns: [], revision: 0 });
|
||||
}
|
||||
@@ -373,7 +410,12 @@ const install = async (
|
||||
crew: 500,
|
||||
train: 90,
|
||||
atmos: 90,
|
||||
turns: denseCurrentCity ? ['징병', '훈련'] : ['징병'],
|
||||
turns: denseCurrentCity
|
||||
? [
|
||||
{ action: 'che_징병', args: { crewType: 1, amount: 300 } },
|
||||
{ action: 'che_화계', args: { destCityId: 1 } },
|
||||
]
|
||||
: [{ action: 'che_징병', args: { crewType: 1, amount: 300 } }],
|
||||
},
|
||||
...(denseCurrentCity
|
||||
? Array.from({ length: 12 }, (_, index) => ({
|
||||
@@ -1082,8 +1124,10 @@ test('current-city wraps dense general names and only shrinks reserved turns', a
|
||||
const rows = page.locator('.generals tbody tr');
|
||||
const reservedTurns = rows.nth(0).locator('.turns');
|
||||
const npcTurns = rows.nth(1).locator('.turns');
|
||||
await expect(reservedTurns).toContainText('1 : 징병');
|
||||
await expect(reservedTurns).toContainText('2 : 훈련');
|
||||
await expect(reservedTurns).toContainText('1 : 【보병】 300명 징병');
|
||||
await expect(reservedTurns).toContainText('2 : 【업】에 화계실행');
|
||||
await expect(reservedTurns.locator('.turn-line').nth(0)).toHaveAttribute('title', '【보병】 300명 징병');
|
||||
await expect(reservedTurns.locator('.turn-line').nth(1)).toHaveAttribute('title', '【업】에 화계실행');
|
||||
await expect(reservedTurns).toHaveClass(/turns--reserved/);
|
||||
await expect(npcTurns).toHaveText('NPC 장수');
|
||||
await expect(npcTurns).not.toHaveClass(/turns--reserved/);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import { useRoute, useRouter } from 'vue-router';
|
||||
import { formatReservedCommandBrief } from '../components/command/reservedCommandBrief';
|
||||
import type { CommandTable } from '../components/command/types';
|
||||
import { cityLevelMap, formatOfficerLevelText, regionMap } from '../utils/nationFormat';
|
||||
import { getNpcColor } from '../utils/npcColor';
|
||||
import { resolveGeneralIconUrl, useDefaultGeneralIcon } from '../utils/generalIcon';
|
||||
@@ -9,10 +11,12 @@ import { trpc } from '../utils/trpc';
|
||||
|
||||
type Result = Awaited<ReturnType<typeof trpc.world.getCurrentCity.query>>;
|
||||
type General = Result['generals'][number];
|
||||
type ReservedCommand = General['turns'][number];
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
const data = ref<Result | null>(null);
|
||||
const commandTable = ref<CommandTable | null>(null);
|
||||
const error = ref('');
|
||||
const selected = ref<number>();
|
||||
let loadSequence = 0;
|
||||
@@ -29,8 +33,10 @@ const load = async (cityId?: number) => {
|
||||
const sequence = ++loadSequence;
|
||||
try {
|
||||
const result = await trpc.world.getCurrentCity.query(cityId ? { cityId } : undefined);
|
||||
const table = await trpc.turns.getCommandTable.query({ generalId: result.me.id });
|
||||
if (sequence !== loadSequence) return;
|
||||
data.value = result;
|
||||
commandTable.value = table;
|
||||
selected.value = result.city.id;
|
||||
error.value = '';
|
||||
} catch (cause) {
|
||||
@@ -76,6 +82,8 @@ const defenceTrainText = (value: number | null) => {
|
||||
return '△';
|
||||
};
|
||||
const generalImage = (general: General): string => resolveGeneralIconUrl(general);
|
||||
const commandBrief = (command: ReservedCommand): string =>
|
||||
formatReservedCommandBrief('general', command.action, command.args, commandTable.value);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -282,8 +290,12 @@ const generalImage = (general: General): string => resolveGeneralIconUrl(general
|
||||
<td>{{ general.atmos ?? '?' }}</td>
|
||||
<td class="turns" :class="{ 'turns--reserved': general.turns.length > 0 }">
|
||||
<template v-if="general.turns.length">
|
||||
<span v-for="(turn, index) in general.turns" :key="index" class="turn-line"
|
||||
>{{ index + 1 }} : {{ turn }}</span
|
||||
<span
|
||||
v-for="(turn, index) in general.turns"
|
||||
:key="index"
|
||||
class="turn-line"
|
||||
:title="commandBrief(turn)"
|
||||
>{{ index + 1 }} : {{ commandBrief(turn) }}</span
|
||||
>
|
||||
</template>
|
||||
<template v-else-if="general.npcState > 1">NPC 장수</template>
|
||||
|
||||
Reference in New Issue
Block a user