forked from BretGG/Team06COMP2930
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
185 lines (151 loc) · 4.98 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require("./app");
var debug = require("debug")("comp2930-team2:server");
var http = require("http");
var players = {};
//EcoQuest code
const numberOfPlayers = 4;
const Game1_XYCoordinates=[{x:110,y:225, isTaken:false},{x:310,y:225,isTaken:false},
{x:510,y:225,isTaken:false},{x:710,y:225,isTaken:false}];
//TODO: //////////////////////////////////
//1. Implement limited number of max players ..
//How to get the variable e.g. numberOfPlayers from game.js?
//emit from game.js doesn't work...=> It can't come from the client side. use namespace instead.
//2. Give the players different colors..
//3. Is there way to get the initial spawn location coordinates from game.js?
//4. Use namespace(multiple channel feature of socket.io) to implement different game
/////////////////////////////////////////
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
/**
* Create HTTP server.
*/
var server = http.Server(app);
var io = require('socket.io').listen(server);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== "listen") {
throw error;
}
var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}
const self = this;
//on the new user connection do the following
io.on('connection',function(socket){
//number of current sockets
const numberOfCurrentPlayers = Object.keys(io.sockets.sockets).length;
console.log('a user connected: '+ socket.id );
let x,y;
console.log("number of USER LISTS ", numberOfCurrentPlayers);
if(numberOfCurrentPlayers<=numberOfPlayers){
for(let i=0; i<Game1_XYCoordinates.length; i++){
if(!Game1_XYCoordinates[i].isTaken){
self.playerNo = i;
console.log("room: " + this);
players[socket.id] = {
playerNo: self.playerNo,
playerId: socket.id,
x: Game1_XYCoordinates[i].x,
y: Game1_XYCoordinates[i].y
};
Game1_XYCoordinates[i].isTaken=true;
console.log("new player added");
printPlayers(Game1_XYCoordinates);
break;
}
}
}else{
console.log("The room is full");
disconnectPlayer(socket.id);
console.log("deleted attempted connect, current players: ", numberOfCurrentPlayers);
}
// send the players object to the new player
socket.emit('currentPlayers', players);
// update all opponent players of the new player
socket.broadcast.emit('newPlayer', players[socket.id]);
//user disconnect
// when a player disconnects, remove them from our players object
socket.on('disconnect', function () {
disconnectPlayer(socket.id);
});
// when a player moves, update the player data
socket.on('playerMovement', function(movementData){
if(players[socket.id]!=undefined){
players[socket.id].x = movementData.x;
players[socket.id].y = movementData.y;
}
// emit a message to all players about the player that moved
socket.broadcast.emit('playerMoved', players[socket.id]);
});
//Kicks the user out when he's not doing anything for 1 min.
setTimeout(() => disconnectPlayer(socket.id), 60000);
////
});//io.on ends here
function printPlayers(coordinates){
for(let i=0;i<coordinates.length;i++){
console.log(coordinates[i].isTaken);
}
}
function disconnectPlayer(id){
console.log('user ',id,' attempts to disconnect..');
if(players[id]!=undefined){
// console.log("disc: ", players[id].playerNo);
Game1_XYCoordinates[players[id].playerNo].isTaken = false;
delete players[id];
io.emit('disconnect', id);
console.log("after disconnect");
printPlayers(Game1_XYCoordinates);
}else{
console.log('User not exist, deleted already or never created');
}
}