Skip to content

Commit

Permalink
feat(Star Map): Add the APIs for adding, removing, and updating solar…
Browse files Browse the repository at this point in the history
… system plugins. Refs #173
  • Loading branch information
alexanderson1993 committed Jan 21, 2022
1 parent febab2c commit f469d69
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
16 changes: 13 additions & 3 deletions server/src/classes/Plugins/Universe/SolarSystem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import systemNames from "server/src/spawners/systemNames";
import {UNIVERSE_RADIUS} from "server/src/utils/constants";
import {generateIncrementedName} from "server/src/utils/generateIncrementedName";
import {randomFromList} from "server/src/utils/randomFromList";
import {AstronomicalUnit, LightMinute} from "server/src/utils/unitTypes";
import BasePlugin from "..";
Expand Down Expand Up @@ -37,10 +38,19 @@ export default class SolarSystemPlugin extends Aspect {
assets = {};

constructor(params: Partial<SolarSystemPlugin>, plugin: BasePlugin) {
const starNames = plugin.aspects.solarSystems.map(s => s.name);
const availableNames = systemNames.filter(val => !starNames.includes(val));
let name = params.name;
if (!name) {
const starNames = plugin.aspects.solarSystems.map(s => s.name);
const availableNames = systemNames.filter(
val => !starNames.includes(val)
);

const name = randomFromList(availableNames) || "Bob"; // If this happens, I'll laugh very hard.
name = randomFromList(availableNames) || "Bob"; // If this happens, I'll laugh very hard.
}
name = generateIncrementedName(
name || "New Solar System",
plugin.aspects.solarSystems.map(solarSystem => solarSystem.name)
);

super({name, ...params}, {kind: "solarSystems"}, plugin);
this.name = name;
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 @@ -6,3 +6,4 @@ export {pluginInputs} from "./plugins";
export {shipsPluginInputs} from "./plugins/ships";
export {themesPluginInput} from "./plugins/themes";
export {officerLogInputs} from "./officersLog";
export {solarSystemsPluginInputs} from "./plugins/solarSystems";
91 changes: 91 additions & 0 deletions server/src/inputs/plugins/solarSystems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import SolarSystemPlugin from "server/src/classes/Plugins/Universe/SolarSystem";
import {DataContext} from "server/src/utils/DataContext";
import {pubsub} from "server/src/utils/pubsub";
import {AstronomicalUnit, LightMinute} from "server/src/utils/unitTypes";
import {getPlugin} from "./utils";

export const solarSystemsPluginInputs = {
pluginSolarSystemCreate(
context: DataContext,
params: {
pluginId: string;
position: {
x: LightMinute;
y: LightMinute;
z: LightMinute;
};
}
) {
const plugin = getPlugin(context, params.pluginId);
const solarSystem = new SolarSystemPlugin(
{position: params.position},
plugin
);
plugin.aspects.solarSystems.push(solarSystem);

pubsub.publish("pluginSolarSystems", {pluginId: params.pluginId});

return {solarSystemId: solarSystem.name};
},
async pluginSolarSystemDelete(
context: DataContext,
params: {
pluginId: string;
solarSystemId: string;
}
) {
const plugin = getPlugin(context, params.pluginId);
const solarSystem = plugin.aspects.solarSystems.find(
solarSystem => solarSystem.name === params.solarSystemId
);
if (!solarSystem) return;
plugin.aspects.solarSystems.splice(
plugin.aspects.solarSystems.indexOf(solarSystem),
1
);

await solarSystem?.removeFile();
pubsub.publish("pluginShips", {pluginId: params.pluginId});
},
async pluginSolarSystemUpdate(
context: DataContext,
params: {
pluginId: string;
solarSystemId: string;
position?: {
x: LightMinute;
y: LightMinute;
z: LightMinute;
};
name?: string;
description?: string;
tags?: string[];
habitableZoneInner?: AstronomicalUnit;
habitableZoneOuter?: AstronomicalUnit;
skyboxKey?: string;
}
) {
const plugin = getPlugin(context, params.pluginId);
if (!params.solarSystemId) throw new Error("Solar System ID is required");
const solarSystem = plugin.aspects.solarSystems.find(
solarSystem => solarSystem.name === params.solarSystemId
);
if (!solarSystem) return {solarSystemId: ""};
if (params.position) solarSystem.position = params.position;
if (params.name) solarSystem.name = params.name;
if (params.description) solarSystem.description = params.description;
if (params.tags) solarSystem.tags = params.tags;
if (params.habitableZoneInner)
solarSystem.habitableZoneInner = params.habitableZoneInner;
if (params.habitableZoneOuter)
solarSystem.habitableZoneOuter = params.habitableZoneOuter;
if (params.skyboxKey) solarSystem.skyboxKey = params.skyboxKey;

pubsub.publish("pluginSolarSystems", {pluginId: params.pluginId});
pubsub.publish("pluginSolarSystem", {
pluginId: params.pluginId,
solarSystemId: solarSystem.name,
});
return {solarSystemId: solarSystem.name};
},
};
1 change: 1 addition & 0 deletions server/src/netRequests/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export {pluginThemesRequest} from "./plugins/themes";
export {clientsRequest} from "./clients";
export {effectsRequest} from "./effects";
export {flightsRequests} from "./flights";
export {pluginSolarSystemsRequest} from "./plugins/solarSystems";
27 changes: 27 additions & 0 deletions server/src/netRequests/plugins/solarSystems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {DataContext} from "server/src/utils/DataContext";
import {getPlugin} from "./utils";

export const pluginSolarSystemsRequest = {
pluginSolarSystems(
context: DataContext,
params: {pluginId: string},
publishParams: {pluginId: string} | null
) {
if (publishParams && params.pluginId !== publishParams.pluginId) throw null;
const plugin = getPlugin(context, params.pluginId);
return plugin.aspects.solarSystems;
},
pluginSolarSystem(
context: DataContext,
params: {pluginId: string; solarSystemId: string},
publishParams: {pluginId: string; solarSystemId: string} | null
) {
if (publishParams && params.pluginId !== publishParams.pluginId) throw null;
const plugin = getPlugin(context, params.pluginId);
const solarSystem = plugin.aspects.solarSystems.find(
solarSystem => solarSystem.name === params.solarSystemId
);
if (!solarSystem) throw null;
return solarSystem;
},
};

0 comments on commit f469d69

Please sign in to comment.