-
Notifications
You must be signed in to change notification settings - Fork 5
/
Login.js
executable file
·191 lines (164 loc) · 4.85 KB
/
Login.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
import React, { Component } from "react";
import { View, Text, TextInput, TouchableOpacity, Alert } from "react-native";
import Pusher from "pusher-js/react-native";
const pusher_app_key = "YOUR PUSHER APP KEY";
const pusher_app_cluster = "YOUR PUSHER APP CLUSTER";
const base_url = "YOUR HTTPS NGROK URL";
class LoginScreen extends Component {
static navigationOptions = {
title: "Login"
};
state = {
username: "",
enteredGame: false
};
constructor(props) {
super(props);
this.pusher = null;
this.myChannel = null;
}
render() {
return (
<View style={styles.wrapper}>
<View style={styles.container}>
<View style={styles.main}>
<View>
<Text style={styles.label}>Enter your username</Text>
<TextInput
style={styles.textInput}
onChangeText={username => this.setState({ username })}
value={this.state.username}
/>
</View>
{!this.state.enteredGame && (
<TouchableOpacity onPress={this.enterGame}>
<View style={styles.button}>
<Text style={styles.buttonText}>Login</Text>
</View>
</TouchableOpacity>
)}
{this.state.enteredGame && (
<Text style={styles.loadingText}>Loading...</Text>
)}
</View>
</View>
</View>
);
}
enterGame = async () => {
const username = this.state.username;
if (username) {
this.setState({
enteredGame: true
});
this.pusher = new Pusher(pusher_app_key, {
authEndpoint: `${base_url}/pusher/auth`,
cluster: pusher_app_cluster,
auth: {
params: { username: username }
},
encrypted: true
});
this.myChannel = this.pusher.subscribe(`private-user-${username}`);
this.myChannel.bind("pusher:subscription_error", status => {
Alert.alert(
"Error",
"Subscription error occurred. Please restart the app"
);
});
this.myChannel.bind("pusher:subscription_succeeded", () => {
this.myChannel.bind("opponent-found", data => {
let opponent =
username == data.player_one ? data.player_two : data.player_one;
const playerOneObjects = {
plank: "plankOne",
wall: "leftWall",
plankColor: "green"
};
const playerTwoObjects = {
plank: "plankTwo",
wall: "rightWall",
plankColor: "blue"
};
const isPlayerOne = username == data.player_one ? true : false;
const myObjects = isPlayerOne ? playerOneObjects : playerTwoObjects;
const opponentObjects = isPlayerOne
? playerTwoObjects
: playerOneObjects;
const myPlank = myObjects.plank;
const myPlankColor = myObjects.plankColor;
const opponentPlank = opponentObjects.plank;
const opponentPlankColor = opponentObjects.plankColor;
const myWall = myObjects.wall;
const opponentWall = opponentObjects.wall;
Alert.alert("Opponent found!", `Your plank color is ${myPlankColor}`);
this.opponentChannel = this.pusher.subscribe(
`private-user-${opponent}`
);
this.opponentChannel.bind("pusher:subscription_error", data => {
console.log("Error subscribing to opponent's channel: ", data);
});
this.opponentChannel.bind("pusher:subscription_succeeded", () => {
this.props.navigation.navigate("Game", {
pusher: this.pusher,
username: username,
myChannel: this.myChannel,
opponentChannel: this.opponentChannel,
opponent: opponent,
isPlayerOne: isPlayerOne,
myPlank: myPlank,
opponentPlank: opponentPlank,
myPlankColor: myPlankColor,
opponentPlankColor: opponentPlankColor,
myWall: myWall,
opponentWall: opponentWall
});
});
this.setState({
username: "",
enteredGame: false
});
});
});
}
};
}
export default LoginScreen;
const styles = {
wrapper: {
flex: 1
},
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
padding: 20,
backgroundColor: "#FFF"
},
fieldContainer: {
marginTop: 20
},
label: {
fontSize: 16
},
textInput: {
height: 40,
marginTop: 5,
marginBottom: 10,
borderColor: "#ccc",
borderWidth: 1,
backgroundColor: "#eaeaea",
padding: 5
},
button: {
alignSelf: "center",
marginTop: 10
},
buttonText: {
fontSize: 18,
color: "#05a5d1"
},
loadingText: {
alignSelf: "center"
}
};