This repository was archived by the owner on Dec 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanager.js
More file actions
executable file
·347 lines (291 loc) · 7.27 KB
/
Copy pathmanager.js
File metadata and controls
executable file
·347 lines (291 loc) · 7.27 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
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
/* ======================================= *
* JSONに変換するプログラム *
* ======================================= */
jQuery.extend({
stringify : function stringify(obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string") obj = '"' + obj + '"';
return String(obj);
} else {
// recurse array or object
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n];
t = typeof(v);
if (obj.hasOwnProperty(n)) {
if (t == "string") v = '"' + v + '"';
else if (t == "object" && v !== null) v = jQuery.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
}
});
function delayCall(callback, scope, time, args)
{
if (typeof scope === "undefined" || scope === null) {
scope = this;
}
if (!Array.isArray(args)) {
if (typeof args !== "undefined") {
args = [args];
} else {
args = [];
}
}
return setTimeout(function () { callback.apply(scope, args); },time);
}
function QuestionManager(data, q_data)
{
this._data = data; // 出力データ
this._q_data = q_data; // 質問データ
this._all_question_num = q_data['all_question_num']; // 全質問数
this._common_question_num = q_data['common_question_num']; // 共通の質問数
// 共通の質問数のほうが全質問数よりも多かったらエラー
if (this._common_question_num > this._all_question_num)
{
alert('ERROR: 全質問数<共通質問数');
}
this._timeout_sec = Math.abs(q_data['timeout_sec']); // タイムアウト時間(秒)
if (this._timeout_sec < 1)
{
alert('ERROR: タイムアウト時間は1秒以上に設定してください');
}
this._cur_question_count = 1; // 現在の回答数
this._cur_word = '';
this._timer = null; // タイムアウトタイマ
this._start_time = 0;
this._question_count = 0; // タイムアウトカウントダウン(秒)
$('#countdown')
.css('top', 0)
.css('left', $('#box1').width() - 50);
}
QuestionManager.prototype.startQuestion = function()
{
var SELF = this;
// データ初期化
this._data.question = [];
this._cur_question_count = 1;
clearTimeout(this._timer);
this._start_time = $.now();
this.changeQuestion(this.nextWord());
// 興味あるをクリックした時の処理
$('#yes').click(function(e)
{
SELF.answerQuestion(1);
});
// 興味ないをクリックした時の処理
$('#no').click(function(e)
{
SELF.answerQuestion(-1);
});
// パスをクリックした時の処理
$('#pass').click(function(e)
{
SELF.passQuestion();
});
// キーバインド有効
this.keybindEnabled(true);
}
QuestionManager.prototype.isCommonQuestion = function()
{
if (this._common_question_num < this._cur_question_count)
{
return false;
}
return true;
}
QuestionManager.prototype.keybindEnabled = function(enable)
{
var SELF = this;
var keybind = function(e)
{
if(e.keyCode == 39)
{
//→
$('#no').click();
}
if(e.keyCode == 37)
{
//←
$('#yes').click();
}
// 共通問題中はパスさせない
if (!SELF.isCommonQuestion())
{
if(e.keyCode == 40)
{
//↓
$('#pass').click();
}
}
}
if (enable)
{
$('body').keydown(keybind);
}
else
{
$('body').unbind('keydown');
}
}
QuestionManager.prototype.countDown = function()
{
var count = this._timeout_sec - this._question_count;
if (count < 0)
{
this.passQuestion();
return;
}
$('#countdown').html(count);
this._question_count++;
this._timer = delayCall(this.countDown, this, 1000);
}
QuestionManager.prototype.nextWord = function()
{
// 共通単語から読み込み
if (this._common_question_num >= this._cur_question_count)
{
return this._q_data['common_words'][this._cur_question_count-1];
}
// 分類単語から読み込み
else
{
var categories = new Array();
$.each(this._q_data['category_words'], function(index, val)
{
categories.push(index);
});
var category_index = Math.floor(Math.random() * categories.length);
var category_words = this._q_data['category_words'][categories[category_index]];
var word_num = category_words.length;
var word_index = Math.floor(Math.random() * word_num);
var word = category_words[word_index];
// パスし続けて質問単語を使い果たしてしまったとき...
if (this._data.question.length - this._common_question_num == word_num)
{
this.finishQuestion();
}
// 重複チェック
while(true)
{
var is_duplicate = false;
for (var i = 0; i < this._data.question.length; i++)
{
if (this._data.question[i]['word'] === word)
{
is_duplicate = true;
}
}
if (is_duplicate)
{
word_index = Math.floor(Math.random() * word_num);
word = category_words[word_index];
}
else
{
break;
}
}
return word;
}
}
QuestionManager.prototype.changeQuestion = function(q_word)
{
// キーバインド無効
// this.keybindEnabled(false);
$('.contents').hide();
$('#answer').hide();
$('#pagenum').hide();
// 共通問題はパスさせない!
if (this.isCommonQuestion())
{
$('#pass').hide();
$('#countdown').hide();
}
else
{
$('#pass').show();
$('#countdown').show();
}
this._question_count = 0;
$('#countdown').html(this._timeout_sec - this._question_count);
this._cur_word = q_word;
$('#txt')
.children('p.q_word')
.remove();
$('<p></p>')
.addClass('q_word')
.html(q_word)
.appendTo('#txt');
$('#pagenum').html(this._cur_question_count + '/' + this._all_question_num);
var SELF = this;
setTimeout(function()
{
$('.contents').fadeIn(100);
$('#answer').fadeIn(100, function()
{
// SELF.keybindEnabled(true);
});
$('#pagenum').fadeIn(100);
}, 500);
}
QuestionManager.prototype.answerQuestion = function(answer)
{
clearTimeout(this._timer);
var time = $.now() - this._start_time;
this._data.question.push({
time : time
, word : this._cur_word
, answer : answer
});
// パスのときはカウントを進ませない
if (answer != 0)
{
this._cur_question_count++;
}
if (this._cur_question_count > this._all_question_num)
{
this.finishQuestion();
return;
}
this.changeQuestion(this.nextWord());
// 共通問題の間はタイムアウトさせない
if (!this.isCommonQuestion())
{
this._timer = delayCall(this.countDown, this, 1000);
}
this._start_time = $.now();
}
QuestionManager.prototype.passQuestion = function()
{
this.answerQuestion(0);
}
QuestionManager.prototype.finishQuestion = function()
{
$('div.contents').hide();
$('#button') .hide();
$('#pagenum') .hide();
$('#countdown') .hide();
$('#result') .show();
$('#result').html('送信中……');
$.ajax({
url : 'filewrite.php'
, type : 'POST'
, async : false
, dataType : 'text'
, data : {
data : $.stringify(this._data)
}
, success : function(data)
{
setTimeout(function()
{
window.location = "thx.html";
}, 100);
}
});
}