SquadJS/plugins/team-randomizer/index.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-05-15 12:42:39 -05:00
import { RCON_CHAT_MESSAGE } from 'squad-server/events/rcon';
export default {
name: 'team-randomizer',
2020-08-22 08:32:39 -05:00
description:
"The `team-randomizer` plugin can be used to randomize teams. It's great for destroying clan stacks or for " +
'social events. It can be run by typing `!randomize` into in-game admin chat.',
2020-08-22 08:32:39 -05:00
defaultDisabled: false,
optionsSpec: {
command: {
type: 'String',
required: false,
default: '!randomize',
description: 'The command used to randomize the teams.'
}
},
2020-05-15 12:42:39 -05:00
init: async (server, options) => {
const commandRegex = new RegExp(`^${options.command}`, 'i');
2020-05-15 12:42:39 -05:00
server.on(RCON_CHAT_MESSAGE, (info) => {
if (info.chat !== 'ChatAdmin') return;
2020-05-15 12:42:39 -05:00
const match = info.message.match(commandRegex);
if (!match) return;
2020-05-15 12:42:39 -05:00
const players = server.players.slice(0);
2020-05-15 12:42:39 -05:00
let currentIndex = players.length;
let temporaryValue;
let randomIndex;
2020-05-15 12:42:39 -05:00
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
2020-05-15 12:42:39 -05:00
// And swap it with the current element.
temporaryValue = players[currentIndex];
players[currentIndex] = players[randomIndex];
players[randomIndex] = temporaryValue;
2020-05-15 12:42:39 -05:00
}
let team = '1';
for (const player of players) {
if (player.teamID !== team) {
server.rcon.execute(`AdminForceTeamChange "${player.steamID}"`);
}
team = team === '1' ? '2' : '1';
}
});
}
};