feat(mods): add battle.onFilterMissions hook and per-template bundled seeding
This commit is contained in:
1 parent
8636d0b71d
commit
5983e3f3e2
6 files changed
+127
-20
No files matched your search
@@ -85,6 +85,16 @@ export type MatchmakeOutcome =
|
||||
| { readonly ok: true; readonly launch: BattleLaunchPlan }
|
||||
| { readonly ok: false; readonly error: string };
|
||||
|
||||
export type MissionPoolFilter = (ctx: {
|
||||
readonly userId: string;
|
||||
readonly modeId: number;
|
||||
readonly modeName: string;
|
||||
readonly economicName: string;
|
||||
readonly minMrank: number;
|
||||
readonly maxMrank: number;
|
||||
readonly missions: readonly MissionHeader[];
|
||||
}) => readonly MissionHeader[];
|
||||
|
||||
export class BattleMatchmaker {
|
||||
readonly #gameModes: GameModes;
|
||||
readonly #vehicles: readonly CatalogueVehicle[];
|
||||
@@ -93,6 +103,7 @@ export class BattleMatchmaker {
|
||||
readonly #tuning: BattleTuning;
|
||||
readonly #spawnRules: BattleSpawnRulesData;
|
||||
readonly #aircraftLoadouts: ReadonlyMap<string, BotAircraftLoadout>;
|
||||
readonly #filterMissions: MissionPoolFilter | undefined;
|
||||
|
||||
constructor(
|
||||
gameModes: GameModes,
|
||||
@@ -101,6 +112,7 @@ export class BattleMatchmaker {
|
||||
tuning: BattleTuning,
|
||||
spawnRules: BattleSpawnRulesData,
|
||||
aircraftLoadouts: ReadonlyMap<string, BotAircraftLoadout>,
|
||||
filterMissions?: MissionPoolFilter,
|
||||
) {
|
||||
this.#gameModes = gameModes;
|
||||
this.#vehicles = vehicles;
|
||||
@@ -109,6 +121,7 @@ export class BattleMatchmaker {
|
||||
this.#tuning = tuning;
|
||||
this.#spawnRules = spawnRules;
|
||||
this.#aircraftLoadouts = aircraftLoadouts;
|
||||
this.#filterMissions = filterMissions;
|
||||
}
|
||||
|
||||
enqueue(input: EnqueueBattleInput): MatchmakeOutcome {
|
||||
@@ -132,8 +145,20 @@ export class BattleMatchmaker {
|
||||
const minMrank = Math.max(declaration.minQueueMrank ?? 0, playerMrank - this.#tuning.brSpreadMrank);
|
||||
const roomCeiling = chosenMode.missionRanks?.maxMrank ?? Infinity;
|
||||
const maxMrank = Math.max(minMrank, playerMrank, Math.min(roomCeiling, playerMrank + this.#tuning.brSpreadMrank));
|
||||
const missions = twoSidedMissionsOf(chosenMode, maxMrank);
|
||||
if (missions.length === 0) return { ok: false, error: 'NO_MISSION_FOR_BR' };
|
||||
const rawMissions = twoSidedMissionsOf(chosenMode, maxMrank);
|
||||
if (rawMissions.length === 0) return { ok: false, error: 'NO_MISSION_FOR_BR' };
|
||||
const filteredMissions = this.#filterMissions
|
||||
? this.#filterMissions({
|
||||
userId: input.userId,
|
||||
modeId: declaration.gameModeId,
|
||||
modeName: declaration.name,
|
||||
economicName: declaration.economicName,
|
||||
minMrank,
|
||||
maxMrank,
|
||||
missions: rawMissions,
|
||||
})
|
||||
: rawMissions;
|
||||
const missions = filteredMissions.length > 0 ? filteredMissions : rawMissions;
|
||||
const mission = pickMission(missions, input.bannedMissions, input.dislikedMissions);
|
||||
const coalitions = selectBattleCoalitions({
|
||||
vehicles: this.#vehicles,
|
||||
|
||||
@@ -11,8 +11,10 @@ import { pathToFileURL } from 'node:url';
|
||||
import type { BlkBlock } from '@wto/blk';
|
||||
import {
|
||||
MOD_API_VERSION,
|
||||
type BattleFilterMissionsContext,
|
||||
type BattleLiveFactContext,
|
||||
type BattleMatchmakeContext,
|
||||
type BattleMissionCandidate,
|
||||
type BattleSettlementContext,
|
||||
type DiscoveredMod,
|
||||
type ModCharHandler,
|
||||
@@ -54,6 +56,7 @@ export class ServerModRuntime {
|
||||
readonly #onServerClosing: ScopedHandler<() => void | Promise<void>>[] = [];
|
||||
readonly #onProfileRefresh: ScopedHandler<(profile: BlkBlock, userId: string) => void>[] = [];
|
||||
readonly #onBeforeMatchmake: ScopedHandler<(ctx: BattleMatchmakeContext) => void>[] = [];
|
||||
readonly #onFilterMissions: ScopedHandler<(ctx: BattleFilterMissionsContext) => readonly BattleMissionCandidate[] | void>[] = [];
|
||||
readonly #onLiveFact: ScopedHandler<(ctx: BattleLiveFactContext) => void>[] = [];
|
||||
readonly #onAfterSettle: ScopedHandler<(ctx: BattleSettlementContext) => void>[] = [];
|
||||
readonly #charHandlers = new Map<string, ScopedHandler<ModCharHandler>>();
|
||||
@@ -172,6 +175,30 @@ export class ServerModRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
runBattleFilterMissions<T extends BattleMissionCandidate>(
|
||||
ctx: Omit<BattleFilterMissionsContext, 'missions'> & { readonly missions: readonly T[] },
|
||||
): readonly T[] {
|
||||
if (this.#onFilterMissions.length === 0) return ctx.missions;
|
||||
let current: readonly T[] = ctx.missions;
|
||||
for (const { modId, fn } of this.#onFilterMissions) {
|
||||
try {
|
||||
const next = fn({ ...ctx, missions: current });
|
||||
if (Array.isArray(next)) {
|
||||
const allowed = new Set(next.map((item) => item.name));
|
||||
const filtered = current.filter((item) => allowed.has(item.name));
|
||||
if (filtered.length > 0) {
|
||||
current = filtered;
|
||||
} else {
|
||||
this.#report(`[mod:${modId}] battle.onFilterMissions returned 0 matching candidates for ${ctx.modeName}; keeping previous pool`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
this.#report(`[mod:${modId}] battle.onFilterMissions error: ${formatError(error)}`);
|
||||
}
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
runBattleLiveFact(ctx: BattleLiveFactContext): void {
|
||||
for (const { modId, fn } of this.#onLiveFact) {
|
||||
try {
|
||||
@@ -284,6 +311,7 @@ export class ServerModRuntime {
|
||||
},
|
||||
battle: {
|
||||
onBeforeMatchmake: (fn) => { this.#onBeforeMatchmake.push({ modId, fn }); },
|
||||
onFilterMissions: (fn) => { this.#onFilterMissions.push({ modId, fn }); },
|
||||
onLiveFact: (fn) => { this.#onLiveFact.push({ modId, fn }); },
|
||||
onAfterSettle: (fn) => { this.#onAfterSettle.push({ modId, fn }); },
|
||||
},
|
||||
|
||||
@@ -179,7 +179,15 @@ export async function startServer(options: ServerOptions): Promise<RunningServer
|
||||
matching?.notifyUser(userId, eventName, payload);
|
||||
};
|
||||
const battleSessions = new BattleSessionStore(tuning.limits.battleSessionsKept);
|
||||
const battleMatchmaker = new BattleMatchmaker(gameModes, vehicles, battleSessions, tuning.battle, spawnRules, botAircraftLoadouts);
|
||||
const battleMatchmaker = new BattleMatchmaker(
|
||||
gameModes,
|
||||
vehicles,
|
||||
battleSessions,
|
||||
tuning.battle,
|
||||
spawnRules,
|
||||
botAircraftLoadouts,
|
||||
(ctx) => modRuntime.runBattleFilterMissions(ctx),
|
||||
);
|
||||
const economy = new BattleEconomy(
|
||||
economyData,
|
||||
tuning.battleEconomy,
|
||||
|
||||
Reference in new issue
Block a user