SquadJS/plugins/team-randomizer/index.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-05-15 12:42:39 -05:00
import { RCON_CHAT_MESSAGE } from 'squad-server/events/rcon';
function shuffle(array) {
let currentIndex = array.length;
let temporaryValue;
let randomIndex;
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
export default function(server, options = {}) {
2020-06-13 11:35:00 -05:00
if (!server) throw new Error('TeamRandomizer must be provided with a reference to the server.');
2020-05-15 12:42:39 -05:00
const command = options.command || '!randomize';
const commandRegex = new RegExp(`^${command}`, 'i');
server.on(RCON_CHAT_MESSAGE, info => {
if (info.chat !== 'ChatAdmin') return;
const match = info.message.match(commandRegex);
if (!match) return;
const players = server.players.slice(0);
shuffle(players);
let team = '1';
for (const player of players) {
if (player.teamID !== team) {
server.rcon.execute(`AdminForceTeamChange "${player.steamID}"`);
}
team = team === '1' ? '2' : '1';
}
});
}