This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
playerleaving.ts
32 lines (30 loc) · 1.64 KB
/
playerleaving.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { removePlayerFromAfkMapsAndSets } from "./afkdetection.js";
import { specPlayerIdList, playerConnStrings, redPlayerIdList, bluePlayerIdList, room, pauseUnpauseGame, restartGameWithCallback } from "./index.js";
import { movePlayerToTeam, moveLastOppositeTeamMemberToSpec } from "./teammanagement.js";
export function handlePlayerLeaving(player: PlayerObject): void {
const playerId: number = player.id;
let playerIdList: number[] = [];
const playerList = room.getPlayerList();
if (redPlayerIdList.includes(playerId) || bluePlayerIdList.includes(playerId)) {
playerIdList = redPlayerIdList.includes(playerId) ? redPlayerIdList : bluePlayerIdList;
if (playerList.length !== 0) handleTeamPlayerLeaving(playerIdList, playerList);
} else {
playerIdList = specPlayerIdList;
}
playerIdList.splice(playerIdList.indexOf(playerId), 1);
removePlayerFromAfkMapsAndSets(playerId);
playerConnStrings.delete(playerId);
if (playerList.length === 0) room.stopGame();
console.log(`>>> ${player.name} saiu da sala.`);
}
function handleTeamPlayerLeaving(teamPlayerIdList: number[], playerList: PlayerObject[]) {
const oppositeTeamPlayerIdList: number[] = teamPlayerIdList === redPlayerIdList ? bluePlayerIdList : redPlayerIdList;
if (playerList.length === 1) {
restartGameWithCallback(() => movePlayerToTeam(playerList[0]!.id, redPlayerIdList));
} else if (specPlayerIdList.length === 0) {
restartGameWithCallback(() => moveLastOppositeTeamMemberToSpec(oppositeTeamPlayerIdList));
} else {
movePlayerToTeam(specPlayerIdList[0]!, teamPlayerIdList);
pauseUnpauseGame();
}
}