-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelect.js
286 lines (278 loc) · 9.49 KB
/
Select.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict';
var React = require('react-native');
var {
StyleSheet,
Text,
View,
Image,
Component,
ListView,
TouchableHighlight,
} = React;
//pull in team images
var TeamImages = require('./TeamImages');
var ParseHelper = require('./ParseHelper');
//make self variable to keep track of this in the different methods
var self
class Select extends Component{
constructor(props){
super(props);
//set self to this to use in later code
self = this;
//set state object
this.state = {
gameData: props.gameData,
images: TeamImages,
awaySelected:props.gameData.Selection == 'AwayTeam',
homeSelected:props.gameData.Selection == 'HomeTeam',
showError:false,
isDouble:props.gameData.isDouble,
selectedAway:[],
selectedHome:[],
league:props.league
}
}
//when the componet is loaded
componentDidMount(){
//create arrays that will hold the other users selected values
let selectedHome = [];
let selectedAway = [];
//call parse helper method that gets other users picks
ParseHelper.getOthersPicks(this.state.gameData.objectID,this.state.league)
.then(results =>{
//for each of the picks decide if the user goes in the home or away selected arrays
results.forEach(n=>{
if (n[1] == "AwayTeam") {
//add user
selectedAway.push(n[0]);
//add blank spot to other array
selectedHome.push("");
}
else{
//add user
selectedHome.push(n[0]);
//add blank spot to other array
selectedAway.push("");
}
})
//set the arrays to the state object
this.setState({
selectedAway:selectedAway,
selectedHome:selectedHome
})
})
}
//function to select the team and save it to parse either as winner or use selection
selectAndSave(selection){
//if the user comes in as admin then store as winner
if (this.props.actAsAdmin) {
//figure out which was selected
if (selection == 'AwayTeam') {
this.setState({awaySelected:true, homeSelected:false});
}else{
this.setState({awaySelected:false, homeSelected:true});
}
//call parse method to save the result as winner
ParseHelper.saveResult(this.state.gameData.objectID, selection, this.returnToGame);
}
else{
//make sure the game hasn't started
if(Date.now()< new Date(this.state.gameData.GameTime)){
//figure out which was selected
if (selection == 'AwayTeam') {
this.setState({awaySelected:true, homeSelected:false});
}else{
this.setState({awaySelected:false, homeSelected:true});
}
//call parse method to save the result as a selection
ParseHelper.saveSelection(this.state.gameData.objectID, this.state.gameData.SelectionId, selection, this.state.isDouble, this.returnToGame);
}
//if the game has started show error saying game can't be saved
else{
this.setState({showError:true})
}
};
}
//function to be used as callback function to go back to games screen
returnToGame(savedId, selection){
//require games again, needs to be in this function otherwise won't work
var Games = require('./Games');
// use transition to get to games component
self.props.navigator.replacePreviousAndPop({
title: 'Week '+ self.state.gameData.Week,
component: Games,
passProps:{
week: self.state.gameData.Week,
update:(selection != self.props.gameData.Selection||self.state.isDouble != self.state.gameData.isDouble),
updatedSelection:selection,
updatedGame:self.state.gameData.objectID,
selectionId:savedId,
actAsAdmin:self.props.actAsAdmin,
isDouble:self.state.isDouble,
league:self.state.league
}
});
}
//function to select the team as double
doDouble(){
//make sure there is a selection
if (!this.state.awaySelected && !this.state.homeSelected) {
//set state to show error if no selection is made
this.setState({
showDoubleError:true,
})
}
else{
//figure out what the selection was
var selection = this.state.awaySelected ? "AwayTeam" : "HomeTeam"
//make sure the game hasn't been started
if(Date.now()< new Date(this.state.gameData.GameTime)){
//if its not already a double
if (!this.state.isDouble) {
//check if the team has already been selected
ParseHelper.checkIfDoubleIsLegal(this.state.gameData[selection])
.then((cont)=>{
//if we get a result then the team is legal and we can continue to add it to the array
if (cont) {
//call parse helper function to cadd the team to the user's double array
return ParseHelper.changeDoubleArray(true,this.state.gameData[selection])
}
else{
//else throw an error
throw{message:"Double already exists"};
}
})
.then(()=>{
//call parse helper method to set the current game as a
return ParseHelper.setDouble(this.state.gameData.Week, this.state.gameData.SelectionId)
})
.then(()=>{
//change the state of this game
self.state.isDouble=!this.state.isDouble;
//run callback Function to go to the games screen with the new info
this.returnToGame(this.state.gameData.SelectionId,selection);
}).catch(err=>{
//set state to show error
this.setState({showDoubleSelectedError:true})
});
}
else{
//undo the double
ParseHelper.setDouble(this.state.gameData.Week, "", () => {
//change the state of the game
self.state.isDouble = !this.state.isDouble
//run callback function to go to the games screen with the new info
this.returnToGame(this.state.gameData.SelectionId, selection);
})
}
}
else{
//show error that game has already stated.
this.setState({showError:true})
}
}
}
render(){
var possibleError = <View/>
//error that game has already started
if (this.state.showError) {
possibleError = <Text style = {styles.errorText}>There was an error when trying to save the selection. Is it past the start time?</Text>
};
//error that a selection hasn't been made before choosing a double
if(this.state.showDoubleError){
possibleError = <Text style = {styles.errorText}>Please select who you think will win before selecting this as your double</Text>
}
//error that team has already been selected
if (this.state.showDoubleSelectedError) {
possibleError = <Text style = {styles.errorText}>You have already selected that team as a double</Text>
};
var image;
//get the double star image
if (this.state.isDouble) {
image = require('image!StarSelected')
}
else{
image = require('image!Star')
};
return(
<View style = {{flex:1,flexDirection:'column'}}>
<View style = {{top:60, paddingBottom:60, paddingTop:15,alignItems:'center'}}>
<TouchableHighlight
onPress = {() =>this.doDouble()}
underlayColor = '#fff'>
<Image source = {image} style = {{ height:60, width:60}}/>
</TouchableHighlight>
<Text>Double</Text>
{possibleError}
</View>
<View style = {styles.container}>
<View style = {styles.teamContainer}>
<Text style = {styles.teamText}>{this.state.gameData.AwayTeam}</Text>
<TouchableHighlight
onPress= {() => this.selectAndSave('AwayTeam')}
underlayColor = '#F5FCFF'
>
<Image
source = {this.state.images[this.state.gameData.AwayTeam]}
style = {[styles.pic, this.state.awaySelected && styles.selected]} />
</TouchableHighlight>
{this.state.selectedAway.map(n =>{
return <Text style = {{padding:10, fontSize:15}}>{n}</Text>
})}
</View>
<Text style = {{fontSize:60, padding:10, top:10}}>@</Text>
<View style = {styles.teamContainer}>
<Text style = {styles.teamText}>{this.state.gameData.HomeTeam}</Text>
<TouchableHighlight
onPress= {() => this.selectAndSave('HomeTeam')}
underlayColor = '#F5FCFF'
>
<Image
source = {this.state.images[this.state.gameData.HomeTeam]}
style = {[styles.pic, this.state.homeSelected &&styles.selected]} />
</TouchableHighlight>
{this.state.selectedHome.map(n =>{
return <Text style = {{padding:10, fontSize:15}}>{n}</Text>
})}
</View>
</View>
</View>
)
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
flexDirection:'row',
justifyContent: 'center',
alignItems:'flex-start',
},
teamText:{
paddingBottom:10,
fontSize:20
},
teamContainer:{
flex:1,
justifyContent:'center',
alignItems:'center',
},
pic:{
height:120,
width:120,
borderRadius:60,
backgroundColor:'#333'
},
selected:{
borderWidth:8,
borderColor:"#45dd55"
},
errorText:{
borderWidth:1,
margin:10,
padding:5,
borderRadius:10,
color:'red',
borderColor:'red'
}
});
module.exports = Select;