NewOnlineGame.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7. public class NewOnlineGame : MonoBehaviour
  8. {
  9. public Button inviteButton;
  10. public Button startButton;
  11. public InputField correctAnswersToWin;
  12. public InputField timeLimitPerQuestion;
  13. public InputField timeLimitPerPlayer;
  14. Text invitedCountText;
  15. InvitePanelScript ips;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. //inviteButton.onClick.AddListener(OpenInvitePanel);
  20. startButton.onClick.AddListener(StartNewGame);
  21. invitedCountText = GameObject.Find("InvitedCountText").GetComponent<Text>();
  22. }
  23. private void Update() {
  24. if (ips == null) {
  25. ips = GameObject.Find("InvitePanel").GetComponent<InvitePanelScript>();
  26. }
  27. if (ips.AreThereInvites()) {
  28. invitedCountText.GetComponent<CanvasGroup>().alpha = 1f;
  29. string text = LocalizationManager.Instance.GetText("INVITE_COUNT_TEXT");
  30. int invitedCount = ips.InvitedCount();
  31. text = String.Format(text, invitedCount.ToString());
  32. invitedCountText.text = text;
  33. startButton.interactable = true;
  34. }
  35. }
  36. private void StartNewGame() {
  37. // Collect all information for new game;
  38. // Send to Database;
  39. int timeLimitQuestion = GetInputFieldValue(timeLimitPerQuestion);
  40. int timeLimitPlayer = GetInputFieldValue(timeLimitPerPlayer);
  41. int correctToWin = GetInputFieldValue(correctAnswersToWin);
  42. List<InviteSearchResult> inviteUsers = ips.GetSelectedUsersForInvite();
  43. List<int> selectedCategoryIds = GameObject.Find("SelectCategoresPanel").GetComponent<SelectCategoryScript>()
  44. .GetSelectedCategories()
  45. .Select(c => c.id)
  46. .ToList();
  47. int newGameId = OnlineDatabase.Instance.SetupNewOnlineGame(timeLimitQuestion, timeLimitPlayer, correctToWin, inviteUsers, selectedCategoryIds);
  48. List<String> invitePlayerNames = new List<String>();
  49. foreach (InviteSearchResult isr in inviteUsers) {
  50. invitePlayerNames.Add(isr.GetName());
  51. }
  52. OnlineDatabase.Instance.SendInviteForNewGame(newGameId, invitePlayerNames, Database.Instance.GetSignedInUser().Value);
  53. }
  54. private int GetInputFieldValue(InputField inputField) {
  55. int returnValue = 0;
  56. if (inputField.text.Equals("")) {
  57. string text = Regex.Match(inputField.placeholder.GetComponent<Text>().text, @"\d+").Value;
  58. Int32.TryParse(text, out returnValue);
  59. } else {
  60. string text = Regex.Match(inputField.text, @"\d+").Value;
  61. Int32.TryParse(text, out returnValue);
  62. }
  63. return returnValue;
  64. }
  65. }