-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
423 lines (360 loc) · 13.3 KB
/
server.js
File metadata and controls
423 lines (360 loc) · 13.3 KB
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
// server.js (com generateId)
const WebSocket = require('ws');
const Player = require('./player.js');
const Weapon = require('./weapon.js');
const { generateId } = require('./utils.js');
class Server {
constructor(port) {
this.port = port;
this.sockets = new Map(); // Map<playerId, socket>
this.players = new Map(); // Map<playerId, Player>
this.weapons = new Map(); // Map<weaponId, Weapon>
this.wss = null;
}
start() {
this.wss = new WebSocket.Server({
port: this.port
});
// Cria algumas armas para teste
this.createWeapon('pistol', 10, 0, 10, { ammo: 12 });
this.createWeapon('rifle', -10, 0, 10, { ammo: 30 });
this.wss.on('connection', (socket) => {
this.onConnect(socket);
});
this.wss.on('error', (error) => {
console.error('Erro no servidor WebSocket:', error);
});
// Intervalo de atualizações
setInterval(() => {
for (const socket of this.sockets.values()) {
if (socket.readyState === 1) {
const players = [];
for (const player of this.players.values()) {
players.push(player.toJSON());
}
// Coleta TODAS as armas
const allWeapons = [];
for (const weapon of this.weapons.values()) {
allWeapons.push(weapon.toJSON());
}
socket.send(JSON.stringify({
type: 'update',
content: {
players,
weapons: allWeapons
}
}));
}
}
}, 25);
setInterval(() => {
this.wss.clients.forEach((socket) => {
if (socket.isAlive === false) {
return socket.terminate();
}
socket.isAlive = false;
socket.ping();
});
}, 15000);
console.log(`Servidor WebSocket rodando na porta ${this.port}`);
return this;
}
createWeapon(type = 'pistol', posX = 0, posY = 0, posZ = 0, content = {}) {
const weaponId = generateId(this.weapons);
const weapon = new Weapon(weaponId, type, content);
weapon.px = posX;
weapon.py = posY;
weapon.pz = posZ;
this.weapons.set(weaponId, weapon);
return weapon;
}
onConnect(socket) {
// Gera ID único para o jogador
const playerId = generateId(this.players);
const player = new Player(playerId);
this.sockets.set(playerId, socket);
this.players.set(playerId, player);
if (this.players.size > 100) {
socket.send(JSON.stringify({
type: 'error',
content: { message: 'Servidor cheio' }
}));
socket.close();
return;
}
console.log(`Jogador ${playerId} conectado. Total: ${this.players.size}`);
// Envia estado inicial
const initialPlayers = [];
for (const p of this.players.values()) {
initialPlayers.push(p.toJSON());
}
const initialWeapons = [];
for (const weapon of this.weapons.values()) {
initialWeapons.push(weapon.toJSON());
}
socket.send(JSON.stringify({
type: 'connected',
content: {
player: player.toJSON(),
players: initialPlayers,
weapons: initialWeapons
}
}));
socket.on('message', (message) => this.onMessage(playerId, message));
socket.on('close', () => this.onClose(playerId));
socket.on('error', (error) => this.onError(playerId, error));
socket.isAlive = true;
socket.on('pong', () => {
socket.isAlive = true;
});
}
onMessage(playerId, message) {
try {
const event = JSON.parse(message);
if (!event || !event.type) return;
const player = this.players.get(playerId);
if (!player || !this.sockets.get(playerId)) return;
switch (event.type) {
case 'update':
this.onUpdate(playerId, event.content);
break;
case 'hit':
this.onHit(playerId, event.content);
break;
case 'heal':
this.onHeal(playerId, event.content);
break;
case 'exit':
this.onExit(playerId, event.content);
break;
case 'pick':
this.onPick(playerId, event.content);
break;
case 'drop':
this.onDrop(playerId, event.content);
break;
default:
console.warn(`Tipo desconhecido: ${event.type} do ${playerId}`);
}
} catch (error) {
console.error(`Erro ao processar mensagem do ${playerId}:`, error);
}
}
onClose(playerId) {
const player = this.players.get(playerId);
if (player) {
// Larga todas as armas ao desconectar
for (let i = 0; i < player.slots.length; i++) {
const weaponId = player.slots[i];
if (weaponId) {
const weapon = this.weapons.get(weaponId);
if (weapon) {
weapon.parentId = null;
weapon.px = player.posX;
weapon.py = player.posY;
weapon.pz = player.posZ;
}
}
}
console.log(`Jogador ${playerId} desconectado`);
}
this.players.delete(playerId);
this.sockets.delete(playerId);
}
onError(playerId, error) {
console.error(`Erro no socket do ${playerId}:`, error);
this.players.delete(playerId);
this.sockets.delete(playerId);
}
onUpdate(playerId, content) {
const player = this.players.get(playerId);
if (!player || !content) return;
// Atualiza dados do jogador
if (content.player) {
const { slots, ...playerData } = content.player;
player.fromJSON(playerData);
}
// Atualiza armas do jogador (opcional)
if (content.weapons && Array.isArray(content.weapons)) {
// Atualiza cada arma que pertence ao jogador
content.weapons.forEach(weaponData => {
if (weaponData && weaponData.id) {
const weapon = this.weapons.get(weaponData.id);
if (weapon && weapon.parentId === playerId) {
// Mantém o parentId (não deixa o cliente alterar)
const originalParentId = weapon.parentId;
weapon.fromJSON(weaponData);
weapon.parentId = originalParentId; // Mantém o parentId original
}
}
});
}
}
onHit(hiterId, content) {
if (!content || !content.target || !content.amount || content.amount <= 0) return;
const { target, amount } = content;
const hiter = this.players.get(hiterId);
const targetPlayer = this.players.get(target);
const targetSocket = this.sockets.get(target);
if (!hiter || !targetPlayer || !targetSocket) return;
if (!hiter.isAlive) return;
targetPlayer.hit(amount);
console.log(`${hiterId} atingiu ${target} com ${amount} de dano`);
targetSocket.send(JSON.stringify({
type: 'hit',
content: { target, amount }
}));
}
onHeal(healerId, content) {
if (!content || !content.target || !content.amount || content.amount <= 0) return;
const { target, amount } = content;
const healer = this.players.get(healerId);
const targetPlayer = this.players.get(target);
if (!healer || !targetPlayer) return;
if (!healer.isAlive || !targetPlayer.isAlive) return;
targetPlayer.heal(amount);
console.log(`${healerId} curou ${target} em ${amount}`);
}
onExit(playerId, content) {
const socket = this.sockets.get(playerId);
if (socket && socket.readyState === 1) {
socket.close();
}
}
onPick(playerId, content) {
if (!content || content.weaponId === undefined) {
console.warn(`Pick inválido de ${playerId}:`, content);
return;
}
const player = this.players.get(playerId);
const weapon = this.weapons.get(content.weaponId);
if (!player || !weapon) {
console.warn(`Jogador ou arma não encontrados: player=${playerId}, weapon=${content.weaponId}`);
return;
}
// Verifica se arma está no chão
if (weapon.parentId !== null) {
console.log(`Arma ${content.weaponId} já está com jogador ${weapon.parentId}`);
return;
}
// Tenta pegar a arma
const slotIndex = player.pick(content.weaponId);
if (slotIndex === -1) {
console.log(`Jogador ${playerId} não tem slots vazios`);
return;
}
weapon.parentId = playerId;
console.log(`Jogador ${playerId} pegou arma ${weapon.id} no slot ${slotIndex}`);
// Envia confirmação
const socket = this.sockets.get(playerId);
if (socket && socket.readyState === 1) {
socket.send(JSON.stringify({
type: 'pick',
content: {
weaponId: weapon.id,
slotIndex: slotIndex
}
}));
}
}
onDrop(playerId, content) {
const player = this.players.get(playerId);
if (!player) return;
// Pode dropar arma específica ou do slot atual
let weaponId;
if (content && content.weaponId !== undefined) {
weaponId = content.weaponId;
// Remove a arma específica do slot do jogador
for (let i = 0; i < player.slots.length; i++) {
if (player.slots[i] === weaponId) {
player.slots[i] = null;
break;
}
}
} else {
// Drop da arma do slot atual
weaponId = player.drop();
}
if (!weaponId) {
console.log(`Jogador ${playerId} não tem arma para largar`);
return;
}
const weapon = this.weapons.get(weaponId);
if (!weapon) return;
weapon.parentId = null;
// Se o jogador enviou posição final da arma, usa, senão coloca na posição do jogador
if (content && content.weaponData) {
// Atualiza posição mas mantém parentId como null
const originalParentId = weapon.parentId;
weapon.fromJSON(content.weaponData);
weapon.parentId = originalParentId; // Garante que fica null
} else {
// Coloca a arma na posição do jogador
weapon.px = player.posX;
weapon.py = player.posY;
weapon.pz = player.posZ;
}
console.log(`Jogador ${playerId} largou arma ${weapon.id}`);
// Envia confirmação
const socket = this.sockets.get(playerId);
if (socket && socket.readyState === 1) {
socket.send(JSON.stringify({
type: 'drop',
content: {
weaponId: weapon.id
}
}));
}
}
// Método para criar arma customizada
createCustomWeapon(weaponType, position, rotation, content) {
const weaponId = generateId(this.weapons);
const weapon = new Weapon(weaponId, weaponType, content);
if (position) {
weapon.px = position.x || 0;
weapon.py = position.y || 0;
weapon.pz = position.z || 0;
}
if (rotation) {
weapon.rx = rotation.x || 0;
weapon.ry = rotation.y || 0;
weapon.rz = rotation.z || 0;
}
this.weapons.set(weaponId, weapon);
return weapon;
}
// Método para remover arma do mundo
removeWeapon(weaponId) {
const weapon = this.weapons.get(weaponId);
if (!weapon) return false;
// Se a arma está com um jogador, remove do slot dele
if (weapon.parentId) {
const player = this.players.get(weapon.parentId);
if (player) {
for (let i = 0; i < player.slots.length; i++) {
if (player.slots[i] === weaponId) {
player.slots[i] = null;
break;
}
}
}
}
this.weapons.delete(weaponId);
return true;
}
stop() {
if (this.wss) {
this.wss.close();
console.log('Servidor WebSocket parado');
}
}
getStats() {
return {
players: this.players.size,
weapons: this.weapons.size,
uptime: process.uptime(),
port: this.port
};
}
}
module.exports = Server;