-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (55 loc) · 2.22 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
const Game = require("./game");
const GameView = require("./gameview");
const secretKey = process.env.SECRET_KEY;
document.addEventListener("DOMContentLoaded", () => {
const canvas = document.getElementById("game");
if(canvas !== null){
canvas.width = 640;
canvas.height = 500;
ctx = canvas.getContext("2d");
fetchScores();
const game = new Game(canvas.height, canvas.width);
const gameView = new GameView(game, ctx, canvas.height, canvas.width).start();
}else{
const canvas = document.getElementById("mobilegame");
canvas.width = 640;
canvas.height = 700;
ctx = canvas.getContext("2d");
const game = new Game(canvas.height, canvas.width);
const gameView = new GameView(game, ctx, canvas.height, canvas.width).start();
}
});
fetchScores = (newScore) => {
document.getElementById('table').innerHTML = "<tr>LOADING SCORES...</tr>";
fetch('/api/scores').then(res =>
res.json()
)
.then( response => {
let scores = (response);
if(newScore && newScore.score < scores[scores.length-1].score && scores.length === 10){
scores = scores.concat(newScore);}
let tableEntries = "";
scores.forEach(score => {
let date = new Date(score.date)
date = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
if(newScore && score._id === newScore._id){
tableEntries += ' <tr style="color: orange;" ><td>' + score.score + '</td><td>' + score.name + '</td><td>' + date + '</td></tr>';
} else{
tableEntries += ' <tr><td>' + score.score + '</td><td>' + score.name + '</td><td>' + date + '</td></tr>'}
});
document.getElementById('table').innerHTML = tableEntries;
});
};
fetchPost = (name, score) => {
fetch("/api/scores", {
method: "POST",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: name, score: score, secretKey: "LC2crxs6iMZHNw9yUpyy"})
}).then(res =>
res.json()
).then(
(newScore)=> {
fetchScores(newScore);
}
);
};