forked from cornguo/mjcount
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.js
More file actions
235 lines (214 loc) · 6.27 KB
/
func.js
File metadata and controls
235 lines (214 loc) · 6.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
var clips = $(document).data('clips');
var names = $(document).data('names');
var sentences = $(document).data('sentences');
var categories = $(document).data('categories');
var playing = null;
var previous = null;
function renderButtons(objs) {
$(Object.keys(objs)).each(function(i, key) {
var obj = $(objs[key]);
if (obj.length > 0) {
var path = obj[0];
var button = $('<button data-token="' + key + '">' + names[key] + '</button>');
button.on('mousedown', function() {
try {
var sound = new Howl({
urls: genPath(path),
onend: function() {this.unload();}
});
sound.play();
} catch(e) {
console.log('Howl is not found');
}
return false;
})
button.holdEvent({handler: tokenUpdater(), time: 600});
if (0 === $('#buttons div[data-category="' + categories[key] + '"]').length) {
var div = $('<div data-category="' + categories[key] + '"><h2>' + categories[key] + '</h2></div>');
$('#buttons').append(div);
}
$('#buttons div[data-category="' + categories[key] + '"]').append(button);
}
});
}
function tokenMake(key) {
return $('<button data-token="' + key + '">' + names[key] + '</button>').on('dblclick', function() {$(this).remove()});
}
function tokenUpdater() {
return function(elementClicked) {
tokenMake(elementClicked.data('token')).appendTo('#tokens');
$('#tokens').sortable({cancel: ''});
}
}
function sayTokens() {
stopPlaying();
updateHash();
var tokens = $('#tokens button');
if (tokens.length > 0) {
tokens.removeClass('talking');
sayToken(tokens);
} else {
$('#lucky').click();
}
}
function feelLucky() {
var sentence = sentences[Math.floor(Math.random()*sentences.length)];
appendTokensByString(sentence);
}
function genPath(key) {
var filename = 'convert/' + key;
return [filename + '.ogg', filename + '.mp3'];
}
function convertTokToKey(key) {
var tokenSet = clips[key];
if ('undefined' !== typeof(tokenSet)) {
return tokenSet[Math.floor(Math.random()*tokenSet.length)];
}
return null;
}
function sayToken(tokens) {
var tokQue = tokens.not('.talking');
if (0 === tokQue.length) {
return;
}
var currentTok = tokQue.first();
playing = new Howl({
urls: genPath(convertTokToKey(currentTok.data('token'))),
onplay: function() {
currentTok.addClass('talking');
var pos = currentTok.position().top - $('#tokens').position().top - 10;
if (pos > 0) {
$('#tokens').animate({scrollTop: $('#tokens').scrollTop() + pos}, 'fast');
}
},
onloaderror: function() {
this.stop();
this.unload();
},
onend: function() {
if (null !== previous) {
previous.unload();
previous = null;
}
if (tokQue.length > 1) {
previous = this;
sayToken(tokens);
} else {
playing = null;
this.unload();
tokens.removeClass('talking');
}
}
});
playing.play();
}
$(document).ready(function() {
renderButtons(clips);
var str = window.location.hash.substr(1);
if (str.length > 0) {
appendTokensByString(str);
setTimeout(function() {
sayTokens();
}, 1000);
}
$('#say').on('click', function() {
sayTokens();
return false;
});
$('#time').on('click', function() {
$('#tokens').empty();
appendTokensByString(getTimeString());
sayTokens();
return false;
});
$('#clear').on('click', function() {
$('#tokens').empty();
stopPlaying();
return false;
});
$('#stop').on('click', function() {
stopPlaying();
$('#tokens button').removeClass('talking');
return false;
});
$('#lucky').on('click', function() {
$('#tokens').empty();
feelLucky();
sayTokens();
return false;
});
$('#getlink').on('click', function() {
return prompt('分享連結', getLink());
});
return false;
});
function stopPlaying() {
if (null !== playing) {
playing.stop();
playing.unload();
}
$('#tokens').animate({scrollTop: 0}, 'fast');
playing = null;
previous = null;
}
function appendTokensByString(str) {
if (str.length > 0) {
var tokens = str.replace(/_/g, ' ').trim().split(' ');
$(tokens).each(function(i, key) {
if ("[TIME]" === key) {
var timeString = getTimeString();
appendTokensByString(timeString);
}
if ('undefined' !== typeof(names[key])) {
$('#tokens').append(tokenMake(key));
}
});
$('#tokens').sortable({cancel: ''});
}
}
function updateHash() {
var tokens = $('#tokens button');
if (tokens.length > 0) {
var hash = '';
tokens.each(function (i, obj) {
hash += $(obj).data('token') + ' ';
});
window.location.hash = hash.trim().replace(/ /g, '_');
}
return false;
}
function getTimeString() {
var date = new Date();
var hour = date.getHours();
var minute = date.getMinutes();
if (0 === minute) {
return convertNumToTok(hour) + ' dian whole';
} else {
return convertNumToTok(hour) + ' dian ' + convertNumToTok(minute) + ' fen';
}
}
function convertNumToTok(num) {
if (0 == num) {
return '0';
} else if (2 == num) {
return '2s';
} else if (10 == num) {
return '10';
} else {
if (num < 10) {
return num;
} else if (num > 10 && num < 20) {
return '10 ' + (num%10);
} else if (num >= 20) {
var retStr = parseInt(num/10) + ' 10 ';
if (num % 10 != 0) {
retStr += num % 10;
}
return retStr;
}
}
}
function getLink() {
updateHash();
return window.location;
}