-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.js
More file actions
111 lines (84 loc) · 2.61 KB
/
backend.js
File metadata and controls
111 lines (84 loc) · 2.61 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
var promptname;
var name;
var enemyname;
var punchbool=0;
var duckboool=0;
var message,alldata,name;
var config = {
apiKey: "AIzaSyCTfmsiPdffvQchUrnDwDv9m8SUSEzo1qU",
authDomain: "machine-f60e6.firebaseapp.com",
databaseURL: "https://machine-f60e6.firebaseio.com",
projectId: "machine-f60e6",
storageBucket: "machine-f60e6.appspot.com",
messagingSenderId: "829526108897"
};
firebase.initializeApp(config);
database = firebase.database();
var gamedatabase = database.ref("games");
function get(){
gamedatabase.once("value",gotData);
function gotData(results){
let data=results.val();
let keys = Object.keys(data);
alldata={
entries:[]
};
for(let key of keys){
let record=data[key];
alldata.entries.push(record);
}
console.log(alldata.entries[0]);
//PROBLEM
for (let i=(alldata.entries.length-1);i>0;i--){
if(alldata.entries[i].name === promptname){
if(alldata.entries[i].punch+alldata.entries[i].duck > (punchbool+duckboool)){
document.body.innerHTML="<center><h2>You lost!</h2></center>";
document.body.style.backgroundColor="red";
}
else{
document.body.innerHTML="<center><h2>You won!</h2></center>";
document.body.style.backgroundColor="green";
}
break;
}
}
//PROBLEM
}//ends here
}
function sendData(){
//getting data starts here
// Make an object with data in it
var data = {
punch:punchbool,
duck:duckboool,
name:name
};
console.log("saving data");
console.log(data);
let color = gamedatabase.push(data, finished);
console.log("Firebase generated key: " + color.key);
tr=true;
// Reload the data for the page
function finished(err) {
if (err) {
console.error("ooops, something went wrong.");
console.error(err);
} else {
console.log('Data saved successfully');
}
}
}
function hit(){
punchbool+=1;
console.log("punch!");
}
function duck(){
duckboool+=1;
console.log("duck");
}
function dataSend(){
name=prompt("your id?");
sendData();
promptname=prompt("what is the name of the person you played this game with?");
get();
}