-
Notifications
You must be signed in to change notification settings - Fork 5
/
pause.js
76 lines (65 loc) · 1.93 KB
/
pause.js
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
/**
* This plugin for Haxball Headless Manager enables players to pause the game
* by writing 'p'. It can also pause game if a player leaves when game has
* started.
*/
let room = HBInit();
room.pluginSpec = {
name: `hr/pause`,
author: `salamini`,
version: `1.0.0`,
dependencies: [
`sav/game-state`,
],
// All times in the config are in seconds.
config: {
// If true, then game is paused if playing player leaves.
pauseWhenPlayerLeaves: true,
// If true, players are allowed to pause the game by writing 'p'.
allowPlayersToPause: true,
// How many times player can pause game (0 is unlimited).
maxPauseTimes: 1,
}
};
let playerPauseCounter = new Map();
function onPlayerLeave(player) {
const pauseWhenPlayerLeaves = room.getConfig('pauseWhenPlayerLeaves');
if (pauseWhenPlayerLeaves && player.team !== 0) {
room.pauseGame(true);
}
playerPauseCounter.delete(player.id);
}
function onGameStop() {
playerPauseCounter = new Map();
}
function onPlayerChat(player, message) {
if (!player) return;
if (player.id === 0) return;
if (player.team === 0) return;
const allowPlayersToPause = room.getConfig('allowPlayersToPause');
if (!allowPlayersToPause || message !== 'p') return;
const gameState = room.getPlugin('sav/game-state');
if (gameState.getGameState() !== gameState.states.STARTED) return;
let counter = playerPauseCounter.get(player.id);
if (!counter) {
counter = 1;
} else {
counter++;
}
playerPauseCounter.set(player.id, counter);
const maxPauseTimes = room.getConfig('maxPauseTimes');
if (maxPauseTimes !== 0 && counter > maxPauseTimes) {
room.sendAnnouncement(
`You are only allowed to pause ${maxPauseTimes} per game!`,
player.id,
0xFF0000
);
return;
}
room.pauseGame(true);
}
room.onRoomLink = function onRoomLink() {
room.onPlayerLeave = onPlayerLeave;
room.onGameStop = onGameStop;
room.onPlayerChat = onPlayerChat;
}