Skip to content

Commit

Permalink
Percy ban (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
vck3000 authored Jan 3, 2025
1 parent 4bb54cc commit e878794
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 59 deletions.
53 changes: 0 additions & 53 deletions assets/scripts/lobby/sockets/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,6 @@ socket.on('adminCommands', (commands) => {

let modCommands;
socket.on('modCommands', (commands) => {
if (!modCommands) {
$('#modActionCloseButton').on('click', () => {
// Send request out.
var formElement = document.querySelector('#modactionform');
var bodyFormData = new FormData(formElement);

const banData = {};
for (var [key, value] of bodyFormData.entries()) {
banData[key] = value;
}

Swal.fire({
title: 'Sending your request...',
onOpen: () => {
Swal.showLoading();

axios({
method: 'POST',
url: '/mod/ban',
data: banData,
config: { headers: { 'Content-Type': 'application/json' } },
})
.then(function (response) {
//handle success
// console.log(response);

$('#modModal').modal('hide');

// Clear the form for next input.
$('#modactionform')[0].reset();

Swal.close();
Swal.fire({
title: response.data,
type: 'success',
});
})
.catch(function (err) {
Swal.close();
Swal.fire({
title: err.response.data,
type: 'error',
});
});
},
});

// console.log($("#modactionform").serializeArray());
// const data = $('#modactionform').serializeArray();

// socket.emit('modAction', data);
});
}
modCommands = commands;
});

Expand Down
48 changes: 48 additions & 0 deletions assets/scripts/lobby/sockets/mods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
$(document).ready(function () {
$('#modActionCloseButton').on('click', () => {
// Send request out.
var formElement = document.querySelector('#modactionform');
var bodyFormData = new FormData(formElement);

const banData = {};
for (var [key, value] of bodyFormData.entries()) {
banData[key] = value;
}

Swal.fire({
title: 'Sending your request...',
onOpen: () => {
Swal.showLoading();

axios({
method: 'POST',
url: '/mod/ban',
data: banData,
config: { headers: { 'Content-Type': 'application/json' } },
})
.then(function(response) {
//handle success
// console.log(response);

$('#modModal').modal('hide');

// Clear the form for next input.
$('#modactionform')[0].reset();

Swal.close();
Swal.fire({
title: response.data,
type: 'success',
});
})
.catch(function(err) {
Swal.close();
Swal.fire({
title: err.response.data,
type: 'error',
});
});
},
});
});
});
6 changes: 4 additions & 2 deletions src/modsadmins/percivals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// It is not the Percival role in Avalon gameplay.

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

export function isPercival(username: string): boolean {
return percivalsArray.includes(username.toLowerCase());
}
}
29 changes: 28 additions & 1 deletion src/routes/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import ModLogComponent from '../views/components/mod/mod_log';
import ReportLog from '../views/components/mod/report';
import { MongoClient } from 'mongodb';
import { SESSIONS_COLLECTION_NAME } from '../constants';
import { isMod } from '../modsadmins/mods';
import { isPercival } from '../modsadmins/percivals';

const router = new Router();

Expand All @@ -41,7 +43,20 @@ const requiredFields = [
'descriptionByMod',
];

router.post('/ban', isModMiddleware, async (req, res) => {
router.post('/ban', async (req, res) => {
if (!isMod(req.user.username) && !isPercival(req.user.username)) {
res.status(401);
res.send(`You are not a moderator or percival.`);
return;
}

const userIsMod = isMod(req.user.username);
const userIsPercy = isPercival(req.user.username);

if ((userIsMod ^ userIsPercy) !== 1) {
throw Error("Expected requesting user to either be a mod or a Percy.");
}

try {
// Catch errors so that it's not shown to users.
// Multiple checks:
Expand Down Expand Up @@ -143,6 +158,18 @@ router.post('/ban', isModMiddleware, async (req, res) => {
return;
}

// Percies can only ban up to 24 hrs. No greater.
if (userIsPercy) {
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() + 1);

if (whenRelease > cutoff) {
res.status(400);
res.send(`Percival roles cannot ban for more than 24 hours.`);
return;
}
}

// Create the data object
const banData = {
ipBan:
Expand Down
14 changes: 14 additions & 0 deletions src/routes/tests/mod.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
describe('TestDates', () => {
it('SetMonthBeyond11', () => {
const a = new Date();

a.setMonth(11);
console.log(a);

a.setMonth(12);
console.log(a);

a.setMonth(13);
console.log(a);
});
});
1 change: 0 additions & 1 deletion src/sockets/commands/mod/mban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export const mban: Command = {
command: 'mban',
help: '/mban: Open the ban interface',
run: async (data, senderSocket) => {
// TODO
// @ts-ignore
if (isMod(senderSocket.request.user.username)) {
senderSocket.emit('openModModal');
Expand Down
2 changes: 2 additions & 0 deletions src/sockets/commands/percival/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { p } from './p';
import { mdc } from '../mod/mdc';
import { mforcemove } from '../mod/mforcemove';
import { mclose } from '../mod/mclose';
import { pban } from './pban';

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

Expand Down Expand Up @@ -39,6 +40,7 @@ const pclose = convertModCommandToPercivalCommand(mclose);

export const percivalCommands: Commands = {
[p.command]: p,
[pban.command]: pban,
[pdc.command]: pdc,
[pforcemove.command]: pforcemove,
[pclose.command]: pclose,
Expand Down
18 changes: 18 additions & 0 deletions src/sockets/commands/percival/pban.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Command } from '../types';
import { isPercival } from '../../../modsadmins/percivals';

export const pban: Command = {
command: 'pban',
help: '/pban: Open the ban interface',
run: async (data, senderSocket) => {
// @ts-ignore
if (isPercival(senderSocket.request.user.username)) {
senderSocket.emit('openModModal');

senderSocket.emit('messageCommandReturnStr', {
message: 'May your judgement bring peace to all!',
classStr: 'server-text',
});
}
},
};
8 changes: 6 additions & 2 deletions src/views/lobby.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,11 +1339,15 @@
></script>
<script
type="text/javascript"
src="<%-getVersionedPath('/scripts/lobby/sockets/myNotifications.js')%>"
src="<%-getVersionedPath('/scripts/lobby/sockets/game.js')%>"
></script>
<script
type="text/javascript"
src="<%-getVersionedPath('/scripts/lobby/sockets/game.js')%>"
src="<%-getVersionedPath('/scripts/lobby/sockets/mods.js')%>"
></script>
<script
type="text/javascript"
src="<%-getVersionedPath('/scripts/lobby/sockets/myNotifications.js')%>"
></script>

<script
Expand Down

0 comments on commit e878794

Please sign in to comment.