-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround.js
98 lines (85 loc) · 2.14 KB
/
round.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
const { v4: uuidv4 } = require('uuid')
class Round {
constructor(opts={}) {
this.questions = []
if (opts.order) {
this.order = opts.order
}
}
addQuestion(question) {
if (question instanceof Array) {
if (question.every(q => q instanceof Question)) {
this.questions.push(...question)
}
} else if (question instanceof Question) {
this.questions.push(question)
}
}
}
class Question {
constructor(opts={}) {
if (!opts.text || !opts.seconds) {
throw new Error('opts.text and opts.seconds must be provided')
}
this.choices = {
correct: null,
incorrect: []
}
this.id = uuidv4()
this.text = opts.text
this.seconds = opts.seconds
this.points = opts.points ? Number(opts.points) : 1
if (opts.order) {
this.order = Number(opts.order)
}
if (opts.choices.correct) {
this.choices.correct = opts.choices.correct
}
if (opts.choices.incorrect) {
if (opts.choices.incorrect instanceof Array) {
this.choices.incorrect.push(...opts.choices.incorrect)
} else {
this.choices.incorrect.push(opts.choices.incorrect)
}
}
}
addChoice(choice) {
if (choice.usable) {
if (choice.correct) {
this.choices.correct = choice
} else if (choice.incorrect) {
this.choices.incorrect.push(choice)
} else {
throw new Error('choice.correct or choice.incorrect must be true')
}
} else {
throw new Error('Choice must be usable before it can be added')
}
}
}
class Choice {
constructor(opts={}) {
this.correct = null
this.incorrect = null
this.text = null
if (opts.text) {
this.text = opts.text
}
if (opts.correct) {
this.correct = true
this.incorrect = false
} else if (opts.incorrect) {
this.correct = false
this.incorrect = true
}
}
get usable() {
return (
(this.text !== null && typeof(this.text) === 'string') && (
(this.correct && this.incorrect == false) ||
(this.incorrect && this.correct == false)
)
)
}
}
module.exports = { Choice, Question, Round }