InvitePanelScript.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. public GameObject inviteSearchResultPrefab;
  12. public GameObject searchResultPanel;
  13. public GameObject OnlineGameSettingsPanel;
  14. private float autoSearchWait = 3f;
  15. private bool searchActive = false;
  16. private float autoSearchTime;
  17. private int searchChars = 0;
  18. private string searchedString = "";
  19. // Start is called before the first frame update
  20. void Start() {
  21. closeButton.onClick.AddListener(HidePanel);
  22. doneButton.onClick.AddListener(HidePanel);
  23. searchButton.onClick.AddListener(CallInviteFunction);
  24. }
  25. // Update is called once per frame
  26. void Update() {
  27. if (searchField.text.Length < 3) {
  28. ClearIvites();
  29. searchActive = false;
  30. searchButton.interactable = false;
  31. } else if (searchActive && searchChars != searchField.text.Length) {
  32. searchChars = searchField.text.Length;
  33. autoSearchTime = Time.time + autoSearchWait;
  34. } else if (searchActive && searchChars == searchField.text.Length && Time.time >= autoSearchTime) {
  35. CallInviteFunction();
  36. } else if (!searchActive && !searchField.text.Equals(searchedString)) {
  37. searchActive = true;
  38. autoSearchTime = Time.time + autoSearchWait;
  39. searchButton.interactable = true;
  40. searchChars = searchField.text.Length;
  41. }
  42. }
  43. internal int InvitedCount() {
  44. InviteSearchResult[] invites = this.GetComponentsInChildren<InviteSearchResult>();
  45. int count = 0;
  46. foreach (InviteSearchResult isr in invites) {
  47. if (isr.ShouldInvite()) {
  48. count++;
  49. }
  50. }
  51. return count;
  52. }
  53. public List<InviteSearchResult> GetSelectedUsersForInvite() {
  54. List<InviteSearchResult> result = new List<InviteSearchResult>();
  55. foreach(InviteSearchResult isr in this.GetComponentsInChildren<InviteSearchResult>()) {
  56. if (isr.ShouldInvite()) {
  57. result.Add(isr);
  58. }
  59. }
  60. return result;
  61. }
  62. public bool AreThereInvites() {
  63. int count = InvitedCount();
  64. return count > 0?true:false;
  65. }
  66. private void ClearIvites() {
  67. InviteSearchResult[] invites = this.GetComponentsInChildren<InviteSearchResult>();
  68. foreach (InviteSearchResult isr in invites) {
  69. if (!isr.ShouldInvite()) {
  70. Destroy(isr.gameObject);
  71. Destroy(isr);
  72. }
  73. }
  74. }
  75. private void CallInviteFunction() {
  76. ClearIvites();
  77. searchedString = searchField.text;
  78. searchActive = false;
  79. List<OnlineDatabase.UserName> foundUsers = OnlineDatabase.Instance.GetUsersToInvite(searchField.text);
  80. InviteSearchResult[] added = searchResultPanel.GetComponentsInChildren<InviteSearchResult>();
  81. foreach (OnlineDatabase.UserName un in foundUsers) {
  82. if (Database.Instance.GetSignedInUser().Key.ToString().Equals(un.id)) {
  83. continue;
  84. }
  85. bool skip = false;
  86. foreach (InviteSearchResult addedRes in added) {
  87. if (addedRes.GetName().Equals(un.username)) {
  88. skip = true;
  89. break;
  90. }
  91. }
  92. if (!skip) {
  93. GameObject question = Instantiate(inviteSearchResultPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  94. InviteSearchResult res = question.GetComponent<InviteSearchResult>();
  95. res.SetName(un.username);
  96. res.SetId(un.id);
  97. res.transform.SetParent(searchResultPanel.transform, false);
  98. }
  99. }
  100. }
  101. void HidePanel() {
  102. OnlineGameSettingsPanel.GetComponentInChildren<invitedPlayersTextScript>().updateText(InvitedCount());
  103. gameObject.SetActive(false);
  104. OnlineGameSettingsPanel.SetActive(true);
  105. }
  106. }