Init DB, Login/Register, Online Data Saving + retreival. Selection bug fixed
This commit is contained in:
67
Assets/Game/Scripts/DBmanager.cs
Normal file
67
Assets/Game/Scripts/DBmanager.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
public class DBmanager : MonoBehaviour
|
||||
{
|
||||
public static string phpRoot = "http://38.242.232.13/upf/";
|
||||
|
||||
public static string username = null;
|
||||
private static int coins = 0;
|
||||
private static int gems = 0;
|
||||
|
||||
public static int Coins => coins;
|
||||
public static int Gems => gems;
|
||||
|
||||
public static bool LoggedIn { get { return username != null; } }
|
||||
public static void LogOut()
|
||||
{
|
||||
username = null;
|
||||
}
|
||||
|
||||
public async static void SetCoins(int newValue, bool justOffline = false){
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("name", username);
|
||||
form.AddField("coins", newValue);
|
||||
if(justOffline){coins=newValue; return;}
|
||||
using(UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_coins.php",form)){
|
||||
var operation = www.SendWebRequest();
|
||||
while(!operation.isDone){
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
if(www.downloadHandler.text=="0"){
|
||||
coins = newValue;
|
||||
}else{
|
||||
Debug.Log("Response : " + www.downloadHandler.text);
|
||||
Debug.LogWarning("Failed to set coins to " + newValue);
|
||||
}
|
||||
}
|
||||
|
||||
GameManagerInstance.gameManager.RefreshData();
|
||||
}
|
||||
|
||||
public async static void SetGems(int newValue,bool justOffline=false){
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("name", username);
|
||||
form.AddField("gems", newValue);
|
||||
if(justOffline){gems=newValue; return;}
|
||||
using(UnityWebRequest www = UnityWebRequest.Post(phpRoot + "set_gems.php",form)){
|
||||
var operation = www.SendWebRequest();
|
||||
while(!operation.isDone){
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
if(www.downloadHandler.text=="0"){
|
||||
gems = newValue;
|
||||
}else{
|
||||
Debug.Log("Response : " + www.downloadHandler.text);
|
||||
Debug.LogWarning("Failed to set gems to " + newValue);
|
||||
}
|
||||
}
|
||||
|
||||
GameManagerInstance.gameManager.RefreshData();
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/DBmanager.cs.meta
Normal file
11
Assets/Game/Scripts/DBmanager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a87a882db4f1a72b59c9447fc6da7af5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
43
Assets/Game/Scripts/GameManager.cs
Normal file
43
Assets/Game/Scripts/GameManager.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
public TMP_Text usernameTxt;
|
||||
public TMP_Text coinsTxt;
|
||||
public TMP_Text gemsTxt;
|
||||
|
||||
void Start()
|
||||
{
|
||||
GameManagerInstance.gameManager = this;
|
||||
|
||||
|
||||
//Go back to login if not logged
|
||||
if(!DBmanager.LoggedIn){
|
||||
SceneManager.LoadScene(0);
|
||||
}else{
|
||||
usernameTxt.text = DBmanager.username;
|
||||
RefreshData();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void RefreshData(){
|
||||
coinsTxt.text = DBmanager.Coins.ToString();
|
||||
gemsTxt.text = DBmanager.Gems.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public static class GameManagerInstance{
|
||||
public static GameManager gameManager;
|
||||
}
|
||||
11
Assets/Game/Scripts/GameManager.cs.meta
Normal file
11
Assets/Game/Scripts/GameManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3867e702552f07efc81ef013fc84fc0f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
110
Assets/Game/Scripts/LoginManager.cs
Normal file
110
Assets/Game/Scripts/LoginManager.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
public class LoginManager : MonoBehaviour
|
||||
{
|
||||
public InputField login_username;
|
||||
public InputField login_password;
|
||||
public Button loginBtn;
|
||||
public InputField reg_username;
|
||||
public InputField reg_password;
|
||||
public Button regBtn;
|
||||
|
||||
void Start()
|
||||
{
|
||||
loginBtn.onClick.AddListener(OnLoginClicked);
|
||||
regBtn.onClick.AddListener(OnRegisterClicked);
|
||||
|
||||
if(PlayerPrefs.HasKey("username") && PlayerPrefs.HasKey("password")){
|
||||
login_username.text = PlayerPrefs.GetString("username");
|
||||
login_password.text = PlayerPrefs.GetString("password");
|
||||
|
||||
OnLoginClicked();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnLoginClicked(){
|
||||
if(login_username.text.Length < 2){
|
||||
MessageDialogInstance.messageDialog.ShowDialog("Error", "Please use a valid Username (should be more than 2 characters)");
|
||||
return;
|
||||
}
|
||||
if(login_password.text.Length < 5){
|
||||
MessageDialogInstance.messageDialog.ShowDialog("Error", "Please use a Strong password (should be more than 5 characters)");
|
||||
return;
|
||||
}
|
||||
|
||||
StartCoroutine(LoginPlayer());
|
||||
}
|
||||
public void OnRegisterClicked(){
|
||||
if(reg_username.text.Length < 2){
|
||||
MessageDialogInstance.messageDialog.ShowDialog("Error", "Please use a valid Username (should be more than 2 characters)");
|
||||
return;
|
||||
}
|
||||
if(reg_password.text.Length < 5){
|
||||
MessageDialogInstance.messageDialog.ShowDialog("Error", "Please use a Strong password (should be more than 5 characters)");
|
||||
return;
|
||||
}
|
||||
|
||||
StartCoroutine(Register());
|
||||
}
|
||||
|
||||
|
||||
IEnumerator LoginPlayer()
|
||||
{
|
||||
loginBtn.interactable=false;
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("name", login_username.text);
|
||||
form.AddField("password", login_password.text);
|
||||
|
||||
WWW www = new WWW(DBmanager.phpRoot+"login.php", form);
|
||||
yield return www;
|
||||
Debug.Log(www.text);
|
||||
if (www.text[0] == '0')
|
||||
{
|
||||
PlayerPrefs.SetString("username", login_username.text);
|
||||
PlayerPrefs.SetString("password", login_password.text);
|
||||
PlayerPrefs.Save();
|
||||
DBmanager.username = login_username.text;
|
||||
DBmanager.SetGems(int.Parse(www.text.Split('\t')[2]),true);
|
||||
DBmanager.SetCoins(int.Parse(www.text.Split('\t')[1]),true);
|
||||
|
||||
UnityEngine.SceneManagement.SceneManager.LoadScene(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("User Login failed. Error #" + www.text);
|
||||
}
|
||||
loginBtn.interactable=true;
|
||||
}
|
||||
|
||||
IEnumerator Register()
|
||||
{
|
||||
regBtn.interactable = false;
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("name", reg_username.text);
|
||||
form.AddField("password", reg_password.text);
|
||||
|
||||
WWW www = new WWW(DBmanager.phpRoot + "register.php", form);
|
||||
yield return www;
|
||||
if (www.text == "0")
|
||||
{
|
||||
Debug.Log("User Registered succesfully");
|
||||
DBmanager.username = reg_username.text;
|
||||
|
||||
UnityEngine.SceneManagement.SceneManager.LoadScene(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("User creation failed " + www.text);
|
||||
}
|
||||
regBtn.interactable=true;
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/LoginManager.cs.meta
Normal file
11
Assets/Game/Scripts/LoginManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d9be0da4b261054338a3bc7efc3921e3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
44
Assets/Game/Scripts/MessageDialog.cs
Normal file
44
Assets/Game/Scripts/MessageDialog.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(CanvasGroup))]
|
||||
public class MessageDialog : MonoBehaviour
|
||||
{
|
||||
public Text titleTxt;
|
||||
public Text messageTxt;
|
||||
public Button actionBtn;
|
||||
|
||||
public bool showing => GetComponent<CanvasGroup>().blocksRaycasts;
|
||||
|
||||
void Start()
|
||||
{
|
||||
MessageDialogInstance.messageDialog = this;
|
||||
actionBtn.onClick.AddListener(OnAction);
|
||||
SetActive(false);
|
||||
}
|
||||
|
||||
void OnAction(){
|
||||
if(showing){
|
||||
SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowDialog(string title, string message){
|
||||
titleTxt.text = title;
|
||||
messageTxt.text = message;
|
||||
|
||||
SetActive(true);
|
||||
}
|
||||
|
||||
public void SetActive(bool value){
|
||||
GetComponent<CanvasGroup>().blocksRaycasts= value;
|
||||
GetComponent<CanvasGroup>().interactable= value;
|
||||
GetComponent<CanvasGroup>().alpha= (value) ? 1: 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static class MessageDialogInstance{
|
||||
public static MessageDialog messageDialog;
|
||||
}
|
||||
11
Assets/Game/Scripts/MessageDialog.cs.meta
Normal file
11
Assets/Game/Scripts/MessageDialog.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f3da2feee9b5e021927efdd7602355e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -9,27 +9,27 @@ LightingSettings:
|
||||
m_Name: New Lighting Settings
|
||||
serializedVersion: 3
|
||||
m_GIWorkflowMode: 1
|
||||
m_EnableBakedLightmaps: 1
|
||||
m_EnableBakedLightmaps: 0
|
||||
m_EnableRealtimeLightmaps: 0
|
||||
m_RealtimeEnvironmentLighting: 1
|
||||
m_BounceScale: 1
|
||||
m_AlbedoBoost: 1
|
||||
m_IndirectOutputScale: 1
|
||||
m_UsingShadowmask: 1
|
||||
m_BakeBackend: 1
|
||||
m_LightmapMaxSize: 1024
|
||||
m_BakeResolution: 40
|
||||
m_UsingShadowmask: 0
|
||||
m_BakeBackend: 2
|
||||
m_LightmapMaxSize: 128
|
||||
m_BakeResolution: 20
|
||||
m_Padding: 2
|
||||
m_TextureCompression: 1
|
||||
m_AO: 0
|
||||
m_AO: 1
|
||||
m_AOMaxDistance: 1
|
||||
m_CompAOExponent: 1
|
||||
m_CompAOExponentDirect: 0
|
||||
m_ExtractAO: 0
|
||||
m_MixedBakeMode: 2
|
||||
m_MixedBakeMode: 0
|
||||
m_LightmapsBakeMode: 1
|
||||
m_FilterMode: 1
|
||||
m_LightmapParameters: {fileID: 15204, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_LightmapParameters: {fileID: 15201, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_ExportTrainingData: 0
|
||||
m_TrainingDataDestination: TrainingData
|
||||
m_RealtimeResolution: 2
|
||||
@@ -40,9 +40,9 @@ LightingSettings:
|
||||
m_FinalGatherFiltering: 1
|
||||
m_PVRCulling: 1
|
||||
m_PVRSampling: 1
|
||||
m_PVRDirectSampleCount: 32
|
||||
m_PVRSampleCount: 512
|
||||
m_PVREnvironmentSampleCount: 256
|
||||
m_PVRDirectSampleCount: 16
|
||||
m_PVRSampleCount: 256
|
||||
m_PVREnvironmentSampleCount: 128
|
||||
m_PVREnvironmentReferencePointCount: 2048
|
||||
m_LightProbeSampleCountMultiplier: 4
|
||||
m_PVRBounces: 2
|
||||
|
||||
@@ -10,11 +10,41 @@ public class ShopScript : MonoBehaviour
|
||||
public string shopButtonName;
|
||||
|
||||
|
||||
public void OnClickBuyCoins(int packNumber){
|
||||
int coinGain =0;
|
||||
int gemCost = 0;
|
||||
switch(packNumber){
|
||||
case 0:
|
||||
coinGain=1000;
|
||||
gemCost =10;
|
||||
break;
|
||||
case 1:
|
||||
coinGain=6300;
|
||||
gemCost =60;
|
||||
break;
|
||||
case 2:
|
||||
coinGain=27600;
|
||||
gemCost =120;
|
||||
break;
|
||||
case 3:
|
||||
coinGain=57600;
|
||||
gemCost =250;
|
||||
break;
|
||||
}
|
||||
|
||||
if(gemCost > DBmanager.Gems){
|
||||
Debug.Log("Not enough gems!");
|
||||
return;
|
||||
}
|
||||
|
||||
DBmanager.SetCoins(DBmanager.Coins + coinGain);
|
||||
DBmanager.SetGems(DBmanager.Gems - gemCost);
|
||||
}
|
||||
|
||||
public void openShop()
|
||||
{
|
||||
shopButtonName = EventSystem.current.currentSelectedGameObject.name;
|
||||
checkButton();
|
||||
|
||||
}
|
||||
|
||||
public void checkButton()
|
||||
|
||||
Reference in New Issue
Block a user