-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignUpManager.cs
210 lines (183 loc) · 7.51 KB
/
SignUpManager.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.Networking;
using Firebase;
using Firebase.Unity.Editor;
using Firebase.Database;
public class SignUpManager : MonoBehaviour {
public GameObject mainCamera;
public GameObject nextButton;
private string username = "";
private string email = "";
private string password = "";
public GameObject nextAvatarButton;
public GameObject prevAvatarButton;
private bool inCharacterSelectionScene = false;
public GameObject AvatarSelection;
public GameObject usernameInput, emailInput, passwordInput, errorDisplay, loadingIndicator, inputBackdrop;
public GameObject background, chooseAvatarSign, chooseAvatarBackdrop;
private int currentInput = 0;
private int usernameConfirmed = -1; // -1 is stand by , 0 is error 1 is success
private bool loading = false;
void Awake() {
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://spaces-d9a3c.firebaseio.com/");
string currentCharacter = PlayerPrefs.GetString("CurrentSkin");
if (currentCharacter != "") {
SceneManager.LoadScene("MainGame");
}
}
void Start() {
}
public void UpdateInput(string input) {
if (currentInput == 0) {
username = input;
} else if (currentInput == 1) {
email = input;
} else {
password = input;
}
}
public void NextInput() {
if (inCharacterSelectionScene) {
string characterSelected = AvatarSelection.GetComponent<AvatarCreation>().SelectedCharacter();
PlayerPrefs.SetString("CurrentSkin", characterSelected);
PlayerPrefs.SetString("myWorldType", "MainGame");
nextAvatarButton.SetActive(false);
prevAvatarButton.SetActive(false);
nextButton.SetActive(false);
chooseAvatarBackdrop.SetActive(false);
chooseAvatarSign.SetActive(false);
StartCoroutine(mainCamera.GetComponent<CameraTour>().MoveCameraToWorld(characterSelected));
return;
}
if (currentInput == 0) {
StartCoroutine(VerifyUsername());
CheckUsername();
nextButton.SetActive(false);
loadingIndicator.transform.parent.gameObject.SetActive(true);
errorDisplay.SetActive(false);
loading = true;
} else if (currentInput == 1) {
emailInput.SetActive(false);
passwordInput.SetActive(true);
currentInput = 2;
} else {
Debug.Log(email);
Debug.Log(password);
loading = true;
nextButton.SetActive(false);
loadingIndicator.transform.parent.gameObject.SetActive(true);
StartCoroutine(MakeRequest("create-account", username.ToLower(), password, NextSceneCallBack, errorDisplay));
}
}
public void CheckUsername() {
DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
reference.Child("usernameList").GetValueAsync().ContinueWith(task => {
DataSnapshot snapshot = task.Result;
Dictionary<string, object> usernames = snapshot.Value as Dictionary<string, object>;
foreach(KeyValuePair<string, object> user in usernames) {
if (username == user.Key) {
Debug.Log("duplicate");
usernameConfirmed = 0;
return;
}
usernameConfirmed = 1;
}
});
}
public IEnumerator VerifyUsername() {
// stop loading indicator
while (usernameConfirmed == -1) {
yield return null;
}
if (usernameConfirmed == 0) {
//error
errorDisplay.SetActive(true);
usernameConfirmed = -1;
} else {
// success
usernameInput.SetActive(false);
emailInput.SetActive(true);
currentInput = 1;
}
loading = false;
nextButton.SetActive(true);
loadingIndicator.transform.parent.gameObject.SetActive(false);
}
void Update() {
if (loading) {
loadingIndicator.transform.RotateAround(loadingIndicator.transform.position, new Vector3(0 , 0, 1), 2f);
} else {
loadingIndicator.transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
public void NextSceneCallBack() {
loading = false;
nextButton.SetActive(true);
loadingIndicator.transform.parent.gameObject.SetActive(false);
prevAvatarButton.SetActive(true);
nextAvatarButton.SetActive(true);
background.SetActive(false);
passwordInput.SetActive(false);
inputBackdrop.SetActive(false);
chooseAvatarSign.SetActive(true);
chooseAvatarBackdrop.SetActive(true);
inCharacterSelectionScene = true;
SetFirebaseProfile();
}
public void SetFirebaseProfile() {
Dictionary<string, object> requests = new Dictionary<string, object>(){};
Dictionary<string, object> coinsInfo = new Dictionary<string, object>(){
{"LastRequest", "none"},
{"ConsecutiveDays", "0"}
};
Dictionary<string, object> user = new Dictionary<string, object>
{
{ "id", PlayerPrefs.GetString("myRoomID").ToLower() },
{ "requests", requests},
{"coins", "0"},
{"coinsInfo", coinsInfo}
};
DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
reference.Child("users").Child(username.ToLower()).SetValueAsync(user);
OSPermissionSubscriptionState status = OneSignal.GetPermissionSubscriptionState();
string oneSignalID = (status.subscriptionStatus.userId != null) ? status.subscriptionStatus.userId : "1";
Dictionary<string, object> usernameList = new Dictionary<string, object> {
{username.ToLower(), oneSignalID}
};
reference.Child("usernameList").UpdateChildrenAsync(usernameList);
}
public delegate void GoToNextScene();
static IEnumerator MakeRequest(string url, string username, string password, GoToNextScene callback, GameObject errorButton) {
WWWForm form = new WWWForm();
form.AddField("username", username);
form.AddField("password", password);
UnityWebRequest www = UnityWebRequest.Post("https://circles-parellano.herokuapp.com/api/" + url, form);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
string response = www.downloadHandler.text;
yield return response;
Debug.Log(response);
JsonResponse data = JsonUtility.FromJson<JsonResponse>(response);
if (data.success == "false") {
Debug.Log(data.message);
errorButton.SetActive(true);
} else {
OneSignal.SetExternalUserId(data.userID.ToString());
PlayerPrefs.SetString("myRoomID", data.userID);
PlayerPrefs.SetString("currentWorldType", "MainGame");
PlayerPrefs.SetString("currentRoomID", data.userID);
PlayerPrefs.SetString("username", username.ToLower());
PlayerPrefs.SetString("accessories", "");
callback();
}
}
}
}