-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.html
224 lines (202 loc) · 8 KB
/
game.html
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
<!DOCTYPE html>
<html>
<head>
<title>PythonicApple</title>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>
<style>
table, th, td {
border: 2px solid black;
border-collapse: collapse;
font-size: 1.25vw;
}
html, body {
overflow: hidden;
height: 100%;
width: 100%;
}
</style>
</head>
<body style="height: 100%; width: 100%;">
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-app.js" type="text/javascript"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-auth.js" type="text/javascript"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-database.js" type="text/javascript"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-analytics.js" type="text/javascript"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyBmWhJlX0kcXWz6FfVo3YJVRz4wxBWZs-4",
authDomain: "pythonicapple.firebaseapp.com",
databaseURL: "https://pythonicapple.firebaseio.com",
projectId: "pythonicapple",
storageBucket: "pythonicapple.appspot.com",
messagingSenderId: "710427168306",
appId: "1:710427168306:web:0415efa3eec534f69c362b",
measurementId: "G-RJT30JEZJ8"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
<form onsubmit="startGame(); return false" id="form">
<label for="username">Enter your username:</label>
<input type="text" id="username">
<button type="submit">Start Game</button>
</form>
<button onclick="window.location.href = '/'">Need the rules?</button>
<div id="tablediv" style="margin:0 auto; position:absolute; top:25%; left:25%; height:50%; width:50%">
</div>
<script>
var ip;
function getIP(json) {
ip = json.ip.split(".").join("_");
}
var ref = firebase.database().ref("scores");
var dataEntries = 0;
ref.once('value', function(snapshot) {
dataEntries = snapshot.numChildren();
setupHomeTable("Can you beat the top score?", "START");
});
var score = 0;
var numScores = 0;
var username;
var dispScore;
var canvas;
var divCanvas;
var tableOfScores;
var tableBody;
var tB;
var tOS;
var lowestScore = -1;
var usernames;
var scores;
var start = false;
var started = false
function setupHomeTable(tabheading, time) {
var usnas = [];
var scors = [];
var ref = firebase.database().ref("scores");
ref.once("value", function(snapshot) {
var data = JSON.parse(JSON.stringify(snapshot));
var ips = Object.keys(data);
ips.forEach(function(ipaddr, index) {
Object.keys(data[ipaddr]).forEach(function(usna) {
usnas.push(usna);
});
Object.values(data[ipaddr]).forEach(function(scor) {
scors.push(scor);
});
});
usernames = [];
scores = [];
var captured = [];
var numberOfScores = scors.length
for(var index = 0; index < numberOfScores; index++) {
var highest = -1;
var kdex = -1;
for(var jdex = 0; jdex < scors.length; jdex++) {
if(scors[jdex] > highest && !(captured.includes(jdex))) {
highest = scors[jdex];
kdex = jdex;
}
}
usernames.push(usnas[kdex]);
scores.push(highest);
captured.push(kdex);
}
if(usernames.length > 10) {
usernames.length = 10;
scores.length = 10;
}
tOS = document.createElement("TABLE");
var tH = document.createElement("THEAD");
var headRow = tH.insertRow(0);
var hc1 = document.createElement("td");
hc1.appendChild(document.createTextNode("Place"));
var hc2 = document.createElement("td");
hc2.appendChild(document.createTextNode("Username"));
var hc3 = document.createElement("td");
hc3.appendChild(document.createTextNode("Score"));
headRow.appendChild(hc1);
headRow.appendChild(hc2);
headRow.appendChild(hc3);
headRow.style.fontWeight = 'bold';
tH.style.textAlign = 'center';
var head = tH.insertRow(0);
var h = document.createElement("td");
h.appendChild(document.createTextNode(tabheading));
h.colSpan = '3';
head.appendChild(h);
head.style.fontWeight = 'bold';
tB = document.createElement("TBODY");
usernames.forEach(function(name, index) {
var row = tB.insertRow();
var c1 = document.createElement("td");
c1.appendChild(document.createTextNode(index+1));
c1.style.textAlign = 'center';
var c2 = document.createElement("td");
c2.appendChild(document.createTextNode(name));
c2.style.textAlign = 'center';
var c3 = document.createElement("td");
c3.appendChild(document.createTextNode(scores[index]));
c3.style.textAlign = 'center';
row.appendChild(c1);
row.appendChild(c2);
row.appendChild(c3);
if(time == "END" && name == username && score == scores[index]) {
row.style.backgroundColor = "green";
}
});
tOS.appendChild(tH);
tOS.appendChild(tB);
tOS.setAttribute("id", "tOS");
tOS.style.height = "100%";
tOS.style.width = "100%";
document.getElementById("tablediv").appendChild(tOS);
});
}
function startGame() {
username = document.getElementById("username").value;
var form = document.getElementById("form");
document.body.removeChild(form);
$("#tOS").remove();
dispScore = document.createElement("H1");
dispScore.innerHTML = "Score: ".concat(score);
document.body.insertBefore(dispScore, document.getElementById("importDiv"));
divCanvas = document.createElement("div");
canvas = document.createElement("CANVAS");
canvas.style.position = "relative";
canvas.id = "canvas";
divCanvas.appendChild(canvas);
document.body.insertBefore(divCanvas, document.getElementById("importDiv"));
start = true;
}
function gameOver() {
var setData = JSON.parse("{\""+username+"\":"+score+"}");
var ref = firebase.database().ref("scores");
ref.once("value", function(snapshot) {
var data = JSON.parse(JSON.stringify(snapshot));
if(!data.hasOwnProperty(ip)) {
firebase.database().ref('scores/'+ip).set(setData);
} else if(data[ip].hasOwnProperty(username) && data[ip][username] < score) {
firebase.database().ref('scores/'+ip).update(setData);
} else if (!data[ip].hasOwnProperty(username)) {
data[ip][username] = score;
firebase.database().ref('scores/').set(data);
}
document.body.removeChild(divCanvas);
setupHomeTable("Did you get the top score?", "END");
});
}
function changeScore(newScore) {
score = newScore;
dispScore.innerHTML = "Score: ".concat(score);
}
</script>
<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
<script type="module" id="importDiv" src="snake.js">import * as snake from "./snake.js"; window.snake = snake;</script>
<style>body {visibility: visible;}</style>
</body>
</html>