-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (51 loc) · 1.97 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
var port = process.env.PORT || 1337;
var express = require('express'),
app = express(),
path = require('path'),
server = app.listen(port),
//http = require('http').Server(app),
io = require('socket.io')(server);
//feed = require('./feed');
var Redis = require('ioredis');
var redis = new Redis();
var gamesAwaiting=[{gameName:"Test Game"}];
var gamesInProgress = [];
app.use(express.static(path.join(__dirname, './dist')));
io.on('connection', function (socket) {
console.log('User connected. Socket id %s', socket.id);
socket.on('joinGame', function (settings,gameName) {
var gameIndex = gamesAwaiting.findIndex(g=>g.gameName==gameName);
if (gameIndex == -1){
socket.emit("gameJoinError","Game No Longer Awaiting");
return;
}
gamesAwaiting.splice(gameIndex,1);
var gameInfo = {gameName:gameName,players:{player1:gameName,player2:settings.playerName}};
gamesInProgress.push(gameInfo);
socket.emit("gameJoined",{gameInfo:gameInfo,gamesAwaiting:gamesAwaiting});
socket.broadcast.emit("gameJoined",{gameInfo:gameInfo,gamesAwaiting:gamesAwaiting});
//socket.broadcast.emit("awaitingGames",gamesAwaiting);
});
socket.on("getAwaitingGames",function(settings){
socket.emit("awaitingGames",gamesAwaiting);
});
socket.on('startGame',function(settings){
gamesAwaiting.push({gameName:settings.playerName});
socket.broadcast.emit("newGame",settings.playerName);
});
socket.on('leaveGame', function () {
});
socket.on("playerMoved", function(action){
var response = {currentPlayer:action.currentPlayer,
flipLocations:action.flipLocations,
lastPosition:action.lastPosition,
gameName:action.gameName
};
console.log("player moving",response);
socket.broadcast.emit("playerMoved",response);
});
socket.on('disconnect', function () {
console.log('User disconnected. Socket id %s', socket.id);
});
});
//http.listen(port);