Skip to content

Commit

Permalink
Merge branch 'module-versions' of github.com:gettakaro/takaro into mo…
Browse files Browse the repository at this point in the history
…dule-versions
  • Loading branch information
emielvanseveren committed Dec 31, 2024
2 parents c6e8de7 + a43e925 commit 3f173c8
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 159 deletions.
2 changes: 2 additions & 0 deletions packages/app-api/src/db/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ export class ModuleRepo extends ITakaroRepo<ModuleModel, ModuleOutputDTO, Module
const { queryVersion } = await this.getModel();
const data = await queryVersion.findById(id).modify('standardExtend');

if (!data) throw new errors.NotFoundError(`Record with id ${id} not found`);

return new ModuleVersionOutputDTO({
...data,
systemConfigSchema: getSystemConfigSchema(data as unknown as ModuleVersionOutputDTO),
Expand Down
22 changes: 21 additions & 1 deletion packages/app-api/src/service/CommandService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { PlayerService } from './PlayerService.js';
import { PlayerOnGameServerService } from './PlayerOnGameserverService.js';
import { UserService } from './User/index.js';
import { ModuleService } from './Module/index.js';
import { InstallModuleDTO } from './Module/dto.js';

export function commandsRunningKey(data: ICommandJobData) {
return `commands-running:${data.pog.id}`;
Expand Down Expand Up @@ -244,7 +245,26 @@ export class CommandService extends TakaroService<CommandModel, CommandOutputDTO
}

const updated = await this.repo.update(id, item);
return updated;

const installations = await this.moduleService.getInstalledModules({ versionId: updated.versionId });
await Promise.all(
installations.map((i) => {
const newSystemConfig = i.systemConfig;
const cmdCfg = newSystemConfig.commands[existing.name];
delete newSystemConfig.commands[existing.name];
newSystemConfig.commands[updated.name] = cmdCfg;
return this.moduleService.installModule(
new InstallModuleDTO({
gameServerId: i.gameserverId,
versionId: i.versionId,
userConfig: JSON.stringify(i.userConfig),
systemConfig: JSON.stringify(newSystemConfig),
}),
);
}),
);

return this.findOne(id);
}

async delete(id: string) {
Expand Down
20 changes: 19 additions & 1 deletion packages/app-api/src/service/CronJobService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TakaroDTO, errors, TakaroModelDTO, traceableClass } from '@takaro/util'
import { PaginatedOutput } from '../db/base.js';
import { ITakaroQuery } from '@takaro/db';
import { randomUUID } from 'crypto';
import { ModuleInstallationOutputDTO } from './Module/dto.js';
import { InstallModuleDTO, ModuleInstallationOutputDTO } from './Module/dto.js';
import { ModuleService } from './Module/index.js';

export class CronJobOutputDTO extends TakaroModelDTO<CronJobOutputDTO> {
Expand Down Expand Up @@ -121,6 +121,24 @@ export class CronJobService extends TakaroService<CronJobModel, CronJobOutputDTO
const updated = await this.repo.update(id, item);

const installedModules = await this.moduleService.getInstalledModules({ versionId: updated.versionId });

await Promise.all(
installedModules.map((i) => {
const newSystemConfig = i.systemConfig;
const cmdCfg = newSystemConfig.cronJobs[existing.name];
delete newSystemConfig.cronJobs[existing.name];
newSystemConfig.cronJobs[updated.name] = cmdCfg;
return this.moduleService.installModule(
new InstallModuleDTO({
gameServerId: i.gameserverId,
versionId: i.versionId,
userConfig: JSON.stringify(i.userConfig),
systemConfig: JSON.stringify(newSystemConfig),
}),
);
}),
);

await Promise.all(installedModules.map((mod) => this.syncModuleCronjobs(mod)));
return updated;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/app-api/src/service/HookService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { HookEvents, isDiscordMessageEvent, EventPayload, EventTypes, EventMappi
import { PlayerOnGameServerService } from './PlayerOnGameserverService.js';
import { PlayerService } from './PlayerService.js';
import { ModuleService } from './Module/index.js';
import { InstallModuleDTO } from './Module/dto.js';

interface IHandleHookOptions {
eventType: EventTypes;
Expand Down Expand Up @@ -165,6 +166,24 @@ export class HookService extends TakaroService<HookModel, HookOutputDTO, HookCre

const updated = await this.repo.update(id, item);

const installations = await this.moduleService.getInstalledModules({ versionId: updated.versionId });
await Promise.all(
installations.map((i) => {
const newSystemConfig = i.systemConfig;
const cmdCfg = newSystemConfig.hooks[existing.name];
delete newSystemConfig.hooks[existing.name];
newSystemConfig.hooks[updated.name] = cmdCfg;
return this.moduleService.installModule(
new InstallModuleDTO({
gameServerId: i.gameserverId,
versionId: i.versionId,
userConfig: JSON.stringify(i.userConfig),
systemConfig: JSON.stringify(newSystemConfig),
}),
);
}),
);

return updated;
}

Expand Down
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 @@ -221,7 +221,7 @@ const tests = [
});

const installationsRes = await this.client.module.moduleInstallationsControllerGetInstalledModules({
filters: { gameServerId: [this.setupData.gameserver.id], moduleId: [this.setupData.lotteryModule.id] },
filters: { gameserverId: [this.setupData.gameserver.id], moduleId: [this.setupData.lotteryModule.id] },
});
const mod = installationsRes.data.data[0];

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
Loading

0 comments on commit 3f173c8

Please sign in to comment.