Login
This commit is contained in:
97
Assets/Scripts/DataManager.cs
Normal file
97
Assets/Scripts/DataManager.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
public static class DataManager{
|
||||
public const string API_ENDPOINT = "http://vps.playpoolstudios.com/faucet/golf/api/";
|
||||
|
||||
public static UserData userData{get; private set;}
|
||||
|
||||
|
||||
public static async void Login(string username,string password){
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("username", username);
|
||||
form.AddField("password", password);
|
||||
form.AddField("key", "#2CuV1Bit^S!sW1ZcgRv8BhrO");
|
||||
|
||||
|
||||
|
||||
using (UnityWebRequest request = UnityWebRequest.Post(API_ENDPOINT + "login.php", form))
|
||||
{
|
||||
var operation = request.SendWebRequest();
|
||||
while (!operation.isDone)
|
||||
{
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
Debug.Log("login response: " +request.downloadHandler.text);
|
||||
|
||||
if(request.downloadHandler.text.Contains("{")){
|
||||
try{
|
||||
userData = JsonConvert.DeserializeObject<UserData>(request.downloadHandler.text);
|
||||
Debug.Log("Success parsing userdata");
|
||||
|
||||
}catch(Exception e){
|
||||
Debug.Log("Error parsing userdata");
|
||||
}
|
||||
}else{
|
||||
if(request.downloadHandler.text == "0"){
|
||||
userData = new UserData(){username = username};
|
||||
}else{
|
||||
MessageBox.ShowMessage("Error logging in, Server said\n" +request.downloadHandler.text);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
LoadingScreen.LoadLevel("MainMenu");
|
||||
|
||||
}
|
||||
|
||||
public static async void GoogleLogin(string username){
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("username", username);
|
||||
form.AddField("key", "#2CuV1Bit^S!sW1ZcgRv8BhrO");
|
||||
|
||||
using (UnityWebRequest request = UnityWebRequest.Post(API_ENDPOINT + "google_login.php", form))
|
||||
{
|
||||
var operation = request.SendWebRequest();
|
||||
while (!operation.isDone)
|
||||
{
|
||||
await Task.Yield();
|
||||
}
|
||||
Debug.Log("glogin response: " +request.downloadHandler.text);
|
||||
|
||||
if(request.downloadHandler.text.Contains("{")){
|
||||
try{
|
||||
userData = JsonConvert.DeserializeObject<UserData>(request.downloadHandler.text);
|
||||
Debug.Log("Success parsing userdata");
|
||||
|
||||
}catch(Exception e){
|
||||
Debug.Log("Error parsing userdata");
|
||||
}
|
||||
}else{
|
||||
if(request.downloadHandler.text == "0"){
|
||||
userData = new UserData(){username = username};
|
||||
}else{
|
||||
MessageBox.ShowMessage("Error logging in, Server said\n" +request.downloadHandler.text);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
LoadingScreen.LoadLevel("MainMenu");
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class UserData{
|
||||
public string username;
|
||||
public int score;
|
||||
}
|
||||
11
Assets/Scripts/DataManager.cs.meta
Normal file
11
Assets/Scripts/DataManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d1442743290bfc6458ee447dc97bebf1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -58,7 +58,7 @@ public class LoadingScreen : MonoBehaviour
|
||||
yield return null;
|
||||
}
|
||||
Debug.Log("Loaded scene " + levelName);
|
||||
yield return new WaitForSecondsRealtime(0.2f);
|
||||
yield return new WaitForSecondsRealtime(2f);
|
||||
|
||||
canvasGroup.blocksRaycasts = false;
|
||||
|
||||
|
||||
@@ -1,17 +1,38 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Google;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class Login : MonoBehaviour
|
||||
{
|
||||
public Button helpBtn;
|
||||
public Button googleLoginBtn;
|
||||
public Button guestLoginBtn;
|
||||
public Button[] switchBtns;
|
||||
public Text statusTxt;
|
||||
|
||||
public GameObject quickLoginPanel, guestLoginPanel;
|
||||
|
||||
public Button BtnGuestLogin;
|
||||
public InputField usernameInput, passwordInput;
|
||||
|
||||
bool quickLoginPanelOn =true;
|
||||
|
||||
void Awake(){
|
||||
helpBtn.onClick.AddListener(OnHelp);
|
||||
googleLoginBtn.onClick.AddListener(OnSignIn);
|
||||
BtnGuestLogin.onClick.AddListener(OnGuestLogin);
|
||||
// guestLoginBtn.onClick.AddListener(SwitchLoginPanel);
|
||||
foreach(Button switchBtn in switchBtns){
|
||||
switchBtn.onClick.AddListener(SwitchLoginPanel);
|
||||
}
|
||||
configuration = new GoogleSignInConfiguration {
|
||||
WebClientId = webClientId,
|
||||
RequestIdToken = true,
|
||||
RequestEmail=true,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -21,9 +42,118 @@ public class Login : MonoBehaviour
|
||||
|
||||
}
|
||||
|
||||
void OnGuestLogin(){
|
||||
if(usernameInput.text.Length < 3){
|
||||
MessageBox.ShowMessage("Please enter a username longer than 3 characters");
|
||||
return;
|
||||
}
|
||||
if(usernameInput.text.Contains("#")){
|
||||
MessageBox.ShowMessage("Characters like #,$,%,^,& can't be included in username");
|
||||
return;
|
||||
}
|
||||
|
||||
if(passwordInput.text.Length < 5){
|
||||
MessageBox.ShowMessage("Please enter a password with more than 5 characters to stay secure!");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
DataManager.Login(usernameInput.text, passwordInput.text);
|
||||
}
|
||||
|
||||
|
||||
void OnHelp(){
|
||||
MessageBox.ShowMessage("This game saves your progress in cloud, in order to preserve the data. We need you to login with either Google or Guest login.", "Why Login?");
|
||||
}
|
||||
|
||||
void SwitchLoginPanel(){
|
||||
quickLoginPanelOn = !quickLoginPanelOn;
|
||||
|
||||
LeanTween.scale(quickLoginPanel, quickLoginPanelOn ? Vector3.one : Vector3.zero, 0.5f).setEase(LeanTweenType.easeOutElastic);
|
||||
LeanTween.scale(guestLoginPanel, quickLoginPanelOn ? Vector3.zero : Vector3.one, 0.5f).setEase(LeanTweenType.easeOutElastic);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Text statusText;
|
||||
|
||||
public string webClientId = "<your client id here>";
|
||||
|
||||
private GoogleSignInConfiguration configuration;
|
||||
|
||||
public void OnSignIn() {
|
||||
GoogleSignIn.Configuration = configuration;
|
||||
GoogleSignIn.Configuration.UseGameSignIn = false;
|
||||
GoogleSignIn.Configuration.RequestIdToken = true;
|
||||
// AddStatusText("Calling SignIn");
|
||||
|
||||
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
|
||||
OnAuthenticationFinished);
|
||||
}
|
||||
|
||||
public void OnSignOut() {
|
||||
// AddStatusText("Calling SignOut");
|
||||
GoogleSignIn.DefaultInstance.SignOut();
|
||||
}
|
||||
|
||||
public void OnDisconnect() {
|
||||
// AddStatusText("Calling Disconnect");
|
||||
GoogleSignIn.DefaultInstance.Disconnect();
|
||||
}
|
||||
|
||||
internal void OnAuthenticationFinished(Task<GoogleSignInUser> task) {
|
||||
if (task.IsFaulted) {
|
||||
using (IEnumerator<System.Exception> enumerator =
|
||||
task.Exception.InnerExceptions.GetEnumerator()) {
|
||||
if (enumerator.MoveNext()) {
|
||||
GoogleSignIn.SignInException error =
|
||||
(GoogleSignIn.SignInException)enumerator.Current;
|
||||
// AddStatusText("Got Error: " + error.Status + " " + error.Message);
|
||||
MessageBox.ShowMessage("Got Error: " + error.Status + " " + error.Message);
|
||||
} else {
|
||||
// AddStatusText("Got Unexpected Exception?!?" + task.Exception);
|
||||
MessageBox.ShowMessage("Got Unexpected Exception?!?" + task.Exception);
|
||||
}
|
||||
}
|
||||
} else if(task.IsCanceled) {
|
||||
// AddStatusText("Canceled");
|
||||
|
||||
} else {
|
||||
// AddStatusText("Welcome: " + task.Result.DisplayName + "!");
|
||||
// MessageBox.ShowMessage($"email: {task.Result.Email}\nDisplay:{task.Result.DisplayName}\ntoken:{task.Result.IdToken}");
|
||||
StartCoroutine(DoneLogin(task.Result.Email));
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator DoneLogin(string username){
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
|
||||
DataManager.GoogleLogin(username);
|
||||
}
|
||||
|
||||
public void OnSignInSilently() {
|
||||
GoogleSignIn.Configuration = configuration;
|
||||
GoogleSignIn.Configuration.UseGameSignIn = false;
|
||||
GoogleSignIn.Configuration.RequestIdToken = true;
|
||||
// AddStatusText("Calling SignIn Silently");
|
||||
|
||||
GoogleSignIn.DefaultInstance.SignInSilently()
|
||||
.ContinueWith(OnAuthenticationFinished);
|
||||
}
|
||||
|
||||
|
||||
public void OnGamesSignIn() {
|
||||
GoogleSignIn.Configuration = configuration;
|
||||
GoogleSignIn.Configuration.UseGameSignIn = true;
|
||||
GoogleSignIn.Configuration.RequestIdToken = false;
|
||||
|
||||
// AddStatusText("Calling Games SignIn");
|
||||
|
||||
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
|
||||
OnAuthenticationFinished);
|
||||
}
|
||||
|
||||
private List<string> messages = new List<string>();
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user