-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
61 lines (51 loc) · 1.46 KB
/
game.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
var io;
var gameSocket;
exports.initGame = function(sio, socket){
io = sio;
gameSocket = socket;
// Host Events
gameSocket.on('pacmanCreateNewGame', pacmanCreateNewGame);
gameSocket.on('pacmanMoved', pacmanMoved);
// Ghost Events
gameSocket.on('ghostJoinedGame', ghostJoinedGame);
gameSocket.on('ghostMoved', ghostMoved);
gameSocket.on('gameOver', gameOver);
}
// Host events functions
/*
The hostCreateNewGame creates a new instance of the game
*/
function pacmanCreateNewGame(){
var thisGameId = ( Math.random() * 100000 ) | 0;
this.gameId = thisGameId;
this.emit('newGameCreated', {gameId: thisGameId, socketId: this.id});
this.join(thisGameId.toString());
console.log("Game created with id:" + thisGameId);
}
function pacmanMoved(data){
this.broadcast.to(this.gameId).emit('updatePacmanMove', data)
}
// Ghost events function
function ghostJoinedGame(data){
// Look up the room ID in the Socket.IO manager object.
var room = gameSocket.adapter.rooms[data.gameId];
console.log("Joined room:" + room)
if(room != undefined){
noUsers = Object.keys(room).length
if(noUsers == 2){
this.emit('notifyRoomFull', {})
}
else{
io.sockets.in(data.gameId).emit('ghostJoinedRoom', data)
this.join(data.gameId)
this.gameId = data.gameId;
this.emit('initGhostScreen', data)
}
}
}
function ghostMoved(data){
this.broadcast.to(this.gameId).emit('updateGhostMove', data)
}
function gameOver(data){
io.sockets.in(this.gameId).emit('showGameOver', data)
}