-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
255 lines (209 loc) · 6.31 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
var http = require('http');
var path = require('path');
var socketio = require('socket.io');
var express = require('express');
var router = express();
var server = http.createServer(router);
var io = socketio.listen(server);
var util = require('util');
var GhostNight = require('./GN.server/GN');
var GameEventManager = require('./GN.server/GameEventManager');
var Room = require('./Room');
router.use(express.static(path.resolve(__dirname, 'client')));
var sockets = [];
var rooms = {};
var roomof = {};
rooms['lobby'] = new Room('lobby');
io.on('connection', function (socket) {
sockets.push(socket);
socket.join('lobby');
socket.on('click', function(click) {
// click: {x, y, button};
// console.log(click);
});
socket.on('create-room', function(data) {
// Assign a random room number
var roomNo = 'lobby';
while(rooms[roomNo] != undefined) {
roomNo = ('000' + Math.floor(1000 * Math.random())).substr(-3, 3);
}
createRoom(socket, roomNo, data.mode);
});
socket.on('join-room', function(roomNo) {
joinRoom(socket, roomNo);
});
socket.on('disconnect', function () {
sockets.splice(sockets.indexOf(socket), 1);
});
});
function createRoom(socket, roomNo, mode) {
var newRoom = new Room(roomNo);
rooms[roomNo] = newRoom;
newRoom.mode = mode;
console.log("room created: " + roomNo);
joinRoom(socket, roomNo);
}
function joinRoom(socket, roomNo) {
// Find the room and check if it exists
var room = rooms[roomNo];
if(room != undefined) {
if(room.players.length >= 2) {
socket.emit('room-full', {});
return false;
}
// Leave the current room
leaveRoom(socket);
// Join new room on socket
socket.join(roomNo);
// Update the roomNo of socket
roomof[socket.id] = roomNo;
// Default result of join
var playerIndex = room.players.length;
var result = {roomNo: roomNo, playerIndex: playerIndex, map: 'm01', mode: room.mode};
if (playerIndex == 0){
// First player in the room
result.opposite = null;
result.side = 'ghost';
} else if(playerIndex == 1){
// Somebody already in
result.map = room.map;
if(room.players[0].side == 'ghost') {
result.opposite = 'ghost';
result.side = 'human';
} else if(room.players[0].side == 'human'){
result.opposite = 'human';
result.side = 'ghost';
} else {
console.log('Something wrong with players[0] side.');
result.opposite = null;
result.side = 'ghost';
}
// Notice the player in the room
room.broadcast('other-joined-room', result);
}
// Add the player
if(result.side == 'ghost')
room.players.push({socket: socket, side: 'ghost'});
else
room.players.push({socket: socket, side: 'human'});
socket.emit('room-joined', result);
// Register the room options
socket.on('choose-side', function(data){
room.players[playerIndex].side = data.side;
room.broadcast('side-chosen', {playerIndex: playerIndex, side: data.side});
});
socket.on('choose-map', function(data){
room.map = data.map;
room.broadcast('map-chosen', {map: data.map});
});
// Register the start button
socket.on('start-game', function(){
if(room.mode == 'SP') {
// Tell the client to load assets
room.broadcast('load-scene', {map: room.map});
}
if(room.mode == 'MP') {
// Check if there are 2 players
if(room.players.length != 2) {
console.log("Players not enough in room ", roomNo);
return false; }
// Check if there are 2 sides of player
if (room.players[0].side == room.players[1].side) {
console.log("Players in same side in room ", roomNo);
return false; }
// Tell the client to load assets
room.broadcast('load-scene', {map: room.map});
}
});
// Register the loading feedback
socket.on('load-complete', function(){
// var room = rooms[roomof[socket.id]];
room.loadedNo++;
if(room.loadedNo == room.players.length) {
startGame(roomof[socket.id]);
}
});
socket.on('send-message', function(data) {
room.broadcast('message-send', {side: room.players[playerIndex].side, message: data.message});
});
} else {
// Create a new room with specified room number
createRoom(socket, roomNo, 'MP');
}
}
function leaveRoom(socket){
// Check if the socket do not have a room
if (roomof[socket.id] == undefined) {return;}
// Leave the current room
socket.leave(roomof[socket.id]);
// Remove all room option listeners
socket.removeAllListeners('choose-side');
socket.removeAllListeners('choose-map');
socket.removeAllListeners('start-game');
socket.removeAllListeners('load-complete');
var room = rooms[roomof[socket.id]];
// Delete the player in room
var playersNumber = room.players.length;
for (var i = 0; i < playersNumber; i++) {
if (room.players[i].socket.id == socket.id) {
room.players.splice(i, 1);
i--; playersNumber--;
}
}
// Reset the room number stored
roomof[socket.id] = undefined;
}
function startGame(roomNo){
var room = rooms[roomNo];
// Create a game event manager to listen the event inside server
var GEM = new GameEventManager(room);
var settings = {
MinDamage: 5,
Room: roomNo,
debug: false,
soul: 1000,
gold: 2500,
soulIncreasing: {value: 5, interval: 2}
};
switch (room.map) {
case 'm01':
settings.TimeLimit = 4 * 60;
settings.gold = 2000;
break;
case 'm02':
settings.TimeLimit = 5 * 60;
settings.gold = 2500;
break;
case 'm03':
settings.TimeLimit = 6 * 60;
settings.gold = 3000;
break;
default:
settings.TimeLimit = 5 * 60;
settings.gold = 2500;
break;
}
// Create the game server
room.GN = new GhostNight(settings, GEM);
if(room.mode == "SP"){
// Setup a simple AI to play the game
var GNAI = new room.GN.AI(room);
room.players.push({socket: GNAI, side: 'human'});
GNAI.on('send-message', function(data) {
room.broadcast('message-send', {side: "human", message: data.message});
});
}
room.SetupSocket(true);
// Load map
room.GN.Scene.LoadMap(room.map, function(err, data){
if(err) console.log(err);
room.GN.GM.mapLoaded = true;
// Start the game server
room.GN.StartGame();
room.broadcast("game-started", {TimeLimit: settings.TimeLimit});
});
}
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function() {
var addr = server.address();
console.log("Server listening at", addr.address + ":" + addr.port);
});