forked from Rio6/IstrolidServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
158 lines (131 loc) · 4.44 KB
/
server.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
var config = require('./config.json');
var WebSocket = require('ws');
require('./fix');
var Istrolid = require('./istrolid.js');
const CMD_WHITELIST = ["alpha", "mouseMove", "playerSelected", "setRallyPoint", "buildRq", "stopOrder", "holdPositionOrder", "followOrder", "selfDestructOrder", "moveOrder", "buildRq", "configGame", "startGame", "startGame", "addAi", "switchSide", "switchSide", "kickPlayer", "surrender"];
global.sim = new Sim();
sim.cheatSimInterval = -12;
sim.lastSimInterval = 0;
global.Server = function() {
var wss = new WebSocket.Server({port: config.port});
var root = null;
var players = {};
var info = {};
this.send = (player, data) => {
let packet = sim.zJson.dumpDv(data);
let client = player.ws;
if(client && client.readyState === WebSocket.OPEN) {
client.send(packet);
}
};
this.sendToRoot = (data) => {
root.sendData(data);
};
this.stop = () => {
console.log("stopping server");
wss.close();
clearInterval(interval);
};
this.say = msg => {
root.sendData(['message', {
text: msg,
channel: config.name,
color: "FFFFFF",
name: "Server",
server: true
}]);
};
var connectToRoot = () => {
root = new WebSocket(config.root_addr);
root.on('open', () => {
console.log("connected to root proxy");
info = {};
});
root.on('close', () => {
console.log("cannot connect to root, retrying");
setTimeout(connectToRoot, 5000);
});
root.on('error', e => {
console.log("connection to root failed");
});
root.sendData = data => {
if(root.readyState === WebSocket.OPEN) {
root.send(JSON.stringify(data));
}
}
};
connectToRoot();
wss.on('connection', (ws, req) => {
console.log("connection from", req.connection.remoteAddress);
let id = req.headers['sec-websocket-key'];
ws.on('message', msg => {
let packet = new DataView(new Uint8Array(msg).buffer);
let data = sim.zJson.loadDv(packet);
//console.log(data);
if(data[0] === 'playerJoin') {
let player = sim.playerJoin(...data);
player.ws = ws;
players[id] = player;
sim.clearNetState();
} else if(CMD_WHITELIST.includes(data[0])) {
sim[data[0]].apply(sim, [players[id],...data.slice(1)]);
}
});
ws.on('close', e => {
players[id].connected = false;
delete players[id];
});
});
var interval = setInterval(() => {
let rightNow = now();
if(sim.lastSimInterval + 1000 / 16 + sim.cheatSimInterval <= rightNow) {
sim.lastSimInterval = rightNow;
if(!sim.paused) {
sim.simulate();
} else {
sim.startingSim();
}
let packet = sim.send();
wss.clients.forEach(client => {
if(client.readyState === WebSocket.OPEN) {
client.send(packet);
}
});
// Send server info
let newInfo = {
name: config.name,
address: "ws://" + config.addr + ":" + config.port,
observers: sim.players.filter(p => p.connected).length,
players: sim.players.filter(p => p.connected).map(p => { return {
name: p.name,
side: p.side,
ai: p.ai
}}),
type: sim.serverType,
version: VERSION,
state: sim.state
};
let diffInfo = {};
for(let k in newInfo) {
if(!simpleEquals(info[k], newInfo[k])) {
diffInfo[k] = newInfo[k];
}
}
if(Object.keys(diffInfo).length > 0) {
root.sendData(['info', config.name, diffInfo]);
info = newInfo;
}
}
}, 17);
};
global.server = new Server();
// Remote repl
var repl = require('repl');
var net = require('net');
net.createServer(function (socket) {
repl.start({
input: socket,
output: socket,
terminal: true
}).on('exit', () => socket.end());
}).listen(5001, "localhost");