-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
232 lines (188 loc) · 6.05 KB
/
server.js
File metadata and controls
232 lines (188 loc) · 6.05 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env node
var playerLimit = 4;
var currentNoOfPlayers = 0;
/**
* Module dependencies.
*/
var app = require("../app");
var debug = require("debug")("comp2930-team2:server");
var http = require("http");
var players = {};
///Code below will be put into different channels in the future to implement another game
var count = 0;
// var increaseX = 200;
// var times = 0;
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);
}
//on the new user connection do the following
/// /////////
/// A1
/// server is up and running
/// server is made here.
/// //////
/// Connected A901
io.on('connection', function(socket) {
console.log('a user connected: ' + socket.id);
let x, y, playerNo;
// console.log(Object.keys(io.sockets.sockets));
//Make new player by new connection on random x,y coordinates.
// if(count+1 <= numberOfPlayers){
if (currentNoOfPlayers < playerLimit){
for (let i = 0; i < playerLimit; i++){
if (!Game1_XYCoordinates[i].isTaken){
Game1_XYCoordinates[i].isTaken = true;
playerNo = i;
///A3 creating player object.
/// playerNo = count
///socket.id unique id given to the new player.
///we have to pass into the game
/// if the room is full it wont add.
players[socket.id] = {
playerNo: i,
playerId: socket.id,
// player spawn location x,y
// x: 110 + (increaseX * (times++)),
x: Game1_XYCoordinates[i].x,
y: Game1_XYCoordinates[i].y
};
console.log("new player added");
currentNoOfPlayers++;
break;
}
}
}
else {
console.log("room is full");
}
// send the players object to the new player
///A8 when the new player connects to the game, it sees whose already ther,
///this line sending the current player to the game. players in an array. pass in array of players.
socket.emit('currentPlayers', players);
// update all other players of the new player
///passing a new player object to the client by emiting.
///A5 sending it to the game file.
socket.broadcast.emit('newPlayer', players[socket.id]);
//user disconnect
// when a player disconnects, remove them from our players object
///A10 disconnect. disconnect is a built in message which is connect and disconnect
///as well as creating your own name tag.
///using your own disconnect name tag, get rid of the players so its disconnected
///
socket.on('disconnect', function() {
console.log('count : ' + playerNo + ' / user disconnected: ', socket.id);
if (Game1_XYCoordinates[playerNo]!=undefined) {
Game1_XYCoordinates[playerNo].isTaken = false;
delete players[socket.id];
currentNoOfPlayers--;
io.emit('disconnect', socket.id);
} else {
console.log("no disconnection");
}
// emit a message to all players to remove this player
});
// when a player moves, update the player data
///A11
///A13 recieves it,
///then sends it to the other players in socket.broadcast.emit*'playermoved'
socket.on('playerMovement', function(movementData) {
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]);
});
});
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function printPlayers(coordinates) {
for (let i = 0; i < coordinates.length; i++) {
console.log(coordinates[i].isTaken);
}
}