-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
443 lines (377 loc) · 13.3 KB
/
app.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// app.js
const { Client } = require('pg');
const pgp = require('pg-promise')();
const dgram = require('dgram');
const fs = require('fs');
const path = require('path');
const http = require('https');
const net = require('net');
const express = require('express');
const RankingManager = require('./ranking'); // Atualize o caminho conforme necessário
const Hall = require('./hall'); // Importe a classe Hall
class GamesNetPanzerBrowser {
constructor() {
this.masterservers = [
{ host: 'netpanzer.io', port: 28900 },
// Adicione outros servidores mestres, se necessário
];
this.visitedmasters = {};
this.mastersstack = [...this.masterservers];
this.gameservers = {};
this.timeout = 2000; // Tempo limite em milissegundos
this.refreshInterval = 15000; // Intervalo de atualização em milissegundos (15 segundos)
this.monthlyStats = {}; // Armazenar estatísticas mensais
this.currentMonth = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long' });
// Configurações do banco de dados PostgreSQL
this.dbConfig = {
host: 'localhost',
port: 5432,
database: 'postgres',
user: 'postgres',
password: 'your_db_password'
};
// Conecta ao banco de dados
this.db = pgp(this.dbConfig);
// Cria uma instância do RankingManager, passando a conexão db
this.rankingManager = new RankingManager(this.monthlyStats, this.db);
// Crie uma instância da classe Hall e passe a conexão do banco de dados
this.hall = new Hall(this.db);
this.app = express();
// Configure the route to handle requests for the top 10 players
this.app.get('/top-players', async (req, res) => {
const { month, year } = req.query;
try {
const topPlayers = await this.hall.getTopPlayers(month, year);
res.json(topPlayers);
} catch (error) {
console.error(`Error fetching top players: ${error.message}`);
res.status(500).json({ message: 'Internal Server Error' });
}
});
// Inicia o processo de atualização dos servidores
this.startServerRefresh();
// Inicia o servidor HTTP para exibir informações dos servidores e o ranking
this.startHTTPServer();
}
start() {
this.browseMasters();
}
browseMasters() {
if (this.mastersstack.length === 0) {
console.log('Finished browsing masters.');
this.getGameServersStatus();
return;
}
const master = this.mastersstack.pop();
this.visitedmasters[`${master.host}:${master.port}`] = master;
const client = new net.Socket();
client.connect(master.port, master.host, () => {
console.log(`Connected to ${master.host}:${master.port}`);
client.write('\\list\\gamename\\netpanzer\\final\\');
});
client.on('data', (data) => {
console.log(`Received from ${master.host}:${master.port}: ${data}`);
const serverList = this.parseServerList(data.toString(), master);
this.addGameServers(serverList);
client.destroy();
this.browseMasters(); // Continue com o próximo servidor mestre
});
client.on('close', () => {
console.log(`Connection closed to ${master.host}:${master.port}`);
});
client.on('error', (err) => {
console.error(`Error connecting to ${master.host}:${master.port}: ${err}`);
this.browseMasters(); // Continue com o próximo servidor mestre em caso de erro
});
}
parseServerList(data, master) {
const servers = [];
const tokens = data.split('\\');
for (let i = 1; i < tokens.length - 1; i += 2) {
if (tokens[i] === 'ip' && tokens[i + 2] === 'port') {
const serverInfo = {
ip: tokens[i + 1],
port: tokens[i + 3],
masterserver: master,
numplayers: 0, // Inicialmente, define o número de jogadores como 0
};
servers.push(serverInfo);
}
}
return servers;
}
addGameServers(serverList) {
serverList.forEach((server) => {
const serverKey = `${server.ip}:${server.port}`;
if (!this.gameservers[serverKey]) {
this.gameservers[serverKey] = server;
}
});
}
getGameServersStatus() {
console.log('Getting game servers status...');
Object.values(this.gameservers).forEach((server) => {
this.queryServerStatus(server);
});
}
queryServerStatus(server) {
const udpClient = dgram.createSocket('udp4');
udpClient.send(Buffer.from('\\status\\final\\'), server.port, server.ip, (err) => {
if (err) {
console.error(`Error sending UDP request to ${server.ip}:${server.port}: ${err}`);
udpClient.close();
}
});
udpClient.on('message', (data, remote) => {
console.log(`Received status from ${server.ip}:${server.port}: ${data}`);
const serverInfo = this.parseServerStatus(data.toString());
this.updateServerInfo(server, serverInfo);
udpClient.close();
});
udpClient.on('error', (err) => {
console.error(`Error receiving UDP response from ${server.ip}:${server.port}: ${err}`);
udpClient.close();
});
}
parseServerStatus(data) {
const serverInfo = {};
const tokens = data.split('\\');
serverInfo.players = [];
for (let i = 0; i < tokens.length - 1; i += 2) {
const key = tokens[i];
const value = tokens[i + 1];
if (key.startsWith('player_')) {
const playerIndex = parseInt(key.split('_')[1], 10);
serverInfo.players[playerIndex] = serverInfo.players[playerIndex] || {};
serverInfo.players[playerIndex].name = value;
} else if (key.startsWith('kills_') || key.startsWith('deaths_')) {
const playerIndex = parseInt(key.split('_')[1], 10);
const statType = key.split('_')[0]; // 'kills' ou 'deaths'
serverInfo.players[playerIndex][statType] = value;
} else {
serverInfo[key] = value;
}
}
return serverInfo;
}
updateServerInfo(server, serverInfo) {
server.cache = serverInfo;
const currentMonth = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long' });
if (currentMonth !== this.currentMonth) {
this.currentMonth = currentMonth;
this.monthlyStats = {};
}
serverInfo.players.forEach((player) => {
const playerName = player.name || 'Unknown';
if (!this.monthlyStats[playerName]) {
this.monthlyStats[playerName] = { player_name: playerName, kills: 0, deaths: 0, lastSessionKills: 0, lastSessionDeaths: 0 };
}
const newKills = parseInt(player.kills || 0, 10);
const newDeaths = parseInt(player.deaths || 0, 10);
if (newKills === 0 && newDeaths === 0) {
if (this.monthlyStats[playerName].kills > 0 || this.monthlyStats[playerName].deaths > 0) {
this.monthlyStats[playerName].lastSessionKills = 0;
this.monthlyStats[playerName].lastSessionDeaths = 0;
}
} else {
this.monthlyStats[playerName].kills += newKills - this.monthlyStats[playerName].lastSessionKills;
this.monthlyStats[playerName].deaths += newDeaths - this.monthlyStats[playerName].lastSessionDeaths;
this.monthlyStats[playerName].lastSessionKills = newKills;
this.monthlyStats[playerName].lastSessionDeaths = newDeaths;
}
});
// Verifica se há jogadores no servidor antes de salvar no banco de dados
if (serverInfo.players.length > 0) {
this.saveDataToDatabase(server, serverInfo);
}
this.rankingManager.updateRanking();
}
// Salva os dados no banco
saveDataToDatabase(server, serverInfo) {
console.log('Saving data to ranking database for server:', server.ip, server.port);
const currentMonthYear = new Date().toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
});
serverInfo.players.forEach((player) => {
const playerName = player.name || 'Unknown';
const playerData = {
player_name: playerName,
kills: parseInt(player.kills || 0, 10),
deaths: parseInt(player.deaths || 0, 10),
month_year: currentMonthYear,
};
console.log('Player data to save to ranking:', playerData);
// Salva os dados do jogador na tabela ranking com a coluna month_year
this.db.none(
`
INSERT INTO ranking(player_name, kills, deaths, month_year)
VALUES($/player_name/, $/kills/, $/deaths/, $/month_year/)
ON CONFLICT (player_name)
DO UPDATE SET
kills = ranking.kills + $/kills/,
deaths = ranking.deaths + $/deaths/
`,
playerData
)
.then(() => {
console.log(`Player data saved to ranking database for ${playerData.player_name}`);
})
.catch((error) => {
console.error(`Error saving player data to ranking database for ${playerData.player_name} in ${playerData.month_year}: ${error}`);
});
});
// Atualiza o ranking após salvar os dados
this.rankingManager.updateRanking();
}
.then(() => {
console.log(`Player data saved to ranking database for ${playerData.player_name}`);
})
.catch ((error) => {
console.error(`Error saving player data to ranking database for ${playerData.player_name} in ${playerData.month_year}: ${error}`);
});
});
// Atualiza o ranking após salvar os dados
this.rankingManager.updateRanking();
}
startHTTPServer() {
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/your_directory/privkey.pem'), // Substitua com o caminho da sua chave privada
cert: fs.readFileSync('/etc/letsencrypt/live/your_directory/fullchain.pem'), // Substitua com o caminho do seu certificado
};
const server = https.createServer(options, async (req, res) => {
if (req.url === '/ranking') {
const rankingHTMLFilePath = path.join(__dirname, 'ranking', 'ranking.html');
const rankingHTML = fs.readFileSync(rankingHTMLFilePath, 'utf-8');
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(rankingHTML);
} else if (req.url === '/hall') {
const hall = new Hall(this.db);
const hallHtml = await hall.getTopPlayersHtml();
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(hallHtml);
} else {
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(this.createHTMLTable());
}
});
server.listen(8080, () => {
console.log('HTTP server is running on port 8080');
});
}
createHTMLTable() {
let html = `
<html>
<head>
<title>Game Servers</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>${this.getCSS()}</style>
</head>
<body>
<table>
<tr>
<th>Porta</th>
<th>Servidor</th>
<th>Map</th>
<th>Estilo de Jogo</th>
<th>Players</th>
<th>Info Players</th>
</tr>`;
Object.values(this.gameservers).forEach(server => {
const cache = server.cache || {};
let playersData = '<ul>';
cache.players.forEach((player, index) => {
playersData += `
<li>
${player.name || 'Unknown'}: Kills: ${player.kills || '0'}, Deaths: ${player.deaths || '0'}
</li>`;
});
playersData += '</ul>';
html += `
<tr>
<td data-label="Porta">${server.port}</td>
<td data-label="Servidor">${cache.hostname || 'N/A'}</td>
<td data-label="Map">${cache.mapname || 'N/A'}</td>
<td data-label="Estilo de Jogo">${cache.gamestyle || 'N/A'}</td>
<td data-label="Players">${cache.numplayers || '0'}</td>
<td data-label="Info Players">${playersData}</td>
</tr>`;
});
html += `
</table>
</body>
</html>`;
return html;
}
getCSS() {
// Implemente a lógica de obtenção do CSS conforme estava no código original
return `
body {
background-color: #f4f4f4;
color: #333;
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
}
h1 {
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
color: #3498db;
}
table {
border-collapse: collapse;
width: 100%;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
th {
background-color: #3498db;
color: #fff;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
a {
color: #3498db;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* Estilo para telas pequenas */
@media screen and (max-width: 768px) {
body {
font-size: 14px;
}
td:not(:last-child)::before {
display: block;
content: attr(data-label) ":";
font-weight: bold;
margin-bottom: 5px;
}
td {
border: none;
position: relative;
padding-top: 5px;
padding-bottom: 5px;
white-space: normal;
text-align: left;
}
}
`;
}
startServerRefresh() {
setInterval(() => {
Object.values(this.gameservers).forEach((server) => {
this.queryServerStatus(server);
});
}, this.refreshInterval);
}
}
// Criar uma instância da classe e iniciar o processo
const browser = new GamesNetPanzerBrowser();
browser.start();