-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathafk-monitor.js
276 lines (237 loc) · 7.83 KB
/
afk-monitor.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* This plugin for Headless Haxball Manager (HHM) monitors the player
* activity and kicks the players that idle too long.
*/
let room = HBInit();
room.pluginSpec = {
name: `hr/afk-monitor`,
author: `salamini`,
version: `1.0.0`,
dependencies: [
`sav/game-state`,
`sav/cron`
],
// All times in the config are in seconds.
config: {
// If true, then only admins will be monitored.
adminsOnly: true,
// Max time player can be AFK.
maxIdleTime: 5 * 60,
// Max time player can be AFK when he is playing.
maxIdleTimeWhenPlaying: 20,
// Max time admins can be AFK when they are required to take action.
maxAdminIdleTime: 20,
// How many seconds beforehand to warn the player before getting kicked.
warnBefore: 7,
// Message to send to player when he is kicked.
kickMessage: 'AFK',
// Enables debugging messages to console (use only if developing).
debug: false
}
};
const debug = room.getConfig('debug')
? (msg) => { console.debug(`hr/afk-monitor: ${msg}`)}
: () => {};
let afkTimeouts = new Map();
let warnAfkTimeouts = new Map();
let lastActiveTimes = new Map();
let lastAdminActivityTime = 0;
let gameStates;
function onPlayerChat(player) {
onPlayerActivity(player);
}
function onPlayerJoin(player) {
refreshLastActiveTime(player);
let gameState = room.getPlugin('sav/game-state').getGameState();
refreshTimeout(player, gameState);
}
function onPlayerAdminChange(changedPlayer, byPlayer) {
let gameState = room.getPlugin('sav/game-state').getGameState();
let players = room.getPlayerList().filter(p => p !== 0);
let admins = players.filter(p => p.admin);
if (players.length === 1 || admins.length > 1) {
lastAdminActivityTime = Date.now();
}
if (!byPlayer || byPlayer.admin) {
lastAdminActivityTime = Date.now();
refreshAllTimeouts(gameState);
} else {
refreshTimeout(changedPlayer, gameState);
}
}
function onPlayerTeamChange(changedPlayer, byPlayer) {
let gameState = room.getPlugin('sav/game-state').getGameState();
if (!byPlayer || byPlayer.admin) {
lastAdminActivityTime = Date.now();
refreshAllTimeouts(gameState);
} else {
refreshLastActiveTime(byPlayer);
refreshTimeout(changedPlayer, gameState);
}
}
function onPlayerKicked(kickedPlayer, reason, ban, byPlayer) {
if (byPlayer && byPlayer.id !== kickedPlayer.id) {
let gameState = room.getPlugin('sav/game-state').getGameState();
lastAdminActivityTime = Date.now();
refreshAllTimeouts(gameState);
}
}
function onPlayerLeave(player) {
removeLastActiveTime(player);
removeTimeout(player);
}
function onGameStart(player) {
lastAdminActivityTime = Date.now();
refreshAllTimeouts(gameStates.STARTED);
}
function onGameStop(player) {
lastAdminActivityTime = Date.now();
refreshAllTimeouts(gameStates.STOPPED);
}
function onGamePause(player) {
lastAdminActivityTime = Date.now();
refreshAllTimeouts(gameStates.PAUSED);
}
function onGameUnpause(player) {
lastAdminActivityTime = Date.now();
refreshAllTimeouts(gameStates.STARTED);
}
function onPlayerActivity(player) {
if (!player || player.id === 0) return;
refreshLastActiveTime(player);
let gameState = room.getPlugin('sav/game-state').getGameState();
refreshTimeout(player, gameState);
}
/**
* Updates the last active time of the player with given id to be the current
* time.
*
* @param {number} player - Id of the player.
*/
function refreshLastActiveTime(player) {
return lastActiveTimes.set(player.id, Date.now());
}
/**
* Removes the tracking of last active time from the player with given id.
*
* @param {number} playerId - Id of the player.
*/
function removeLastActiveTime(player) {
return lastActiveTimes.delete(player.id);
}
/**
* Removes the warning and kick timeouts from the player with given id.
*
* @param {number} playerId - Id of the player.
*/
function removeTimeout(player) {
let timeout = afkTimeouts.get(player.id);
if (timeout) clearTimeout(timeout);
timeout = warnAfkTimeouts.get(player.id);
if (timeout) clearTimeout(timeout);
afkTimeouts.delete(player.id);
warnAfkTimeouts.delete(player.id);
}
/**
* Refreshes the warn and kick timeouts of all players in the room.
*/
function refreshAllTimeouts(gameState) {
const players = room.getPlayerList().filter((p) => p.id !== 0);
for (let player of players) {
refreshTimeout(player, gameState);
}
}
function getPlayersInTeams() {
return room.getPlayerList().filter((p) => p.id !== 0 && p.team !== 0);
}
/**
* Refreshes the kick and warning and kick timeouts of the player with given id.
* Calculates the new times based on the game state, player admin status and
* players last active time.
*
* @param {number} playerId - Id of the player.
*/
function refreshTimeout(player, gameState) {
if (!player) {
removeLastActiveTime(player);
removeTimeout(player);
return;
}
let adminsOnly = room.getConfig('adminsOnly');
if (adminsOnly && !player.admin) return;
let maxIdleTime = 0;
let currentTime = Date.now();
let lastActiveTime = lastActiveTimes.get(player.id);
lastActiveTime = lastActiveTime || currentTime;
// determine max idle times for admins
if (player.admin) {
let actionIsRequired =
gameState === gameStates.STOPPED ||
gameState === gameStates.PAUSED ||
(gameState !== gameStates.STOPPED && getPlayersInTeams() < 1)
if (actionIsRequired) {
maxIdleTime = room.getConfig('maxAdminIdleTime');
lastActiveTime = lastActiveTime < lastAdminActivityTime
? lastAdminActivityTime
: lastActiveTime;
} else if (gameState !== gameStates.STOPPED && player.team !== 0) {
maxIdleTime = room.getConfig('maxIdleTimeWhenPlaying');
// ignore the last active time so players dont kicked straight away when
// moved to team or when game is started
lastActiveTime = currentTime;
} else {
maxIdleTime = room.getConfig('maxIdleTime');
}
// determine max idle times for players
} else {
if (gameState !== gameStates.STOPPED && player.team !== 0) {
maxIdleTime = room.getConfig('maxIdleTimeWhenPlaying');
// ignore the last active time so players dont kicked straight away when
// moved to team or when game is started
lastActiveTime = currentTime;
} else {
maxIdleTime = room.getConfig('maxIdleTime');
}
}
maxIdleTimeInMs = maxIdleTime * 1000;
maxIdleTimeInMs -= currentTime - lastActiveTime;
let timeToWarn = maxIdleTimeInMs - (room.getConfig('warnBefore') * 1000);
removeTimeout(player);
debug(`Kicking ${player.name} in ${maxIdleTimeInMs / 1000} seconds.`);
let timeout = setTimeout(kickInactivePlayer, maxIdleTimeInMs, player.id);
let warnTimeout = setTimeout(warnInactivePlayer, timeToWarn, player.id);
afkTimeouts.set(player.id, timeout);
warnAfkTimeouts.set(player.id, warnTimeout);
}
/**
* Kicks the player when he has been idling too long.
*
* @param {number} playerId - Id of the player.
*/
function kickInactivePlayer(playerId) {
const kickMessage = room.getConfig('kickMessage');
room.kickPlayer(playerId, kickMessage, false);
}
/**
* Warns the player when he has been idling too long.
*
* @param {number} playerId - Id of the player.
*/
function warnInactivePlayer(playerId) {
let msg = `Show you are not AFK or get kicked in ${room.getConfig('warnBefore')} seconds.`
room.sendAnnouncement(msg, playerId, 0xFF0000);
}
room.onRoomLink = function onRoomLink() {
gameStates = room.getPlugin('sav/game-state').states;
room.onPlayerChat = onPlayerChat;
room.onPlayerAdminChange = onPlayerAdminChange;
room.onPlayerTeamChange = onPlayerTeamChange;
room.onPlayerJoin = onPlayerJoin;
room.onPlayerLeave = onPlayerLeave;
room.onGameStart = onGameStart;
room.onGameStop = onGameStop;
room.onGamePause = onGamePause;
room.onGameUnpause = onGameUnpause;
room.onPlayerKicked = onPlayerKicked;
room.onPlayerActivity = onPlayerActivity;
}