-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quiz.gs
100 lines (90 loc) · 2.56 KB
/
Quiz.gs
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
/** @class */
class Quiz {
/**
* ID of quiz
* @type {Number}
*/
// sn;
/**
* Question of quiz
* @type {String}
*/
// question;
/**
* Options of this quiz, total length 4.
* @type {Array<String>}
*/
// options;
/**
* Answer of this quiz, valid option: 1, 2, 3, 4.
* @type {Number}
*/
// answer;
/**
* Author's Bahamut account
* @type {String}
*/
// author;
/**
* ID of board
* @type {Number}
*/
// bsn;
/**
* @param {Array<String>|null} rowData raw data of row of quiz
*/
constructor(rowData) {
if (!rowData) return;
this.sn = Number(rowData[0]);
this.question = rowData[1];
this.options = [rowData[2], rowData[3], rowData[4], rowData[5]];
this.answer = Number(rowData[6]);
this.author = rowData[7];
this.bsn = Number(rowData[8]);
}
/**
* Convert aand validate raw data from client
* @param requestData data sent from client
* @throws {Error} when data is not satisified fields which quiz need
* @returns {Quiz}
*/
static from(requestData) {
let quiz = new Quiz();
if (requestData.sn &&
Number.isNaN(Number.parseInt(requestData.sn)) == false)
quiz.sn = Number(requestData.sn);
else throw new Error("INVALID_SN");
if (requestData.question &&
typeof requestData.question === "string" &&
requestData.question != "")
quiz.question = decodeURIComponent(requestData.question);
else throw new Error("INVALID_QUESTION");
if (
requestData.options &&
requestData.options instanceof Array &&
requestData.options.length == 4 &&
requestData.options.every(value => typeof value === "string" && value != ""))
quiz.options = requestData.options.map(decodeURIComponent);
else throw new Error("INVALID_OPTIONS");
if (requestData.author &&
typeof requestData.author === "string" &&
requestData.author != "")
quiz.author = requestData.author;
else throw new Error("INVALID_AUTHOR");
if (requestData.bsn &&
Number.isNaN(Number.parseInt(requestData.bsn)) == false)
quiz.bsn = Number(requestData.bsn);
else throw new Error("INVALID_BSN");
if (requestData.correctness &&
requestData.this_answered &&
[1, 2, 3, 4].includes(Number(requestData.this_answered)))
quiz.answer = Number(requestData.this_answered);
return quiz;
}
toString() {
return JSON.stringify(this);
}
toArray() {
return [this.sn, this.question, this.options[0], this.options[1], this.options[2], this.options[3], this.answer, this.author, this.bsn];
}
}