MainMenu.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using UnityEngine.UI;
  7. using UnityEngine.SceneManagement;
  8. public class MainMenu : MonoBehaviour {
  9. public Button startNewGameButton;
  10. public Button quitButton;
  11. public Transform localGamesPanelTitle;
  12. public Transform onlineGamesPanelTitle;
  13. public GameObject localGamePrefab;
  14. public GameObject onlineGamePrefab;
  15. public GameObject finishedGamePrefab;
  16. private int loggedInUserId;
  17. private DateTime lastPlayedLocal;
  18. private DateTime lastPlayedOnline;
  19. private void Start() {
  20. startNewGameButton.onClick.AddListener(StartNewGameAction);
  21. quitButton.onClick.AddListener(() => { Application.Quit(); });
  22. }
  23. private void Awake() {
  24. GetGames();
  25. UpdateOnlineGamesLists();
  26. if (lastPlayedLocal > lastPlayedOnline) {
  27. localGamesPanelTitle.GetComponent<GamesScrollController>().expandThis(localGamesPanelTitle.name);
  28. } else {
  29. onlineGamesPanelTitle.GetComponent<GamesScrollController>().expandThis(onlineGamesPanelTitle.name);
  30. }
  31. }
  32. public void UpdateOnlineGamesLists() {
  33. KeyValuePair<int, string> loggedInUser = Database.Instance.GetSignedInUser();
  34. Transform onlineGamesList = GameObject.Find("OnlineGamesList").transform;
  35. Transform finishedGamesList = GameObject.Find("FinishedGamesList").transform;
  36. foreach (OnlineGameScript ogs in onlineGamesList.GetComponentsInChildren<OnlineGameScript>()) {
  37. Destroy(ogs);
  38. Destroy(ogs.gameObject);
  39. }
  40. int onlineIndex = 1;
  41. int finishedGamesIndex = 1;
  42. if (checkInternetConnection()) {
  43. List<OnlineGameScript> onlineGames = OnlineDatabase.Instance.GetOnlineGames(loggedInUser.Key, loggedInUser.Value, onlineGamePrefab);
  44. if (onlineGames != null) {
  45. foreach (OnlineGameScript ogs in onlineGames) {
  46. DateTime.TryParse(ogs.LastPlayedDate, out DateTime onlineLastPlayedDate);
  47. if (lastPlayedOnline == null || onlineLastPlayedDate > lastPlayedOnline) {
  48. lastPlayedOnline = onlineLastPlayedDate;
  49. }
  50. if (ogs.GameStatus.Equals("DECLINED")) {
  51. ogs.transform.SetParent(finishedGamesList, false);
  52. ogs.transform.SetSiblingIndex(finishedGamesIndex++);
  53. } else {
  54. ogs.transform.SetParent(onlineGamesList, false);
  55. ogs.transform.SetSiblingIndex(onlineIndex++);
  56. }
  57. }
  58. }
  59. } else {
  60. // Display no internet information
  61. Debug.Log("No internet");
  62. }
  63. }
  64. private void GetGames() {
  65. KeyValuePair<int, string> loggedInUser = Database.Instance.GetSignedInUser();
  66. List<LocalGameScript> localGames = Database.Instance.GetLocalGames(localGamePrefab);
  67. Transform localGamesList = GameObject.Find("LocalGamesList").transform;
  68. Transform finishedGamesList = GameObject.Find("FinishedGamesList").transform;
  69. foreach(LocalGameScript lgs in localGamesList.GetComponentsInChildren<LocalGameScript>()) {
  70. Destroy(lgs);
  71. Destroy(lgs.gameObject);
  72. }
  73. foreach (LocalGameScript lgs in finishedGamesList.GetComponentsInChildren<LocalGameScript>()) {
  74. Destroy(lgs);
  75. Destroy(lgs.gameObject);
  76. }
  77. int localIndex = 1;
  78. int finishedIndex = 1;
  79. foreach (LocalGameScript lgs in localGames) {
  80. DateTime.TryParse(lgs.LastPlayedDate, out DateTime localLastPlayedDate);
  81. if (lastPlayedLocal == null || localLastPlayedDate > lastPlayedLocal) {
  82. lastPlayedLocal = localLastPlayedDate;
  83. }
  84. if (lgs.FinishedDate.Equals("")) {
  85. lgs.transform.SetParent(localGamesList.transform, false);
  86. lgs.transform.SetSiblingIndex(localIndex++);
  87. } else {
  88. lgs.DisableStartButton();
  89. lgs.transform.SetParent(finishedGamesList.transform, false);
  90. lgs.transform.SetSiblingIndex(finishedIndex++);
  91. }
  92. }
  93. }
  94. private bool checkInternetConnection() {
  95. bool internetConnectivityActive;
  96. UnityWebRequest www = UnityWebRequest.Get("http://google.com");
  97. www.SendWebRequest();
  98. if (www.isNetworkError || www.isHttpError) {
  99. internetConnectivityActive = false;
  100. } else {
  101. internetConnectivityActive = true;
  102. }
  103. return internetConnectivityActive;
  104. }
  105. private void StartNewGameAction() {
  106. SceneManager.LoadScene("NewGame");
  107. }
  108. private void SetPanelVisibility(string panelName, bool isVisible) {
  109. CanvasGroup panelCG= GameObject.Find(panelName).gameObject.GetComponent<CanvasGroup>();
  110. panelCG.alpha = (isVisible?1f:0f);
  111. panelCG.interactable = (isVisible?true:false);
  112. panelCG.blocksRaycasts = (isVisible ? true : false);
  113. }
  114. }