-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-logic.js
134 lines (95 loc) · 3.48 KB
/
game-logic.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
/**
* Here is where we should register event listeners and emitters.
*/
var io
var gameSocket
// gamesInSession stores an array of all active socket connections
var gamesInSession = []
const initializeGame = (sio, socket) => {
/**
* initializeGame sets up all the socket event listeners.
*/
// initialize global variables.
io = sio
gameSocket = socket
// pushes this socket to an array which stores all the active sockets.
gamesInSession.push(gameSocket)
// Run code when the client disconnects from their socket session.
gameSocket.on("disconnect", onDisconnect)
// Sends new move to the other socket session in the same room.
gameSocket.on("new move", newMove)
// User creates new game room after clicking 'submit' on the frontend
gameSocket.on("createNewGame", createNewGame)
// User joins gameRoom after going to a URL with '/game/:gameId'
gameSocket.on("playerJoinGame", playerJoinsGame)
gameSocket.on('request username', requestUserName)
gameSocket.on('recieved userName', recievedUserName)
// register event listeners for video chat app:
videoChatBackend()
}
function videoChatBackend() {
// main function listeners
gameSocket.on("callUser", (data) => {
io.to(data.userToCall).emit('hey', {signal: data.signalData, from: data.from});
})
gameSocket.on("acceptCall", (data) => {
io.to(data.to).emit('callAccepted', data.signal);
})
}
function playerJoinsGame(idData) {
/**
* Joins the given socket to a session with it's gameId
*/
// A reference to the player's Socket.IO socket object
var sock = this
// Look up the room ID in the Socket.IO manager object.
var room = io.sockets.adapter.rooms[idData.gameId]
// console.log(room)
// If the room exists...
if (room === undefined) {
this.emit('status' , "This game session does not exist." );
return
}
if (room.length < 2) {
// attach the socket id to the data object.
idData.mySocketId = sock.id;
// Join the room
sock.join(idData.gameId);
console.log(room.length)
if (room.length === 2) {
io.sockets.in(idData.gameId).emit('start game', idData.userName)
}
// Emit an event notifying the clients that the player has joined the room.
io.sockets.in(idData.gameId).emit('playerJoinedRoom', idData);
} else {
// Otherwise, send an error message back to the player.
this.emit('status' , "There are already 2 people playing in this room." );
}
}
function createNewGame(gameId) {
// Return the Room ID (gameId) and the socket ID (mySocketId) to the browser client
this.emit('createNewGame', {gameId: gameId, mySocketId: this.id});
// Join the Room and wait for the other player
this.join(gameId)
}
function newMove(move) {
/**
* First, we need to get the room ID in which to send this message.
* Next, we actually send this message to everyone except the sender
* in this room.
*/
const gameId = move.gameId
io.to(gameId).emit('opponent move', move);
}
function onDisconnect() {
var i = gamesInSession.indexOf(gameSocket);
gamesInSession.splice(i, 1);
}
function requestUserName(gameId) {
io.to(gameId).emit('give userName', this.id);
}
function recievedUserName(data) {
data.socketId = this.id
io.to(data.gameId).emit('get Opponent UserName', data);
}
exports.initializeGame = initializeGame