This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (44 loc) · 1.41 KB
/
app.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
/* jshint esversion: 6 */
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
const game = require("./game");
var games = [
new game.game(4) //, new game.game(5)
];
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.use(express.static("res"));
io.on("connection", function (socket) {
var i = 0;
for (let game of games) {
i++;
let port = game.useNextThruster();
if (port !== false) {
console.log("someone registered on #" + i + ":" + port);
socket.on("init", fn => { // initialize
fn({
"sw": game.stage["width"],
"sh": game.stage["height"],
"port": port
});
});
socket.on("gmev", msg => game.setState(port, msg)); // game event
socket.on("sreq", fn => fn(game.rocketCoords())); // state request
socket.on("disconnect", msg => {
game.disconnectThruster(port)
console.log("#" + i + ":" + port + " disconnected");
});
break;
}
}
})
http.listen(3000, function () {
console.log("listening on *:3000");
})
function update() {
for (let game of games) game.act();
}
setInterval(update, 20);