-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (138 loc) · 4.97 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
/*
原网址: https://camuskit-bonustime.netlify.com/
感谢作者把代码写得如此清晰而通用. 所有写出整洁代码的人都值得尊敬.
BearDic, 2018-10-29.
*/
$(document).ready(function () {
console.log('%c原网址: https://camuskit-bonustime.netlify.com/', 'color:blue;font-size:1.5em');
console.log('%c感谢原作者把代码写得如此清晰而通用. 所有写出整洁代码的人都值得尊敬.', 'color:blue;font-size:1.5em');
console.log('%cBearDic, 2018-10-29. ', 'color:black;font-size:1.2em');
var getConfig = function () {
const url = new URL(window.location.href);
var minNum = 1, maxNum = 999, size = 4, hasBackground = false, cardBackground = false, showMode = 0;
minNum = parseInt(url.searchParams.get("min") || "1");
maxNum = parseInt(url.searchParams.get("max") || "999");
if (maxNum<1000)
{
$("#num4").parent().remove();
$(".col-xs-4").css("width","33.33333333%");
size=3;
}
else size = 4;
const customBackground = url.searchParams.get("bg_url");
cardBackground = false;
showMode = 1;
return [minNum, maxNum, size, customBackground, cardBackground, showMode];
};
// Add special background
var addBackground = function (bgUrl) {
$('body').css('background-image', `url(${bgUrl})`);
};
var addCardBackground = function () {
for (var i = 0; i < 10; i++) {
$('#card' + i).css('background', 'url(\'images/' + i + '.jpg\') no-repeat center 200px').text('');
}
};
// Get a random number, and return as a string array
var getRandomNum = function (usedNum, minNum, maxNum, size) {
var randomNum = Math.round(Math.random() * (maxNum - minNum + 1) + minNum-0.5);
while(Math.floor(randomNum/10)-randomNum%10 == 0)
randomNum = Math.round(Math.random() * (maxNum - minNum + 1) + minNum-0.5);
usedNum.push(randomNum);
var str = randomNum.toString();
while (str.length < size) str = '0' + str;
//str = str.substr(0,1) + '&' + str.substr(1,1);
//str = '??' + str;
return str.split('');
};
// Toggle between Cube and Ring
var toggleShape = function () {
$('#shape').toggleClass('ring').toggleClass('cube');
};
// Open ring and show num, or inversely
var toggleBetweenNumAndCube = function (ringNum, cubeNum) {
var $shape = $('#shape');
$shape.toggleClass('ringShow_' + ringNum).toggleClass('cubeShow_' + cubeNum);
$shape.css('-webkit-transform', 'rotateY(' + (-36 * ringNum) + 'deg)');
};
// Let card jump
var toggleCardJump = function (num) {
$('#card' + num).toggleClass('jump' + num);
};
// Write the number into num block
var displayNum = function (cardNum, showNum) {
$('#num' + cardNum).text(showNum);
};
// Let the number jump
var toggleNumJump = function (num) {
$('#num' + num).toggleClass('jumpDown');
};
// The whole lottery process with one num
var showNumByTurn = function (times, targetNum, originNum) {
toggleShape();
toggleBetweenNumAndCube(targetNum, originNum);
setTimeout(function () {
toggleCardJump(numArr[times]);
setTimeout(function () {
displayNum(times + 1, numArr[times]);
toggleNumJump(times + 1);
toggleShape();
toggleBetweenNumAndCube(targetNum, targetNum);
toggleCardJump(numArr[times]);
}, 1100);
}, 3300);
};
// Show all num once
var showNumOnce = function (numArr) {
for (var i = 0; i < numArr.length; i++) {
displayNum(i + 1, numArr[i]);
toggleNumJump(i + 1);
}
};
// clear numbers
var clearNum = function (arr) {
for (var i = 0; i < arr.length; i++) {
toggleNumJump(i + 1);
$('#num' + (i + 1)).text('');
}
};
// 0: minNum, 1: maxNum, 2: numLength, 3: hasBackground.
var config = getConfig();
// Add background
if (config[3])
addBackground(config[3]);
if (config[4])
addCardBackground();
// Lottery
var usedArr = [];
var numArr = getRandomNum(usedArr, config[0], config[1], config[2]);
var pressTimes = 0, preNum = 0;
$('body').keydown(function (event) {
if (config[5] === 0) {
if (event.which === 32) {
if (pressTimes < numArr.length) {
showNumByTurn(pressTimes, numArr[pressTimes], preNum);
preNum = numArr[pressTimes];
pressTimes++;
} else {
pressTimes = 0;
clearNum(numArr);
numArr = getRandomNum(usedArr, config[0], config[1], config[2]);
}
}
} else if (config[5] === 1) {
if (event.which === 32 && pressTimes === 0) {
toggleShape();
pressTimes++;
} else if (event.which === 32 && pressTimes === 1) {
toggleShape();
showNumOnce(numArr);
pressTimes++;
} else if (event.which === 32 && pressTimes === 2) {
clearNum(numArr);
numArr = getRandomNum(usedArr, config[0], config[1], config[2]);
pressTimes = 0;
}
}
})
});