Skip to content

Commit

Permalink
feat(lobbies): add admin api (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry authored Sep 30, 2024
1 parent d64816b commit fdeec74
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
19 changes: 12 additions & 7 deletions modules/lobbies/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,25 @@
"description": "Finds an existing lobby with a given query. This will not create a new lobby, see `find_or_create` instead.",
"public": true
},
"force_gc": {
"name": "Force Garbage Collection",
"description": "Rarely used. Forces the matchmaker to purge lobbies & players.",
"public": false
},
"list_regions": {
"name": "List Regions",
"description": "List available regions.",
"public": true
},
"force_gc": {
"name": "Force Garbage Collection",
"description": "Rarely used. Forces the matchmaker to purge lobbies & players.",
"public": true
},
"fetch_lobby_manager_state": {
"name": "Fetch Lobby Manager State",
"description": "See full state of the lobby manager for debugging.",
"public": false
"public": true
},
"reset_lobby_manager_state": {
"name": "Reset Lobby Manager State",
"description": "Reset lobby manager state. For debugging only.",
"public": false
"public": true
}
},
"actors": {
Expand Down Expand Up @@ -151,6 +151,11 @@
"name": "Build Not Found",
"description": "Build not found. Check that there is a build with the provided version & that the bulid is enabled.",
"internal": false
},
"forbidden": {
"name": "Forbidden",
"description": "Cannot access this command.",
"internal": false
}
},
"dependencies": {
Expand Down
6 changes: 4 additions & 2 deletions modules/lobbies/scripts/fetch_lobby_manager_state.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ScriptContext } from "../module.gen.ts";
import { adminGuard } from "../utils/admin.ts";

export interface Request {

adminToken: string;
}

export interface Response {
Expand All @@ -10,8 +11,9 @@ export interface Response {

export async function run(
ctx: ScriptContext,
_req: Request,
req: Request,
): Promise<Response> {
adminGuard(ctx, req.adminToken);
const state = await ctx.actors
.lobbyManager.getOrCreateAndCall(
"default",
Expand Down
5 changes: 4 additions & 1 deletion modules/lobbies/scripts/force_gc.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { ScriptContext } from "../module.gen.ts";
import { adminGuard } from "../utils/admin.ts";

export interface Request {
adminToken: string;
}

export interface Response {
}

export async function run(
ctx: ScriptContext,
_req: Request,
req: Request,
): Promise<Response> {
adminGuard(ctx, req.adminToken);
await ctx.actors.lobbyManager.getOrCreateAndCall(
"default",
undefined,
Expand Down
6 changes: 4 additions & 2 deletions modules/lobbies/scripts/reset_lobby_manager_state.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ScriptContext } from "../module.gen.ts";
import { adminGuard } from "../utils/admin.ts";

export interface Request {

adminToken: string;
}

export interface Response {
Expand All @@ -10,8 +11,9 @@ export interface Response {

export async function run(
ctx: ScriptContext,
_req: Request,
req: Request,
): Promise<Response> {
adminGuard(ctx, req.adminToken);
await ctx.actors.lobbyManager.destroy("default");
return {};
}
Expand Down
7 changes: 7 additions & 0 deletions modules/lobbies/utils/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { RuntimeError } from "../module.gen.ts";

export function adminGuard(ctx: any, inputToken: string) {
const adminToken = ctx.environment.get("ADMIN_TOKEN");
if (!adminToken) throw new RuntimeError("forbidden");
if (inputToken != adminToken) throw new RuntimeError("forbidden");
}

0 comments on commit fdeec74

Please sign in to comment.