LobbyManager_RandomMatch.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Photon.Pun;
  6. using Photon.Realtime;
  7. using UnityEngine.SceneManagement;
  8. using System;
  9. using System.Linq;
  10. using Hashtable = ExitGames.Client.Photon.Hashtable;
  11. public class LobbyManager_RandomMatch : MonoBehaviourPunCallbacks
  12. {
  13. [Header("message text")]
  14. public Text MessageText;
  15. [Header("tiem count text")]
  16. public Text TimeText;
  17. [Header("return to title button")]
  18. public GameObject ReturnButton;
  19. [Header("deck info panel")]
  20. public DeckInfoPanel deckInfoPanel;
  21. public Animator anim;
  22. public LoadingObject loadingObject;
  23. public LoadingObject disconnectLoadingObject;
  24. //Room name for an already existing random match room
  25. string RandomRoomName;
  26. bool endLoadingText = false;
  27. private bool isCoroutineRunning = false;
  28. //String that must be included in the random match room
  29. string RandomKey
  30. {
  31. get
  32. {
  33. return "randomMatchRoom";
  34. }
  35. }
  36. //Whether or not a room is being processed
  37. bool startJoin = false;
  38. #region Open Lobby Screen
  39. public void SetUpLobby()
  40. {
  41. Opening.instance.OffYesNoObjects();
  42. Opening.instance.deck.trialDraw.Close();
  43. Opening.instance.deck.deckListPanel.Close();
  44. ContinuousController.instance.isAI = false;
  45. ContinuousController.instance.isRandomMatch = true;
  46. this.gameObject.SetActive(true);
  47. //this.battleRule = battleRule;
  48. ContinuousController.instance.StartCoroutine(ConnectCoroutine());
  49. anim.SetInteger("Open", 1);
  50. anim.SetInteger("Close", 0);
  51. }
  52. #endregion
  53. #region Close Lobby Screen
  54. public void OffLobby()
  55. {
  56. this.gameObject.SetActive(false);
  57. }
  58. #endregion
  59. #region Leave from Random Match Screen
  60. public void CloseLobby()
  61. {
  62. ContinuousController.instance.StartCoroutine(CloseLobbyCoroutine());
  63. }
  64. public IEnumerator CloseLobbyCoroutine()
  65. {
  66. yield return ContinuousController.instance.StartCoroutine(disconnectLoadingObject.StartLoading("Now Loading"));
  67. ReturnButton.SetActive(false);
  68. once1 = true;
  69. DoneCompleteMatching = true;
  70. m = true;
  71. n = true;
  72. #region Leave From Room
  73. if (PhotonNetwork.InRoom)
  74. {
  75. PhotonNetwork.LeaveRoom();
  76. }
  77. yield return new WaitWhile(() => PhotonNetwork.InRoom);
  78. #endregion
  79. OffLobby();
  80. yield return ContinuousController.instance.StartCoroutine(disconnectLoadingObject.EndLoading());
  81. }
  82. #endregion
  83. #region 初期化
  84. public IEnumerator Init()
  85. {
  86. n = false;
  87. m = false;
  88. once1 = false;
  89. endLoadingText = false;
  90. time = 0;
  91. RandomRoomName = "";
  92. startJoin = false;
  93. DoneCompleteMatching = false;
  94. ContinuousController.instance.LoadingTextCoroutine = null;
  95. ReturnButton.SetActive(true);
  96. MessageText.text = "";
  97. TimeText.gameObject.SetActive(true);
  98. StartCoroutine(TimeCountUp());
  99. timer = 0;
  100. count = true;
  101. if (PhotonNetwork.InLobby)
  102. {
  103. PhotonNetwork.LeaveLobby();
  104. }
  105. yield return new WaitWhile(() => PhotonNetwork.InLobby);
  106. if (PhotonNetwork.IsConnected)
  107. {
  108. PhotonNetwork.Disconnect();
  109. }
  110. yield return new WaitWhile(() => PhotonNetwork.IsConnected);
  111. }
  112. #endregion
  113. #region Time text count up
  114. int time = 0;
  115. IEnumerator TimeCountUp()
  116. {
  117. time = 0;
  118. TimeText.gameObject.SetActive(true);
  119. while (!DoneCompleteMatching)
  120. {
  121. time += (int)Time.deltaTime;
  122. string min = (time / 60).ToString();
  123. string sec = (time % 60).ToString();
  124. if (min.Length == 1)
  125. {
  126. min = $"0{min}";
  127. }
  128. if (sec.Length == 1)
  129. {
  130. sec = $"0{sec}";
  131. }
  132. TimeText.text = $"{min}:{sec}";
  133. time++;
  134. yield return new WaitForSeconds(1f);
  135. }
  136. TimeText.gameObject.SetActive(false);
  137. }
  138. #endregion
  139. #region Connect to Photon and Lobby
  140. IEnumerator ConnectCoroutine()
  141. {
  142. //yield return StartCoroutine(loadingObject.StartLoading("Now Loading"));
  143. if (ContinuousController.instance.BattleDeckData != null)
  144. {
  145. deckInfoPanel.SetUpDeckInfoPanel(ContinuousController.instance.BattleDeckData);
  146. }
  147. if (ReturnButton != null)
  148. {
  149. ReturnButton.SetActive(false);
  150. }
  151. endLoadingText = true;
  152. yield return ContinuousController.instance.StartCoroutine(Init());
  153. yield return new WaitForSeconds(0.5f);
  154. endLoadingText = false;
  155. ContinuousController.instance.LoadingTextCoroutine = ContinuousController.instance.StartCoroutine(SetWaitingText(
  156. LocalizeUtility.GetLocalizedString(
  157. EngMessage: "Connecting",
  158. JpnMessage: "接続中"
  159. )));
  160. MessageText.transform.localPosition = new Vector3(-180, -193, 0);
  161. yield return ContinuousController.instance.StartCoroutine(PhotonUtility.ConnectToLobbyCoroutine());
  162. yield return ContinuousController.instance.StartCoroutine(PhotonUtility.SignUpBattleDeckData());
  163. yield return new WaitForSeconds(0.5f);
  164. StartRandomMatch();
  165. yield return new WaitWhile(() => !PhotonNetwork.InRoom);
  166. yield return new WaitForSeconds(0.1f);
  167. yield return ContinuousController.instance.StartCoroutine(loadingObject.EndLoading());
  168. ContinuousController.instance.StartCoroutine(TimeCountUp());
  169. ReturnButton.SetActive(true);
  170. }
  171. #endregion
  172. #region ReturnToTitle()
  173. public void ReturnToTitle()
  174. {
  175. SceneManager.LoadSceneAsync("Opening");
  176. DoneCompleteMatching = true;
  177. }
  178. #endregion
  179. #region Callback on room list update
  180. bool m;
  181. bool n;
  182. public override void OnRoomListUpdate(List<RoomInfo> roomList)
  183. {
  184. if (this.gameObject.activeSelf)
  185. {
  186. if (!PhotonNetwork.InRoom && PhotonNetwork.InLobby && !m)
  187. {
  188. m = true;
  189. PhotonNetwork.LeaveLobby();
  190. }
  191. if (!PhotonNetwork.InRoom && PhotonNetwork.InLobby && n)
  192. {
  193. GetRandomMatcingRoom(roomList);
  194. }
  195. }
  196. }
  197. #endregion
  198. #region Search for random matching rooms available
  199. public void GetRandomMatcingRoom(List<RoomInfo> roomInfo)
  200. {
  201. if (startJoin)
  202. {
  203. return;
  204. }
  205. m = false;
  206. n = false;
  207. if (roomInfo == null || roomInfo.Count == 0)
  208. {
  209. return;
  210. }
  211. RandomRoomName = null;
  212. for (int i = 0; i < roomInfo.Count; i++)
  213. {
  214. int p = roomInfo[i].PlayerCount;
  215. string n = roomInfo[i].Name;
  216. int m = roomInfo[i].MaxPlayers;
  217. object c = roomInfo[i].CustomProperties["RoomCreator"];
  218. if (p != 0 && m != 0 && c != null)
  219. {
  220. if (n.Contains(RandomKey))
  221. {
  222. RandomRoomName = n;
  223. }
  224. }
  225. }
  226. }
  227. #endregion
  228. #region Callback when leaving the lobby
  229. public override void OnLeftLobby()
  230. {
  231. if (this.gameObject.activeSelf)
  232. {
  233. if (m)
  234. {
  235. n = true;
  236. PhotonNetwork.JoinLobby();
  237. }
  238. }
  239. }
  240. #endregion
  241. #region Process to create a room
  242. public IEnumerator CreateRoomCoroutine(bool isRandomMatch)
  243. {
  244. yield return new WaitWhile(() => !PhotonNetwork.IsConnectedAndReady);
  245. yield return new WaitWhile(() => !PhotonNetwork.InLobby);
  246. //Setting up the room to be created
  247. RoomOptions roomOptions = new RoomOptions();
  248. roomOptions.IsVisible = true; //Make the room visible in the lobby.
  249. roomOptions.IsOpen = true; //Allow other players to enter the room
  250. roomOptions.PublishUserId = true;
  251. roomOptions.MaxPlayers = 2;
  252. //To display room creator in room custom properties, store creator's name
  253. roomOptions.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable()
  254. {
  255. { "RoomCreator",PhotonNetwork.NickName },
  256. };
  257. //Display custom property information in the lobby
  258. roomOptions.CustomRoomPropertiesForLobby = new string[]
  259. {
  260. "RoomCreator",
  261. };
  262. string RoomName = StringUtils.GeneratePassword_AlpahabetNum(8);
  263. if (isRandomMatch)
  264. {
  265. RoomName += RandomKey;
  266. }
  267. //Room Creation
  268. PhotonNetwork.CreateRoom(RoomName, roomOptions, null);
  269. while (!PhotonNetwork.InRoom)
  270. {
  271. yield return null;
  272. }
  273. endLoadingText = true;
  274. yield return new WaitForSeconds(0.2f);
  275. endLoadingText = false;
  276. ContinuousController.instance.LoadingTextCoroutine = StartCoroutine(SetWaitingText(
  277. LocalizeUtility.GetLocalizedString(
  278. EngMessage: "Matching",
  279. JpnMessage: "マッチング中"
  280. )));
  281. MessageText.transform.localPosition = new Vector3(-148, -193, 0);
  282. if (ReturnButton != null)
  283. {
  284. ReturnButton.SetActive(true);
  285. }
  286. }
  287. #endregion
  288. #region Random match starts after entering the lobby
  289. public void StartRandomMatch()
  290. {
  291. if (ReturnButton != null)
  292. {
  293. ReturnButton.SetActive(true);
  294. }
  295. //If there is no room, make room.
  296. if (String.IsNullOrEmpty(RandomRoomName))
  297. {
  298. StartCoroutine(CreateRoomCoroutine(true));
  299. }
  300. //If there's room, I'll go in.
  301. else
  302. {
  303. startJoin = true;
  304. PhotonNetwork.JoinRoom(RandomRoomName);
  305. }
  306. }
  307. #endregion
  308. private IEnumerator SetWaitingText(string defaultString)
  309. {
  310. if (isCoroutineRunning)
  311. {
  312. yield break; // Exit if already running
  313. }
  314. isCoroutineRunning = true;
  315. float waitTime = 0.18f;
  316. int count = 0;
  317. while (!endLoadingText)
  318. {
  319. count++;
  320. if (count >= 4)
  321. {
  322. count = 0;
  323. }
  324. MessageText.text = defaultString;
  325. for (int i = 0; i < count; i++)
  326. {
  327. MessageText.text += ".";
  328. }
  329. yield return new WaitForSeconds(waitTime);
  330. }
  331. isCoroutineRunning = false;
  332. }
  333. public void StartLoadingText(string defaultString)
  334. {
  335. if (!isCoroutineRunning)
  336. {
  337. endLoadingText = false; // Ensure the flag is reset when starting
  338. StartCoroutine(SetWaitingText(defaultString));
  339. }
  340. }
  341. public void StopLoadingText()
  342. {
  343. endLoadingText = true;
  344. }
  345. bool count = true;
  346. float timer = 0;
  347. Button ReturnButtonButton;
  348. private void Start()
  349. {
  350. ReturnButtonButton = ReturnButton.transform.GetChild(0).GetComponent<Button>();
  351. }
  352. private void LateUpdate()
  353. {
  354. if (PhotonNetwork.InRoom)
  355. {
  356. if (PhotonNetwork.CurrentRoom.PlayerCount == PhotonNetwork.CurrentRoom.MaxPlayers)
  357. {
  358. if (PhotonNetwork.IsMasterClient)
  359. {
  360. StartCoroutine(GoNextScene());
  361. }
  362. if (ReturnButtonButton != null)
  363. {
  364. ReturnButtonButton.enabled = false;
  365. }
  366. }
  367. else
  368. {
  369. if (ReturnButtonButton != null)
  370. {
  371. ReturnButtonButton.enabled = true;
  372. }
  373. }
  374. }
  375. else
  376. {
  377. ReturnButtonButton.enabled = true;
  378. }
  379. if (DoneCompleteMatching)
  380. {
  381. if (ReturnButtonButton != null)
  382. {
  383. ReturnButton.SetActive(false);
  384. }
  385. }
  386. }
  387. #region After matching is complete, transition to the battle scene.
  388. bool DoneCompleteMatching = false;
  389. bool once1 = false;
  390. IEnumerator GoNextScene()
  391. {
  392. if (DoneCompleteMatching || once1)
  393. {
  394. yield break;
  395. }
  396. once1 = true;
  397. yield return new WaitForSeconds(0.1f);
  398. PhotonNetwork.CurrentRoom.IsVisible = false;
  399. photonView.RPC("GoToBattleScene", RpcTarget.All);
  400. }
  401. #endregion
  402. [PunRPC]
  403. public void GoToBattleScene()
  404. {
  405. if (DoneCompleteMatching)
  406. {
  407. return;
  408. }
  409. endLoadingText = true;
  410. DoneCompleteMatching = true;
  411. TimeText.gameObject.SetActive(false);
  412. MessageText.text = LocalizeUtility.GetLocalizedString(
  413. EngMessage: "Matching completed!",
  414. JpnMessage: "マッチングしました!"
  415. );
  416. MessageText.transform.localPosition = new Vector3(-390, -193, 0);
  417. ReturnButton.SetActive(false);
  418. ContinuousController.instance.StartCoroutine(GoToBattleSceneCoroutine());
  419. }
  420. IEnumerator GoToBattleSceneCoroutine()
  421. {
  422. ContinuousController.instance.StartCoroutine(Opening.instance.OpeningBGM.FadeOut(0.2f));
  423. Debug.Log("Matching complted!");
  424. yield return new WaitForSeconds(0.1f);
  425. //Opening.instance.MainCamera.gameObject.SetActive(false);
  426. foreach (Camera camera in Opening.instance.openingCameras)
  427. {
  428. camera.gameObject.SetActive(false);
  429. }
  430. yield return new WaitForSeconds(0.1f);
  431. SceneManager.LoadSceneAsync("BattleScene", LoadSceneMode.Additive);
  432. yield return null;
  433. }
  434. }
  435. #region Generation of random strings
  436. public static class StringUtils
  437. {
  438. public static string GeneratePassword_AlpahabetNum(int length)
  439. {
  440. string PASSWORD_CHARS =
  441. "0123456789abcdefghijklmnopqrstuvwxyz";
  442. var sb = new System.Text.StringBuilder(length);
  443. var r = new System.Random();
  444. for (int i = 0; i < length; i++)
  445. {
  446. int pos = r.Next(PASSWORD_CHARS.Length);
  447. char c = PASSWORD_CHARS[pos];
  448. sb.Append(c);
  449. }
  450. return sb.ToString();
  451. }
  452. public static string GeneratePassword_Num(int length)
  453. {
  454. string PASSWORD_CHARS =
  455. "0123456789";
  456. var sb = new System.Text.StringBuilder(length);
  457. var r = new System.Random();
  458. for (int i = 0; i < length; i++)
  459. {
  460. int pos = r.Next(PASSWORD_CHARS.Length);
  461. char c = PASSWORD_CHARS[pos];
  462. sb.Append(c);
  463. }
  464. return sb.ToString();
  465. }
  466. }
  467. #endregion