SquadJS/squad-server/plugins/team-randomizer.js
2020-10-23 11:29:38 +01:00

56 lines
1.5 KiB
JavaScript

import BasePlugin from './base-plugin.js';
export default class TeamRandomizer extends BasePlugin {
static get description() {
return (
"The <code>TeamRandomizer</code> can be used to randomize teams. It's great for destroying clan stacks or for " +
'social events. It can be run by typing, by default, <code>!randomize</code> into in-game admin chat'
);
}
static get defaultEnabled() {
return true;
}
static get optionsSpecification() {
return {
command: {
required: false,
description: 'The command used to randomize the teams.',
default: 'randomize'
}
};
}
constructor(server, options, optionsRaw) {
super(server, options, optionsRaw);
this.server.on(`CHAT_COMMAND:${this.options.command}`, async (info) => {
if (info.chat !== 'ChatAdmin') return;
const players = this.server.players.slice(0);
let currentIndex = players.length;
let temporaryValue;
let randomIndex;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = players[currentIndex];
players[currentIndex] = players[randomIndex];
players[randomIndex] = temporaryValue;
}
let team = '1';
for (const player of players) {
if (player.teamID !== team) await this.server.rcon.switchTeam(player.steamID);
team = team === '1' ? '2' : '1';
}
});
}
}