forked from dpwhittaker/swg-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swgclient.js
197 lines (181 loc) · 6.59 KB
/
swgclient.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
const SOEProtocol = require("./SOEProtocol");
const dgram = require('dgram');
var server = {};
module.exports.params = {};
module.exports.login = function(cfg) {
server = cfg;
server.ChatRoomIDs = [];
Login();
}
module.exports.isConnected = false;
module.exports.paused = false;
module.exports.sendChat = function(message, user, roomID) {
if (!module.exports.isConnected) return;
console.log("sending chat to game channel " + module.exports.params.ChatRooms[roomID] + ": " + user + ": " + message);
send("ChatSendToRoom", {Message: message + " [DIS]", RoomID: server.ChatRoomIDs[roomID]});
}
module.exports.recvChat = function(message, player, roomID) {}
module.exports.serverDown = function() {}
module.exports.serverUp = function() {}
module.exports.reconnected = function() {}
module.exports.sendTell = function(player, message) {
if (!module.exports.isConnected) return;
console.log("sending tell to: " + player + ": " + message);
send("ChatInstantMessageToCharacter", {ServerName: server.ServerName, PlayerName: player, Message: message});
}
module.exports.sendWho = function(roomID) {
if(!module.exports.isConnected) return;
console.log("checking population of channel " + module.exports.params.ChatRooms[roomID]);
send("ChatQueryRoom", {RoomPath: `SWG.${server.ServerName}.${module.exports.params.ChatRooms[roomID]}`});
}
module.exports.recvWho = function(data, roomID) {}
module.exports.recvTell = function(from, message) {}
module.exports.pingAttempts = 0;
var lastMessageTime = new Date();
function handleMessage(msg, info) {
lastMessageTime = new Date();
if (info.port == server.PingPort) return;
var packets;
try {
packets = SOEProtocol.Decode(msg);
} catch (ex) {
console.log(ex.toString());
Login();
return;
}
if (!packets) return;
for (var packet of packets) {
//if (!packet.type.startsWith("1b24f808"))
// console.log("recv: " + packet.type);
if (handlePacket[packet.type])
handlePacket[packet.type](packet);
//else console.log("No handler for " + packet.type);
}
}
var socket;
var loggedIn;
var handlePacket = {};
handlePacket["Ack"] = function(packet) {}
handlePacket["SessionResponse"] = function(packet) {
if (!loggedIn) {
send("LoginClientID", {Username: server.Username, Password:server.Password});
} else {
send("ClientIdMsg");
}
}
handlePacket["LoginClientToken"] = function(packet) {
loggedIn = true;
}
handlePacket["LoginEnumCluster"] = function(packet) {
server.ServerNames = packet.Servers;
}
handlePacket["LoginClusterStatus"] = function(packet) {
// console.log(packet);
server.Servers = packet.Servers;
}
handlePacket["EnumerateCharacterId"] = function(packet) {
var character = packet.Characters[module.exports.params.Character];
if (!character)
for (var c in packet.Characters)
if (packet.Characters[c].Name.startsWith(module.exports.params.Character))
character = packet.Characters[c];
var serverData = server.Servers[character.ServerID];
server.Address = serverData.IPAddress;
server.Port = serverData.Port;
server.PingPort = serverData.PingPort;
server.CharacterID = character.CharacterID;
server.ServerName = server.ServerNames[character.ServerID].Name;
send("SessionRequest");
}
handlePacket["ClientPermissions"] = function(packet) {
send("SelectCharacter", {CharacterID: server.CharacterID});
for(var room in module.exports.params.ChatRooms) {
setTimeout(() => {
send("ChatCreateRoom", {RoomPath: `SWG.${server.ServerName}.${room}`})
setTimeout(() => send("CmdSceneReady"), 1000);
}, 1000);
}
}
handlePacket["ChatRoomList"] = function(packet) {
// console.log(JSON.stringify(packet, null, 2));
for (var roomID in packet.Rooms) {
var room = packet.Rooms[roomID];
var roomName = room.RoomPath.replace("SWG." + server.ServerName + ".", "");
if (module.exports.params.ChatRooms.includes(roomName)) {
server.ChatRoomIDs.push(room.RoomID);
send("ChatEnterRoomById", {RoomID: room.RoomID});
}
}
}
handlePacket["ChatQueryRoomResults"] = function(packet) {
console.log("received query data...");
module.exports.recvWho(packet, server.ChatRoomIDs.indexOf(packet.RoomID));
}
handlePacket["ChatOnEnteredRoom"] = function(packet) {
// console.log(JSON.stringify(packet, null, 2));
if (server.ChatRoomIDs.includes(packet.RoomID) && packet.PlayerName == module.exports.params.Character) {
if (!module.exports.isConnected) {
module.exports.isConnected = true;
console.log("connected");
module.exports.reconnected();
}
if (fails >= 3) module.exports.serverUp();
fails = 0;
}
}
handlePacket["ChatRoomMessage"] = function(packet) {
// console.log(JSON.stringify(packet, null, 2));
if (server.ChatRoomIDs.includes(packet.RoomID) && packet.CharacterName != module.exports.params.Character.toLowerCase())
module.exports.recvChat(packet.Message, packet.CharacterName, server.ChatRoomIDs.indexOf(packet.RoomID));
}
handlePacket["ChatInstantMessageToClient"] = function(packet) {
module.exports.recvTell(packet.PlayerName, packet.Message);
}
function Login() {
loggedIn = false;
module.exports.isConnected = false;
server.Address = server.LoginAddress;
server.Port = server.LoginPort;
server.PingPort = undefined;
socket = dgram.createSocket('udp4');
socket.on('message', handleMessage);
send("SessionRequest");
}
function send(type, data) {
var buf = SOEProtocol.Encode(type, data);
if (buf) {
// console.log("send: " + type);
if (Array.isArray(buf)) {
for (var b of buf) {
socket.send(b, server.Port, server.Address);
}
}
else
socket.send(buf, server.Port, server.Address);
}
}
var fails = 0;
setInterval(() => {
if (module.exports.paused) return;
send("Ack");
if(module.exports.pingAttempts > 2) {
fails++;
module.exports.isConnected = false;
if(fails == 3) module.exports.serverDown();
lastMessageTime = new Date();
Login();
}
if (new Date() - lastMessageTime > 10000) {
fails++;
module.exports.isConnected = false;
if (fails == 3) module.exports.serverDown();
lastMessageTime = new Date();
Login();
}
}, 100);
setInterval(() => {
if (!server.PingPort || !module.exports.isConnected) return;
var buf = new Buffer(4);
buf.writeUInt32LE((new Date().getTime() & 0xFFFFFFFF) >>> 0);
socket.send(buf, server.PingPort, server.Address);
}, 1000);