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
/
Copy pathindex.ts
111 lines (94 loc) · 3.93 KB
/
index.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import HaxballJS from "haxball.js";
import * as fs from "fs";
import { handlePlayerActivity, checkAndHandleInactivePlayers } from "./afkdetection.js";
import { handlePlayerJoining } from "./playerjoining.js";
import { handlePlayerLeaving } from "./playerleaving.js";
import { handleTeamWin } from "./teammanagement.js";
import { checkAndHandleBadWords, checkAndHandleSpam } from "./moderation.js";
import { checkAndHandleCommands } from "./commands.js";
export const debuggingMode = false;
const scoreLimit: number = 3;
const timeLimit: number = 3;
export const playerConnStrings = new Map<number, string>();
export const adminAuthList: Set<string> = new Set(fs.readFileSync("lists/adminlist.txt", "utf8").split("\n").map((line: string) => line.trim()));
export const badWordList: Set<string> = new Set(fs.readFileSync("lists/badwords.txt", "utf8").split("\n").map((line: string) => line.trim()));
const tokenFile: string = fs.readFileSync("token.txt", "utf8");
const practiceStadium: string = fs.readFileSync("stadiums/practice.hbs", "utf8");
const stadium2x2: string = fs.readFileSync("stadiums/futsal2x2.hbs", "utf8");
const stadium3x3: string = fs.readFileSync("stadiums/futsal3x3.hbs", "utf8");
export let specPlayerIdList: number[] = [];
export let redPlayerIdList: number[] = [];
export let bluePlayerIdList: number[] = [];
export let room: RoomObject;
HaxballJS.then((HBInit) => {
room = HBInit({
roomName: "⚖️ FUTSAL 24/7 JUSTO (~15 DE PING) ⚖️",
maxPlayers: 16,
public: !debuggingMode,
noPlayer: true,
geo: {
code: "PT",
lat: 41.15144214309606,
lon: -8.613879659626768
},
token: tokenFile, //https://haxball.com/headlesstoken
});
room.setScoreLimit(scoreLimit);
room.setTimeLimit(timeLimit);
room.setTeamsLock(true);
room.setCustomStadium(practiceStadium);
room.onRoomLink = function (url: string) {
console.log(url);
};
room.onPlayerJoin = function (player: PlayerObject): void {
handlePlayerJoining(player);
}
room.onPlayerLeave = function (player: PlayerObject): void {
handlePlayerLeaving(player);
}
room.onTeamGoal = function (teamId: number) {
const scores = room.getScores();
const teamScore = teamId === 1 ? scores.red : scores.blue;
const teamPlayerIdList = teamId === 1 ? redPlayerIdList : bluePlayerIdList;
if (teamScore === scoreLimit || scores.time > timeLimit * 60) restartGameWithCallback(() => handleTeamWin(teamPlayerIdList));
}
//triggers *only* when a team is winning and the timer runs out,
//because the room is also listening for the onTeamGoal event, which triggers first
room.onTeamVictory = function (scores: ScoresObject): void {
const teamPlayerIdList = scores.red > scores.blue ? redPlayerIdList : bluePlayerIdList;
restartGameWithCallback(() => handleTeamWin(teamPlayerIdList));
}
room.onPlayerActivity = function (player: PlayerObject): void {
handlePlayerActivity(player.id);
}
room.onGameTick = function (): void {
if (!debuggingMode) checkAndHandleInactivePlayers();
}
room.onPlayerChat = function (player: PlayerObject, message: string): boolean {
console.log(`${player.name}: ${message}`);
return !checkAndHandleCommands(player, message) && !checkAndHandleBadWords(player, message) && !checkAndHandleSpam(player, message);
}
});
export function restartGameWithCallback(callback: () => void): void {
room.stopGame();
callback();
setAppropriateStadium();
room.startGame();
const playerList: PlayerObject[] = room.getPlayerList();
if (playerList.length !== 1) pauseUnpauseGame();
}
function setAppropriateStadium() {
const playerList = room.getPlayerList();
const playerListLength = playerList.length;
if (playerListLength === 1) {
room.setCustomStadium(practiceStadium);
} else if (playerListLength >= 6) {
room.setCustomStadium(stadium3x3);
} else {
room.setCustomStadium(stadium2x2);
}
}
export function pauseUnpauseGame() {
room.pauseGame(true);
room.pauseGame(false);
}