-
Notifications
You must be signed in to change notification settings - Fork 4
/
Game.js
201 lines (180 loc) · 4.37 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
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
const _ = require(`./messages`)
const ytdl = require(`ytdl-core`)
const {songReplace} = require(`./song_replace`)
const {LoggerFactory} = require(`logger.js`)
const logger = LoggerFactory.getLogger(`main`, `blue`)
/**
* Represents a game
*/
class Game {
/**
* @param {Client} client Discord.js client
*/
constructor(client) {
/**
* Discord.js client
* @type {?Client}
*/
this.client = client
/**
* Whether or not joined vc successfully
* @type {?boolean}
*/
this.status
/**
* Whether or not someone answered correctly
* @type {?boolean}
*/
this.correct
/**
* The id of timer for open the space between quizzes
* @type {?number}
*/
this.timeout
/**
* The current video data
* @type {?Video}
*/
this.current
/**
* Video data used for quizzes
* @type {?Video[]}
*/
this.songs
/**
* The quiz times counter
* @type {?number}
*/
this.count
/**
* The stream dispatcher
* @type {?StreamDispatcher}
*/
this.dispatcher
/**
* The voice connection
* @type {?VoiceConnection}
*/
this.connection
/**
* The TextChannel to use for quizzes
* @type {?TextChannel}
*/
this.tc
/**
* The VoiceChannel to use for quizzes
* @type {?VoiceChannel}
*/
this.vc
}
/**
* Init the game
* @param {TextChannel} tc TextChannel
* @param {VoiceChannel} vc VoiceChannel
*/
init(tc, vc) {
this.tc = tc
this.vc = vc
}
/**
* Set all class variable to null
* @private
*/
deinit() {
this.status = null
this.correct = null
this.timeout = null
this.current = null
this.songs = null
this.count = null
this.dispatcher = null
this.connection = null
}
/**
* Connect to the VoiceChannel
* @returns {Promise<boolean>}
* @private
*/
async connect() {
this.connection = await this.vc.join().catch(error => {
if (this.vc.full) {
this.tc.send(_.JOIN_VC.FULL(this.vc.name))
} else if (!this.vc.joinable) {
this.tc.send(_.JOIN_VC.NO_PERMISSION(this.vc.name))
} else {
this.tc.send(_.JOIN_VC.UNKNOWN_ERROR(this.vc.name))
logger.error(_.CONSOLE.JOIN_VC_ERROR(error))
}
})
if (this.connection) this.status = true
return this.status
}
/**
* Initialize a quiz
* @private
*/
preQuiz() {
if (typeof this.count !== `number`) this.count = 0; else this.count++
this.tc.send(_.QUIZ.NEXTQUIZ(this.count))
this.correct = false
this.current = this.songs[Math.floor(Math.random() * this.songs.length)]
this.current.answers = songReplace(this.current.title).filter(e => e)
logger.info(require(`util`).inspect(this.current))
if (!this.current.answers.length) this.preQuiz()
else this.timeout = this.client.setTimeout(() => this.quiz(), 5000)
}
/**
* Quiz
* @private
*/
quiz() {
this.tc.send(_.QUIZ.START)
const stream = ytdl(this.current.id, {filter: `audioonly`})
this.dispatcher = this.connection.play(stream)
.on(`finish`, () => {
if (!this.correct)
this.tc.send(_.QUIZ.UNCORRECT(this.current.title))
if (this.status) this.preQuiz()
})
}
/**
* The video data
* @typedef {Object} Video
* @property {string} id The id of the video
* @property {string} title The title of the video
* @property {string[]} [answers] The quiz answers of the video
*/
/**
* Start the quiz
* @param {Video[]} songs Video data used for quizzes
*/
async start(songs) {
this.songs = songs
const status = await this.connect()
if (status) this.preQuiz()
}
/**
* Stop quiz
*/
gameend() {
this.client.clearTimeout(this.timeout)
this.status = false
this.correct = true
this.dispatcher.end()
this.connection.disconnect()
this.deinit()
}
/**
* Check answer
* @param {string} text Answer to check
*/
check(text) {
if (this.current.answers.map(a => a.toLowerCase()).some(answer => text.toLowerCase().includes(answer)) ||
((/\w/g).test(text) && this.current.title.includes(text))) { // needs to be 100% match but easy than others, you can't type just "-" or something
this.correct = true
this.tc.send(_.QUIZ.CORRECT(this.current.title))
this.dispatcher.end()
}
}
}
module.exports = Game