feat(frontend): restore betting and NPC list pages
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import { TRPCError } from '@trpc/server';
|
||||
import { asRecord } from '@sammo-ts/common';
|
||||
|
||||
import type { GameApiContext } from '../../context.js';
|
||||
import { zWorldStateConfig, zWorldStateMeta } from '../../context.js';
|
||||
import { loadMapLayout } from '../../maps/mapLayout.js';
|
||||
import { loadPublicMap } from '../../maps/worldMap.js';
|
||||
import { procedure, router } from '../../trpc.js';
|
||||
import { loadTraitNames } from '../nation/shared.js';
|
||||
import { z } from 'zod';
|
||||
|
||||
type WorldTrendSnapshot = {
|
||||
year: number;
|
||||
@@ -37,6 +40,8 @@ type NationCountRow = {
|
||||
count: number;
|
||||
};
|
||||
|
||||
type NpcListSort = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
|
||||
|
||||
const PUBLIC_CACHE_TTL_SECONDS = 600;
|
||||
|
||||
const buildPublicCacheKey = (ctx: GameApiContext, key: string): string =>
|
||||
@@ -151,6 +156,52 @@ const loadCachedNationList = async (ctx: GameApiContext): Promise<NationSummary[
|
||||
return summary;
|
||||
};
|
||||
|
||||
const normalizeTraitKey = (value: string): string | null => (value && value !== 'None' ? value : null);
|
||||
|
||||
const readFiniteMetaNumber = (meta: Record<string, unknown>, key: string): number => {
|
||||
const value = meta[key];
|
||||
return typeof value === 'number' && Number.isFinite(value) ? value : 0;
|
||||
};
|
||||
|
||||
const compareString = (left: string, right: string): number => {
|
||||
if (left === right) {
|
||||
return 0;
|
||||
}
|
||||
return left < right ? -1 : 1;
|
||||
};
|
||||
|
||||
const sortNpcList = <T extends {
|
||||
name: string;
|
||||
nationId: number;
|
||||
statTotal: number;
|
||||
leadership: number;
|
||||
strength: number;
|
||||
intelligence: number;
|
||||
experience: number;
|
||||
dedication: number;
|
||||
}>(rows: T[], sort: NpcListSort): T[] =>
|
||||
rows.sort((left, right) => {
|
||||
switch (sort) {
|
||||
case 2:
|
||||
return left.nationId - right.nationId;
|
||||
case 3:
|
||||
return right.statTotal - left.statTotal;
|
||||
case 4:
|
||||
return right.leadership - left.leadership;
|
||||
case 5:
|
||||
return right.strength - left.strength;
|
||||
case 6:
|
||||
return right.intelligence - left.intelligence;
|
||||
case 7:
|
||||
return right.experience - left.experience;
|
||||
case 8:
|
||||
return right.dedication - left.dedication;
|
||||
case 1:
|
||||
default:
|
||||
return compareString(left.name, right.name);
|
||||
}
|
||||
});
|
||||
|
||||
export const publicRouter = router({
|
||||
getMapLayout: procedure.query(async ({ ctx }) => {
|
||||
return loadMapLayout(ctx.profile.scenario);
|
||||
@@ -208,4 +259,109 @@ export const publicRouter = router({
|
||||
intelligence: general.intel,
|
||||
}));
|
||||
}),
|
||||
getNpcList: procedure
|
||||
.input(
|
||||
z
|
||||
.object({
|
||||
sort: z.number().int().min(1).max(8).catch(1).optional(),
|
||||
})
|
||||
.optional()
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const sort = (input?.sort ?? 1) as NpcListSort;
|
||||
const [generals, nations] = await Promise.all([
|
||||
ctx.db.general.findMany({
|
||||
where: { npcState: { gt: 0 } },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
npcState: true,
|
||||
nationId: true,
|
||||
leadership: true,
|
||||
strength: true,
|
||||
intel: true,
|
||||
experience: true,
|
||||
dedication: true,
|
||||
personalCode: true,
|
||||
specialCode: true,
|
||||
special2Code: true,
|
||||
meta: true,
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
}),
|
||||
ctx.db.nation.findMany({
|
||||
select: { id: true, name: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
const personalityKeys = generals.map((general) => normalizeTraitKey(general.personalCode));
|
||||
const domesticKeys = generals.map((general) => normalizeTraitKey(general.specialCode));
|
||||
const warKeys = generals.map((general) => normalizeTraitKey(general.special2Code));
|
||||
const [personalityMap, domesticMap, warMap] = await Promise.all([
|
||||
loadTraitNames(personalityKeys, 'personality'),
|
||||
loadTraitNames(domesticKeys, 'domestic'),
|
||||
loadTraitNames(warKeys, 'war'),
|
||||
]);
|
||||
const nationMap = new Map(nations.map((nation) => [nation.id, nation.name]));
|
||||
|
||||
// Legacy select_pool rows preceded possessed NPC rows before its stable-value sort.
|
||||
const pool = generals.filter((general) => general.npcState >= 2);
|
||||
const possessed = generals.filter((general) => general.npcState === 1);
|
||||
const rows = [...pool, ...possessed].map((general) => {
|
||||
const meta = asRecord(general.meta);
|
||||
const personalityKey = normalizeTraitKey(general.personalCode);
|
||||
const domesticKey = normalizeTraitKey(general.specialCode);
|
||||
const warKey = normalizeTraitKey(general.special2Code);
|
||||
const ownerName =
|
||||
general.npcState === 1
|
||||
? typeof meta.owner_name === 'string'
|
||||
? meta.owner_name
|
||||
: typeof meta.ownerName === 'string'
|
||||
? meta.ownerName
|
||||
: ''
|
||||
: '';
|
||||
|
||||
return {
|
||||
id: general.id,
|
||||
name: general.name,
|
||||
npcState: general.npcState,
|
||||
ownerName,
|
||||
level: readFiniteMetaNumber(meta, 'explevel'),
|
||||
nationId: general.nationId,
|
||||
nationName: nationMap.get(general.nationId) ?? '-',
|
||||
personality: personalityKey
|
||||
? {
|
||||
key: personalityKey,
|
||||
name: personalityMap.get(personalityKey)?.name ?? personalityKey,
|
||||
info: personalityMap.get(personalityKey)?.info ?? '',
|
||||
}
|
||||
: null,
|
||||
specialDomestic: domesticKey
|
||||
? {
|
||||
key: domesticKey,
|
||||
name: domesticMap.get(domesticKey)?.name ?? domesticKey,
|
||||
info: domesticMap.get(domesticKey)?.info ?? '',
|
||||
}
|
||||
: null,
|
||||
specialWar: warKey
|
||||
? {
|
||||
key: warKey,
|
||||
name: warMap.get(warKey)?.name ?? warKey,
|
||||
info: warMap.get(warKey)?.info ?? '',
|
||||
}
|
||||
: null,
|
||||
statTotal: general.leadership + general.strength + general.intel,
|
||||
leadership: general.leadership,
|
||||
strength: general.strength,
|
||||
intelligence: general.intel,
|
||||
experience: general.experience,
|
||||
dedication: general.dedication,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
sort,
|
||||
generals: sortNpcList(rows, sort),
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user