-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-stuff.js
85 lines (74 loc) · 1.93 KB
/
firebase-stuff.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
const firebaseConfig = {
apiKey: "AIzaSyBrAnW65FVKNAcNns3zVfa0z1MT_I0___c",
authDomain: "hacksc-538d5.firebaseapp.com",
databaseURL: "https://hacksc-538d5.firebaseio.com",
projectId: "hacksc-538d5",
storageBucket: "hacksc-538d5.appspot.com",
messagingSenderId: "548745669097"
};
const init = () => {
firebase.initializeApp(firebaseConfig);
};
const initGrid = (presetGrid, currentPos) => {
var points = [];
for (x=0; x<16; x++) {
points[x] = [];
for (y=0; y<16; y++) {
var tile = presetGrid[x][y];
points[x][y] = {
'bomb': tile.bomb,
'number': tile.number,
'revealed': tile.revealed,
'flagged': tile.flagged
};
}
}
firebase.database().ref('grids/').update({
'points': points,
'isDead': false,
'currentPosition': currentPos,
'won': false
});
// console.log(points);
};
const stepOn = (x, y) => {
firebase.database().ref('/grids/points/' + x + '/' + y).update({revealed: true});
firebase.database().ref('/grids/currentPosition').update({x: x, y: y});
};
const flagBomb = (x,y) => {
firebase.database().ref('/grids/points/' + x + '/' + y).update({flagged: true});
};
const died = (x,y) => {
firebase.database().ref('/grids').update({isDead: true});
};
const getGridState = () => {
return firebase.database().ref('/grids').once('value').then((snapshot) => {
return snapshot.val();
});
}
const checkWin = () => {
return (async function(){
var state = await getGridState();
var reveledCount = 0;
for (x=0; x<16; x++) {
for (y=0; y<16; y++) {
if (state.points[x][y].revealed == false && state.points[x][y].bomb == false)
{
return false;
}
}
}
return true;
})();
}
const updateWin = () => {
firebase.database().ref('/grids').update({won: true});
}
const resetGrid = (newGrid) => {
initGrid(newGrid);
}
const setNumBombs = (numBombs) => {
firebase.database().ref('/grids').update({numBombs: numBombs});
}
init();
var database = firebase.database();