This repository was archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·115 lines (100 loc) · 2.78 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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
var player = {
p1: {
turn: true,
ships: []
},
p2: {
turn: false,
ships: []
}
}
var connectedUsers = []; //keep to two
var gameStart = false;
io.on('connection', function(socket){
//stuff about connections
socket.emit('initIdentity', socket.id);
console.log("A user connected");
connectedUsers.push(socket.id);
console.log(connectedUsers);
if(connectedUsers.length == 1){
socket.emit('initIdentity', 'p1')
}
if(connectedUsers.length == 2){
socket.emit("initIdentity", 'p2');
}
if (connectedUsers.length > 2) {
console.log(connectedUsers.length + " users");
io.to(socket.id).emit("full");
}
if (connectedUsers.length < 2) {
io.to(socket.id).emit("waitForJoin");
}
if (connectedUsers.length == 2) {
console.log("game start");
gameStart = true;
//initialize();
io.emit("initGame");
}
socket.on('disconnect', function(){
connectedUsers.splice(connectedUsers.indexOf(socket.id));
console.log("A user disconnected");
console.log(connectedUsers);
io.emit("disconnect");
});
//////////////////////////
socket.on("move", function (clientInfo) {
var movingPlayer = clientInfo[0]; //p1 or p2
var otherPlayer;
if (movingPlayer === "p1") {
otherPlayer = "p2";
}
else {
otherPlayer = "p1";
}
if (player[movingPlayer].turn) {
player = clientInfo[1]; //player object
// console.log(movingPlayer + " " + otherPlayer);
// player[movingPlayer].turn = false;
// console.log("movingPlayer is " + movingPlayer + " and it is now " + player[movingPlayer].turn);
// player[otherPlayer].turn = true;
// console.log("otherPlayer is " + otherPlayer + " and it is now " + player[otherPlayer].turn);
io.to(connectedUsers[(connectedUsers.indexOf(socket.id)+1)%2]).emit("yourTurn",player);
//io.to(connectedUsers[(connectedUsers.indexOf(socket.id)+1)]).emit("endTurn", player);
// console.log(movingPlayer + " " + player[movingPlayer].turn + " " + otherPlayer + " " + player[otherPlayer].turn);
}
else {
console.log(clientInfo[1]);
}
});
});
http.listen(3000, function(){
console.log("listening on port 3000");
});
// function initialize() {
// console.log("initialize()");
// if (connectedUsers.length == 2) {
// io.to(connectedUsers[0]).emit("initIdentity", "p1");
// players.push({
// id: "p1",
// socket: connectedUsers[0],
// turn: true,
// ships: {}
// });
// io.to(connectedUsers[1]).emit("initIdentity", "p2");
// players.push({
// id: "p2",
// socket: connectedUsers[1],
// turn: false,
// ships: {}
// });
// }
// console.log(players);
// }