-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
161 lines (135 loc) · 4.51 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
159
160
161
var config = require('./config.json');
var WebSocket = require('ws');
require('./fix');
var Istrolid = require('./istrolid.js');
const allowedCmds = ["playerJoin", "mouseMove", "playerSelected", "setRallyPoint", "buildRq", "stopOrder", "holdPositionOrder", "followOrder", "selfDestructOrder", "moveOrder", "configGame", "startGame", "addAi", "switchSide", "kickPlayer", "surrender"]
global.sim = new Sim();
Sim.prototype.cheatSimInterval = -12;
Sim.prototype.lastSimInterval = 0;
global.Server = function() {
var wss = new WebSocket.Server({port: process.env.PORT || config.port});
var root = null;
var players = {};
var lastInfoTime = 0;
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");
sendInfo();
lastInfoTime = now();
});
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));
}
}
};
var sendInfo = () => {
// Send server info
let info = {
name: config.name,
address: "ws://" + config.addr + ":" + config.port,
observers: sim.players.filter(p => p.connected && !p.ai).length,
players: sim.players.filter(p => p.connected && !p.ai).map(p => { return {
name: p.name,
side: p.side,
ai: false
}}),
type: sim.serverType,
version: VERSION,
state: sim.state
};
root.sendData(['setServer', info]);
};
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(data[0] === 'configGame') {
let gameType = data[1].type;
sim.configGame(players[id], gameType);
} else if(allowedCmds.includes(data[0])) {
sim[data[0]].apply(sim, [players[id],...data.slice(1)]);
}
});
ws.on('close', e => {
if(players[id]) {
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);
}
});
}
if(rightNow - lastInfoTime > 15000) {
sendInfo();
lastInfoTime = rightNow;
}
}, 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());
socket.on('error', () => {});
}).listen(5001, "localhost");