LobbyManager_RandomMatch.cs 15 KB

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