Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / Login / LoginAndRegister.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Events;
  6. using UnityEngine.SceneManagement;
  7. public class LoginAndRegister : MonoBehaviour
  8. {
  9. public InputField username, password;
  10. public Button submitButton, registerButton;
  11. private ServerConnection ServerConnection;
  12. public Text messageWarning;
  13. private float timer = 0f;
  14. private float timerCoolDown = 1f;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. ServerConnection = new ServerConnection();
  19. registerButton.onClick.AddListener(RegisterPlayer);
  20. submitButton.onClick.AddListener(Login);
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. }
  26. void RegisterPlayer()
  27. {
  28. ServerConnection.RegisterPlayerInfo info = new ServerConnection.RegisterPlayerInfo(username.text, password.text);
  29. string json = JsonUtility.ToJson(info);
  30. Debug.Log(json);
  31. StartCoroutine(ServerConnection.PostRequest(ServerConnection.BaseAPI + "/player/new", json, PlayerRegInfo));
  32. }
  33. void Login()
  34. {
  35. ServerConnection.RegisterPlayerInfo info = new ServerConnection.RegisterPlayerInfo(username.text, password.text);
  36. string json = JsonUtility.ToJson(info);
  37. StartCoroutine(ServerConnection.PostRequest(ServerConnection.BaseAPI + "/player/login", json, PlayerGetData));
  38. Debug.Log(json);
  39. }
  40. public void PlayerGetData(string json)
  41. {
  42. LoginInfo info = JsonUtility.FromJson<LoginInfo>(json);
  43. PlayerData.loginInfo = info;
  44. if (info != null)
  45. {
  46. SceneManager.LoadScene(1);
  47. }
  48. }
  49. void PlayerRegInfo(string json)
  50. {
  51. timer = timerCoolDown;
  52. messageWarning.text = "Register Successful";
  53. //Debug.Log(json);
  54. }
  55. private void FixedUpdate()
  56. {
  57. if (messageWarning.text != string.Empty)
  58. {
  59. timer -= Time.deltaTime;
  60. }
  61. if (timer <= 0)
  62. {
  63. messageWarning.text = string.Empty;
  64. }
  65. }
  66. }
  67. public class LoginInfo
  68. {
  69. public string username;
  70. public int id;
  71. public int score;
  72. }