Skip to content

Commit

Permalink
fix: waypoints module
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele committed Dec 29, 2024
1 parent 9da4535 commit bc5bc22
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 153 deletions.
23 changes: 14 additions & 9 deletions packages/app-api/src/service/RoleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,20 @@ export class RoleService extends TakaroService<RoleModel, RoleOutputDTO, RoleCre
async getPermissions() {
const moduleService = new ModuleService(this.domainId);
const installedModules = await moduleService.getInstalledModules({});
const modulePermissions = installedModules.flatMap((mod) =>
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const waypointsSetup = async function (this: IntegrationTest<WaypointsSetup>): P
};
};

async function setupSecondServer() {
async function setupSecondServer(this: IntegrationTest<WaypointsSetup>) {
const newGameServer = await this.client.gameserver.gameServerControllerCreate({
name: 'newServer',
connectionInfo: JSON.stringify({
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -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}`))
Expand Down
67 changes: 14 additions & 53 deletions packages/lib-modules/src/modules/teleports/commands/setwaypoint.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down Expand Up @@ -36,22 +36,21 @@ 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');

if (!waypointsInstallation) {
console.log('Waypoints module not found, installing it.');
waypointsInstallation = (
await takaro.module.moduleInstallationsControllerInstallModule({
gameServerId,
gameServerId: data.gameServerId,
versionId: waypointsDefinition.latestVersion.id,
})
).data.data;
Expand Down
41 changes: 41 additions & 0 deletions packages/lib-modules/src/modules/teleports/functions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

0 comments on commit bc5bc22

Please sign in to comment.