forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
142 lines (140 loc) · 4.75 KB
/
server.js
File metadata and controls
142 lines (140 loc) · 4.75 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
// Generated by CoffeeScript 2.5.1
(function() {
module.exports = function(type) {
var Chunk, Convert, app, config, convert, express, fs, http, io, mineflayer, opn, request, server, sf, socketInfo, vec3;
//biblioteki
opn = require("opn");
fs = require("fs");
config = JSON.parse(fs.readFileSync(__dirname + "/../config.json"));
http = require("http");
express = require('express');
app = express();
server = http.createServer(app);
io = require("socket.io")(server);
request = require('request');
mineflayer = require('mineflayer');
Chunk = require("prismarine-chunk")(config.realServer.version);
vec3 = require("vec3");
Convert = require('ansi-to-html');
convert = new Convert();
//początkowe zmienne
sf = {};
socketInfo = {};
//Konfiguracja serwera express
if (type === "production") {
app.use(express.static(__dirname + "/dist/"));
} else {
app.use(express.static(__dirname + "/client/"));
}
app.use(function(req, res, next) {
res.set('Cache-Control', 'no-store');
return next();
});
server.listen(config["port"], function() {
return console.log(`Server is running on \x1b[34mhttp://localhost:${config["port"]}\x1b[0m`);
});
// opn("http://#{config.host}:#{config['express-port']}")
//websocket
return io.sockets.on("connection", function(socket) {
var bot;
socketInfo[socket.id] = {};
bot = socketInfo[socket.id];
return socket.on("initClient", function(data) {
var botEventMap, emit, i, inv, results, socketEventMap;
console.log("[\x1b[32m+\x1b[0m] " + data.nick);
//Dodawanie informacji o graczu do socketInfo
socketInfo[socket.id] = data;
socketInfo[socket.id].bot = mineflayer.createBot({
host: config.realServer.ip,
port: config.realServer.port,
username: socketInfo[socket.id].nick,
version: config.realServer.version
});
bot = function() {
if (socketInfo[socket.id] !== void 0) {
return socketInfo[socket.id].bot;
} else {
return null;
}
};
emit = function(array) {
return io.to(socket.id).emit(...array);
};
//Eventy otrzymywane z serwera minecraftowego
bot()._client.on("map_chunk", function(packet) {
var cell;
cell = new Chunk();
cell.load(packet.chunkData, packet.bitMap, false, true);
emit(["mapChunk", cell.sections, packet.x, packet.z, packet.biomes]);
});
botEventMap = {
"move": function() {
emit(["move", bot().entity.position]);
},
"health": function() {
emit(["hp", bot().health]);
emit(["food", bot().food]);
},
"spawn": function() {
emit(["spawn", bot().entity.yaw, bot().entity.pitch]);
},
"kicked": function(reason, loggedIn) {
emit(["kicked", reason]);
},
"message": function(msg) {
emit(["msg", convert.toHtml(msg.toAnsi())]);
},
"experience": function() {
emit(["xp", bot().experience]);
},
"blockUpdate": function(oldb, newb) {
emit(["blockUpdate", [newb.position.x, newb.position.y, newb.position.z, newb.stateId]]);
}
};
for (i in botEventMap) {
(function(i) {
return socketInfo[socket.id].bot.on(i, function() {
if (bot() !== null) {
botEventMap[i](...arguments);
}
});
})(i);
}
inv = "";
socketInfo[socket.id].int = setInterval(function() {
var inv_new;
inv_new = JSON.stringify(bot().inventory.slots);
if (inv !== inv_new) {
inv = inv_new;
emit(["inventory", bot().inventory.slots]);
}
emit(["entities", bot().entities]);
}, 10);
socketEventMap = {
"move": function(state, toggle) {
bot().setControlState(state, toggle);
},
"command": function(com) {
bot().chat(com);
},
"rotate": function(data) {
bot().look(...data);
},
"disconnect": function() {
try {
clearInterval(socketInfo[socket.id].int);
console.log("[\x1b[31m-\x1b[0m] " + socketInfo[socket.id].nick);
socketInfo[socket.id].bot.end();
delete socketInfo[socket.id];
} catch (error) {}
}
};
results = [];
for (i in socketEventMap) {
results.push(socket.on(i, socketEventMap[i]));
}
return results;
});
});
};
}).call(this);