-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
227 lines (208 loc) · 5.27 KB
/
script.js
File metadata and controls
227 lines (208 loc) · 5.27 KB
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
let playerList = [];
const cellSize = 25;
const rows = 15;
const columns = 35;
let cpt = 0;
let gridGame = Array(rows + 2).fill().map(() => Array(columns + 2).fill(0));
let nbAlive = 0;
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const bar = document.getElementById("progress");
let divAlive = document.getElementById('nbAlive');
const table = document.getElementById("leaderboard");
const tbody = document.getElementById("tbody");
class Player {
constructor(name, score = 0) {
this.name = name;
this.score = score;
}
getName() {
return this.name;
}
getScore() {
return this.score;
}
play() {
canvas.style.pointerEvents = "none";
loop();
cpt = 0;
updateScore(this);
updateJson();
}
}
readJson();
render(true);
function readJson() {
fetch('/players.json')
.then(response => response.json())
.then(data => {
// create Player instances from the data and add them to the array
for (const playerData of data) {
console.log(typeof playerData);
const player = JSON.parse(playerData);
playerList.push(player);
}
playerList.sort((a, b) => b.score - a.score);
});
}
async function updateJson() {
const data = JSON.stringify(playerList);
fetch('./players.json', {
method: 'POST',
body: data
})
.then(response => {
if (response.ok) {
console.log('Successfully updated JSON file');
} else {
console.error('Error updating JSON file');
}
});
}
function updateScore(player) {
setTimeout(function() {
canvas.style.pointerEvents = "auto";
if (player.score < nbAlive) {
player.score = nbAlive;
}
sort_playerList();
updateLeaderboard();
}, 11000)
}
function loop() {
setTimeout(function() {
next();
render();
cpt++;
bar.style.width = (cpt * 100 / 20) + "%";
if (cpt < 20) {
loop();
}
}, 300)
}
function sort_playerList() {
playerList.sort((a, b) => b.score - a.score);
}
function updateLeaderboard() {
console.log(typeof playerList[0]);
const topPlayer = playerList.slice(0, 10);
console.log(topPlayer);
tbody.innerHTML = "";
for (let i = 0; i < topPlayer.length; i++) {
let row = document.createElement("TR");
if (i == 0) {
row.setAttribute("id", "first");
}
if (i == 1) {
row.setAttribute("id", "second");
}
if (i == 2) {
row.setAttribute("id", "third");
}
let cell1 = document.createElement("TD");
let cell2 = document.createElement("TD");
let cell3 = document.createElement("TD");
cell1.innerHTML = i + 1;
cell2.innerHTML = topPlayer[i].getName();
cell3.innerHTML = topPlayer[i].getScore();
row.appendChild(cell1);
row.appendChild(cell2);
row.appendChild(cell3);
tbody.appendChild(row);
}
table.appendChild(tbody);
}
function getPlayer(name) {
for (let i = 0; i < playerList.length; i++) {
if (playerList[i].getName() == name) {
return playerList[i];
}
}
let newPlayer = new Player(name);
playerList.push(newPlayer);
return newPlayer;
}
function render(updTable = false) {
for (let r = 1; r <= rows; r++) {
for (let c = 1; c <= columns; c++) {
if (gridGame[r][c] === 1) {
ctx.fillStyle = "#D3D3D3";
} else {
ctx.fillStyle = "#444";
}
ctx.fillRect((c - 1) * cellSize, (r - 1) * cellSize, cellSize, cellSize);
ctx.strokeRect((c - 1) * cellSize, (r - 1) * cellSize, cellSize, cellSize);
}
}
nbAlive = count();
let msg = `${nbAlive} CELLS ALIVE`;
divAlive.innerText = msg;
if (updTable) {
sort_playerList();
updateLeaderboard();
}
}
canvas.addEventListener("click", function(event) {
const rect = canvas.getBoundingClientRect();
let x = event.clientX - rect.left;
let y = event.clientY - rect.top;
console.log(`x ${x}, y ${y}`);
if (x > 10 && y > 10) {
x -= 10;
y -= 10;
const row = Math.floor(y / cellSize);
const col = Math.floor(x / cellSize);
gridGame[row + 1][col + 1] = gridGame[row + 1][col + 1] > 0 ? 0 : 1;
render();
}
});
function getState(tab, i, j) {
s = tab[i - 1][j - 1] + tab[i - 1][j] + tab[i - 1][j + 1] + tab[i][j - 1] + tab[i][j + 1] + tab[i + 1][j - 1] + tab[i + 1][j] + tab[i + 1][j + 1];
if (s == 2)
return tab[i][j];
else {
if (s == 3)
return 1;
else
return 0;
}
}
function next() {
let gridCopy = Array(rows + 2).fill().map(() => Array(columns + 2).fill(0));
for (let i = 1; i <= rows; i++) {
for (let j = 1; j <= columns; j++) {
let etat = getState(gridGame, i, j);
gridCopy[i][j] = etat;
}
}
// tableau modifie etape n+1
for (let i = 1; i <= rows; i++) {
for (let j = 1; j <= columns; j++)
gridGame[i][j] = gridCopy[i][j];
}
}
function pressPlayButton() {
const playerName = document.getElementById("pseudo").value;
if (playerName != "") {
let player = getPlayer(playerName);
player.play();
} else {
alert("You must need to enter you pseudo !");
}
}
function reset() {
run = false;
gridGame = Array(rows + 2).fill().map(() => Array(columns + 2).fill(0));
render();
}
function count() {
nb = 0;
for (let r = 1; r <= rows; r++) {
for (let c = 1; c <= columns; c++) {
if (gridGame[r][c] === 1) {
nb++;
}
}
}
return nb;
}