-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot_class.gs
360 lines (319 loc) · 12 KB
/
chatbot_class.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
function str2HTML(s){
return s.replace(/(?:\r\n|\r|\n)/g, '<br>');
}
function simplify_teamname(s){
return s.slice(0,2)+s.slice(-1);
}
class Quote{
constructor(cmd_string){ // formatted: (userID)++(timestamp)++(command)
[this.user_id,this.time,this.cmd] = cmd_string.split('++')
this.resp = '';
}
toString(add_cmd_tags = false){
var s = `${this.user_id.slice(0,10)}>> ${this.cmd}`
if(add_cmd_tags){s = `<span style="color:${cb_color['cmd']};font-weight:bold;">${s}</span>`}
if(this.resp != ''){s+=`${this.resp}`};
return s;
}
toHTML(){
return str2HTML(this.toString(true));
}
serialize(){
return `${this.user_id}++${this.time}++${this.cmd}`
}
}
class Conversation{
/////////// CONSTRUCTOR FUNCTION
constructor(pf,rm,st){
this.pf = pf;
this.rm = rm;
this.st = st;
this.st_obj = new CB_Stage(pf,rm,st); //new object that retrieves relevant data;
this.property_key = `cmdlog_pf${this.pf}-rm${this.rm}-st${this.st}`;
this.quotes = [];
if(this.is_empty() ){this.init()}
this.load()
this.compute_state = 0; //0: un-initialized, 1: under computation, 2: finished.
Logger.log(''+this);
}
////////// SERIALIZED LOGISTICS
is_empty(){ // checks for any serialized data within properties
return (PropertiesService.getDocumentProperties().getProperty(this.property_key) == null)
}
init(){
var init_string = `${getUserID()}++${getNow()}++init`;
PropertiesService.getDocumentProperties().setProperty(this.property_key,init_string);
return [init_string,]
}
load(){ // brings serialized document property into this.quotes[]
var raw_string = PropertiesService.getDocumentProperties().getProperty(this.property_key);
if(raw_string == null){return null}
this.quotes = [];
for(var s of raw_string.split("\n")){this.quotes.push(new Quote(s));}
}
save(){ // saves this.quotes[] into serialized string without responses
var serialized_string = '';
for(var q of this.quotes){
serialized_string += q.serialize() + '\n';
}
PropertiesService.getDocumentProperties().setProperty(this.property_key,serialized_string.slice(0,-1));
}
////////// COMMAND MANIPULATION
add_command(cmd){
this.quotes.push(new Quote(`${getUserID()}++${getNow()}++${cmd}`));
this.save();
}
pop_last(){ // for "undo" command
if(this.quotes.length >= 2){
this.quotes.pop();
this.save();
}
}
////////// OUTPUT / REPRESENTATION
toString(add_cmd_tags = false){
var s = '';
for(var q of this.quotes){
s += q.toString(add_cmd_tags) + '\n';
}
return s
}
toHTML(){
return str2HTML(this.toString(true));
}
////////// IMPORTANT: Command Responses
init_computation(){
this.all_problems = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17];
this.rule = {
'B':this.st_obj.retreive_rule('B'),
'P':this.st_obj.retreive_rule('P'),
'a':this.st_obj.retreive_rule('a'),
'b':this.st_obj.retreive_rule('b'),
'c':this.st_obj.retreive_rule('c'),
'd':this.st_obj.retreive_rule('d'),
}
this.rule_description = {
'B':"Banned in KYPT",
'P':"Presented in this PF",
'a':"Rejected by Reporter",
'b':"Presented by Reporter",
'c':"Opposed by Opponent",
'd':"Presented by Opponent",
'o':"Out of Range",
}
this.active_rules = ['B','P','a','b','c','d'];
this.new_rejects = [];
this.num_rejects = this.st_obj.retreive_num_prev_rejects();
this.possible_problems = [];
this.stabilize();
// Logger.log(this.rule);
// Logger.log(typeof(this.rule['b'][0]))
// Logger.log(this.possible_problems);
// this.quotes[0].resp = this.possible_problems;
this.challenged_p = 0; // challenged problem (0 means no problem challenged.)
this.compute_state = 1;
}
challenge_conflicts(p,only_active = true){
var conflict = [];
if(this.all_problems.includes(p) == false){
conflict.push("o");
return conflict
}
if(only_active){ // check for active rules only (used generally)
for(var r of this.active_rules){if(this.rule[r].includes(p)){conflict.push(r);}}
}
else{ // check for all rules (used in tables)
for(var r in this.rule){if(this.rule[r].includes(p)){conflict.push(r);}}
}
// Logger.log(conflict);
return conflict
}
can_challenge(p){
return(this.challenge_conflicts(p).length == 0)
}
can_reject(){
return(this.num_rejects < regulation['max_rejects']);
}
apply_rules(){
this.possible_problems = [];
for(var p of this.all_problems){
if(this.can_challenge(p)){
this.possible_problems.push(p);
}
}
// Logger.log(this.possible_problems)
}
remove_rule(){
var removeed_rule = this.active_rules[this.active_rules.length-1]
this.active_rules = this.active_rules.slice(0,-1);
return removeed_rule;
}
stabilize(){
var removeed_rules = [];
this.apply_rules();
while(this.possible_problems.length < regulation['remove_th'] && this.active_rules.length > 2){
removeed_rules.push(this.remove_rule());
this.apply_rules();
}
return removeed_rules;
}
html_people(){
var html = '<table>';
html += '<tr><th>Rep.</th><th>Opp.</th><th>Rev.</th></tr>';
html += `<tr><td>${simplify_teamname(this.st_obj.get_team('r'))}</td><td>${simplify_teamname(this.st_obj.get_team('o'))}</td><td>${simplify_teamname(this.st_obj.get_team('e'))}</td></tr>`;
html += '</table>';
return html
}
html_table(){
var html = '<table>';
var temp_conflicts = []
// first row (header)
html += '<tr>'
html += `<th>##</th>`
for (let r in this.rule){
if(this.active_rules.includes(r)){html += `<th>${r}</th>`;}
else{html += `<th style="color:${cb_color['rela']}">${r}</th>`;}
}
html += '</tr>'
// number rows
for (var num of this.all_problems){
html += '<tr>'
if(this.possible_problems.includes(num)){html += `<td style="color:${cb_color['chal']}">`;}
else{html += '<td>';}
html += `${num}</td>`
temp_conflicts = this.challenge_conflicts(num,false);
for (let r in this.rule){
if(this.active_rules.includes(r)){html += '<td>';}
else{html += `<td style="color:${cb_color['rela']}">`;}
if(temp_conflicts.includes(r)){html+=`${r}`;}
html += '</td>';
}
html += '</tr>'
}
html += '</table>'
return html
}
text_table(){
var s = '#\t: conflicts';
for(var num of this.all_problems){
s += `\n${num}\t: ${this.challenge_conflicts(num)}`;
}
return s
}
text_summary(){
var s = '';
s+= `New Rejects:${this.new_rejects}`;
s+= `\nTotal # of Rej.:${this.num_rejects}`;
if(this.compute_state < 2){
s+= `\nChallenge-able:\n<span style="color:${cb_color['chal']}">${this.possible_problems}</span>`;
s+= `\nCurrently Challenged: <span style="color:${cb_color['chal']}"><b>${((this.challenged_p==0) ? "none":this.challenged_p)}</b></span>`;
}
else{s+= `\nAccepted: <span style="color:${cb_color['acc']}"><b>${this.challenged_p}</b></span>`;}
return s;
}
tooltip(){
var tooltip = '';
if(this.compute_state < 2){
if(this.challenged_p < 1){ // no challenged problem
tooltip += `<span style="color:${cb_color["chal"]}"><b>##(1~${this.all_problems[this.all_problems.length-1]})</b>:challenge problem</span>`;
}
else{
tooltip += `<span style="color:${cb_color["acc"]}"><b>a</b>:accept problem</span>`;
tooltip += `\n<span style="color:${cb_color["rej"]}"><b>r</b>:reject problem</span>`;
}
}
else{tooltip += 'Computation Finished.'}
tooltip += `\n<span style="color:${cb_color["undo"]}"><b>u</b>:undo last command</span>`;
return str2HTML(tooltip);
}
status(table = 'html'){
// var s = '<hr>';
var s = '';
s+= this.html_people();
if(table == 'text'){s+=this.text_table();}
if(table == 'html'){s+=this.html_table();}
s+= this.text_summary();
s += '<hr>'
return str2HTML(s);
}
insert_result(do_insert = true){
if(do_insert == false){return ;}
var rejects = this.new_rejects
while(rejects.length < 7){rejects.push("")}
this.st_obj.get_range_rej().setValues([this.new_rejects]);
this.st_obj.get_range_acc().setValue([this.challenged_p]);
}
// Possible commands:
// i: initialize
// num(1~17): challenge probelem
// a: accept challenged problem
// r: reject challenged problem
execute_cmd(index,do_insert = true){ // executes the i th command within this.quotes. adds the response.
var cmd = this.quotes[index].cmd.slice(0,2);
var cmd_type = "a"; //"a": action (i,a,r), "c": challenge (1~17)
if(/\d/.test(cmd)){cmd = parseInt(cmd,10); cmd_type = "c"}
else {cmd = cmd[0] ; cmd_type = "a"}
Logger.log(`Processing QUOTE#${index} (${this.quotes[index].cmd}) <preprocessed: ${cmd} , type: ${cmd_type}>`);
var response = `[${cmd_type}:${cmd}]`;
var err = false;
if(this.compute_state == 2) {response += "\n[ERR] Computation Finished. No Actions Possible" ; err = true;}
else if(this.challenged_p != 0 && (cmd_type == "c")){response += "\n[ERR] Cannot Challenge Multiple Problems at once!"; err = true;}
else if(this.challenged_p == 0 && (cmd_type == "a")){response += "\n[ERR] No problems are challenged yet!" ; err = true;}
if(cmd_type == "a" && (err == false)){
if(cmd == 'i'){
if(this.compute_state != 0){response += "\n[ERR] Computation was Already Initialized.";err = true;}
else{this.init_computation();response += `\n[ PF${this.pf} RM${this.rm} ST${this.st} ] Initialized`;}
}
else if(this.compute_state == 0){response += "\n[ERR] You Must First Initialize Computation!";err = true;}
else if(cmd == 'r'){
if(this.can_reject()){
this.rule['a'].push(this.challenged_p);
this.new_rejects.push(this.challenged_p);
this.num_rejects += 1;
response += `\n[OK] P#.<span style="color:${cb_color["chal"]}">${this.challenged_p}</span> <span style="color:${cb_color["rej"]}">Rejected</span>`;
this.challenged_p = 0;
var removeed_rules = this.stabilize();
for(var r of removeed_rules){
response += `\n<span style="color:${cb_color['rela']}">!!Rule[${r}] Removeed!!\n(${this.rule_description[r]})</span>`;
}
}
else{
response += "\n[ERR] Cannot Reject (max rejects reached)";
err = true;
}
}
else if(cmd == 'a'){
response += `\n[OK] P#.<span style="color:${cb_color["chal"]}">${this.challenged_p}</span> <span style="color:${cb_color["acc"]}">Accepted</span>.`;
response += `\n<span style="font-weight:bold;">Challenge Complete.</span>`;
response += `\n<span style="color:${cb_color["rej"]}"->Rej.:${this.new_rejects}</span>`;
response += `\n<span style="color:${cb_color["acc"]}"->Acc.:${this.challenged_p}</span>`;
this.insert_result(do_insert);
this.compute_state = 2;
}
else{response += "\n[ERR] Invalid Command (possible commands: [a/r])";err = true;}
}
if(cmd_type == "c" && (err == false)){
if(this.can_challenge(cmd)){
this.challenged_p = cmd;
response += `\n[OK] P#.<span style="color:${cb_color["chal"]}">${this.challenged_p} Challenged</span>`;
}
else{
response += "\n[ERR] Cannot Challenge Problem\nConflicts:";
var conflicts = this.challenge_conflicts(cmd);
for(var c of conflicts){
response += `\n[${c}]${this.rule_description[c]}`;
}
err = true;
}
}
// if(err){response += "\nERR occurred. Nothing changed."}
// else{response += " [O]"}
// if(index == this.quotes.length -1){response += `${this.status()}`;}
this.quotes[index].resp = response;
return response;
}
execute_all(do_insert = true){
for(var index = 0;index < this.quotes.length;index++){this.execute_cmd(index,do_insert);}
Logger.log("Processing Complete")
Logger.log(this.toString())
}
}