InvitePanelScript.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class InvitePanelScript : MonoBehaviour {
  7. public Button closeButton;
  8. public Button searchButton;
  9. public Button doneButton;
  10. public InputField searchField;
  11. private String lastSearchValue = "";
  12. public GameObject inviteSearchResultPrefab;
  13. public GameObject searchResultPanel;
  14. public GameObject OnlineGameSettingsPanel;
  15. [SerializeField] Button findRandomPlayerButton;
  16. // Start is called before the first frame update
  17. void Start() {
  18. closeButton.onClick.AddListener(HidePanel);
  19. doneButton.onClick.AddListener(HidePanel);
  20. findRandomPlayerButton.onClick.AddListener(FindRandomPlayer);
  21. searchButton.onClick.AddListener(CallInviteFunction);
  22. }
  23. // Update is called once per frame
  24. void Update() {
  25. if (searchField.text.Length < 3) {
  26. searchButton.interactable = false;
  27. } else {
  28. searchButton.interactable = true;
  29. }
  30. }
  31. internal int InvitedCount() {
  32. InviteSearchResult[] invites = this.GetComponentsInChildren<InviteSearchResult>();
  33. int count = 0;
  34. foreach (InviteSearchResult isr in invites) {
  35. if (isr.ShouldInvite()) {
  36. count++;
  37. }
  38. }
  39. return count;
  40. }
  41. void FindRandomPlayer() {
  42. searchField.text = "";
  43. lastSearchValue = "";
  44. ClearIvites();
  45. List<OnlineDatabase.UserName> users = Database.Instance.FindRandomPlayer(Database.Instance.GetSignedInUser().Key);
  46. if (users != null) {
  47. AddToInvitePanel(users);
  48. }
  49. }
  50. public List<InviteSearchResult> GetSelectedUsersForInvite() {
  51. List<InviteSearchResult> result = new List<InviteSearchResult>();
  52. foreach(InviteSearchResult isr in this.GetComponentsInChildren<InviteSearchResult>()) {
  53. if (isr.ShouldInvite()) {
  54. result.Add(isr);
  55. }
  56. }
  57. return result;
  58. }
  59. public bool AreThereInvites() {
  60. int count = InvitedCount();
  61. return count > 0?true:false;
  62. }
  63. private void ClearIvites() {
  64. InviteSearchResult[] invites = this.GetComponentsInChildren<InviteSearchResult>();
  65. foreach (InviteSearchResult isr in invites) {
  66. if (!isr.ShouldInvite()) {
  67. Destroy(isr.gameObject);
  68. Destroy(isr);
  69. }
  70. }
  71. }
  72. private void CallInviteFunction() {
  73. if ((lastSearchValue.Equals("") || !searchField.text.Equals("")) && !lastSearchValue.Equals(searchField.text)) {
  74. lastSearchValue = searchField.text;
  75. ClearIvites();
  76. List<OnlineDatabase.UserName> foundUsers = OnlineDatabase.Instance.GetUsersToInvite(searchField.text);
  77. if (foundUsers != null) {
  78. AddToInvitePanel(foundUsers);
  79. }
  80. }
  81. }
  82. private void AddToInvitePanel(List<OnlineDatabase.UserName> foundUsers) {
  83. InviteSearchResult[] added = searchResultPanel.GetComponentsInChildren<InviteSearchResult>();
  84. foreach (OnlineDatabase.UserName un in foundUsers) {
  85. if (Database.Instance.GetSignedInUser().Key.ToString().Equals(un.id)) {
  86. continue;
  87. }
  88. bool skip = false;
  89. foreach (InviteSearchResult addedRes in added) {
  90. if (addedRes.GetName().Equals(un.username)) {
  91. skip = true;
  92. break;
  93. }
  94. }
  95. int resCount = 0;
  96. if (!skip) {
  97. GameObject invSearchRes = Instantiate(inviteSearchResultPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  98. InviteSearchResult res = invSearchRes.GetComponent<InviteSearchResult>();
  99. res.SetName(un.username);
  100. res.SetId(un.id);
  101. res.transform.SetParent(searchResultPanel.transform, false);
  102. res.transform.SetSiblingIndex(resCount++);
  103. }
  104. }
  105. }
  106. void HidePanel() {
  107. OnlineGameSettingsPanel.GetComponentInChildren<invitePanelController>().updateText(InvitedCount());
  108. gameObject.SetActive(false);
  109. OnlineGameSettingsPanel.SetActive(true);
  110. }
  111. }