/** * Seeds the squadron directory and new members' activity periods from tuning/clans.jsonc. * The directory is written when the server first creates `/clans.json`. */ import type { ClanTuning, PresetClan, PresetMember } from './clan-tuning.ts'; import { ClanRole, type ClanActivityPeriodEntry, type ClanMemberRecord, type ClanRecord } from './clan-types.ts'; const SECONDS_PER_DAY = 86400; export function createDefaultClans(tuning: ClanTuning, nowSec = Math.floor(Date.now() / 1000)): ClanRecord[] { const todayDay = Math.floor(nowSec / SECONDS_PER_DAY); const createdBase = nowSec - tuning.presetClans.createdDaysAgo * SECONDS_PER_DAY; return tuning.presetClans.clans.map((spec, specIndex) => buildSeededClan(spec, specIndex, createdBase + spec.createdAfterDays * SECONDS_PER_DAY, todayDay, tuning)); } /** * The activity periods a member starts with: `periodCount` periods (defaulting to * `activity.startingPeriods`), each with the given activity and the squadron RP it earns. */ export function buildDefaultPeriods( todayDay: number, tuning: ClanTuning, periodActivity = tuning.activity.maxPerPeriod, periodCount = tuning.activity.startingPeriods, ): ClanActivityPeriodEntry[] { if (periodCount <= 0) return []; const { maxPerPeriod, squadronRpPerFullPeriod, rewardPeriodDays } = tuning.activity; const exp = Math.round((periodActivity / maxPerPeriod) * squadronRpPerFullPeriod); return Array.from({ length: periodCount }, (_, idx) => ({ day: todayDay - idx * rewardPeriodDays, activity: periodActivity, exp })); } /** * Idempotently appends deterministic filler NPCs to an existing preset clan whose * persisted roster is smaller than its configured `targetMemberCount`, without altering * any existing player or NPC members. */ export function backfillPresetClanMembers( clan: ClanRecord, spec: PresetClan, specIndex: number, todayDay: number, tuning: ClanTuning, ): boolean { const targetCount = Math.min(clan.mlimit, spec.targetMemberCount); if (clan.members.length >= targetCount) return false; const existingUids = new Set(clan.members.map((m) => m.uid)); const fillerSpecs = buildPresetFillerSpecs(spec, specIndex, tuning); let added = false; for (let i = 0; i < fillerSpecs.length && clan.members.length < targetCount; i++) { const npc = fillerSpecs[i]!; if (existingUids.has(npc.uid)) continue; existingUids.add(npc.uid); const memberIdx = spec.members.length + i; clan.members.push(buildSeededMember(spec, npc, memberIdx, clan.cdateSec, todayDay, tuning)); added = true; } return added; } function buildSeededClan( spec: PresetClan, specIndex: number, cdateSec: number, todayDay: number, tuning: ClanTuning, ): ClanRecord { const mlimit = spec.type === 'battalion' ? tuning.memberLimits.battalionInitial : tuning.memberLimits.normal; const allSpecs: PresetMember[] = [ ...spec.members, ...buildPresetFillerSpecs(spec, specIndex, tuning), ].slice(0, mlimit); const members = allSpecs.map((npc, idx) => buildSeededMember(spec, npc, idx, cdateSec, todayDay, tuning)); const leader = spec.members[0]!; return { id: spec.id, name: spec.name, tag: spec.tag, lastPaidTag: spec.tag, type: spec.type, slogan: spec.slogan, desc: spec.desc, announcement: spec.announcement, region: spec.region, status: 'open', autoaccept: spec.autoAccept, mlimit, mspent: 0, cdateSec, changedByUid: leader.uid, changedByNick: leader.nick, changedTimeSec: cdateSec, regionLastUpdatedSec: 0, lastRewardDay: todayDay, stats: { dr_era5_arc: spec.rating.arc, dr_era5_hist: spec.rating.hist, dr_era5_sim: spec.rating.sim, ...tuning.presetClans.stats }, members, candidates: [], blacklist: [], logs: [{ id: 1, ev: 'create', timeSec: cdateSec, uId: leader.uid, uN: leader.nick, name: spec.name, type: spec.type, tag: spec.tag, desc: spec.desc, slogan: spec.slogan, region: spec.region, announcement: spec.announcement, }], bestRewards: [], }; } function buildPresetFillerSpecs(spec: PresetClan, specIndex: number, tuning: ClanTuning): PresetMember[] { const mlimit = spec.type === 'battalion' ? tuning.memberLimits.battalionInitial : tuning.memberLimits.normal; const targetCount = Math.min(mlimit, Math.max(spec.members.length, spec.targetMemberCount)); const needed = targetCount - spec.members.length; if (needed <= 0) return []; const filler = tuning.presetClans.fillerMembers; const clanSlot = presetClanSlot(spec.id, specIndex); const usedUids = new Set(spec.members.map((m) => m.uid)); const span = Math.max(0, filler.activityMax - filler.activityMin); const result: PresetMember[] = []; let ordinal = spec.members.length + 1; while (result.length < needed) { const uid = String(filler.uidBase + clanSlot * filler.uidStride + ordinal); if (usedUids.has(uid)) { ordinal++; continue; } usedUids.add(uid); const callsign = filler.callsigns[(clanSlot * 7 + ordinal) % filler.callsigns.length]!; const title = filler.titles[(clanSlot * 3 + ordinal) % filler.titles.length]!; const icon = filler.icons[(clanSlot * 5 + ordinal) % filler.icons.length]!; const unit = filler.units[(clanSlot * 11 + ordinal) % filler.units.length]!; const activityOffset = span > 0 ? (clanSlot * 13 + ordinal * 17) % (span + 1) : 0; const activity = Math.min(tuning.activity.maxPerPeriod, filler.activityMin + activityOffset); result.push({ uid, nick: `${callsign}_${clanSlot}${String(ordinal).padStart(2, '0')}`, role: ClanRole.REGULAR, title, icon, unit, activity, }); ordinal++; } return result; } function presetClanSlot(clanId: string, specIndex: number): number { const parsed = Number(clanId); if (Number.isSafeInteger(parsed) && parsed > 0) { const remainder = parsed % 1000; if (remainder > 0) return remainder; } return specIndex + 1; } function buildSeededMember( spec: PresetClan, npc: PresetMember, idx: number, cdateSec: number, todayDay: number, tuning: ClanTuning, ): ClanMemberRecord { const periods = buildDefaultPeriods(todayDay, tuning, npc.activity, tuning.presetClans.npcHistoryPeriods); const { step, floor } = tuning.presetClans.memberRating; return { uid: npc.uid, nick: npc.nick, role: npc.role, joinDateSec: cdateSec + idx * tuning.presetClans.memberJoinSpacingMinutes * 60, isNpc: true, npcTitle: npc.title, npcPilotIcon: npc.icon, npcFrame: '', npcShowcaseUnit: npc.unit, curActivity: npc.activity, totalActivity: periods.length > 0 ? periods.reduce((sum, p) => sum + p.activity, 0) : npc.activity, earnedSquadronExp: periods.reduce((sum, p) => sum + p.exp, 0), ratingArc: Math.max(floor.arc, spec.rating.arc - idx * step.arc), ratingHist: Math.max(floor.hist, spec.rating.hist - idx * step.hist), ratingSim: Math.max(floor.sim, spec.rating.sim - idx * step.sim), periods, }; }