The launch script patch used to assemble the whole MissionSettings block on the client: reading the mission file for localization keys and ticket rules, filling the AI vehicle pool, rank window and extra slot costs. All of that is pre-battle data the local service already owns. - multiplayer-missions dataset v3 keeps the scalar parameters of mission_settings.mission and of each per-difficulty sub-block, with their BLK types. - battle-mission-settings.ts builds the complete MissionSettings block from the launch plan (same parameters as before). - battle-launch-notify.ts sends it as a typed tree, so ints and reals keep the types the engine reads, plus the lobby settings and the per-battle record the in-battle patches read from nestdb. - The launch patch shrinks from 208 to about 70 lines: rebuild the DataBlock, set _gameMode from the client's own native constant, enter the session lobby and start the LocalMP battle. Behaviour is unchanged; coalitions, extra slots and economy are reworked in later commits. Verified: npm run typecheck, check-gui (33 patches).
297 lines
12 KiB
TypeScript
297 lines
12 KiB
TypeScript
/**
|
|
* Pure local-battle matchmaking rules (`match.enqueue`):
|
|
* validates the chosen PvP mode and country, computes the player's lineup BR, selects a
|
|
* two-sided multiplayer mission honoring banned/disliked map preferences, pairs AB/RB/SB
|
|
* nation coalitions, builds the `customBots` pool and extra battle slots, and records a
|
|
* `BattleSession`.
|
|
*/
|
|
import { randomInt } from 'node:crypto';
|
|
import type { BattleSpawnRulesData } from '../game-data/datasets/battle-spawn-rules.ts';
|
|
import type { MissionHeader } from '../game-data/datasets/multiplayer-missions.ts';
|
|
import type { CatalogueVehicle } from '../game-data/datasets/vehicle-catalogue.ts';
|
|
import type { GameModes, OfferedGameMode } from '../game-modes/game-modes.ts';
|
|
import { MISSION_DIFFICULTY } from '../game-modes/mode-definition.ts';
|
|
import { serverTimeNow, serverTimeToUnixSeconds } from '../profile/server-time.ts';
|
|
import { selectBattleCoalitions, type CoalitionPairing } from './battle-coalitions.ts';
|
|
import { selectExtraBattleSlots, type ExtraBattleSlot } from './battle-extra-slots.ts';
|
|
import {
|
|
lineupMrank,
|
|
selectCustomBots,
|
|
unitClassOfExpClass,
|
|
vehicleMrank,
|
|
type CustomBotsPool,
|
|
} from './battle-ranks.ts';
|
|
import type { BattleSessionStore } from './battle-session-store.ts';
|
|
import type { BattleTuning } from './battle-tuning.ts';
|
|
|
|
export interface EnqueueBattleInput {
|
|
readonly userId: string;
|
|
readonly mode: OfferedGameMode;
|
|
readonly modes?: readonly OfferedGameMode[];
|
|
readonly country: string;
|
|
readonly slotVehicles: readonly string[];
|
|
readonly selectedSlotVehicle?: string;
|
|
readonly bannedMissions: ReadonlySet<string>;
|
|
readonly dislikedMissions: ReadonlySet<string>;
|
|
}
|
|
|
|
export interface BattleLaunchPlan {
|
|
readonly sessionId: string;
|
|
readonly modeId: number;
|
|
readonly modeName: string;
|
|
readonly economicName: string;
|
|
readonly missionName: string;
|
|
/** The mission as the game describes it, including the parameters copied into the mission settings. */
|
|
readonly mission: MissionHeader;
|
|
readonly missionFile: string;
|
|
readonly missionType: string;
|
|
readonly level?: string;
|
|
readonly environment?: string;
|
|
readonly weather?: string;
|
|
readonly difficulty: string;
|
|
readonly country: string;
|
|
readonly playerTeam: 1 | 2;
|
|
readonly countryAllies: string;
|
|
readonly countryAxis: string;
|
|
readonly countriesAllies: readonly string[];
|
|
readonly countriesAxis: readonly string[];
|
|
readonly symmetricTeams: boolean;
|
|
readonly minMrank: number;
|
|
readonly maxMrank: number;
|
|
readonly maxPlayers: number;
|
|
readonly maxBots: number;
|
|
readonly tankBotRespawns: number;
|
|
readonly airBotRespawns: number;
|
|
readonly useTankBots: boolean;
|
|
readonly useShipBots: boolean;
|
|
readonly useSpawnScore: boolean;
|
|
readonly customBots: CustomBotsPool;
|
|
readonly extraBattleSlots: readonly ExtraBattleSlot[];
|
|
readonly randomUnits: Readonly<Record<string, readonly string[]>>;
|
|
readonly randomUnitsEventName?: string;
|
|
}
|
|
|
|
export type MatchmakeOutcome =
|
|
| { readonly ok: true; readonly launch: BattleLaunchPlan }
|
|
| { readonly ok: false; readonly error: string };
|
|
|
|
export class BattleMatchmaker {
|
|
readonly #gameModes: GameModes;
|
|
readonly #vehicles: readonly CatalogueVehicle[];
|
|
readonly #vehiclesByName: ReadonlyMap<string, CatalogueVehicle>;
|
|
readonly #sessions: BattleSessionStore;
|
|
readonly #tuning: BattleTuning;
|
|
readonly #spawnRules: BattleSpawnRulesData | undefined;
|
|
|
|
constructor(
|
|
gameModes: GameModes,
|
|
vehicles: readonly CatalogueVehicle[],
|
|
sessions: BattleSessionStore,
|
|
tuning: BattleTuning,
|
|
spawnRules?: BattleSpawnRulesData,
|
|
) {
|
|
this.#gameModes = gameModes;
|
|
this.#vehicles = vehicles;
|
|
this.#vehiclesByName = new Map(vehicles.map((vehicle) => [vehicle.name, vehicle]));
|
|
this.#sessions = sessions;
|
|
this.#tuning = tuning;
|
|
this.#spawnRules = spawnRules;
|
|
}
|
|
|
|
enqueue(input: EnqueueBattleInput): MatchmakeOutcome {
|
|
const knownCountries = this.#gameModes.countries();
|
|
if (!knownCountries.includes(input.country)) return { ok: false, error: 'BAD_PARAMS' };
|
|
|
|
const candidates = input.modes && input.modes.length > 0 ? input.modes : [input.mode];
|
|
const chosenMode = this.#pickQueuedMode(candidates, input.slotVehicles, input.selectedSlotVehicle);
|
|
if (!chosenMode) return { ok: false, error: 'PVE_NOT_SUPPORTED' };
|
|
|
|
const { declaration } = chosenMode;
|
|
const activeVehicles = activeVehiclesForMode(declaration, input.slotVehicles, input.selectedSlotVehicle, this.#vehiclesByName);
|
|
const playerMrank = lineupMrank(
|
|
activeVehicles, this.#vehiclesByName, declaration.difficulty, declaration.requiredClass, declaration.allowedClasses,
|
|
);
|
|
const minMrank = Math.max(declaration.minQueueMrank ?? 0, playerMrank - this.#tuning.brSpreadMrank);
|
|
const maxMrank = Math.max(minMrank, playerMrank + this.#tuning.brSpreadMrank);
|
|
const mission = pickMission(twoSidedMissionsOf(chosenMode), input.bannedMissions, input.dislikedMissions);
|
|
const coalitions = selectBattleCoalitions({
|
|
vehicles: this.#vehicles,
|
|
knownCountries,
|
|
playerCountry: input.country,
|
|
difficulty: declaration.difficulty,
|
|
requiredClass: declaration.requiredClass,
|
|
playerMrank,
|
|
minMrank,
|
|
maxMrank,
|
|
tuning: this.#tuning,
|
|
});
|
|
const customBots = selectCustomBots(this.#vehicles, {
|
|
difficulty: declaration.difficulty,
|
|
requiredClass: declaration.requiredClass,
|
|
allowedClasses: declaration.allowedClasses,
|
|
countries: [...coalitions.countriesAllies, ...coalitions.countriesAxis],
|
|
centerMrank: playerMrank,
|
|
minMrank,
|
|
maxMrank,
|
|
limitPerType: this.#tuning.botPoolLimitPerType,
|
|
});
|
|
return {
|
|
ok: true,
|
|
launch: this.#buildLaunchPlan(input, chosenMode, mission, coalitions, minMrank, maxMrank, customBots),
|
|
};
|
|
}
|
|
|
|
#pickQueuedMode(
|
|
candidates: readonly OfferedGameMode[],
|
|
slotVehicles: readonly string[],
|
|
selectedSlotVehicle: string | undefined,
|
|
): OfferedGameMode | undefined {
|
|
const pvpModes = candidates.filter(
|
|
(mode) => mode.declaration.displayType !== 'pve_battle' && twoSidedMissionsOf(mode).length > 0,
|
|
);
|
|
if (pvpModes.length === 0) return undefined;
|
|
const eligible = pvpModes.filter((mode) =>
|
|
isModeEligibleForLineup(mode.declaration, slotVehicles, selectedSlotVehicle, this.#vehiclesByName));
|
|
const pool = eligible.length > 0 ? eligible : pvpModes.filter((mode) => !mode.declaration.tag);
|
|
if (pool.length === 0) return pvpModes[0];
|
|
const classics = pool.filter((mode) => !mode.declaration.tag);
|
|
const subModes = pool.filter((mode) => Boolean(mode.declaration.tag));
|
|
if (subModes.length > 0 && classics.length > 0) {
|
|
const useSubMode = randomInt(1_000_000) < Math.round(this.#tuning.subModeChance * 1_000_000);
|
|
const bucket = useSubMode ? subModes : classics;
|
|
return bucket[randomInt(bucket.length)];
|
|
}
|
|
return pool[randomInt(pool.length)];
|
|
}
|
|
|
|
#buildLaunchPlan(
|
|
input: EnqueueBattleInput,
|
|
chosenMode: OfferedGameMode,
|
|
mission: MissionHeader,
|
|
coalitions: CoalitionPairing,
|
|
minMrank: number,
|
|
maxMrank: number,
|
|
customBots: CustomBotsPool,
|
|
): BattleLaunchPlan {
|
|
const { declaration } = chosenMode;
|
|
const desiredPlayers = declaration.isSmallTeams ? this.#tuning.smallTeamsMaxPlayers : this.#tuning.maxPlayers;
|
|
const maxPlayers = Math.min(desiredPlayers, maxSlotsForMission(mission, desiredPlayers));
|
|
const maxBots = Math.min(this.#tuning.maxBots, Math.max(1, maxPlayers - 1));
|
|
const missionLocName = resolveMissionLocName(mission);
|
|
const environment = declaration.environmentOverride ?? mission.environment;
|
|
const session = this.#sessions.create({
|
|
userId: input.userId,
|
|
modeId: declaration.gameModeId,
|
|
modeName: declaration.name,
|
|
economicName: declaration.economicName,
|
|
difficulty: declaration.difficulty,
|
|
requiredClass: declaration.requiredClass,
|
|
missionName: mission.name,
|
|
missionPath: mission.path,
|
|
...(missionLocName !== undefined ? { missionLocName } : {}),
|
|
country: input.country,
|
|
startedAtSec: serverTimeToUnixSeconds(serverTimeNow()),
|
|
});
|
|
const allowedSet = new Set(declaration.allowedClasses);
|
|
const extraPlan = selectExtraBattleSlots({
|
|
country: input.country,
|
|
slotVehicles: input.slotVehicles,
|
|
declaration,
|
|
sessionMaxMrank: maxMrank,
|
|
vehiclesByName: this.#vehiclesByName,
|
|
...(this.#spawnRules ? { rules: this.#spawnRules } : {}),
|
|
});
|
|
return {
|
|
sessionId: session.sessionId,
|
|
modeId: declaration.gameModeId,
|
|
modeName: declaration.name,
|
|
economicName: declaration.economicName,
|
|
missionName: mission.name,
|
|
mission,
|
|
missionFile: mission.path,
|
|
missionType: mission.type ?? 'domination',
|
|
...(mission.level !== undefined ? { level: mission.level } : {}),
|
|
...(environment !== undefined ? { environment } : {}),
|
|
...(mission.weather !== undefined ? { weather: mission.weather } : {}),
|
|
difficulty: MISSION_DIFFICULTY[declaration.difficulty],
|
|
country: input.country,
|
|
playerTeam: coalitions.playerTeam,
|
|
countryAllies: coalitions.countryAllies,
|
|
countryAxis: coalitions.countryAxis,
|
|
countriesAllies: coalitions.countriesAllies,
|
|
countriesAxis: coalitions.countriesAxis,
|
|
symmetricTeams: coalitions.symmetricTeams,
|
|
minMrank,
|
|
maxMrank,
|
|
maxPlayers,
|
|
maxBots,
|
|
tankBotRespawns: this.#tuning.tankBotRespawns,
|
|
airBotRespawns: this.#tuning.airBotRespawns,
|
|
useTankBots: allowedSet.has('tank'),
|
|
useShipBots: allowedSet.has('ship'),
|
|
useSpawnScore: declaration.requiredClass === 'tank' && declaration.difficulty === 'realistic',
|
|
customBots,
|
|
extraBattleSlots: extraPlan.extraBattleSlots,
|
|
randomUnits: extraPlan.randomUnits,
|
|
...(extraPlan.randomUnitsEventName ? { randomUnitsEventName: extraPlan.randomUnitsEventName } : {}),
|
|
};
|
|
}
|
|
}
|
|
|
|
function twoSidedMissionsOf(mode: OfferedGameMode): MissionHeader[] {
|
|
return mode.missions.filter((m) => m.playerSlots.teamA > 0 && m.playerSlots.teamB > 0);
|
|
}
|
|
|
|
function activeVehiclesForMode(
|
|
declaration: OfferedGameMode['declaration'],
|
|
slotVehicles: readonly string[],
|
|
selectedSlotVehicle: string | undefined,
|
|
byName: ReadonlyMap<string, CatalogueVehicle>,
|
|
): readonly string[] {
|
|
if (declaration.multiSlot !== false || !selectedSlotVehicle) return slotVehicles;
|
|
const selected = byName.get(selectedSlotVehicle);
|
|
return selected && unitClassOfExpClass(selected.unitClass) === declaration.requiredClass
|
|
? [selectedSlotVehicle]
|
|
: slotVehicles;
|
|
}
|
|
|
|
function isModeEligibleForLineup(
|
|
declaration: OfferedGameMode['declaration'],
|
|
slotVehicles: readonly string[],
|
|
selectedSlotVehicle: string | undefined,
|
|
byName: ReadonlyMap<string, CatalogueVehicle>,
|
|
): boolean {
|
|
if (!declaration.tag) return true;
|
|
const active = activeVehiclesForMode(declaration, slotVehicles, selectedSlotVehicle, byName);
|
|
const minMrank = declaration.minQueueMrank ?? 0;
|
|
const minCrafts = declaration.minCraftsToPlay ?? 1;
|
|
const matchingCount = active.filter((name) => {
|
|
const vehicle = byName.get(name);
|
|
if (!vehicle || unitClassOfExpClass(vehicle.unitClass) !== declaration.requiredClass) return false;
|
|
return vehicleMrank(vehicle, declaration.difficulty, declaration.requiredClass) >= minMrank;
|
|
}).length;
|
|
return matchingCount >= minCrafts;
|
|
}
|
|
|
|
function pickMission(missions: readonly MissionHeader[], banned: ReadonlySet<string>, disliked: ReadonlySet<string>): MissionHeader {
|
|
const unbanned = missions.filter((mission) => !banned.has(mission.name));
|
|
const poolAfterBan = unbanned.length > 0 ? unbanned : missions;
|
|
const preferred = poolAfterBan.filter((mission) => !disliked.has(mission.name));
|
|
const pool = preferred.length > 0 ? preferred : poolAfterBan;
|
|
return pool[randomInt(pool.length)]!;
|
|
}
|
|
|
|
function maxSlotsForMission(mission: MissionHeader, desiredMaxPlayers: number): number {
|
|
const { teamA, teamB } = mission.playerSlots;
|
|
return Math.max(2, Math.min(desiredMaxPlayers, Math.min(teamA, teamB) * 2));
|
|
}
|
|
|
|
function resolveMissionLocName(mission: MissionHeader): string | undefined {
|
|
if (mission.locName && mission.locName.length > 0) return mission.locName;
|
|
if (mission.postfix && mission.postfix.length > 0 && mission.name.endsWith(mission.postfix)) {
|
|
const baseName = mission.name.slice(0, -mission.postfix.length);
|
|
if (baseName.length > 0) return `missions/${mission.postfix};missions/${baseName}`;
|
|
}
|
|
return undefined;
|
|
}
|