Skip to content

Commit

Permalink
feat: Add configuration options for Reactor systems.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderson1993 committed Jan 28, 2023
1 parent 2d74f20 commit 694bdcf
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 0 deletions.
175 changes: 175 additions & 0 deletions client/src/pages/Config/ShipSystems/SystemConfigs/reactor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import {q} from "@client/context/AppContext";
import {toast} from "@client/context/ToastContext";
import InfoTip from "@thorium/ui/InfoTip";
import Input from "@thorium/ui/Input";
import {useContext, useReducer} from "react";
import {Navigate, useParams} from "react-router-dom";
import {ShipPluginIdContext} from "../../Ships/ShipSystemOverrideContext";
import {OverrideResetButton} from "../OverrideResetButton";

export default function ReactorsConfig() {
const {pluginId, systemId, shipId} = useParams() as {
pluginId: string;
systemId: string;
shipId: string;
};
const shipPluginId = useContext(ShipPluginIdContext);

const [system] = q.plugin.systems.reactor.get.useNetRequest({
pluginId,
systemId,
shipId,
shipPluginId,
});
const [rekey, setRekey] = useReducer(() => Math.random(), Math.random());
const key = `${systemId}${rekey}`;
if (!system) return <Navigate to={`/config/${pluginId}/systems`} />;

return (
<fieldset key={key} className="flex-1 overflow-y-auto">
<div className="flex flex-wrap">
<div className="flex-1 pr-4">
<div className="pb-2 flex items-center">
<Input
labelHidden={false}
inputMode="numeric"
pattern="[0-9]*"
label="Optimal Output Percent"
placeholder={"0.5"}
helperText={
"The percent of the possible reactor output that provides the highest fuel-to-energy conversion."
}
defaultValue={system.optimalOutputPercent}
onBlur={async e => {
if (!e.target.value || isNaN(Number(e.target.value))) return;
try {
await q.plugin.systems.reactor.update.netSend({
pluginId,
systemId: systemId,
shipId,
shipPluginId,
optimalOutputPercent: Number(e.target.value),
});
} catch (err) {
if (err instanceof Error) {
toast({
title: "Error changing optimal output percent",
body: err.message,
color: "error",
});
}
}
}}
/>
<OverrideResetButton
property="optimalOutputPercent"
setRekey={setRekey}
className="mt-6"
/>
<InfoTip>
<p className="mb-2">
If the optimal output percent is set to 0.7 (70%) and the
reactor is set to run at 100% output, it will produce more
energy, but consume fuel at about 1.5x per unit of power
produced.
</p>
<p className="mb-2">
Likewise, if the reactor is producing 50% of the possible power,
it would only consume 0.7x the fuel per unit of power produced,
which would make the fuel last longer.
</p>
<p className="mb-2">
This value is the default setting for the reactors.
</p>
</InfoTip>
</div>
<div className="pb-2 flex items-center">
<Input
labelHidden={false}
inputMode="numeric"
pattern="[0-9]*"
label="Number of Reactors"
placeholder={"0.5"}
helperText={
"How many reactors will be created when this system is spawned on a ship."
}
defaultValue={system.reactorCount}
onBlur={async e => {
if (!e.target.value || isNaN(Number(e.target.value))) return;
try {
await q.plugin.systems.reactor.update.netSend({
pluginId,
systemId: systemId,
shipId,
shipPluginId,
reactorCount: Number(e.target.value),
});
} catch (err) {
if (err instanceof Error) {
toast({
title: "Error changing reactor count",
body: err.message,
color: "error",
});
}
}
}}
/>
<OverrideResetButton
property="reactorCount"
setRekey={setRekey}
className="mt-6"
/>
<InfoTip>
<p className="mb-2">
Reactor's power output is automatically determined when a flight
starts based on the power requirements of the systems associated
with the ship. Adding more reactors allows each to have
different power outputs, changes in heat, efficiency, damage,
etc. and provides some degree of redundancy.
</p>
</InfoTip>
</div>
<div className="pb-2 flex items-center">
<Input
labelHidden={false}
inputMode="numeric"
pattern="[0-9]*"
label="Power Multiplier"
placeholder={"1"}
helperText={
"Determines the total output of all reactors by multiplying this value by the default power for all ship systems."
}
defaultValue={system.powerMultiplier}
onBlur={async e => {
if (!e.target.value || isNaN(Number(e.target.value))) return;
try {
await q.plugin.systems.reactor.update.netSend({
pluginId,
systemId: systemId,
shipId,
shipPluginId,
powerMultiplier: Number(e.target.value),
});
} catch (err) {
if (err instanceof Error) {
toast({
title: "Error changing power multiplier",
body: err.message,
color: "error",
});
}
}
}}
/>
<OverrideResetButton
property="powerMultiplier"
setRekey={setRekey}
className="mt-6"
/>
</div>
</div>
</div>
</fieldset>
);
}
2 changes: 2 additions & 0 deletions client/src/pages/Config/data/systems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {impulse} from "./impulse";
import {warp} from "./warp";
import {inertialDampeners} from "./inertialDampeners";
import {thrusters} from "./thrusters";
import {reactor} from "./reactor";

const systemTypes = createUnionSchema(
Object.keys(ShipSystemTypes) as (keyof typeof ShipSystemTypes)[]
Expand All @@ -24,6 +25,7 @@ export const systems = t.router({
warp,
inertialDampeners,
thrusters,
reactor,
all: t.procedure
.input(z.object({pluginId: z.string()}).optional())
.filter((publish: {pluginId: string} | null, {input}) => {
Expand Down
66 changes: 66 additions & 0 deletions client/src/pages/Config/data/systems/reactor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import ReactorPlugin from "@server/classes/Plugins/ShipSystems/Reactor";
import {pubsub} from "@server/init/pubsub";
import {t} from "@server/init/t";
import inputAuth from "@server/utils/inputAuth";
import {z} from "zod";
import {
getShipSystem,
getShipSystemForInput,
pluginFilter,
systemInput,
} from "../utils";

export const reactor = t.router({
get: t.procedure
.input(systemInput)
.filter(pluginFilter)
.request(({ctx, input}) => {
const system = getShipSystem({input, ctx});

if (system.type !== "reactor") throw new Error("System is not Reactor");

return system as ReactorPlugin;
}),
update: t.procedure
.input(
z.object({
pluginId: z.string(),
systemId: z.string(),
shipPluginId: z.string().optional(),
shipId: z.string().optional(),
optimalOutputPercent: z.number().optional(),
reactorCount: z.number().optional(),
powerMultiplier: z.number().optional(),
})
)
.send(({ctx, input}) => {
inputAuth(ctx);
const [system, override] = getShipSystemForInput(ctx, input);
const shipSystem = override || system;

if (typeof input.optimalOutputPercent === "number") {
shipSystem.optimalOutputPercent = Math.min(
1,
Math.max(0, input.optimalOutputPercent)
);
}
if (typeof input.reactorCount === "number") {
shipSystem.reactorCount = Math.max(0, input.reactorCount);
}
if (typeof input.powerMultiplier === "number") {
shipSystem.powerMultiplier = Math.max(0, input.powerMultiplier);
}

pubsub.publish.plugin.systems.get({
pluginId: input.pluginId,
});
if (input.shipPluginId && input.shipId) {
pubsub.publish.plugin.ship.get({
pluginId: input.shipPluginId,
shipId: input.shipId,
});
}

return shipSystem;
}),
});
6 changes: 6 additions & 0 deletions server/src/classes/Plugins/ShipSystems/Reactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export default class ReactorPlugin extends BaseShipSystemPlugin {
* any lower increases overall efficiency, making fuel last longer.
*/
optimalOutputPercent: number;
/**
* Determines the total output of all reactors by multiplying this by all
* ship system's default power.
*/
powerMultiplier: number;
/**
* The max power output of each reactor is determined by the power
* required by all of the systems on the ship divided by the
Expand All @@ -26,6 +31,7 @@ export default class ReactorPlugin extends BaseShipSystemPlugin {

this.optimalOutputPercent = params.optimalOutputPercent || 0.5;
this.reactorCount = params.reactorCount || 4;
this.powerMultiplier = params.powerMultiplier || 1;
}
makeEntities(overrides?: Record<string, any>): Entity[] {
return Array.from({length: this.reactorCount}).map(() =>
Expand Down

0 comments on commit 694bdcf

Please sign in to comment.