Skip to content

Commit

Permalink
feat(Ship Plugin): Add the ability to assign ship systems to a ship p…
Browse files Browse the repository at this point in the history
…lugin.
  • Loading branch information
alexanderson1993 committed Apr 28, 2022
1 parent 32e77a6 commit 6a4bf1c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 16 deletions.
2 changes: 1 addition & 1 deletion client/src/components/ui/SearchableList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function SearchableList<
{items.map(c => {
return (
<li
key={c.id}
key={JSON.stringify(c.id)}
className={`list-group-item ${
deepEqual(c.id, selectedItem) ? "selected" : ""
}`}
Expand Down
37 changes: 29 additions & 8 deletions client/src/pages/Config/Ships/Systems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,41 @@ import {useNetRequest} from "client/src/context/useNetRequest";
import {useParams} from "react-router-dom";
import Button from "@thorium/ui/Button";
import {useState} from "react";
import {capitalCase} from "change-case";
import {netSend} from "client/src/context/netSend";
export function Systems() {
const {pluginId, shipId} = useParams() as {pluginId: string; shipId: string};
const allSystems = useNetRequest("pluginShipSystems");
const ship = useNetRequest("pluginShip", {pluginId, shipId});

const [allPlugins, setAllPlugins] = useState(false);

const systems = allSystems.filter(sys =>
allPlugins ? true : sys.pluginName === pluginId
const systems = allSystems.filter(
sys =>
!ship.shipSystems.some(
ss => ss.systemId === sys.name && ss.pluginId === sys.pluginName
) && (allPlugins ? true : sys.pluginName === pluginId)
);
return (
<>
<div className="w-72">
{/* TODO April 27 2022 - Figure out some way to define and determine the maximum number of
one type of system that can be assigned to a ship. Ex. only one impulse engine should be assignable. */}
<h3 className="text-2xl font-bold">Available Systems</h3>
<SearchableList
showSearchLabel={false}
selectedItem={null}
setSelectedItem={() => {}}
setSelectedItem={item => {
netSend("pluginShipToggleSystem", {
shipId,
pluginId,
systemId: item.systemId,
systemPlugin: item.pluginId,
});
}}
items={systems.map(c => ({
id: c.name,
category: c.type,
id: {systemId: c.name, pluginId: c.pluginName},
category: capitalCase(c.type),
label: c.name,
pluginName: c.pluginName,
}))}
Expand All @@ -50,16 +64,23 @@ export function Systems() {
<SearchableList
showSearchLabel={false}
selectedItem={null}
setSelectedItem={() => {}}
setSelectedItem={item => {
netSend("pluginShipToggleSystem", {
shipId,
pluginId,
systemId: item.systemId,
systemPlugin: item.pluginId,
});
}}
items={ship.shipSystems
.map(c => {
const system = allSystems.find(
s => s.name === c.systemId && s.pluginName === c.pluginId
);
if (!system) return null;
return {
id: c.systemId,
category: system.type,
id: {systemId: c.systemId, pluginId: system.pluginName},
category: capitalCase(system.type),
label: system.name,
pluginName: system.pluginName,
};
Expand Down
44 changes: 44 additions & 0 deletions server/src/inputs/plugins/ships/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,48 @@ export const shipsPluginInputs = {
}
}
},
pluginShipToggleSystem(
context: DataContext,
params: {
pluginId: string;
shipId: string;
systemId: string;
systemPlugin: string;
}
) {
inputAuth(context);
const plugin = getPlugin(context, params.pluginId);
if (!params.shipId) throw new Error("Ship ID is required");
const ship = plugin.aspects.ships.find(ship => ship.name === params.shipId);
if (!ship) return {shipId: ""};

const systemPlugin = getPlugin(context, params.systemPlugin);
const system = systemPlugin.aspects.shipSystems.find(
system => system.name === params.systemId
);
if (!system) return ship;

const existingSystem = ship.shipSystems.find(
system =>
system.systemId === params.systemId &&
system.pluginId === params.systemPlugin
);

if (existingSystem) {
ship.shipSystems.splice(ship.shipSystems.indexOf(existingSystem), 1);
} else {
ship.shipSystems.push({
systemId: params.systemId,
pluginId: params.systemPlugin,
});
}

pubsub.publish("pluginShips", {pluginId: params.pluginId});
pubsub.publish("pluginShip", {
pluginId: params.pluginId,
shipId: ship.name,
});

return ship;
},
};
22 changes: 15 additions & 7 deletions server/src/netRequests/plugins/shipSystems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ export const pluginShipSystemsRequests = {
) {
if (publishParams && params.pluginId !== publishParams.pluginId) throw null;
if (!params?.pluginId)
return context.server.plugins.reduce(
(acc, plugin) => acc.concat(plugin.aspects.shipSystems),
[] as typeof plugin.aspects.shipSystems
);
return context.server.plugins
.reduce(
(acc, plugin) => acc.concat(plugin.aspects.shipSystems),
[] as typeof plugin.aspects.shipSystems
)
.map(({plugin, ...shipSystem}) => ({
...shipSystem,
pluginName: plugin.name,
}));
const plugin = getPlugin(context, params.pluginId);
return plugin.aspects.shipSystems;
return plugin.aspects.shipSystems.map(({plugin, ...shipSystem}) => ({
...shipSystem,
pluginName: plugin.name,
}));
},
pluginShipSystem<T extends keyof AllShipSystems>(
context: DataContext,
Expand All @@ -27,12 +35,12 @@ export const pluginShipSystemsRequests = {
) {
if (publishParams && params.pluginId !== publishParams.pluginId) throw null;
const plugin = getPlugin(context, params.pluginId);
const system = plugin.aspects.shipSystems.find(
const {plugin: sysPlugin, ...system} = plugin.aspects.shipSystems.find(
system => system.name === params.systemId
) as AllShipSystems[keyof AllShipSystems];
if (!system) throw null;

return system as AllShipSystems[T];
return {...system, pluginName: plugin.name} as AllShipSystems[T];
},
availableShipSystems() {
return Object.keys(ShipSystemTypes).map(key => {
Expand Down

0 comments on commit 6a4bf1c

Please sign in to comment.