108 lines
3.9 KiB
TypeScript
108 lines
3.9 KiB
TypeScript
import { content } from "@deadhead/content";
|
|
import type { PlayerState } from "@deadhead/schema";
|
|
import { pilotLevel, skillPointsAvailable } from "@deadhead/sim";
|
|
import { and, eq, isNull } from "drizzle-orm";
|
|
import type { Queryable } from "../db/client";
|
|
import {
|
|
bays,
|
|
companies,
|
|
discoveredLocations,
|
|
dispatches,
|
|
pilots,
|
|
players,
|
|
ships,
|
|
} from "../db/schema";
|
|
import { toApiDispatch, toApiStatus } from "./mappers";
|
|
|
|
/** Reads everything the client needs about one player. */
|
|
export async function loadPlayerState(db: Queryable, playerId: string): Promise<PlayerState> {
|
|
const [player] = await db.select().from(players).where(eq(players.id, playerId));
|
|
const [company] = await db.select().from(companies).where(eq(companies.playerId, playerId));
|
|
if (!player || !company) throw new Error(`Player ${playerId} not found`);
|
|
|
|
const shipRows = await db.select().from(ships).where(eq(ships.playerId, playerId));
|
|
const pilotRows = await db.select().from(pilots).where(eq(pilots.playerId, playerId));
|
|
const bayRows = await db.select().from(bays).where(eq(bays.playerId, playerId));
|
|
const discovered = await db
|
|
.select({ locationId: discoveredLocations.locationId })
|
|
.from(discoveredLocations)
|
|
.where(eq(discoveredLocations.playerId, playerId));
|
|
const active = await db
|
|
.select()
|
|
.from(dispatches)
|
|
.where(and(eq(dispatches.playerId, playerId), eq(dispatches.status, "in_flight")));
|
|
const unacknowledged = await db
|
|
.select()
|
|
.from(dispatches)
|
|
.where(
|
|
and(
|
|
eq(dispatches.playerId, playerId),
|
|
eq(dispatches.status, "resolved"),
|
|
isNull(dispatches.acknowledgedAt),
|
|
),
|
|
);
|
|
|
|
const discoveredIds = new Set(discovered.map((d) => d.locationId));
|
|
const shipById = new Map(shipRows.map((s) => [s.id, s]));
|
|
const activeByShip = new Map(active.filter((d) => d.shipId).map((d) => [d.shipId!, d.id]));
|
|
const pilotByShip = new Map(
|
|
pilotRows.filter((p) => p.assignedShipId).map((p) => [p.assignedShipId!, p.id]),
|
|
);
|
|
|
|
return {
|
|
company: { id: company.id, name: company.name, credits: company.credits },
|
|
ships: shipRows.map((s) => ({
|
|
id: s.id,
|
|
modelId: s.modelId,
|
|
name: s.name,
|
|
status: toApiStatus(s.status),
|
|
locationId: s.status === "in_flight" ? null : s.locationId,
|
|
dispatchId: activeByShip.get(s.id) ?? null,
|
|
pilotId: pilotByShip.get(s.id) ?? null,
|
|
})),
|
|
pilots: pilotRows.map((p) => {
|
|
const level = pilotLevel(p.xp, content.config);
|
|
const locationId =
|
|
p.status === "in_flight"
|
|
? null
|
|
: p.assignedShipId
|
|
? (shipById.get(p.assignedShipId)?.locationId ?? null)
|
|
: p.locationId;
|
|
return {
|
|
id: p.id,
|
|
name: p.name,
|
|
isAvatar: p.isAvatar,
|
|
xp: p.xp,
|
|
level,
|
|
skills: p.skills,
|
|
skillPointsAvailable: skillPointsAvailable(level, p.skillPointsSpent, content.config),
|
|
assignedShipId: p.assignedShipId,
|
|
status: toApiStatus(p.status),
|
|
locationId,
|
|
};
|
|
}),
|
|
bays: bayRows.map((b) => ({ id: b.id, locationId: b.locationId })),
|
|
locations: content.locations.map((l) => ({ id: l.id, discovered: discoveredIds.has(l.id) })),
|
|
tutorial:
|
|
player.tutorialActive && player.loanerLocationId
|
|
? {
|
|
loanerLocationId: player.loanerLocationId as "earth" | "moon",
|
|
runInFlight: active.some((d) => d.tutorial),
|
|
}
|
|
: null,
|
|
activeDispatches: active.map(toApiDispatch),
|
|
summaries: unacknowledged.map((d) => ({
|
|
dispatchId: d.id,
|
|
shipId: d.record.shipId,
|
|
pilotId: d.pilotId,
|
|
originId: d.record.originId,
|
|
destinationId: d.record.destinationId,
|
|
tutorial: d.tutorial,
|
|
credits: d.resolution?.credits ?? 0,
|
|
pilotXp: d.resolution?.pilotXp ?? 0,
|
|
discoveredLocationId: d.resolution?.discoversDestination ? d.record.destinationId : null,
|
|
resolvedAt: d.resolvedAt ?? d.arrivalAt,
|
|
})),
|
|
};
|
|
}
|