Skip to content

Commit

Permalink
feat(Inertial Dampeners): Add the plugin definition and config UI for…
Browse files Browse the repository at this point in the history
… inertial dampeners.
  • Loading branch information
alexanderson1993 committed May 2, 2022
1 parent 48b254a commit c5890ca
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 4 deletions.
5 changes: 4 additions & 1 deletion client/src/pages/Config/ShipSystems/ShipSystemsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ export function ShipSystemsList() {
<DropdownItem
key={system.type}
onClick={async () => {
const name = await prompt({header: "Enter system name"});
const name = await prompt({
header: "Enter system name",
defaultValue: capitalCase(system.type),
});
if (typeof name !== "string") return;
try {
const result = await netSend("pluginShipSystemCreate", {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {Navigate, useParams, useNavigate} from "react-router-dom";
import {useNetRequest} from "client/src/context/useNetRequest";
import Input from "@thorium/ui/Input";
import {AllShipSystems} from "server/src/classes/Plugins/ShipSystems/shipSystemTypes";
import {netSend} from "client/src/context/netSend";
import {toast} from "client/src/context/ToastContext";

export default function InertialDampenersConfig() {
const {pluginId, systemId} = useParams() as {
pluginId: string;
systemId: string;
};
const system = useNetRequest("pluginShipSystem", {
pluginId,
type: "inertialDampeners",
systemId,
}) as AllShipSystems["inertialDampeners"];
if (!system) return <Navigate to={`/config/${pluginId}/systems`} />;

return (
<fieldset key={systemId} className="flex-1 overflow-y-auto">
<div className="flex flex-wrap">
<div className="flex-1 pr-4">
<div className="pb-2">
<Input
labelHidden={false}
inputMode="numeric"
pattern="[0-9]*"
label="Dampening"
placeholder={"1"}
helperText={
"Number > 0 that affects how fast the ship slows. Lower numbers slow the ship faster."
}
defaultValue={system.dampening}
onBlur={async e => {
if (!e.target.value || isNaN(Number(e.target.value))) return;
try {
await netSend("pluginInertialDampenersUpdate", {
pluginId,
shipSystemId: systemId,
dampening: Number(e.target.value),
});
} catch (err) {
if (err instanceof Error) {
toast({
title: "Error changing dampening",
body: err.message,
color: "error",
});
}
}
}}
/>
</div>
</div>
</div>
</fieldset>
);
}
3 changes: 2 additions & 1 deletion client/src/pages/Config/ShipSystems/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {useParams} from "react-router-dom";
import {lazy, Suspense} from "react";
import {LoadingSpinner} from "@thorium/ui/LoadingSpinner";

const pathRegx = /\.\/SystemConfigs\/(.*)\.tsx/g;
console.log(import.meta.glob("./SystemConfigs/*.tsx"));
const systemConfigs = Object.fromEntries(
Object.entries(import.meta.glob("./SystemConfigs/*.tsx")).map(
([path, mod]) => {
const pathRegx = /\.\/SystemConfigs\/(.*)\.tsx/g;
const [, name] = pathRegx.exec(path)!;

return [name, lazy(mod as any)];
Expand Down
34 changes: 34 additions & 0 deletions server/src/classes/Plugins/ShipSystems/InertialDampeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import BasePlugin from "..";
import BaseShipSystemPlugin from "./BaseSystem";
import {ShipSystemFlags} from "./shipSystemTypes";

export default class InertialDampenersPlugin extends BaseShipSystemPlugin {
static flags: ShipSystemFlags[] = [];
type: "inertialDampeners" = "inertialDampeners";
/**
* A number > 0. Pulls the ship's velocity to make it match the current heading
* and affects how fast the ship slows down when engines are turned off.
* Lower number means slow the ship down faster */
dampening: number;
constructor(params: Partial<InertialDampenersPlugin>, plugin: BasePlugin) {
super(params, plugin);
this.dampening = params.dampening || 1;
}
}

/**
* Dampening works like such.
*
* An acceleration is applied to the ship equal to the current velocity
* multiplied by the reciprocal of the dampening factor plus 1. So 1 / (d + 1).
* This makes it so the dampening factor is just a positive number and we don't
* have to deal with reciprocals of decimals less than 1.
* If our dampening factor is 10 we're going 300 km/s and stop, in the first
* frame, we will slow down by 27 km/s. , then 24 km/s, etc. It's a logarithmic
* plateau which will eventually be cut off to 0 km/s.
*
* This will be based on the current velocity of the ship that is _not_ in the
* current direction of acceleration. So if our acceleration is {x:1, y:0, z:0}
* and our velocity is {x: 21, y:5, z:2}, our dampening acceleration will be
* {x: 0, y: 0.45, z:0.18}.
*/
2 changes: 2 additions & 0 deletions server/src/classes/Plugins/ShipSystems/shipSystemTypes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import GenericSystemPlugin from "./Generic";
import ImpulseEnginesPlugin from "./ImpulseEngines";
import InertialDampenersPlugin from "./InertialDampeners";

export const ShipSystemTypes = {
impulseEngines: ImpulseEnginesPlugin,
generic: GenericSystemPlugin,
inertialDampeners: InertialDampenersPlugin,
};

export type ShipSystemFlags = "power" | "heat" | "efficiency";
Expand Down
1 change: 1 addition & 0 deletions server/src/inputs/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export {starPluginInputs} from "./plugins/universe/stars";
export {planetPluginInputs} from "./plugins/universe/planets";
export {shipSystemsPluginInput} from "./plugins/shipSystems";
export {impulseEnginesPluginInput} from "./plugins/shipSystems/impulseEngines";
export {inertialDampenersPluginInput} from "./plugins/shipSystems/inertialDampeners";
49 changes: 49 additions & 0 deletions server/src/inputs/plugins/shipSystems/inertialDampeners.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import InertialDampenersPlugin from "server/src/classes/Plugins/ShipSystems/InertialDampeners";
import {createMockDataContext} from "server/src/utils/createMockDataContext";
import {shipSystemsPluginInput} from ".";
import {inertialDampenersPluginInput} from "./inertialDampeners";

describe("inertial dampeners plugin input", () => {
it("should create a new impulse engine system", async () => {
const dataContext = createMockDataContext();
const created = await shipSystemsPluginInput.pluginShipSystemCreate(
dataContext,
{
pluginId: "Test Plugin",
type: "inertialDampeners",
name: "Test Inertial Dampeners",
}
);

expect(created).toBeTruthy();
expect(created.shipSystemId).toEqual("Test Inertial Dampeners");
const system = dataContext.server.plugins[0].aspects.shipSystems[0];
if (!(system instanceof InertialDampenersPlugin))
throw new Error("Not inertial dampeners");
expect(system.type).toEqual("inertialDampeners");
expect(system.dampening).toEqual(1);
});
it("should update an impulse engine system", async () => {
const dataContext = createMockDataContext();
const created = await shipSystemsPluginInput.pluginShipSystemCreate(
dataContext,
{
pluginId: "Test Plugin",
type: "inertialDampeners",
name: "Test Inertial Dampeners",
}
);
const system = dataContext.server.plugins[0].aspects.shipSystems[0];

if (!(system instanceof InertialDampenersPlugin))
throw new Error("Not inertial dampeners");
expect(system.dampening).toEqual(1);
inertialDampenersPluginInput.pluginInertialDampenersUpdate(dataContext, {
pluginId: "Test Plugin",
shipSystemId: "Test Inertial Dampeners",
dampening: 2000,
});

expect(system.dampening).toEqual(2000);
});
});
47 changes: 47 additions & 0 deletions server/src/inputs/plugins/shipSystems/inertialDampeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import InertialDampenersPlugin from "server/src/classes/Plugins/ShipSystems/InertialDampeners";
import {DataContext} from "server/src/utils/DataContext";
import inputAuth from "server/src/utils/inputAuth";
import {pubsub} from "server/src/utils/pubsub";
import {getPlugin} from "../utils";

function getShipSystem(
context: DataContext,
pluginId: string,
shipSystemId: string
): InertialDampenersPlugin {
const plugin = getPlugin(context, pluginId);
const shipSystem = plugin.aspects.shipSystems.find(
s => s.name === shipSystemId
) as InertialDampenersPlugin;
if (!shipSystem || shipSystem.type !== "inertialDampeners") {
throw new Error("Ship system not found");
}
return shipSystem;
}

export const inertialDampenersPluginInput = {
async pluginInertialDampenersUpdate(
context: DataContext,
params: {
pluginId: string;
shipSystemId: string;
dampening: number;
}
) {
inputAuth(context);
const shipSystem = getShipSystem(
context,
params.pluginId,
params.shipSystemId
);
if (typeof params.dampening === "number" && params.dampening > 0) {
shipSystem.dampening = params.dampening;
}
pubsub.publish("pluginShipSystem", {
pluginId: params.pluginId,
systemId: params.shipSystemId,
});

return shipSystem;
},
};
6 changes: 4 additions & 2 deletions server/src/netRequests/plugins/shipSystems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ export const pluginShipSystemsRequests = {
) {
if (publishParams && params.pluginId !== publishParams.pluginId) throw null;
const plugin = getPlugin(context, params.pluginId);
const {plugin: sysPlugin, ...system} = plugin.aspects.shipSystems.find(
console.log(plugin.aspects.shipSystems);
const shipSystem = plugin.aspects.shipSystems.find(
system => system.name === params.systemId
) as AllShipSystems[keyof AllShipSystems];
if (!system) throw null;
if (!shipSystem) throw null;
const {plugin: sysPlugin, ...system} = shipSystem;

return {...system, pluginName: plugin.name} as AllShipSystems[T];
},
Expand Down

0 comments on commit c5890ca

Please sign in to comment.