-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbt.js
More file actions
238 lines (198 loc) · 6.48 KB
/
cbt.js
File metadata and controls
238 lines (198 loc) · 6.48 KB
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
$(document).ready(function(){
console.log("document ready")
console.log("constructors and lists");
var Logic = (function(){
var idcounter = 0;
return function (title, txt){
this.title = title;
this.text = txt;
this.id = "log" + idcounter++;
};
}());
var logics = [
new Logic("Black and White", "Things are rarely all one way or another."),
new Logic("Forgetting the compitition.", "For most things there is lots of compitition. "
+ "You are not the worst person / parent / partner / employee in your neighborhood much less the world."),
new Logic("Comparing up insted of down.", "When we look at people who have more we see our lack. "
+ "When we look people who have less we see some of what we have."),
new Logic("Things change", "Impermanance as Budihst say."),
new Logic("Taking part for the whole", "That guy looks good (right now)."
+ " She is (looks) confident (in this conversation)..."
+ " I wish I could be handsom and confident like them."
+ " This take a little known information and ignores all the things that are not known."),
];
var txtHTML = "";
for(var i = 0; i < logics.length; i++){
txtHTML += "<tr id='" + logics[i].id + "'>"
+ "<td class='title'>" + logics[i].title + "</td>"
+ "<td class='text'>" + logics[i].text + "</td>"
+ "<td class='play'>0</td>"
+ "<td class='time'>1:00</td>"
+ "<td class='id'>" + logics[i].id + "</td></tr>";
}
$("#logics tr:last").after(txtHTML);
var Response = (function(){
var idcounter =0;
return function(txt, confirm){
this.text = txt;
this.confirm = confirm;
this.id = "res" + idcounter++;
};
}());
var responses = [
new Response("Yes", true),
new Response("No", false),
];
var Prompt = (function(){
var idcounter = 0;
return function Prompt(txt, confirm, response = [], logic = []){
this.text = txt;
this.confirm = confirm;
this.responses = response;
this.logics = logic;
this.id = "prm" + idcounter++;
};
}());
var prompts = [
new Prompt("You are willing to play.", true, [responses[0], responses[1]], []),
new Prompt("You are willing to feel better!", true, [responses[0], responses[1]], []),
new Prompt("You are the worst person in the world.", false, [responses[0], responses[1]], []),
new Prompt("You are willing to change and grow!", true, [responses[0], responses[1]], []),
new Prompt("You are brave!", true, [responses[0], responses[1]], []),
new Prompt("Almost everything people do are skills to be learned.", true, [responses[0], responses[1]], []),
new Prompt("You never do anything right!", false, [responses[0], responses[1]], []),
];
var txtHTML = "";
for(var i = 0; i < prompts.length; i++){
txtHTML += "<tr id='" + prompts[i].id + "'>"
+ "<td class='text'>" + prompts[i].text + "</td>"
+ "<td class='confirm'>" + prompts[i].confirm + "</td>"
+ "<td class='play'>0</td>"
+ "<td class='time'>1:00</td>"
+ "<td>" + prompts[i].id + "</td></tr>";
}
$("#prompts tr:last").after(txtHTML);
function Bout(prompt, responses, pick = null){
this.prompt = prompt;
this.responses = responses;
this.pick = pick;
this.time = new Date();
}
var bouts = [];
console.log("click handling");
$(".response").click(function(){
$this = $(this);
var bout = bouts[bouts.length-1];
var index = parseInt($this.attr('id').substring(3));
var notPlaying = $this.hasClass("afirm") === false && $this.hasClass("refuse") === false;
notPlaying = notPlaying && bout === undefined;
if(notPlaying) {
console.log("notPlaying");
return;
}
var $prompt = $("#" + bout.prompt.id + " .play");
$prompt.html(parseInt($prompt.html())+1);
bout.time = new Date().getTime() - bout.time;
$prompt = $("#" + bout.prompt.id + " .time");
var avgTime = Math.floor(bouts.filter(
function(b){return b.prompt.id == bout.prompt.id;}
).reduce(
function(a, b){
return{time: a.time + b.time};
}
).time / bouts.filter(
function(b){return b.prompt.id == bout.prompt.id;}
).length);
console.log(avgTime);
$prompt.html(avgTime);
var $score = $("#score");
if(
($this.hasClass("afirm") && bout.prompt.confirm)
|| ($this.hasClass("refuse") && !bout.prompt.confirm)
){
bout.pick = index;
console.log("You win");
$score.html(parseInt($score.html()) + 1);
}else if(
($this.hasClass("afirm") && !bout.prompt.confirm)
|| ($this.hasClass("refuse") && bout.prompt.confirm)
){
bout.pick = index;
console.log("You loose");
$score.html(parseInt($score.html()) - 1);
}else{
console.log("Should neve run.");
}
clear();
display(new Bout(randomPick(prompts), [responses[0], responses[1]]));
});
$("#playstop").click(function(){
console.log("playstop click");
if($("#playstop").html() === "Play"){
$("#playstop").html("Stop");
var prom = randomPick(prompts);
var bout = new Bout(
prom,
[responses[0], responses[1]]
);
display(bout);
}else{
$("#playstop").html("Play");
clear();
}
});
console.log("application helper functions");
//later should have stuff about user and level
function display(bout){
bouts.push(bout);
var $res = $("#btn0");
$res.html(bout.responses[0].text);
if(bout.responses[0].confirm === true){
$res.removeClass("refuse");
$res.addClass("afirm");
}else{
$res.addClass("refuse");
$res.removeClass("afirm");
}
$res = $("#btn1");
$res.html(bout.responses[1].text);
if(bout.responses[1].confirm === true){
$res.removeClass("refuse");
$res.addClass("afirm");
}else{
$res.addClass("refuse");
$res.removeClass("afirm");
}
$("#prompt").html(bout.prompt.text);
}
function clear(){
console.log("clear");
var $res = $(".response");
var bout = bouts[bouts.length-1];
if(bout !== undefined && bout.pick === null){
bouts.pop();
}
for(var i = 0; i < $res.length; i++){
}
}
});
//adapted from https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function arrayShuffle(arr){
var array = [];
for(var i = 0; i < arr.length; i++) array.push(arr[i]);
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function randomPick(arr){
return arr[Math.floor(Math.random() * arr.length)];
}