-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (112 loc) · 3.43 KB
/
index.js
File metadata and controls
122 lines (112 loc) · 3.43 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
const net = require('net');
const bson = require('bson');
const fs = require('fs');
var packetQueue = [];
function BSONDecode(buffer) {
return bson.deserialize(buffer.slice(4));
}
function BSONEncode(json) {
const data = bson.serialize(json);
var buf = Buffer.alloc(4 + data.byteLength);
buf.writeInt32LE(4 + data.byteLength);
data.copy(buf, 4);
return buf;
}
function SendPacket(data) {
packetQueue.push(data);
}
function SendAll(socket) {
var json = {};
packetQueue.forEach((item, index) => {
json["m" + String(index)] = item;
});
json["mc"] = packetQueue.length;
socket.write(BSONEncode(json));
if (packetQueue[0].ID != "p") {
console.log(json);
}
packetQueue = [];
}
function HandlePlayerLogon(socket) {
let playerData = fs.readFileSync("player.dat"); // https://github.com/playingoDEERUX/PixelWorldsServer2 (player.dat)
playerData = playerData.slice(4);
var resp = bson.deserialize(playerData);
console.log(resp);
socket.write(BSONEncode(resp));
}
function PacketProcessing(data, socket) {
if (data == null || !data.hasOwnProperty("mc")) return data;
var msgCount = data["mc"];
if (msgCount == 0) {
SendPacket({
ID: "p"
});
}
if (!msgCount == 0) console.log(data);
for(let i = 0; i < msgCount; i++) {
var current = data["m" + String(i)];
var messageId = current["ID"];
switch (messageId) {
case "VChk":
SendPacket({
ID: "VChk",
VN: 94
});
break;
case "GPd":
HandlePlayerLogon(socket);
break;
case "TTjW":
break;
case "RenamePlayer":
let username = current["UN"];
console.log("Renaming player to " + username);
SendPacket({
ID: "RenamePlayer",
S: true,
UN: username
});
break;
case "ST":
SendPacket({
ID: "ST",
STime: ((new Date()).getTime() * 10000) + 621355968000000000,
SSlp: 30
});
break;
case "MWli":
if (!fs.existsSync(`./world/${current["WN"]}.json`)) {
SendPacket({
ID: "MWli",
WN: current["WN"],
Ct: -3
});
}
else {
SendPacket({
ID: "MWli",
WN: current["WN"],
Ct: Math.floor(Math.random() * 50)
});
}
break;
default:
console.log("Unknown message ID: " + messageId);
SendPacket({
ID: "p"
});
break;
}
}
}
const server = net.createServer((socket) => {
socket.on('data', (data) => {
var packet = BSONDecode(data);
PacketProcessing(packet, socket);
if (packetQueue.length > 0) {
SendAll(socket);
}
});
}).listen(10001, () => {
console.log("Server started");
});