forked from HuntForCode/playa-vista-adventure
-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.js
268 lines (252 loc) · 7.72 KB
/
App.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import React from 'react';
import haversine from './haversine.js';
import { Platform, StyleSheet, Text, View, StatusBar } from 'react-native';
import { MapView, Constants, Location, Permissions, SQLite } from 'expo';
import ClueDescription from './components/ClueDescription';
import ClueOverlay from './components/ClueOverlay';
import CheckInButton from './components/CheckInButton';
import ResetButton from './components/ResetButton';
import db from './controllers/db';
import StartButton from './components/StartButton';
import dbController from './controllers/dbController';
export default class App extends React.Component {
state = {
isGameStarted: false,
clue: '',
clueId: null,
clueLocation: null,
location: null,
errorMessage: null,
distance: 0,
cluesCompleted: 0,
checkinButtonColor: 'transparent',
savedClue: false
};
//hi
componentWillMount() {
if (Platform.OS === 'android' && !Constants.isDevice) {
this.setState({
errorMessage: 'Oops, this will not work on Sketch in an Android emulator. Try it on your device!',
});
} else {
this._getLocationAsync();
this._watchPositionAsync();
}
}
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied',
});
}
let location = await Location.getCurrentPositionAsync({});
this.setState({ location });
};
//always gets current position
_watchPositionAsync = async () => {
await Location.watchPositionAsync({ enableHighAccuracy: true, distanceInterval: 4 },
(location) => {
this.setState({ location });
});
};
_getSavedClue = () => {
db.transaction(tx => {
tx.executeSql(`SELECT * FROM user
INNER JOIN clue
ON user.curr_clue=clue.location_id
INNER JOIN location
ON clue.location_id=location.id;`, [],
(_, result) => {
console.log("success getting saved clue", result);
if (result.rows.length) {
let record = result.rows.item(0);
console.log('record', record)
this.setState({
isGameStarted: true,
clueId: record.id,
clue: record.description,
cluesCompleted: record.id - 1,
clueLocation: {
latitude: record.latitude,
longitude: record.longitude,
placename: record.place_name,
radius: record.radius
}
});
}
},
(_, err) => console.log("error getting new clue", err)
)
})
}
_getNewClue = () => {
db.transaction(tx => {
tx.executeSql(`SELECT *
FROM clue INNER JOIN location
ON clue.location_id = location.id
WHERE completed = 0;`, [],
(_, result) => {
if (result.rows.length) {
let record = result.rows.item(this.state.cluesCompleted);
this.setState({
isGameStarted: true,
savedClue: true,
clue: record.description,
clueId: record.id,
clueLocation: {
latitude: record.latitude,
longitude: record.longitude,
placename: record.place_name,
radius: record.radius
}
});
}
},
(_, err) => console.log("error getting new clue", err)
);
});
};
_startPressed = () => {
console.log('start pressed!');
dbController.populate();
this._getSavedClue();
if (this.state.savedClue) {
console.log('no saved clue')
this._getNewClue();
}
this.setState({ isGameStarted: true });
};
_checkInPressed = () => {
console.log('check in pressed!');
db.transaction(tx => {
tx.executeSql(`SELECT * FROM user`, [], (_, result) => console.log('user table content -->', result))
})
this._getLocationAsync();
const distance = haversine.getDistance(this.state.location.coords.latitude, this.state.location.coords.longitude, this.state.clueLocation.latitude, this.state.clueLocation.longitude);
this.setState({ distance })
if (distance <= this.state.clueLocation.radius) {
this._getNewClue();
let completed = this.state.cluesCompleted;
completed++;
this.setState({ cluesCompleted: completed, checkinButtonColor: 'green' });
this._toggleColor();
db.transaction(tx => {
tx.executeSql(`UPDATE user
SET curr_clue = ?;`, [++completed],
(_, result) => console.log('updated curr_clue in user table', result),
(_, err) => console.log("error updating user table", err)
);
});
}
else {
console.log('location not found!');
this.setState({checkinButtonColor: 'red'});
this._toggleColor();
}
};
_resetPressed = () => {
db.transaction(tx => {
tx.executeSql(`UPDATE user
SET curr_clue = ?;`, [1],
(_, result) => console.log('updated curr_clue in user table', result),
(_, err) => console.log("error updating user table", err)
);
});
this.setState({isGameStarted: false,
cluesCompleted: 0,
checkinButtonColor: 'transparent'
});
}
_toggleColor = () => {
setTimeout(() => {
this.setState({checkinButtonColor: 'transparent'})
}, 500)
}
render() {
if (this.state.location == null) {
return (<View style={styles.container} />);
}
else {
return (
<View style={styles.container}>
<StatusBar hidden />
<MapView
style={styles.mapView}
provider={'google'}
region={{
latitude: this.state.location.coords.latitude,
longitude: this.state.location.coords.longitude,
latitudeDelta: 0,//0.0922,
longitudeDelta: 0.01//0.0421,
}}
>
<MapView.Marker
image={require('./assets/cat.png')}
coordinate={{
latitude: this.state.location.coords.latitude,
longitude: this.state.location.coords.longitude
}}
/>
</MapView>
{
this.state.isGameStarted
? null
: <StartButton
style={styles.startButton}
startGame={this._startPressed}
/>
}
{
this.state.isGameStarted &&
<CheckInButton style={styles.checkInButton} checkIn={this._checkInPressed} checkinButtonColor={this.state.checkinButtonColor} />
}
{
this.state.isGameStarted &&
<ClueOverlay style={styles.clueOverlay} clue={this.state.clue} cluesCompleted={this.state.cluesCompleted} />
}
{
this.state.isGameStarted &&
<ResetButton style={styles.resetButton} reset={this._resetPressed} />
}
</View>
);
}
}
}
//stylesheet for react-native
const styles = StyleSheet.create({
container: {
flex: 1
},
mapView: {
flex: 30
},
clueOverlay: {
height: 80,
backgroundColor: '#01579B',
},
checkInButton: {
height: 80,
width: 80,
borderRadius: 40,
position: 'absolute',
bottom: 100,
alignSelf: 'center'
},
resetButton: {
position: 'absolute',
top: 0,
left: 0,
height: 40,
width: 80,
backgroundColor: 'purple'
},
startButton: {
height: 70,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#FFB6C1'
}
});