-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.js
274 lines (234 loc) · 7.23 KB
/
misc.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
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
/// <summary> Keyboard code case insensitivity. </summary>
function case_i_equals(charCode, keycode){
return (charCode === keycode || charCode - KEYCODES.CAPS_OFFSET === keycode);
}
/// <summary> Log to console if logging is supported. </summary>
/// <remarks>
/// <para> Logging is for debugging only. </summary>
/// </remarks>
function log(o){
if(console !== undefined && console.log !== undefined){
console.log(o);
}
return o;
}
/// <summary> Iterate the members of a class and bind them if they
/// are functions that are not contained in the prototype.
/// </summary>
function bindAllFunctions(obj){
for(p in obj){
if(obj.hasOwnProperty(p) && typeof p === "function"){
obj[p] = p.bind(obj);
}
}
}
/// <summary> Find the index of the item whose execution with toNumF is
/// the miniumum.
/// </summary>
Array.minIndex = function( array, toNumF ){
toNumF = typeof(toNumF) != 'undefined' ? toNumF : function(e){ return e};
var index = -1;
var runningMin = Number.MAX_VALUE;
array.forEach(function(ele, ind){
if(toNumF(ele) < runningMin){
index = ind;
runningMin = toNumF(ele);
}
});
return index;
};
/// <summary> Find the array item whote execution with toNumF is
/// the minimum.
/// </summary>
Array.min = function(array, toNumF){
var idx = Array.minIndex(array, toNumF);
if(idx !== -1){
return array[idx];
}
}
/// <summary> Shuffle the array. </summary>
Array.shuffle = function(array) {
var tmp, current, top = array.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}
/// <summary> Find the first item in the array where the predicate
/// function returns true.
/// </summary>
Array.prototype.detect = function(f){
for(var i = 0; i < this.length; i++){
if(f(this[i])){
return this[i];
}
}
}
/// <summary> Keep track of currently playing sounds. </summary>
SoundsPlaying = {fire: 0};
/// <summary> Play a sound. </summary>
function playSound(soundID){
if(SoundsPlaying[soundID]){
soundID +="_"
}
SoundsPlaying[soundID]++;
var ele = $("#sounds #" + soundID)[0];
try{
ele.play();
}catch(err){
SoundsPlaying[soundID] = 0;
}
window.setTimeout(function(){
SoundsPlaying[soundID]--;
},1500);
}
////////////////////////////////////////////////////////////////////////////////
// Backports for IE compatibility.
/// <summary> Mozilla developer.mozilla.org bind() backport. </summary>
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP
? this
: oThis || window,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
/// <summary> Mozilla developer.mozilla.org some() backport. </summary>
if (!Array.prototype.some)
{
Array.prototype.some = function(fun /*, thisp */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t && fun.call(thisp, t[i], i, t))
return true;
}
return false;
};
}
/// <summary> Mozilla developer.mozilla.org filter() backport. </summary>
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}
/// <summary> Mozilla developer.mozilla.org forEach() backport. </summary>
if ( !Array.prototype.forEach ) {
Array.prototype.forEach = function( callback, thisArg ) {
var T, k;
if ( this == null ) {
throw new TypeError( " this is null or not defined" );
}
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0; // Hack to convert O.length to a UInt32
// 4. If IsCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if ( {}.toString.call(callback) != "[object Function]" ) {
throw new TypeError( callback + " is not a function" );
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if ( thisArg ) {
T = thisArg;
}
// 6. Let k be 0
k = 0;
// 7. Repeat, while k < len
while( k < len ) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if ( k in O ) {
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
kValue = O[ k ];
// ii. Call the Call internal method of callback with T as the this value and
// argument list containing kValue, k, and O.
callback.call( T, kValue, k, O );
}
// d. Increase k by 1.
k++;
}
// 8. return undefined
};
}
/// <summary> Mozilla developer.mozilla.org indexOf() backport. </summary>
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict";
if (this === void 0 || this === null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) {
return -1;
}
var n = 0;
if (arguments.length > 0) {
n = Number(arguments[1]);
if (n !== n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n !== 0 && n !== Infinity && n !== -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
}
}