/** * Authentic nation coalition pairing for Arcade, Realistic, and Simulator battles. * * Filters eligible countries by tech-tree vehicle availability in the match's BR window, * assigns the player uniformly to Team 1 (`allies`) or Team 2 (`axis`), and builds * per-team bare country lists (`"usa"`, `"germany"`, ...) with `playerCountry` at index 0 * of the player's team array. */ import { randomInt } from 'node:crypto'; import type { CatalogueVehicle } from '../game-data/datasets/vehicle-catalogue.ts'; import type { ModeDifficulty, UnitClass } from '../game-modes/catalogue/game-mode-declaration.ts'; import { stripCountryPrefix, unitClassOfExpClass, vehicleMrank } from './battle-ranks.ts'; import type { BattleTuning } from './battle-tuning.ts'; const SIM_WW2_ALLIES: readonly string[] = ['usa', 'ussr', 'britain', 'china', 'france', 'israel']; const SIM_WW2_AXIS: readonly string[] = ['germany', 'japan', 'italy', 'sweden']; const SIM_CW_WEST: readonly string[] = ['usa', 'germany', 'britain', 'japan', 'italy', 'france', 'sweden', 'israel']; const SIM_CW_EAST: readonly string[] = ['ussr', 'china']; export interface CoalitionPairingInput { readonly vehicles: readonly CatalogueVehicle[]; readonly knownCountries: readonly string[]; readonly playerCountry: string; readonly difficulty: ModeDifficulty; readonly requiredClass: UnitClass; readonly playerMrank: number; readonly minMrank: number; readonly maxMrank: number; readonly tuning: BattleTuning; } export interface CoalitionPairing { readonly playerTeam: 1 | 2; readonly countryAllies: string; readonly countryAxis: string; readonly countriesAllies: readonly string[]; readonly countriesAxis: readonly string[]; readonly symmetricTeams: boolean; } export function selectBattleCoalitions(input: CoalitionPairingInput): CoalitionPairing { const playerCountry = stripCountryPrefix(input.playerCountry); const eligible = eligibleCountriesForBracket(input, playerCountry); const playerTeam: 1 | 2 = randomInt(2) === 0 ? 1 : 2; const sides = input.difficulty === 'arcade' ? buildArcadeSides(eligible, playerCountry) : input.difficulty === 'simulator' ? buildSimulatorSides(eligible, playerCountry, input.playerMrank, input.tuning.simColdWarMinMrank) : buildRealisticSides(eligible, playerCountry, input.requiredClass, input.tuning); const countriesAllies = playerTeam === 1 ? sides.playerSide : sides.enemySide; const countriesAxis = playerTeam === 2 ? sides.playerSide : sides.enemySide; return { playerTeam, countryAllies: countriesAllies[0] ?? playerCountry, countryAxis: countriesAxis[0] ?? playerCountry, countriesAllies, countriesAxis, symmetricTeams: sides.symmetricTeams, }; } interface TeamSides { readonly playerSide: readonly string[]; readonly enemySide: readonly string[]; readonly symmetricTeams: boolean; } function eligibleCountriesForBracket(input: CoalitionPairingInput, playerCountry: string): string[] { const knownBare = [...new Set(input.knownCountries.map(stripCountryPrefix).filter((c) => c.length > 0))]; const knownSet = new Set(knownBare); const inWindow = new Set(); const inClass = new Set(); for (const vehicle of input.vehicles) { if (!isStandardTreeVehicle(vehicle, input.requiredClass)) continue; const bare = stripCountryPrefix(vehicle.country); if (!knownSet.has(bare)) continue; inClass.add(bare); const mrank = vehicleMrank(vehicle, input.difficulty, input.requiredClass); if (mrank >= input.minMrank && mrank <= input.maxMrank) inWindow.add(bare); } inWindow.add(playerCountry); if (inWindow.size >= 2) return knownBare.filter((c) => inWindow.has(c)); inClass.add(playerCountry); if (inClass.size >= 2) return knownBare.filter((c) => inClass.has(c)); return knownBare.includes(playerCountry) ? knownBare : [playerCountry, ...knownBare]; } function isStandardTreeVehicle(vehicle: CatalogueVehicle, requiredClass: UnitClass): boolean { return vehicle.inTechTree && !vehicle.onlyShownWhenOwned && !vehicle.premiumPack && !vehicle.gift && !vehicle.event && vehicle.researchType === undefined && unitClassOfExpClass(vehicle.unitClass) === requiredClass; } function buildArcadeSides(eligible: readonly string[], playerCountry: string): TeamSides { const playerSide = placeFirst(shuffle(eligible), playerCountry); const enemySide = shuffle(eligible); return { playerSide, enemySide, symmetricTeams: true }; } function buildSimulatorSides( eligible: readonly string[], playerCountry: string, playerMrank: number, coldWarMinMrank: number, ): TeamSides { const eligibleSet = new Set(eligible); const blocA = (playerMrank < coldWarMinMrank ? SIM_WW2_ALLIES : SIM_CW_WEST).filter((c) => eligibleSet.has(c)); const blocB = (playerMrank < coldWarMinMrank ? SIM_WW2_AXIS : SIM_CW_EAST).filter((c) => eligibleSet.has(c)); const playerInB = blocB.includes(playerCountry); const rawPlayerBloc = playerInB ? [...blocB] : [...blocA]; const rawEnemyBloc = playerInB ? [...blocA] : [...blocB]; if (!rawPlayerBloc.includes(playerCountry)) rawPlayerBloc.unshift(playerCountry); const playerOthers = shuffle(rawPlayerBloc.filter((c) => c !== playerCountry)); let enemySide = shuffle(rawEnemyBloc.filter((c) => c !== playerCountry)); if (enemySide.length === 0 && playerOthers.length > 0) { enemySide = [playerOthers.pop()!]; } if (enemySide.length === 0) { const fallback = eligible.find((c) => c !== playerCountry) ?? playerCountry; enemySide = [fallback]; } return { playerSide: [playerCountry, ...playerOthers], enemySide, symmetricTeams: false, }; } function buildRealisticSides( eligible: readonly string[], playerCountry: string, requiredClass: UnitClass, tuning: BattleTuning, ): TeamSides { const mixedChance = requiredClass === 'tank' ? tuning.rbGroundMixedChance : requiredClass === 'ship' ? tuning.rbNavalMixedChance : tuning.rbAirMixedChance; const roll = randomInt(1_000_000) / 1_000_000; if (roll >= mixedChance && eligible.length >= 2) { return buildDisjointRealisticSides(eligible, playerCountry, tuning); } return buildMixedRealisticSides(eligible, playerCountry, tuning); } function buildDisjointRealisticSides( eligible: readonly string[], playerCountry: string, tuning: BattleTuning, ): TeamSides { const others = shuffle(eligible.filter((c) => c !== playerCountry)); const total = eligible.length; const minC = tuning.rbCoalitionMinCountries; const maxC = tuning.rbCoalitionMaxCountries; const playerMax = total >= minC * 2 ? Math.min(maxC, total - minC) : Math.min(maxC, total - 1); const playerMin = total >= minC * 2 ? Math.min(minC, playerMax) : 1; const playerCount = randomRangeInclusive(playerMin, playerMax); const remaining = total - playerCount; const enemyMax = Math.min(maxC, remaining); const enemyMin = Math.min(minC, enemyMax); const enemyCount = randomRangeInclusive(enemyMin, enemyMax); return { playerSide: [playerCountry, ...others.slice(0, playerCount - 1)], enemySide: others.slice(playerCount - 1, playerCount - 1 + enemyCount), symmetricTeams: false, }; } function buildMixedRealisticSides( eligible: readonly string[], playerCountry: string, tuning: BattleTuning, ): TeamSides { if (eligible.length < 2 || randomInt(2) === 0) { return buildArcadeSides(eligible, playerCountry); } const total = eligible.length; const minCount = Math.min(tuning.rbCoalitionMinCountries, total); const maxCount = Math.min(tuning.rbCoalitionMaxCountries, total); const playerCount = randomRangeInclusive(minCount, maxCount); const enemyCount = randomRangeInclusive(minCount, maxCount); const others = shuffle(eligible.filter((c) => c !== playerCountry)); const playerSide = [playerCountry, ...others.slice(0, playerCount - 1)]; const shared = playerSide[randomInt(playerSide.length)]!; const enemyPool = shuffle(eligible.filter((c) => c !== shared)); const enemySide = shuffle([shared, ...enemyPool.slice(0, enemyCount - 1)]); return { playerSide, enemySide, symmetricTeams: sameCountrySet(playerSide, enemySide), }; } function placeFirst(countries: readonly string[], first: string): string[] { return [first, ...countries.filter((c) => c !== first)]; } function sameCountrySet(a: readonly string[], b: readonly string[]): boolean { if (a.length !== b.length) return false; const setB = new Set(b); return a.every((item) => setB.has(item)); } function randomRangeInclusive(min: number, max: number): number { return max <= min ? min : min + randomInt(max - min + 1); } function shuffle(items: readonly T[]): T[] { const copy = [...items]; for (let i = copy.length - 1; i > 0; i--) { const j = randomInt(i + 1); const tmp = copy[i]!; copy[i] = copy[j]!; copy[j] = tmp; } return copy; }