Files
tidefront/packages/engine/src/fields-escort.ts
T
db9979 c20c26dacb Felder 2: Geleitabteilung, Verstärkung, Radar-Vorwarnung, Wochenziele Felderträge und Gründung
- Geleitabteilung aus der Besatzung begleitet Ertragskonvois und kämpft bei Abfangversuchen
  (Kartell, Kriegsgegner); verteidigt das Feld nicht, Rest bleibt gültige Besatzung
- Verstärkung: Besetzen gegen eigenes Feld fügt Schiffe der Besatzung hinzu (auch aus
  anderem Hafen, Veteranen gewichtet gemischt)
- Radar-Vorwarnung (field_warning) bei Spielerangriffen auf Felder, Push und Meldung
- Wochenziele feldertrag und gruendung
- Balance fields: radarWarningFactor 0.5, escortMinRemainCommandPoints 6
- Migration 0016_felder2 (resource_field.escort), Simulation, Doku, Tests
2026-11-27 08:46:26 +01:00

59 lines
3.8 KiB
TypeScript

// Felder: Geleitabteilung (docs/REGELN-FELDER.md, Abschnitt Geleitschutz). Ein Teil der Besatzung begleitet die
// Ertragskonvois des Felds und kämpft bei Abfangversuchen (Kartell, Kriegsgegner) mit; er verteidigt das Feld nicht.
// Gespeichert in resource_field.escort; die Schiffe bleiben Teil der Besatzungsmission (returning), Verluste im
// Konvoikampf werden dort abgezogen. Eingehängt in naval-convoy.ts (liveEscort, fightEscort) und fields-run.ts.
import { and, eq } from "drizzle-orm";
import { activeUnits, clampEscort, escortCheck, subtractCounts, type Counts } from "@tidefront/game-core";
import { tables, type DbOrTx, type Tx } from "@tidefront/db";
import { balance, GameError, type Engine } from "./context.ts";
import { runCommand } from "./commands.ts";
import { fieldById } from "./fields-core.ts";
/** Aktuelle Geleitabteilung einer Besatzungsmission (leer, wenn das Feld nicht mehr gehalten wird). */
export async function fieldEscortOf(db: DbOrTx, missionId: number | null): Promise<Counts> {
if (missionId === null) return {};
const [f] = await db.select({ escort: tables.resourceField.escort, ownerId: tables.resourceField.ownerId })
.from(tables.resourceField).where(eq(tables.resourceField.missionId, missionId));
if (!f || f.ownerId === null) return {};
const [m] = await db.select({ status: tables.mission.status, returning: tables.mission.returning }).from(tables.mission).where(eq(tables.mission.id, missionId));
if (!m || m.status !== "stationed") return {};
return clampEscort(activeUnits((m.returning as Counts | null) ?? {}), (f.escort as Counts) ?? {});
}
/** Verluste der Geleitabteilung im Konvoikampf: aus Besatzung und Geleit streichen. */
export async function fieldEscortLosses(tx: Tx, missionId: number | null, before: Counts, after: Counts): Promise<void> {
if (missionId === null) return;
const lost = subtractCounts(before, after);
if (Object.keys(lost).length === 0) return;
const [m] = await tx.select().from(tables.mission).where(eq(tables.mission.id, missionId)).for("update");
if (!m || m.status !== "stationed") return;
const garrison = subtractCounts(activeUnits((m.returning as Counts | null) ?? {}), lost);
await tx.update(tables.mission).set({ returning: garrison }).where(eq(tables.mission.id, m.id));
const [f] = await tx.select({ id: tables.resourceField.id, escort: tables.resourceField.escort }).from(tables.resourceField)
.where(eq(tables.resourceField.missionId, missionId));
if (f) {
const escort = clampEscort(garrison, subtractCounts((f.escort as Counts) ?? {}, lost));
await tx.update(tables.resourceField).set({ escort }).where(eq(tables.resourceField.id, f.id));
}
}
/**
* Geleitabteilung eines eigenen Felds festlegen (leer = aufheben). Gilt ab dem nächsten Ertragskonvoi.
* Codes: no_field, field_no_garrison, field_escort_invalid, field_escort_too_large.
*/
export function setFieldEscort(engine: Engine, playerId: number, key: string, fieldId: number, units: Counts) {
return runCommand<{ ok: true; escort: Counts }>(engine, playerId, key, "fieldEscort", async (ctx) => {
const field = await fieldById(ctx.tx, fieldId, true);
if (!field || field.ownerId !== playerId) throw new GameError("no_field");
const [m] = field.missionId !== null
? await ctx.tx.select().from(tables.mission).where(and(eq(tables.mission.id, field.missionId), eq(tables.mission.status, "stationed")))
: [];
if (!m) throw new GameError("field_no_garrison");
const r = escortCheck(activeUnits((m.returning as Counts | null) ?? {}), units, balance);
if (!r.ok) throw new GameError(r.code);
await ctx.tx.update(tables.resourceField).set({ escort: r.escort }).where(eq(tables.resourceField.id, field.id));
ctx.notify.push([[playerId], "missions"]);
return { ok: true as const, escort: r.escort };
});
}