-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
185 lines (160 loc) · 4.72 KB
/
game.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
let coins = 0;
let gems = 0;
let clicks = 0;
const clicksToMine = 100;
const helpers = [];
let coinValue = 0.5;
let chart;
let helperCount = 0;
let previousCoinValue = coinValue;
function onClick() {
clicks++;
document.getElementById("clicks-left").innerText = clicksToMine - clicks;
if (clicks >= clicksToMine) {
mineCoin();
}
}
function mineCoin() {
const lastBlock = blockchain.lastBlock();
const lastProof = lastBlock.proof;
const proof = blockchain.proofOfWork(lastProof);
blockchain.newTransaction("0", "user", 1);
const previousHash = blockchain.hash(lastBlock);
const newBlock = blockchain.newBlock(proof, previousHash);
coins++;
document.getElementById("coins-count").innerText = coins;
clicks = 0;
document.getElementById("clicks-left").innerText = clicksToMine - clicks;
addBlockToTable(newBlock);
triggerCoinAnimation();
}
function addBlockToTable(block) {
const table = document
.getElementById("blockchain-table")
.getElementsByTagName("tbody")[0];
const row = table.insertRow();
const cell1 = row.insertCell(0);
const cell2 = row.insertCell(1);
const cell3 = row.insertCell(2);
const cell4 = row.insertCell(3);
const cell5 = row.insertCell(4);
const cell6 = row.insertCell(5);
cell1.innerHTML =
'<button class="btn btn-danger" onclick="sellCoin(this, ' +
block.index +
')">Sell</button>';
cell2.innerHTML = block.index;
cell3.innerHTML = new Date(block.timestamp).toLocaleString();
cell4.innerHTML = JSON.stringify(block.transactions);
cell5.innerHTML = block.proof;
cell6.innerHTML = block.previous_hash;
}
function sellCoin(button, blockIndex) {
button.parentElement.parentElement.remove();
coins--;
gems += Math.round(coinValue * 10) / 10;
document.getElementById("coins-count").innerText = coins;
document.getElementById("gems-count").innerText = gems.toFixed(1);
}
function sellAllCoins() {
gems += Math.round(coins * coinValue * 10) / 10;
coins = 0;
document.getElementById("coins-count").innerText = coins;
document.getElementById("gems-count").innerText = gems.toFixed(1);
clearBlockchainTable();
}
function clearBlockchainTable() {
const table = document
.getElementById("blockchain-table")
.getElementsByTagName("tbody")[0];
while (table.rows.length > 0) {
table.deleteRow(0);
}
}
function triggerCoinAnimation() {
const animationLeft = document.getElementById("mine-animation-left");
const animationRight = document.getElementById("mine-animation-right");
animationLeft.style.display = "inline";
animationRight.style.display = "inline";
setTimeout(() => {
animationLeft.style.display = "none";
animationRight.style.display = "none";
}, 1000);
}
function buyHelper() {
if (gems >= 3) {
gems -= 3;
document.getElementById("gems-count").innerText = gems.toFixed(1);
helperCount++;
document.getElementById("helper-count").innerText = helperCount;
const helper = document.createElement("div");
helper.classList.add("helper");
helper.innerText = "🤖";
document.getElementById("helpers").appendChild(helper);
helpers.push(helper);
setInterval(() => {
onClick();
}, 1000);
} else {
alert("Not enough gems to buy a helper!");
}
}
function updateCoinValue() {
// Generate a random value between 0.1 and 1, rounding to the nearest tenth
previousCoinValue = coinValue;
coinValue = Math.round((Math.random() * 0.9 + 0.1) * 10) / 10;
const priceElement = document.getElementById("current-price");
const arrowElement = document.getElementById("price-arrow");
priceElement.innerText = coinValue.toFixed(1);
if (coinValue > previousCoinValue) {
arrowElement.innerHTML = "🚀";
arrowElement.style.color = "green";
} else if (coinValue < previousCoinValue) {
arrowElement.innerHTML = "🔻";
arrowElement.style.color = "red";
} else {
arrowElement.innerHTML = "-";
}
updateChart();
}
function updateChart() {
const labels = chart.data.labels;
const data = chart.data.datasets[0].data;
if (labels.length >= 20) {
labels.shift();
data.shift();
}
labels.push("");
data.push(coinValue);
chart.update();
}
function initializeChart() {
const ctx = document.getElementById("coinValueChart").getContext("2d");
chart = new Chart(ctx, {
type: "line",
data: {
labels: [],
datasets: [
{
label: "Coin Value",
data: [],
borderColor: "rgba(75, 192, 192, 1)",
borderWidth: 2,
fill: false,
},
],
},
options: {
scales: {
y: {
beginAtZero: true,
max: 1,
},
},
},
});
}
document.addEventListener("DOMContentLoaded", (event) => {
initializeChart();
setInterval(updateCoinValue, 2000);
});