-
Notifications
You must be signed in to change notification settings - Fork 16
/
GameSession.js
185 lines (149 loc) · 5.06 KB
/
GameSession.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
const Timer = require('./Timer');
const Player = require('./Player');
const Question = require('./Question');
const GAME_STATES = {
IN_PROGRESS: 'in_progress',
WAITING: 'waiting'
}
class GameSessionEvent {
constructor({ data, eventName, message } ) {
this.data = data;
this.eventName = eventName;
this.message = message;
}
}
class GameSession {
constructor({ io }) {
this.players = [];
this.question = null;
this.events = [];
this.timer = new Timer({ gameSession: this })
this.state = GAME_STATES.WAITING;
this.gameMaster = null;
this.socket = io;
this.playersIndex = {}
}
createGameEvent({ message, data, eventName }) {
const event = new GameSessionEvent({ message, data, eventName })
this.events.push(event);
return event;
}
emitEvent(event) {
this.socket.emit(event.eventName, event);
}
emitGameEvent({ message, data, eventName }) {
const event = this.createGameEvent({ message, data, eventName });
this.emitEvent(event)
}
createQuestion({ event }) {
if (this.state === GAME_STATES.IN_PROGRESS) return;
const question = new Question({ question: event.question, answer: event.answer });
this.question = question;
this.emitGameEvent({
message: `QUESTION: ${this.question.question}`,
eventName: 'question_created',
})
this.state = GAME_STATES.IN_PROGRESS;
this.timer.start();
this.timer.onTimeExpired((gameSession) => {
gameSession.emitGameEvent({
message: `TIMER: game session has expired, The answer to the question is ${this.question.answer}`,
eventName: 'time_expired',
})
gameSession.state = GAME_STATES.WAITING;
gameSession.assignGameMaster()
})
}
emitScores() {
let scores = ''
for (const player of this.players) {
scores += `${player.name}: ${player.score},\n`
}
this.emitGameEvent({
message: `SCOREBOARD:\n ${scores}`,
eventName: 'general_message',
})
}
assignGameMaster(retry = false) {
if (!retry) {
this.emitScores();
}
// reset game master
for(const player of this.players) {
if (player.isGameMaster) {
player.setGameMaster(false);
break;
}
};
this.gameMaster = null;
const randomIndex = Math.floor(Math.random() * this.players.length)
let newGameMaster = this.players[randomIndex];
if (!newGameMaster) {
this.assignGameMaster(retry);
}
newGameMaster.setGameMaster(true)
this.gameMaster = newGameMaster;
this.emitGameEvent({
message: `NEW GAME MASTER: New game master is ${this.gameMaster.name}`,
eventName: 'new_game_master',
data: this.players,
})
}
guessAnswer({ event, socket }) {
if (this.state === GAME_STATES.WAITING) return;
const isAnswer = this.question.isAnswer(event.answer);
const player = this.playersIndex[socket.id];
if (!isAnswer) {
this.emitGameEvent({
message: `GUESS: ${player.name} guessed ${event.answer}`,
eventName: 'guess',
isAnswer
})
} else {
this.emitGameEvent({
message: `ANSWER: The answer is ${event.answer}. 10 points awarded to ${player.name}`,
eventName: 'guess',
isAnswer
})
player.score += 10;
this.timer.stop();
this.state = GAME_STATES.WAITING;
this.assignGameMaster();
}
}
join({event, socket }) {
if (this.state === GAME_STATES.IN_PROGRESS) {
this.emitGameEvent({
message: `Game is in progress, try again in ${this.timer.secondsLeft()} seconds`,
eventName: 'join_error'
})
}
if (!event.data.name || !event.data.name.trim().length) {
this.emitGameEvent({
message: `Must provide a name`,
eventName: 'join_error'
})
}
if (this.state === GAME_STATES.WAITING) {
const player = new Player({ name: event.data.name, id: socket.id });
this.playersIndex[socket.id] = player;
if (!this.gameMaster) {
player.setGameMaster(true)
this.gameMaster = player;
}
this.players.push(player);
this.emitGameEvent({
message: `${player.name} just joined`,
data: {
player,
gameMaster: this.gameMaster
},
eventName: 'player_joined'
})
}
}
exit({ socket }) {
this.players = this.players.filter((pl) => pl.id !== socket.id)
}
}
module.exports = GameSession;