Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions squad-server/plugins/team-randomizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class TeamRandomizer extends BasePlugin {
}

static get defaultEnabled() {
return true;
return false;
}

static get optionsSpecification() {
Expand Down Expand Up @@ -38,28 +38,49 @@ export default class TeamRandomizer extends BasePlugin {

async onChatCommand(info) {
if (info.chat !== 'ChatAdmin') return;
if (this.randomizing) {
await this.server.rcon.warn(info.player.eosID, 'Randomization already in progress.');
return;
}
this.randomizing = true;

const players = this.server.players.slice(0);
try {
const players = this.server.players.slice(0);

let currentIndex = players.length;
let temporaryValue;
let randomIndex;
let currentIndex = players.length;

while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
while (currentIndex !== 0) {
const randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
[players[currentIndex], players[randomIndex]] = [
players[randomIndex],
players[currentIndex]
];
}

temporaryValue = players[currentIndex];
players[currentIndex] = players[randomIndex];
players[randomIndex] = temporaryValue;
}
await this.server.rcon.broadcast('Teams are being randomized, please wait.');

let team = '1';
let team = '1';
let switched = 0;

for (const player of players) {
if (player.teamID !== team) await this.server.rcon.switchTeam(player.eosID);
for (const player of players) {
if (player.teamID !== team) {
try {
await this.server.rcon.switchTeam(player.eosID);
switched++;
} catch (error) {
this.verbose(1, `Failed to switch ${player.name}: ${error.message}`);
}
}
team = team === '1' ? '2' : '1';
}

team = team === '1' ? '2' : '1';
await this.server.rcon.warn(
info.player.eosID,
`Randomization complete. ${switched} player(s) switched.`
);
} finally {
this.randomizing = false;
}
}
}