feat: add reserved turns management for generals and nations; enhance command selection UI

This commit is contained in:
2026-01-17 02:27:50 +00:00
parent 0fed50b5a9
commit f13f8bc1cf
8 changed files with 552 additions and 15 deletions
+52
View File
@@ -6,6 +6,8 @@ import { buildTurnCommandTable } from '../../turns/commandTable.js';
import {
MAX_GENERAL_TURNS,
MAX_NATION_TURNS,
listGeneralTurns,
listNationTurns,
setGeneralTurn,
setNationTurn,
shiftGeneralTurns,
@@ -76,6 +78,56 @@ export const turnsRouter = router({
});
}),
reserved: router({
getGeneral: authedProcedure
.input(
z.object({
generalId: z.number().int().positive(),
})
)
.query(async ({ ctx, input }) => {
const general = await ctx.db.general.findUnique({
where: { id: input.generalId },
});
if (!general) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'General not found.',
});
}
return listGeneralTurns(ctx.db, input.generalId);
}),
getNation: authedProcedure
.input(
z.object({
generalId: z.number().int().positive(),
})
)
.query(async ({ ctx, input }) => {
const general = await ctx.db.general.findUnique({
where: { id: input.generalId },
});
if (!general) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'General not found.',
});
}
if (general.nationId <= 0) {
throw new TRPCError({
code: 'PRECONDITION_FAILED',
message: 'General is not part of a nation.',
});
}
if (general.officerLevel < 5) {
throw new TRPCError({
code: 'FORBIDDEN',
message: 'General is not an officer.',
});
}
return listNationTurns(ctx.db, general.nationId, general.officerLevel);
}),
setGeneral: authedProcedure
.input(
z.object({
+14
View File
@@ -108,6 +108,11 @@ export const loadGeneralTurns = async (db: DatabaseClient, generalId: number): P
return buildTurnListFromRows(rows, MAX_GENERAL_TURNS);
};
export const listGeneralTurns = async (db: DatabaseClient, generalId: number): Promise<ReservedTurnView[]> => {
const turns = await loadGeneralTurns(db, generalId);
return serializeTurnList(turns);
};
export const loadNationTurns = async (
db: DatabaseClient,
nationId: number,
@@ -120,6 +125,15 @@ export const loadNationTurns = async (
return buildTurnListFromRows(rows, MAX_NATION_TURNS);
};
export const listNationTurns = async (
db: DatabaseClient,
nationId: number,
officerLevel: number
): Promise<ReservedTurnView[]> => {
const turns = await loadNationTurns(db, nationId, officerLevel);
return serializeTurnList(turns);
};
export const setGeneralTurn = async (
db: DatabaseClient,
generalId: number,
+129 -5
View File
@@ -1,12 +1,21 @@
import { describe, expect, it } from 'vitest';
import type { DatabaseClient, GameApiContext, GameProfile, WorldStateRow } from '../src/context.js';
import type {
DatabaseClient,
GameApiContext,
GameProfile,
WorldStateRow,
GeneralRow,
GeneralTurnRow,
NationTurnRow,
} from '../src/context.js';
import type { RedisConnector } from '@sammo-ts/infra';
import { InMemoryBattleSimTransport } from '../src/battleSim/inMemoryTransport.js';
import { InMemoryTurnDaemonTransport } from '../src/daemon/inMemoryTransport.js';
import { InMemoryFlushStore } from '../src/auth/flushStore.js';
import { RedisAccessTokenStore } from '../src/auth/accessTokenStore.js';
import { appRouter } from '../src/router.js';
import type { GameSessionTokenPayload } from '@sammo-ts/common';
const profile: GameProfile = {
id: 'che',
@@ -14,19 +23,77 @@ const profile: GameProfile = {
name: 'che:default',
};
const buildGeneralRow = (overrides?: Partial<GeneralRow>): GeneralRow => {
const base: GeneralRow = {
id: 1,
userId: null,
name: '테스트',
nationId: 0,
cityId: 1,
troopId: 0,
npcState: 0,
affinity: null,
bornYear: 180,
deadYear: 300,
picture: null,
imageServer: 0,
leadership: 50,
strength: 50,
intel: 50,
injury: 0,
experience: 0,
dedication: 0,
officerLevel: 0,
gold: 1000,
rice: 1000,
crew: 0,
crewTypeId: 0,
train: 0,
atmos: 0,
weaponCode: 'None',
bookCode: 'None',
horseCode: 'None',
itemCode: 'None',
turnTime: new Date('2026-01-01T00:00:00Z'),
recentWarTime: null,
age: 20,
startAge: 20,
personalCode: 'None',
specialCode: 'None',
special2Code: 'None',
lastTurn: {},
meta: {},
penalty: {},
createdAt: new Date('2026-01-01T00:00:00Z'),
updatedAt: new Date('2026-01-01T00:00:00Z'),
};
return { ...base, ...(overrides ?? {}) };
};
const buildContext = (options?: {
state?: WorldStateRow | null;
transport?: InMemoryTurnDaemonTransport;
battleSim?: InMemoryBattleSimTransport;
general?: GeneralRow | null;
generalTurns?: GeneralTurnRow[];
nationTurns?: NationTurnRow[];
auth?: GameSessionTokenPayload | null;
}): GameApiContext => {
const transport = options?.transport ?? new InMemoryTurnDaemonTransport();
const battleSim = options?.battleSim ?? new InMemoryBattleSimTransport();
const generalTurns = options?.generalTurns ?? [];
const nationTurns = options?.nationTurns ?? [];
const db = {
worldState: {
findFirst: async () => options?.state ?? null,
},
general: {
findUnique: async () => null,
findUnique: async ({ where }: { where: { id: number } }) => {
if (!options?.general) {
return null;
}
return options.general.id === where.id ? options.general : null;
},
},
city: {
findUnique: async () => null,
@@ -35,12 +102,16 @@ const buildContext = (options?: {
findUnique: async () => null,
},
generalTurn: {
findMany: async () => [],
findMany: async ({ where }: { where: { generalId: number } }) =>
generalTurns.filter((row) => row.generalId === where.generalId),
deleteMany: async () => ({}),
createMany: async () => ({}),
},
nationTurn: {
findMany: async () => [],
findMany: async ({ where }: { where: { nationId: number; officerLevel: number } }) =>
nationTurns.filter(
(row) => row.nationId === where.nationId && row.officerLevel === where.officerLevel
),
deleteMany: async () => ({}),
createMany: async () => ({}),
},
@@ -52,12 +123,26 @@ const buildContext = (options?: {
},
profile.name
);
const auth: GameSessionTokenPayload = options?.auth ?? {
version: 1,
profile: profile.name,
issuedAt: new Date('2026-01-01T00:00:00Z').toISOString(),
expiresAt: new Date('2026-01-02T00:00:00Z').toISOString(),
sessionId: 'session-1',
user: {
id: 'user-1',
username: 'tester',
displayName: 'Tester',
roles: [],
},
sanctions: {},
};
return {
db: db as unknown as DatabaseClient,
turnDaemon: transport,
battleSim,
profile,
auth: null,
auth,
redis: {} as unknown as RedisConnector['client'],
accessTokenStore,
flushStore: new InMemoryFlushStore(),
@@ -111,4 +196,43 @@ describe('appRouter', () => {
expect(response?.state).toBe('paused');
expect(response?.queueDepth).toBe(2);
});
it('returns reserved general turns', async () => {
const general = buildGeneralRow({ id: 11 });
const generalTurns: GeneralTurnRow[] = [
{
id: 1,
generalId: 11,
turnIdx: 0,
actionCode: 'che_화계',
arg: {},
createdAt: new Date('2026-01-01T00:00:00Z'),
},
];
const caller = appRouter.createCaller(buildContext({ general, generalTurns }));
const response = await caller.turns.reserved.getGeneral({ generalId: 11 });
expect(response[0]?.action).toBe('che_화계');
expect(response[0]?.index).toBe(0);
});
it('returns reserved nation turns', async () => {
const general = buildGeneralRow({ id: 12, nationId: 3, officerLevel: 5 });
const nationTurns: NationTurnRow[] = [
{
id: 1,
nationId: 3,
officerLevel: 5,
turnIdx: 0,
actionCode: 'che_포상',
arg: {},
createdAt: new Date('2026-01-01T00:00:00Z'),
},
];
const caller = appRouter.createCaller(buildContext({ general, nationTurns }));
const response = await caller.turns.reserved.getNation({ generalId: 12 });
expect(response[0]?.action).toBe('che_포상');
expect(response[0]?.index).toBe(0);
});
});