-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogue.js
111 lines (93 loc) · 3.42 KB
/
dialogue.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
var base = window.location.pathname.split("/").length == 2 ? "./" : "../"
base = window.location.pathname.split("/").length > 2 ? "../../" : base
var botui = new BotUI('bot');
var fixed_delay = 700;
var protocol;
// // TODO: fix menu button issue
// var interrupt_requested;
// var interrupt_executed;
function updateUI(protocol, i) {
// // TODO: fix menu button issue
// if (interrupt_requested) {
// console.log("getting here");
// interrupt_executed=true;
// interrupt_requested=false;
// return;
// }
var curr = protocol[i];
botui.message.bot({
delay: Math.max(curr.message.length * 10, fixed_delay),
loading: true,
content: curr.message
})
.then(function () {
// autoscroll to bottom
$(".botui-messages-container").stop().animate({ scrollTop: $(".botui-messages-container")[0].scrollHeight}, fixed_delay);
fixed_delay = curr.Timer == "Timer" ? 30000 : 700;
if (curr.options) {
var options = formatOptions(curr.options);
return botui.action.button({
delay: fixed_delay,
action: options
}).then(function (res) {
if (curr.dialogue) { // load next dialogue
var res_index = curr.options.indexOf(res.value);
var json_script = curr.dialogue[res_index];
var filepath = base + 'scripts/' + persona + json_script;
console.log(filepath);
$.getJSON(filepath,loadDialogue);
} else {
loadMessage(protocol, i + 1);
}
});
} else {
loadMessage(protocol, i + 1);
}
});
}
function loadDialogue(data) {
protocol = data;
loadMessage(protocol,0);
}
function loadMessage(protocol, i) { // recursive message display function
if (i >= Object.keys(protocol).length) { return; } // end recursion
if (protocol[i].message == "CLEAR") { clearChat(protocol, i); }
else if (protocol[i].message == "PERSONA") { savePersona(protocol, i); }
else { updateUI(protocol, i);}
}
function formatOptions(options) {
var formatted_opt = [];
for (var j = 0; j < options.length; j++) {
var cell = {};
cell.text = options[j];
cell.value = options[j];
formatted_opt.push(cell);
}
return formatted_opt;
}
function clearChat(protocol, i) {
$('.botui-messages-container').children().fadeOut(500)
.promise().then(function(){
loadMessage(protocol, i + 1);
});
}
var persona = '';
function savePersona(protocol, i) { // preliminary function for saving persona choice during session
persona = protocol[i]["persona-choice"] + "/";
loadMessage(protocol, i + 1);
}
function menu() {
// // TODO: fix preliminary code to interrupt previous dialogue rendering before showing menu content
// interrupt_requested = true;
// setTimeout(() => {
// interrupt_executed = false;
// botui.message.removeAll();
// botui.action.hide();
// menu_script = persona == '' ? 'scripts/init.json' : base + 'scripts/' + persona + "menu.json";
// $.getJSON(menu_script,loadDialogue);
// }, 2000);
botui.message.removeAll();
botui.action.hide();
menu_script = persona == '' ? 'scripts/init.json' : base + 'scripts/' + persona + "menu.json";
$.getJSON(menu_script,loadDialogue);
}