forked from pmav/game-of-life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (53 loc) · 1.41 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
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var _ = require("underscore");
var uuid = require("uuid/v4");
var Log = require("log");
var log = new Log("debug");
clients = {};
games = {};
map = {
bases: [{ x: 52, y: 368 }, { x: 1220, y: 368 }],
columns: 180,
rows: 86
};
process.on("SIGINFO", function() {
clients = {};
});
io.on("connection", function(socket) {
//Create new player
console.log("Hello");
socket.on("ready", function(player) {
console.log("Ready with ", clients);
clients[player.peerID] = player;
clients[player.peerID].base = map.bases[clients.length - 1];
if (_.size(clients) === 2) {
console.log("Starting Game with ", clients);
io.emit("game", {
caller: player.peerID, //Most recent connection initiates with least
clients: clients,
map: map,
generation: 0
});
}
});
socket.on("disconnect", function() {
console.log("Client Disconnected");
});
socket.on("restart", function() {
clients = {};
});
});
app.use(
"/socket.io",
express.static(__dirname + "/node_modules/socket.io-client/dist")
);
app.use(express.static(__dirname));
app.set("port", process.env.PORT || 5000);
app.get("/", function(req, res) {
// console.log("Got Root");
res.sendFile(__dirname + "/index.html");
});
http.listen(process.env.PORT || 3000);