-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
172 lines (152 loc) · 4.65 KB
/
index.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
var dialogElem = document.getElementById('dialog');
var dialogBoxElem = document.getElementById('dialog-box');
var talkBtnElem = document.getElementById('talk_btn');
var timer, counter;
var message = [];
var messageDelay = 7000;
var topicDelay = 2*60000;
var variables = {
player: 'Darling'
};
var talk = true;
var conversation_start = true;
function toggleTalk() {
if (talk === false) {
setTimeout(loadConversation, 2000);
talk = true;
talkBtnElem.innerHTML = 'Disable Talk';
} else {
clearText();
dialogBoxElem.style.opacity = 0;
talk = false;
talkBtnElem.innerHTML = 'Enable Talk';
}
}
function fillVariables(text) {
return text.replace(/\[.+\]/g, str => `${variables[str.slice(1,-1)]}`)
}
function animateText(text) {
counter = 1;
timer = setInterval('loadText(" ' + text + '")', 30);
}
function loadText(text) {
dialogElem.innerHTML = text.substring(0, counter);
if (counter >= text.length) {
clearInterval(timer);
} else {
counter++;
}
}
function clearText() {
clearInterval(timer);
dialogElem.innerHTML = "";
};
function onSkip() {
if (talk !== true) {
return;
}
clearText();
if (message.length) {
if (dialogBoxElem.style.opacity !== "1") {
dialogBoxElem.style.opacity = 1;
}
var text = fillVariables(message.shift());
animateText(text);
} else {
dialogBoxElem.style.opacity = 0;
}
}
document.addEventListener('keypress', onSkip);
document.getElementsByTagName('body')[0].addEventListener('click', onSkip);
function getRandomNumber(low, high) {
return low + Math.floor(Math.random()*high);
}
function rotateMessages() {
if (message.length) {
onSkip();
setTimeout(rotateMessages, messageDelay);
} else {
onSkip();
setTimeout(loadConversation, topicDelay);
}
}
function isNewUser() {
if (typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
if (localStorage.getItem("monika.chr")) {
return false;
} else {
localStorage.setItem('monika.chr','Dont delete my file');
return true;
}
} else {
// Sorry! No Web Storage support..
return true;
}
}
function loadConversation() {
if (talk !== true) {
return;
}
if (conversation_start) {
conversation_start = false;
if (isNewUser()) {
// load random topic
var topicsList = Object.keys(topics);
topic = topicsList[getRandomNumber(0,topicsList.length)];
message = topics[topic];
rotateMessages();
} else {
// load a starter
var topicsList = Object.keys(conversation_starters);
topic = topicsList[getRandomNumber(0,topicsList.length)];
message = conversation_starters[topic];
rotateMessages();
}
} else {
// load random topic
var topicsList = Object.keys(topics);
topic = topicsList[getRandomNumber(0,topicsList.length)];
message = topics[topic];
rotateMessages();
}
}
(function(){setTimeout(loadConversation, 12000);})();
function doFullScreen() {
console.log('request full screen');
setTimeout(() => requestFullScreen(document.body), 100);
}
function cancelFullScreen(el) {
var requestMethod = el.cancelFullScreen||el.webkitCancelFullScreen||el.mozCancelFullScreen||el.exitFullscreen;
if (requestMethod) { // cancel full screen.
requestMethod.call(el);
} else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
var wscript = new ActiveXObject("WScript.Shell");
if (wscript !== null) {
wscript.SendKeys("{F11}");
}
}
}
function requestFullScreen(el) {
// Supports most browsers and their versions.
var requestMethod = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen;
if (requestMethod) { // Native full screen.
requestMethod.call(el);
} else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
var wscript = new ActiveXObject("WScript.Shell");
if (wscript !== null) {
wscript.SendKeys("{F11}");
}
}
return false;
}
function toggleFull() {
var elem = document.body; // Make the body go full screen.
var isInFullScreen = (document.fullScreenElement && document.fullScreenElement !== null) || (document.mozFullScreen || document.webkitIsFullScreen);
if (isInFullScreen) {
cancelFullScreen(document);
} else {
requestFullScreen(elem);
}
return false;
}