-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
133 lines (109 loc) · 3.51 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
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
let io;
let gameSocket;
// Called by indexjs to initalize game
exports.initGame = (sio, socket) => {
io = sio;
gameSocket = socket;
gameSocket.emit("connected", { message: "User connected!" });
// Host events
gameSocket.on("hostCreateNewGame", hostCreateNewGame);
gameSocket.on("hostRoomFull", hostStartGame);
// Player events
gameSocket.on("playerJoinGame", playerJoinGame);
gameSocket.on("getNewQuestion", sendQuestion);
gameSocket.on("playerAnswer", playerAnswer);
};
function sendQuestion(dataid) {
// random question and handling done here
// probably pick question and then add it to an array of used questions or something
let data = getNewQuestion();
// io.sockets.in(gameId).emit("newQuestion", data);
console.log(data.question);
console.log(dataid);
if (dataid.round === 0) {
io.sockets.in(dataid.gameId).emit("newQuestion", data);
}
else if (data.question) {
//io.sockets.in(dataid.gameId).emit("newQuestion", data);
io.to(dataid.socketId).emit("newQuestion", data);
}
else {
io.sockets.in(dataid.gameId).emit("gameOver", data);
//io.to(dataid.socketId).emit("newQuestion", data);
}
}
function getNewQuestion() {
let questions = [
{
question: "What is 2+2?",
answer: "4",
solution: "2+2 = 4. Refer to textbook chapter 1."
},
{
question: "What is 7*8?",
answer: "56",
solution: "7*8 = 56. Refer to textbook chapter 2."
},
{
question: "What is 10^2?",
answer: "100",
solution: "10^2 = 10*10 = 100. Refer to textbook chapter 2.2."
}
];
let max = questions.length;
let index = Math.floor(Math.random() * max);
let data = questions[index];
//questions.splice(index, 1);
console.log(questions);
return data;
}
function playerAnswer(data) {
// console.log('Player ID: ' + data.playerId + ' answered a question with: ' + data.answer);
io.sockets.emit("hostCheckAnswer", data);
}
function hostStartGame(data) {
console.log("game started");
data.round = 0;
//console.log(data);
sendQuestion(data);
var counter = 60;
var countdown = setInterval(function(){
io.sockets.emit('count', counter);
counter--;
console.log(counter);
if (counter === 0) {
io.sockets.emit('gameOver', "Game Over!!");
clearInterval(countdown);
}
}, 1000);
}
function hostCreateNewGame() {
// Create a unique Socket.IO Room
let thisGameId = (Math.random() * 100000) | 0;
// Return the Room ID (gameId) and the socket ID (mySocketId) to the browser client
this.emit("newGameCreated", { gameId: thisGameId, mySocketId: this.id });
// Join the Room and wait for the players
this.join(thisGameId.toString());
}
function playerJoinGame(data) {
console.log("Player " + data.playerName + " attempting to join game: " + data.gameId);
// A reference to the player's Socket.IO socket object
let sock = this;
// Look up the room ID in the Socket.IO manager object.
//console.log(gameSocket.rooms);
let room = gameSocket.rooms;
// If the room exists...
if (room != undefined) {
// attach the socket id to the data object.
data.mySocketId = sock.id;
// Join the room
sock.join(data.gameId);
console.log("Player " + data.playerName + " joining game: " + data.gameId);
// Emit an event notifying the clients that the player has joined the room.
io.sockets.in(data.gameId).emit("playerJoinedRoom", data);
}
else {
// Otherwise, send an error message back to the player.
this.emit("error", { message: "This room does not exist." });
}
}