-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
executable file
·107 lines (85 loc) · 3.16 KB
/
script.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
const ICONS = [
'apple', 'apricot', 'banana', 'big_win', 'cherry', 'grapes', 'lemon', 'lucky_seven', 'orange', 'pear', 'strawberry', 'watermelon',
];
/**
* @type {number} The minimum spin time in seconds
*/
const BASE_SPINNING_DURATION = 2.7;
/**
* @type {number} The additional duration to the base duration for each row (in seconds).
* It makes the typical effect that the first reel ends, then the second, and so on...
*/
const COLUMN_SPINNING_DURATION = 0.3;
var cols;
window.addEventListener('DOMContentLoaded', function(event) {
cols = document.querySelectorAll('.col');
setInitialItems();
});
function setInitialItems() {
let baseItemAmount = 40;
for (let i = 0; i < cols.length; ++i) {
let col = cols[i];
let amountOfItems = baseItemAmount + (i * 3); // Increment the amount for each column
let elms = '';
let firstThreeElms = '';
for (let x = 0; x < amountOfItems; x++) {
let icon = getRandomIcon();
let item = '<div class="icon" data-item="' + icon + '"><img src="items/' + icon + '.png"></div>';
elms += item;
if (x < 3) firstThreeElms += item; // Backup the first three items because the last three must be the same
}
col.innerHTML = elms + firstThreeElms;
}
}
/**
* Called when the start-button is pressed.
*
* @param elem The button itself
*/
function spin(elem) {
let duration = BASE_SPINNING_DURATION + randomDuration();
for (let col of cols) { // set the animation duration for each column
duration += COLUMN_SPINNING_DURATION + randomDuration();
col.style.animationDuration = duration + "s";
}
// disable the start-button
elem.setAttribute('disabled', true);
// set the spinning class so the css animation starts to play
document.getElementById('container').classList.add('spinning');
// set the result delayed
// this would be the right place to request the combination from the server
window.setTimeout(setResult, BASE_SPINNING_DURATION * 1000 / 2);
window.setTimeout(function () {
// after the spinning is done, remove the class and enable the button again
document.getElementById('container').classList.remove('spinning');
elem.removeAttribute('disabled');
}.bind(elem), duration * 1000);
}
/**
* Sets the result items at the beginning and the end of the columns
*/
function setResult() {
for (let col of cols) {
// generate 3 random items
let results = [
getRandomIcon(),
getRandomIcon(),
getRandomIcon()
];
let icons = col.querySelectorAll('.icon img');
// replace the first and last three items of each column with the generated items
for (let x = 0; x < 3; x++) {
icons[x].setAttribute('src', 'items/' + results[x] + '.png');
icons[(icons.length - 3) + x].setAttribute('src', 'items/' + results[x] + '.png');
}
}
}
function getRandomIcon() {
return ICONS[Math.floor(Math.random() * ICONS.length)];
}
/**
* @returns {number} 0.00 to 0.09 inclusive
*/
function randomDuration() {
return Math.floor(Math.random() * 10) / 100;
}