Skip to content

Commit

Permalink
Adding percival role feature (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
kev306 authored Jan 3, 2025
1 parent a356c04 commit 0635788
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 34 deletions.
16 changes: 16 additions & 0 deletions assets/scripts/lobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,22 @@ function checkMessageForCommands(message, chatBox) {
}
}

if (percivalCommands) {
for (var key in percivalCommands) {
if (percivalCommands.hasOwnProperty(key)) {
// console.log(key + " -> " + commands[key]);
if (messageCommand === percivalCommands[key].command) {
// console.log("percivals");
// console.log("Command: " + commands[key].command + " called.");
commandCalled = percivalCommands[key].command;
validCommandFound = true;

break;
}
}
}
}

if (TOCommands) {
for (var key in TOCommands) {
if (TOCommands.hasOwnProperty(key)) {
Expand Down
5 changes: 5 additions & 0 deletions assets/scripts/lobby/sockets/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ socket.on('modCommands', (commands) => {
modCommands = commands;
});

let percivalCommands;
socket.on('percivalCommands', (commands) => {
percivalCommands = commands;
});

let TOCommands;
socket.on('TOCommands', (commands) => {
TOCommands = commands;
Expand Down
30 changes: 12 additions & 18 deletions assets/scripts/lobby/sockets/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,24 @@ socket.on('dont-reconnect', () => {
});

socket.on('disconnect', () => {
if (serverRestarting) {
if (serverRestarting || !autoReconnect) {
showDangerAlert(`You have been disconnected. Please refresh the page.`);
return;
}

if (!autoReconnect) {
showDangerAlert(
`You have logged on another device and have been disconnected.`
);
} else {
const chats = $('.chat-list');
for (const chat of chats) {
chat.innerHTML = '';
}
const chats = $('.chat-list');
for (const chat of chats) {
chat.innerHTML = '';
}

setTimeout(() => {
attemptReconnect();
}, 500);
setTimeout(() => {
attemptReconnect();
}, 500);

if (!intervalId) {
intervalId = setInterval(() => {
attemptReconnect();
}, 5000);
}
if (!intervalId) {
intervalId = setInterval(() => {
attemptReconnect();
}, 5000);
}
});

Expand Down
9 changes: 8 additions & 1 deletion src/gameplay/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Role } from './roles/types';
import { Phase } from './phases/types';
import { millisToStr } from '../util/time';
import { RoomPlayer } from './types';
import { isMod } from '../modsadmins/mods';
import { isAdmin } from '../modsadmins/admins';

export class RoomConfig {
host: string;
Expand Down Expand Up @@ -113,7 +115,12 @@ class Room {
);

// check if the player is a moderator or an admin, if so bypass
if (!(socket.isModSocket || socket.isAdminSocket)) {
if (
!(
isMod(socket.request.user.username) ||
isAdmin(socket.request.user.username)
)
) {
// if the room has a password and user hasn't put one in yet
if (
this.joinPassword !== undefined &&
Expand Down
9 changes: 9 additions & 0 deletions src/modsadmins/percivals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Note this is a site Percival role that assists the Mods with site moderation.
// It is not the Percival role in Avalon gameplay.

// all in lower case
export const percivalsArray: string[] = [];

export function isPercival(username: string): boolean {
return percivalsArray.includes(username.toLowerCase());
}
5 changes: 3 additions & 2 deletions src/sockets/commands/mod/mdc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export const mdc: Command = {
allSockets[getIndexFromUsername(allSockets, args[1], true)];

if (targetSock) {
targetSock.emit('redirect', '/logout');
targetSock.disconnect();
targetSock.emit('dont-reconnect');
targetSock.disconnect(true);

senderSocket.emit('messageCommandReturnStr', {
message: `Disconnected ${args[1]} successfully.`,
classStr: 'server-text',
Expand Down
39 changes: 39 additions & 0 deletions src/sockets/commands/percival/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Command, Commands } from '../types';
import { p } from './p';
import { mdc } from '../mod/mdc';

// Percival commands are intended to be a strict subset of mod commands.

export function convertModCommandToPercivalCommand(
modCommand: Command,
): Command {
const modPrefix = 'm';
const percivalPrefix = 'p';

// Throw error if invalid command is passed
if (!modCommand.command.startsWith(modPrefix)) {
throw new Error(`Incorrect command prefix. Expected to start with: "${modPrefix}" Got: "${modCommand.command}"`);
}

if (!modCommand.help.startsWith(`/${modPrefix}`)) {
throw new Error(
`Incorrect command help prefix. Expected to start with: "/${modPrefix}" Got: "${modCommand.help}"`,
);
}

return {
command: modCommand.command.replace(
modPrefix,
percivalPrefix,
),
help: modCommand.help.replace(modPrefix, percivalPrefix),
run: modCommand.run,
};
}

const pdc = convertModCommandToPercivalCommand(mdc);

export const percivalCommands: Commands = {
[p.command]: p,
[pdc.command]: pdc,
};
23 changes: 23 additions & 0 deletions src/sockets/commands/percival/p.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Command } from '../types';
import { SocketUser } from '../../types';
import { percivalCommands } from './index';

export const p: Command = {
command: 'p',
help: '/p: show percival commands.',
run: async (args: string[], socket: SocketUser) => {
const dataToSend = [];

for (const key in percivalCommands) {
if (percivalCommands.hasOwnProperty(key)) {
const commandKey = key as keyof typeof percivalCommands;
dataToSend.push({
message: percivalCommands[commandKey].help,
classStr: 'server-text',
});
}
}

socket.emit('messageCommandReturnStr', dataToSend);
},
};
25 changes: 12 additions & 13 deletions src/sockets/sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import JSON from 'circular-json';

import { isAdmin } from '../modsadmins/admins';
import { isMod } from '../modsadmins/mods';
import { isPercival } from '../modsadmins/percivals';
import { isTO } from '../modsadmins/tournamentOrganizers';
import {
GAME_MODE_NAMES,
Expand All @@ -26,6 +27,7 @@ import { MessageWithDate, Quote } from './quote';

import { adminCommands } from './commands/admin';
import { modCommands } from './commands/mod';
import { percivalCommands } from './commands/percival';
import { userCommandsImported } from './commands/user';
import { mtogglepause } from './commands/mod/mtogglepause';
import { mrevealallroles } from './commands/mod/mrevealallroles';
Expand Down Expand Up @@ -599,25 +601,14 @@ export const server = function (io: SocketServer): void {
// send the user the list of commands
socket.emit('commands', userCommands);

// initialise not mod and not admin
socket.isAdminSocket = false;
socket.isModSocket = false;
socket.isTOSocket = false;

if (isAdmin(socket.request.user.username)) {
// promote to admin socket
socket.isAdminSocket = true;

// TODO this shouldn't be sent out as separate commands. Merge these.

// send the user the list of commands
socket.emit('adminCommands', adminCommands);
}

if (isMod(socket.request.user.username)) {
// promote to mod socket
socket.isModSocket = true;

// send the user the list of commands
socket.emit('modCommands', modCommands);

Expand Down Expand Up @@ -668,9 +659,12 @@ export const server = function (io: SocketServer): void {
});
}

if (isTO(socket.request.user.username)) {
socket.isTOSocket = true;
if (isPercival(socket.request.user.username)) {
// send the user the list of commands
socket.emit('percivalCommands', percivalCommands);
}

if (isTO(socket.request.user.username)) {
// send the user the list of commands
socket.emit('TOCommands', TOCommands);
}
Expand Down Expand Up @@ -1172,6 +1166,11 @@ function messageCommand(data) {
adminCommands[data.command].run(data.args, this, ioGlobal);
} else if (modCommands[data.command] && isMod(this.request.user.username)) {
modCommands[data.command].run(data.args, this, ioGlobal);
} else if (
percivalCommands[data.command] &&
isPercival(this.request.user.username)
) {
dataToSend = percivalCommands[data.command].run(data.args, this, ioGlobal);
} else if (TOCommands[data.command] && isTO(this.request.user.username)) {
dataToSend = TOCommands[data.command].run(data.args, this, ioGlobal);
} else if (userCommands[data.command]) {
Expand Down

0 comments on commit 0635788

Please sign in to comment.