From 6a4bf1c002d9472c630a7e6c3c947929c1f77d9e Mon Sep 17 00:00:00 2001 From: Alex Anderson Date: Wed, 27 Apr 2022 20:41:25 -0400 Subject: [PATCH] feat(Ship Plugin): Add the ability to assign ship systems to a ship plugin. --- client/src/components/ui/SearchableList.tsx | 2 +- client/src/pages/Config/Ships/Systems.tsx | 37 ++++++++++++---- server/src/inputs/plugins/ships/index.ts | 44 +++++++++++++++++++ .../netRequests/plugins/shipSystems/index.ts | 22 +++++++--- 4 files changed, 89 insertions(+), 16 deletions(-) diff --git a/client/src/components/ui/SearchableList.tsx b/client/src/components/ui/SearchableList.tsx index 221141f3..63295ff8 100644 --- a/client/src/components/ui/SearchableList.tsx +++ b/client/src/components/ui/SearchableList.tsx @@ -79,7 +79,7 @@ function SearchableList< {items.map(c => { return (
  • - 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 ( <>
    + {/* 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. */}

    Available Systems

    {}} + 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, }))} @@ -50,7 +64,14 @@ export function Systems() { {}} + setSelectedItem={item => { + netSend("pluginShipToggleSystem", { + shipId, + pluginId, + systemId: item.systemId, + systemPlugin: item.pluginId, + }); + }} items={ship.shipSystems .map(c => { const system = allSystems.find( @@ -58,8 +79,8 @@ export function Systems() { ); 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, }; diff --git a/server/src/inputs/plugins/ships/index.ts b/server/src/inputs/plugins/ships/index.ts index 7e088c37..52673a4c 100644 --- a/server/src/inputs/plugins/ships/index.ts +++ b/server/src/inputs/plugins/ships/index.ts @@ -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; + }, }; diff --git a/server/src/netRequests/plugins/shipSystems/index.ts b/server/src/netRequests/plugins/shipSystems/index.ts index 74589fc1..8cc755c2 100644 --- a/server/src/netRequests/plugins/shipSystems/index.ts +++ b/server/src/netRequests/plugins/shipSystems/index.ts @@ -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( context: DataContext, @@ -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 => {