-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpcodeHandler.ts
More file actions
91 lines (79 loc) · 3.31 KB
/
Copy pathOpcodeHandler.ts
File metadata and controls
91 lines (79 loc) · 3.31 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
import { Socket } from "socket.io";
import { world } from "./WorldManager";
import { World } from "./World";
import { Spell } from "./Spell";
export interface Packet {
opcode: string;
data: any;
}
export class OpcodeHandler {
private handlers: Record<string, (socket: Socket, world: World, data: any) => void> = {};
constructor(){
this.registerOpcodes();
}
private registerOpcodes() {
// CMSG_CAST_SPELL -> HandleSpellCast
this.handlers["CMSG_CAST_SPELL"] = (socket, world, data) => this.handleSpellCast(socket, world, data);
this.handlers["CMSG_AUTH"] = (socket, world, data) => this.handleAuth(socket, world, data);
this.handlers["CMSG_SELECTED_TARGET"] = (socket, world, data) => this.handleSelectTarget(socket, world, data);
}
public route(socket: Socket, world: World, packet: Packet) {
const handler = this.handlers[packet.opcode];
if (handler) {
handler(socket, world, packet.data);
} else {
console.warn(`No handler for opcode: ${packet.opcode}`);
}
}
private handleSelectTarget(socket: Socket, world: World, data: any) {
console.log(`[OPCODE] Handling target selection from socket ${socket.id} with data:`, data);
const target = world.units.get(data.targetId);
if (!target) {
console.warn(`[TARGET] Target with GUID ${data.targetId} not found.`);
return;
}
const player = world.getPlayerBySocket(socket);
if (!player) {
console.warn(`[TARGET] Player not found for socket ${socket.id}`);
return;
}
player.SetTargetByGuid(data.targetId);
// For simplicity, we just log the target selection. In a real implementation, you'd set the player's target.
console.log(`[TARGET] Player ${player.guid} selected target ${target.guid}`);
}
private handleAuth(socket: Socket, world: World, data: any) {
console.log(`[OPCODE] Handling auth from socket ${socket.id} with data:`, data);
const { username, password } = data;
if (password == "password123") {
console.log(`[AUTH] Authentication successful for username: ${username}`);
world.addPlayer(socket);
socket.emit('SMSG_PACKET', {
opcode: 'SMSG_AUTH_RESPONSE',
data: {
success: true,
message: `Welcome to Aether, ${data.username}!`
}
});
} else {
console.warn(`[AUTH] Authentication failed for username: ${username}`);
socket.emit('SMSG_PACKET', {
opcode: 'SMSG_AUTH_RESPONSE',
data: {
success: false,
message: `Authentication failed for ${data.username}.`
}
});
}
}
private handleSpellCast(socket: Socket, world: World, data: any) {
const player = world.getPlayerBySocket(socket);
if (!player) {
console.warn("Player not found for socket");
return;
}
console.log(`[OPCODE] Handling spell cast from player ${player.socket.id} with data:`, data);
const spell = new Spell(data.spellId, player);
player.AddSpellToList(spell);
spell.prepare();
}
}