-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
192 lines (166 loc) · 5.63 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
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
var cardDestin;
var cardOrigin;
window.onload = () => {
insertDefaultsCards();
initCardMachine();
renderWallet();
};
function initCardMachine() {
const btnElements = document.getElementsByClassName("btn");
for (let btnElement of btnElements) {
btnElement.addEventListener("click", (e) => {
playAudio();
const mOrK = document.querySelector(".mk");
const textElement = document.getElementById("display-input");
let txt = textElement.textContent;
const dataKey = btnElement.getAttribute("data-key");
if (dataKey == "Backspace") {
mOrK.textContent = null;
textElement.textContent = "0";
} else if (!!parseInt(txt) && ["m", "k"].includes(dataKey)) {
mOrK.textContent = dataKey;
if (dataKey == "m") {
let value = txt.includes(".") ? txt + "00" : txt + "000";
textElement.textContent = value;
}
setTimeout(async () => {
const cardDestinyElement = document.querySelector(
".card-control.insert-left .card"
);
const cardOriginElement = document.querySelector(
".card-control.insert-right .card"
);
if (!cardDestinyElement) {
alert("Escolha um cartão para receber o valor");
} else if (!cardOriginElement) {
alert("Escolha um cartão para enviar o valor");
} else {
console.log("transfere " + textElement.textContent);
const value = parseFloat(textElement.textContent);
const cardOriginOld = getCardStorage(cardOriginElement.id);
const cardDestinyOld = getCardStorage(cardDestinyElement.id);
transfere(cardOriginElement.id, cardDestinyElement.id, value);
const cardDestinyNew = getCardStorage(cardDestinyElement.id);
const cardOriginNew = getCardStorage(cardOriginElement.id);
await animateValueAsync(
textElement,
cardOriginOld.saldo,
cardOriginNew.saldo
);
await animateValueAsync(
textElement,
cardDestinyOld.saldo,
cardDestinyNew.saldo
);
// await showBalance(cardOriginElement.id, textElement);
// await showBalance(cardDestinyElement.id, textElement);
textElement.textContent = "0";
mOrK.textContent = null;
}
}, 1500);
} else if ([...Array(10).keys(), "."].map(String).includes(dataKey)) {
if (txt == 0) txt = "";
let value = txt + btnElement.getAttribute("data-key");
if (value.length < 10) {
textElement.textContent = value;
} else {
alert("coloca validacao do tamanho max de n");
}
}
});
}
document.addEventListener("keyup", (e) => {
const keyElement = document.querySelector(`[data-key='${e.key}']`);
if (keyElement) {
keyElement.classList.add("active");
keyElement.click();
}
});
}
function renderWallet() {
const walletElement = document.getElementById("wallet");
const cards = getCards();
for (let i in cards) {
const card = cards[i];
const cardElement = createCardElement(card);
cardElement.classList.add("card-bg-" + i);
cardElement.onclick = function () {
const clone = cardElement.cloneNode(true);
if (!cardOrigin || !cardDestin) {
cardElement.style.display = "none";
}
if (!cardDestin) {
cardDestin = true;
insertCardLeft(clone);
clone.onclick = () => {
cardDestin = false;
cardElement.style.display = "block";
removeCardLeft(clone);
};
} else if (cardDestin && !cardOrigin) {
cardOrigin = true;
insertCardRight(clone);
clone.onclick = () => {
cardOrigin = false;
cardElement.style.display = "block";
removeCardRight(clone);
};
}
};
walletElement.appendChild(cardElement);
}
}
async function playAudioAsync(x = 1, duration) {
for (let i = 0; i < x; i++) {
console.log("gug", i);
await playAudio(duration);
}
}
async function playAudio(duration) {
const clickAudio = document.getElementById("click-audio");
if (!duration) duration = clickAudio.duration * 1000;
return new Promise((resolve, reject) => {
clickAudio.currentTime = 0;
clickAudio.play();
setTimeout(resolve, duration);
});
}
async function animateValueAsync(element, to, from) {
element.textContent = to;
sleep(1500);
await playAudioAsync(3, 150);
let valueTo = parseFloat(to);
let valueFrom = parseFloat(from);
const isPlus = valueFrom > valueTo;
let valueDiff = to - from;
if (valueDiff < 0) valueDiff = valueDiff * -1;
let diff = 10;
if (valueDiff > 10000) {
diff = 500;
} else if (valueDiff > 1000) {
diff = 150;
} else if (valueDiff > 100) {
diff = 100;
}
return new Promise((resolve) => {
const intervalId = setInterval(async () => {
playAudio();
valueTo = isPlus ? valueTo + diff : valueTo - diff;
element.textContent = valueTo;
if (
(isPlus && valueTo >= valueFrom) ||
(!isPlus && valueTo <= valueFrom)
) {
clearInterval(intervalId);
element.textContent = valueFrom;
await sleep(1500);
element.textContent = "-";
await sleep(800);
resolve();
}
}, 70);
});
}
async function sleep(millis) {
return new Promise((resolve) => setTimeout(resolve, millis));
}