-
Notifications
You must be signed in to change notification settings - Fork 26
/
Database.js
203 lines (183 loc) · 5.12 KB
/
Database.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
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
// @flow
import firebase from '@react-native-firebase/app';
import levels from './Levels';
export default {
/**
* Stores all the tasks that still need pushing to the database
*/
taskResults: ([]: Array<any>),
groupCompletes: ([]: Array<any>),
/**
* These functions determine whether there is a level up that needs to be popped up.
*/
pendingLvlUp: -1,
getPendingLevelUp(): any {
// $FlowFixMe[object-this-reference]
return this.pendingLvlUp;
},
setPendingLevelUp(val: number) {
// $FlowFixMe[object-this-reference]
this.pendingLvlUp = val;
},
// the max level in the app
maxLevel: 36,
/**
* Returns the current level of the user
* @returns {*}
*/
getLevel(): number {
// $FlowFixMe[object-this-reference]
return this.getLevelForExp(this.distance);
},
/**
* Returns the entire level object, mainly for showing the badge
* @returns {*}
*/
getLevelObject(): any {
// $FlowFixMe[object-this-reference]
return levels[this.getLevel()];
},
/**
* Get a custom level object based on level (shown for level ups)
* @param customLevel
* @returns {*}
*/
getCustomLevelObject(customLevel: number): {} {
return levels[customLevel];
},
/**
* Get the level for the exp
* @param exp
* @returns {number}
*/
getLevelForExp(exp: number): number {
let toReturn: number = 1;
try {
// $FlowFixMe[object-this-reference]
const parent = this;
Object.keys(levels).forEach(level => {
if (exp > levels[parent.maxLevel]) {
toReturn = parent.maxLevel;
} else if (
exp > levels[level].expRequired &&
exp < levels[parseInt(level, 10) + 1].expRequired
) {
toReturn = parseInt(level, 10);
}
});
// $FlowFixMe[object-this-reference]
if (toReturn > this.maxLevel) {
// $FlowFixMe[object-this-reference]
toReturn = this.maxLevel;
} else if (toReturn < 1) {
toReturn = 1;
}
} catch (err) {
console.log(err);
}
return toReturn;
},
/**
* Returns the firebase timestamp
* We need to make sure that the timestamps are in ISO 8601 format.
* This should look like this: "2021-07-15T15:49:00.324Z".
* Otherwise only the results, but not the timestamps will be uploaded into Firebase.
* Results without timestamps will be dropped by the back end.
*/
getTimestamp(): string {
return firebase.database().getServerTime().toISOString();
},
/**
* Handles the initial loading of the app (what we consider login)
* to see if the user has already used MapSwipe before.
* @returns {Promise}
*/
offlineGroups: ([]: Array<any>),
interval: null,
totalRequests: ({}: { ... }),
totalRequestsOutstanding2: ({}: { ... }),
totalRequestsOutstandingByGroup: ({}: { ... }),
isDownloading: ({}: { ... }),
/**
* Whether the project has any offline groups
* @param project
* @returns {boolean}
*/
hasOfflineGroups(project: string): boolean {
// $FlowFixMe[object-this-reference]
for (let i = 0; i < this.offlineGroups.length; i += 1) {
// $FlowFixMe[object-this-reference]
if (this.offlineGroups[i].indexOf(project) !== -1) {
return true;
}
}
return false;
},
/**
* Whether the project has any open downloads
* @param project
* @returns {boolean}
*/
hasOpenDownloads(project: string): boolean {
console.log(project);
return false;
},
};
/**
* Format the project progress for display
*/
export function getProjectProgressForDisplay(progress: number): string {
let r;
if (progress < 99) {
r = Math.max(0, progress).toFixed(0);
} else if (progress < 100) {
r = '99';
} else {
r = '100';
}
return r;
}
/**
* Calculate how much 1 tile is in square kilometers
* @param zoomLevel
* @returns {number}
*/
export function getSqKmForZoomLevelPerTile(zoomLevel: number): number {
if (zoomLevel === 23) {
return 2.29172838e-5;
}
if (zoomLevel === 22) {
return 9.11795814e-5;
}
if (zoomLevel === 21) {
return 0.000364718326;
}
if (zoomLevel === 20) {
return 0.00146082955;
}
if (zoomLevel === 19) {
return 0.00584331821;
}
if (zoomLevel === 18) {
return 0.0233732728; // (((0.5972 * 256) ^ 2) / (1000 ^ 2))
}
if (zoomLevel === 17) {
return 0.0934774368;
}
if (zoomLevel === 16) {
return 0.373941056;
}
if (zoomLevel === 15) {
return 1.4957016;
}
if (zoomLevel === 14) {
return 5.98280642;
}
if (zoomLevel === 13) {
return 23.9314761;
}
if (zoomLevel === 12) {
return 95.7254037;
}
return 95.72;
}