-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_server5000.js
98 lines (81 loc) · 2.4 KB
/
game_server5000.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
var socketio = require("socket.io");
var io = socketio.listen(5000);
console.log("Game Server Running on Port 5000");
// start copy from here
// Chat Logic
middlewares = [];
players = [];
function sendToMiddlewares(type, message) {
middlewares.forEach((sock) => {
sock.emit(type, message);
});
}
function sendPlayersList() {
sendToMiddlewares("playersList", players);
}
io.on("connection", (sock) => {
console.log("Middleware got connected");
/// Prevention of Duplicate Usernames
sock.on("addPlayerRequest", (msg) => {
var rqId = msg[0];
var name = msg[1];
var name_exist = false;
players.forEach((p) => {
if (p == name) {
name_exist = true;
}
});
if (name_exist == false) {
sock.emit(name + rqId, true);
} else {
sock.emit(name + rqId, false);
}
});
middlewares.push(sock);
sock.on("add", (response) => {
players.push(response);
console.log(players);
sendPlayersList();
});
sock.on("remove", (response) => {
for (var k = 0 ; k < players.length ; k++) {
if (players[k] == response) {
players.splice(k, 1);
break;
}
}
sendPlayersList();
});
sock.on("chatMessage", (response) => {
sendToMiddlewares("chatMessage", response);
});
});
// Game Logic
const path = require("path");
const Datauri = require("datauri");
const datauri = new Datauri();
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
function setupAuthoritativePhaser() {
JSDOM.fromFile(path.join(__dirname, 'authoritative/index.html'), {
// To run the scripts in the html file
runScripts: "dangerously",
// Also load supported external resources
resources: "usable",
// So requestAnimatinFrame events fire
pretendToBeVisual: true
}).then((dom) => {
dom.window.URL.createObjectURL = (blob) => {
if (blob){
return datauri.format(blob.type, blob[Object.getOwnPropertySymbols(blob)[0]]._buffer).content;
}
};
dom.window.URL.revokeObjectURL = (objectURL) => {};
dom.window.gameLoaded = () => {
};
dom.window.io = io;
}).catch((error) => {
console.log(error.message);
});
}
setupAuthoritativePhaser();