mirror of
https://github.com/db9979/tidefront.git
synced 2026-12-05 12:24:13 +01:00
- packages/world: Hexraster Nordatlantik und Mittelmeer, Küstenfelder, Nestplätze, Handelshäfen - packages/db: Drizzle-Schema und Migration (game_event, trade_ledger, command_log, ...) - packages/engine: Worker-Queue mit FOR UPDATE SKIP LOCKED, Handler für Bau, Ausbildung, Missionen, Kartell, Lieferungen; Befehle mit Idempotenz-Schlüssel; Börse mit gesperrter Marktzeile und Journal; Prüflauf; Integrationstests gegen PostgreSQL - apps/api: Fastify, tRPC, Zod, Sitzungs-Cookie, Valkey für Pub/Sub und Limits, /ws - apps/worker: Ereignisschleife, Prüflauf, Aufräumen
40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
// Startdaten nach der Migration: Nester, Marktzeilen, Einladungscodes (idempotent).
|
|
import { sql } from "drizzle-orm";
|
|
import { market as marketCore } from "@tidefront/game-core";
|
|
import { nestSites } from "@tidefront/world";
|
|
import { tables, type Db } from "@tidefront/db";
|
|
import { marketBalance } from "./context.ts";
|
|
|
|
/** INVITE_CODES="CODE1:5,CODE2" -> Code mit Anzahl Nutzungen (Vorgabe 1). */
|
|
export function parseInviteCodes(raw: string | undefined): { code: string; maxUses: number }[] {
|
|
return (raw ?? "")
|
|
.split(",")
|
|
.map((s) => s.trim())
|
|
.filter(Boolean)
|
|
.map((s) => {
|
|
const [code, uses] = s.split(":");
|
|
return { code: code.trim(), maxUses: Math.max(1, Number(uses ?? 1) || 1) };
|
|
});
|
|
}
|
|
|
|
export async function seed(db: Db, inviteCodes: string | undefined): Promise<{ nests: number; invites: number }> {
|
|
const sites = nestSites();
|
|
await db
|
|
.insert(tables.nest)
|
|
.values(sites.map((s) => ({ col: s.col, row: s.row, level: s.level })))
|
|
.onConflictDoNothing();
|
|
const m = marketCore.createMarketState(marketBalance, Date.now());
|
|
await db
|
|
.insert(tables.market)
|
|
.values(Object.entries(m).map(([resource, state]) => ({ resource, state })))
|
|
.onConflictDoNothing();
|
|
const codes = parseInviteCodes(inviteCodes);
|
|
for (const c of codes) {
|
|
await db
|
|
.insert(tables.inviteCode)
|
|
.values(c)
|
|
.onConflictDoUpdate({ target: tables.inviteCode.code, set: { maxUses: sql`greatest(${tables.inviteCode.maxUses}, ${c.maxUses})` } });
|
|
}
|
|
return { nests: sites.length, invites: codes.length };
|
|
}
|