From bc5bc22e6f0909dee50f69a40098e9e21de6cdf8 Mon Sep 17 00:00:00 2001 From: Niek Candaele Date: Sun, 29 Dec 2024 22:36:41 +0100 Subject: [PATCH] fix: waypoints module --- packages/app-api/src/service/RoleService.ts | 23 ++++--- .../teleports/waypoints.integration.test.ts | 10 +-- .../teleports/commands/deletewaypoint.js | 43 +----------- .../teleports/commands/listwaypoints.js | 43 +----------- .../modules/teleports/commands/setwaypoint.js | 67 ++++--------------- .../teleports/commands/teleportwaypoint.js | 7 +- .../src/modules/teleports/functions/utils.js | 41 ++++++++++++ 7 files changed, 81 insertions(+), 153 deletions(-) diff --git a/packages/app-api/src/service/RoleService.ts b/packages/app-api/src/service/RoleService.ts index 3032d8cef4..a5d8bcf8d1 100644 --- a/packages/app-api/src/service/RoleService.ts +++ b/packages/app-api/src/service/RoleService.ts @@ -353,15 +353,20 @@ export class RoleService extends TakaroService - mod.version.permissions.map((permission) => ({ - ...permission, - module: { - id: mod.id, - name: mod.module.name, - }, - })), - ) as PermissionOutputDTO[]; + const modulePermissions = installedModules + // Ensure each module only appears once + // We fetch the installations above, so module X can be installed on server A and B + .filter((mod, index, self) => self.findIndex((m) => m.versionId === mod.versionId) === index) + // Then transform the permissions to a flat array + .flatMap((mod) => + mod.version.permissions.map((permission) => ({ + ...permission, + module: { + id: mod.id, + name: mod.module.name, + }, + })), + ) as PermissionOutputDTO[]; const systemPermissions = await this.repo.getSystemPermissions(); diff --git a/packages/lib-modules/src/__tests__/teleports/waypoints.integration.test.ts b/packages/lib-modules/src/__tests__/teleports/waypoints.integration.test.ts index 8d5f8b4f94..8b9ce419cd 100644 --- a/packages/lib-modules/src/__tests__/teleports/waypoints.integration.test.ts +++ b/packages/lib-modules/src/__tests__/teleports/waypoints.integration.test.ts @@ -58,7 +58,7 @@ const waypointsSetup = async function (this: IntegrationTest): P }; }; -async function setupSecondServer() { +async function setupSecondServer(this: IntegrationTest) { const newGameServer = await this.client.gameserver.gameServerControllerCreate({ name: 'newServer', connectionInfo: JSON.stringify({ @@ -68,10 +68,10 @@ async function setupSecondServer() { type: GameServerTypesOutputDTOTypeEnum.Mock, }); - await this.client.module.moduleInstallationsControllerInstallModule( - newGameServer.data.data.id, - this.setupData.teleportsModule.id, - ); + await this.client.module.moduleInstallationsControllerInstallModule({ + gameServerId: newGameServer.data.data.id, + versionId: this.setupData.teleportsModule.latestVersion.id, + }); const connectedEvents = (await new EventsAwaiter().connect(this.client)).waitForEvents( GameEvents.PLAYER_CONNECTED, diff --git a/packages/lib-modules/src/modules/teleports/commands/deletewaypoint.js b/packages/lib-modules/src/modules/teleports/commands/deletewaypoint.js index 8c64545e80..8fe346112f 100644 --- a/packages/lib-modules/src/modules/teleports/commands/deletewaypoint.js +++ b/packages/lib-modules/src/modules/teleports/commands/deletewaypoint.js @@ -1,8 +1,5 @@ import { takaro, data, checkPermission, TakaroUserError } from '@takaro/helpers'; - -function getWaypointName(name) { - return `waypoint ${name}`; -} +import { ensureWaypointsModule, getWaypointName } from './utils.js'; async function main() { const { pog, gameServerId, arguments: args } = data; @@ -11,44 +8,6 @@ async function main() { throw new TakaroUserError('You do not have permission to manage waypoints.'); } - async function ensureWaypointsModule() { - let waypointsDefinition = ( - await takaro.module.moduleControllerSearch({ - filters: { - name: ['Waypoints'], - }, - }) - ).data.data[0]; - - if (!waypointsDefinition) { - console.log('Waypoints module definition not found, creating it.'); - waypointsDefinition = ( - await takaro.module.moduleControllerCreate({ - name: 'Waypoints', - description: 'Waypoints module for the teleport system.', - }) - ).data.data; - } - - let waypointsInstallation = ( - await takaro.module.moduleInstallationsControllerGetInstalledModules({ - filters: { gameServerId: [gameServerId] }, - }) - ).data.data.find((module) => module.name === 'Waypoints'); - - if (!waypointsInstallation) { - console.log('Waypoints module not found, installing it.'); - waypointsInstallation = ( - await takaro.module.moduleInstallationsControllerInstallModule({ - gameServerId, - versionId: waypointsDefinition.latestVersion.id, - }) - ).data.data; - } - - return { waypointsInstallation, waypointsDefinition }; - } - const { waypointsInstallation } = await ensureWaypointsModule(); const variable = await takaro.variable.variableControllerSearch({ diff --git a/packages/lib-modules/src/modules/teleports/commands/listwaypoints.js b/packages/lib-modules/src/modules/teleports/commands/listwaypoints.js index f3cdaefd73..8cbadb3a44 100644 --- a/packages/lib-modules/src/modules/teleports/commands/listwaypoints.js +++ b/packages/lib-modules/src/modules/teleports/commands/listwaypoints.js @@ -1,49 +1,12 @@ -import { takaro, data, checkPermission } from '@takaro/helpers'; +import { data, checkPermission } from '@takaro/helpers'; +import { ensureWaypointsModule } from './utils.js'; async function main() { const { pog, gameServerId } = data; - async function ensureWaypointsModule() { - let waypointsDefinition = ( - await takaro.module.moduleControllerSearch({ - filters: { - name: ['Waypoints'], - }, - }) - ).data.data[0]; - - if (!waypointsDefinition) { - console.log('Waypoints module definition not found, creating it.'); - waypointsDefinition = ( - await takaro.module.moduleControllerCreate({ - name: 'Waypoints', - description: 'Waypoints module for the teleport system.', - }) - ).data.data; - } - - let waypointsInstallation = ( - await takaro.module.moduleInstallationsControllerGetInstalledModules({ - filters: { gameServerId: [gameServerId] }, - }) - ).data.data.find((module) => module.name === 'Waypoints'); - - if (!waypointsInstallation) { - console.log('Waypoints module not found, installing it.'); - waypointsInstallation = ( - await takaro.module.moduleInstallationsControllerInstallModule({ - gameServerId, - versionId: waypointsDefinition.latestVersion.id, - }) - ).data.data; - } - - return { waypointsInstallation, waypointsDefinition }; - } - const { waypointsDefinition } = await ensureWaypointsModule(); - const allWaypoints = waypointsDefinition.commands; + const allWaypoints = waypointsDefinition.latestVersion.commands; const waypointsWithPermission = allWaypoints .filter((waypoint) => checkPermission(pog, `WAYPOINTS_USE_${waypoint.trigger.toUpperCase()}_${gameServerId}`)) diff --git a/packages/lib-modules/src/modules/teleports/commands/setwaypoint.js b/packages/lib-modules/src/modules/teleports/commands/setwaypoint.js index 9c77a44c5b..1fb703d8f0 100644 --- a/packages/lib-modules/src/modules/teleports/commands/setwaypoint.js +++ b/packages/lib-modules/src/modules/teleports/commands/setwaypoint.js @@ -1,8 +1,5 @@ import { takaro, data, checkPermission, TakaroUserError } from '@takaro/helpers'; - -function getWaypointName(name) { - return `waypoint ${name}`; -} +import { ensureWaypointsModule, getWaypointName } from './utils.js'; async function main() { const { pog, gameServerId, module: mod, arguments: args } = data; @@ -11,44 +8,6 @@ async function main() { throw new TakaroUserError('You do not have permission to manage waypoints.'); } - async function ensureWaypointsModule() { - let waypointsDefinition = ( - await takaro.module.moduleControllerSearch({ - filters: { - name: ['Waypoints'], - }, - }) - ).data.data[0]; - - if (!waypointsDefinition) { - console.log('Waypoints module definition not found, creating it.'); - waypointsDefinition = ( - await takaro.module.moduleControllerCreate({ - name: 'Waypoints', - description: 'Waypoints module for the teleport system.', - }) - ).data.data; - } - - let waypointsInstallation = ( - await takaro.module.moduleInstallationsControllerGetInstalledModules({ - filters: { gameServerId: [gameServerId] }, - }) - ).data.data.find((module) => module.name === 'Waypoints'); - - if (!waypointsInstallation) { - console.log('Waypoints module not found, installing it.'); - waypointsInstallation = ( - await takaro.module.moduleInstallationsControllerInstallModule({ - gameServerId, - versionId: waypointsDefinition.latestVersion.id, - }) - ).data.data; - } - - return { waypointsInstallation, waypointsDefinition }; - } - const { waypointsInstallation, waypointsDefinition } = await ensureWaypointsModule(); try { @@ -76,14 +35,14 @@ async function main() { }); await takaro.command.commandControllerCreate({ - moduleId: waypointsInstallation.moduleId, + versionId: waypointsInstallation.versionId, name: `waypoint ${args.waypoint} server ${gameServerId}`, trigger: args.waypoint, helpText: `Teleport to waypoint ${args.waypoint}.`, function: teleportCommand.data.data[0].function.code, }); - const existingPermissions = waypointsDefinition.permissions || []; + const existingPermissions = waypointsDefinition.latestVersion.permissions || []; const permissionInputDTOs = existingPermissions.map((permission) => ({ permission: permission.permission, description: permission.description, @@ -94,15 +53,17 @@ async function main() { const gameServer = (await takaro.gameserver.gameServerControllerGetOne(gameServerId)).data.data; await takaro.module.moduleControllerUpdate(waypointsInstallation.moduleId, { - permissions: [ - { - permission: `WAYPOINTS_USE_${args.waypoint.toUpperCase()}_${gameServerId}`, - description: `Use the waypoint ${args.waypoint} on ${gameServer.name}.`, - friendlyName: `Use waypoint ${args.waypoint} on ${gameServer.name}`, - canHaveCount: false, - }, - ...permissionInputDTOs, - ], + latestVersion: { + permissions: [ + { + permission: `WAYPOINTS_USE_${args.waypoint.toUpperCase()}_${gameServerId}`, + description: `Use the waypoint ${args.waypoint} on ${gameServer.name}.`, + friendlyName: `Use waypoint ${args.waypoint} on ${gameServer.name}`, + canHaveCount: false, + }, + ...permissionInputDTOs, + ], + }, }); // Need to reinstall the module to ensure the new commands systemconfig and permissions are properly in place diff --git a/packages/lib-modules/src/modules/teleports/commands/teleportwaypoint.js b/packages/lib-modules/src/modules/teleports/commands/teleportwaypoint.js index b1dd55c625..0baa4d7ce7 100644 --- a/packages/lib-modules/src/modules/teleports/commands/teleportwaypoint.js +++ b/packages/lib-modules/src/modules/teleports/commands/teleportwaypoint.js @@ -7,7 +7,7 @@ function getWaypointName(name) { async function main() { const { pog, gameServerId, trigger, module, itemId } = data; - const triggeredCommand = module.module.commands.find((command) => command.id === itemId); + const triggeredCommand = module.version.commands.find((command) => command.id === itemId); if (!triggeredCommand) { throw new Error('Waypoint not found.'); @@ -36,14 +36,13 @@ async function main() { waypointsDefinition = ( await takaro.module.moduleControllerCreate({ name: 'Waypoints', - description: 'Waypoints module for the teleport system.', }) ).data.data; } let waypointsInstallation = ( await takaro.module.moduleInstallationsControllerGetInstalledModules({ - filters: { gameServerId: [gameServerId] }, + filters: { gameserverId: [data.gameServerId] }, }) ).data.data.find((module) => module.name === 'Waypoints'); @@ -51,7 +50,7 @@ async function main() { console.log('Waypoints module not found, installing it.'); waypointsInstallation = ( await takaro.module.moduleInstallationsControllerInstallModule({ - gameServerId, + gameServerId: data.gameServerId, versionId: waypointsDefinition.latestVersion.id, }) ).data.data; diff --git a/packages/lib-modules/src/modules/teleports/functions/utils.js b/packages/lib-modules/src/modules/teleports/functions/utils.js index 95598f7da9..413446c3fe 100644 --- a/packages/lib-modules/src/modules/teleports/functions/utils.js +++ b/packages/lib-modules/src/modules/teleports/functions/utils.js @@ -34,3 +34,44 @@ export async function findTp(tpName, playerId, pub = false) { sortDirection: 'asc', }); } + +export async function ensureWaypointsModule() { + let waypointsDefinition = ( + await takaro.module.moduleControllerSearch({ + filters: { + name: ['Waypoints'], + }, + }) + ).data.data[0]; + + if (!waypointsDefinition) { + console.log('Waypoints module definition not found, creating it.'); + waypointsDefinition = ( + await takaro.module.moduleControllerCreate({ + name: 'Waypoints', + }) + ).data.data; + } + + let waypointsInstallation = ( + await takaro.module.moduleInstallationsControllerGetInstalledModules({ + filters: { gameserverId: [data.gameServerId] }, + }) + ).data.data.find((module) => module.name === 'Waypoints'); + + if (!waypointsInstallation) { + console.log('Waypoints module not found, installing it.'); + waypointsInstallation = ( + await takaro.module.moduleInstallationsControllerInstallModule({ + gameServerId: data.gameServerId, + versionId: waypointsDefinition.latestVersion.id, + }) + ).data.data; + } + + return { waypointsInstallation, waypointsDefinition }; +} + +export function getWaypointName(name) { + return `waypoint ${name}`; +}