carrier schema
This commit is contained in:
parent
b4293b2736
commit
873273027e
@ -1,29 +1,58 @@
|
||||
import Updux, { createPayloadAction } from "updux";
|
||||
import u from "@yanick/updeep-remeda";
|
||||
import { reqs, type Reqs } from "$lib/shipDux/reqs";
|
||||
import * as j from "json-schema-shorthand";
|
||||
import { reqsSchema } from "./reqs";
|
||||
import type { FromSchema } from "json-schema-to-ts";
|
||||
|
||||
type Squadron = {
|
||||
type: string;
|
||||
reqs: Reqs;
|
||||
};
|
||||
const squadronTypes = [
|
||||
"standard",
|
||||
"fast",
|
||||
"heavy",
|
||||
"interceptor",
|
||||
"attack",
|
||||
"long range",
|
||||
"torpedo",
|
||||
] as const;
|
||||
|
||||
const initialState = {
|
||||
type SquadronType = (typeof squadronTypes)[number];
|
||||
|
||||
const squadronCost = {
|
||||
standard: 3,
|
||||
fast: 4,
|
||||
heavy: 5,
|
||||
interceptor: 3,
|
||||
attack: 4,
|
||||
"long range": 4,
|
||||
torpedo: 6,
|
||||
} as const;
|
||||
|
||||
const squadronSchema = j.object({
|
||||
type: {
|
||||
type: "string",
|
||||
enum: squadronTypes,
|
||||
},
|
||||
reqs: reqsSchema,
|
||||
} as const);
|
||||
|
||||
type SquadronSchema = FromSchema<typeof squadronSchema>;
|
||||
|
||||
const carrierSchema = j.object({
|
||||
nbrBays: j.number({ default: 0 }),
|
||||
squadrons: j.array(squadronSchema, { default: [] }),
|
||||
reqs: reqsSchema,
|
||||
} as const);
|
||||
|
||||
type CarrierSchema = FromSchema<typeof carrierSchema>;
|
||||
|
||||
const initialState: CarrierSchema = {
|
||||
nbrBays: 0,
|
||||
squadrons: [] as Squadron[],
|
||||
squadrons: [],
|
||||
reqs,
|
||||
};
|
||||
|
||||
export const squadronTypes = [
|
||||
{ type: "standard", cost: 3 },
|
||||
{ type: "fast", cost: 4 },
|
||||
{ type: "heavy", cost: 5 },
|
||||
{ type: "interceptor", cost: 3 },
|
||||
{ type: "attack", cost: 4 },
|
||||
{ type: "long range", cost: 4 },
|
||||
{ type: "torpedo", cost: 6 },
|
||||
];
|
||||
|
||||
const setNbrCarrierBays = createPayloadAction<number>("setNbrCarrierBays");
|
||||
|
||||
const setSquadronType = createPayloadAction(
|
||||
"setSquadronType",
|
||||
(id: number, type: string) => ({ id, type })
|
||||
@ -34,7 +63,7 @@ export const carrierDux = new Updux({
|
||||
actions: { setNbrCarrierBays, setSquadronType },
|
||||
});
|
||||
|
||||
function calcBaysReqs(bays) {
|
||||
function calcBaysReqs(bays: number): Reqs {
|
||||
return {
|
||||
mass: 9 * bays,
|
||||
cost: 18 * bays,
|
||||
@ -52,9 +81,9 @@ const adjustSquadrons = (bays: number) => (squadrons) => {
|
||||
...squadrons,
|
||||
...Array.from({ length: bays - squadrons.length })
|
||||
.fill({
|
||||
type: squadronTypes[0].type,
|
||||
type: squadronTypes[0],
|
||||
reqs: {
|
||||
cost: 6 * squadronTypes[0].cost,
|
||||
cost: 6 * squadronCost[squadronTypes[0]],
|
||||
mass: 6,
|
||||
},
|
||||
})
|
||||
@ -83,9 +112,9 @@ carrierDux.addMutation(setSquadronType, ({ id, type }) => {
|
||||
});
|
||||
});
|
||||
|
||||
function squadronReqs(type: string) {
|
||||
function squadronReqs(type: SquadronType): Reqs {
|
||||
return {
|
||||
mass: 6,
|
||||
cost: 6 * squadronTypes.find((s) => s.type === type)?.cost,
|
||||
cost: 6 * squadronCost[type],
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user