LobbyManager_RandomMatch.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. if (PhotonNetwork.InLobby)
  104. {
  105. PhotonNetwork.LeaveLobby();
  106. }
  107. yield return new WaitWhile(() => PhotonNetwork.InLobby);
  108. if (PhotonNetwork.IsConnected)
  109. {
  110. PhotonNetwork.Disconnect();
  111. }
  112. yield return new WaitWhile(() => PhotonNetwork.IsConnected);
  113. }
  114. #endregion
  115. #region Time text count up
  116. int time = 0;
  117. IEnumerator TimeCountUp()
  118. {
  119. time = 0;
  120. TimeText.gameObject.SetActive(true);
  121. while (!DoneCompleteMatching)
  122. {
  123. string min = (time / 60).ToString();
  124. string sec = (time % 60).ToString();
  125. if (min.Length == 1)
  126. {
  127. min = $"0{min}";
  128. }
  129. if (sec.Length == 1)
  130. {
  131. sec = $"0{sec}";
  132. }
  133. TimeText.text = $"{min}:{sec}";
  134. time++;
  135. yield return _waitForSeconds1;
  136. }
  137. TimeText.gameObject.SetActive(false);
  138. }
  139. #endregion
  140. #region Connect to Photon and Lobby
  141. IEnumerator ConnectCoroutine()
  142. {
  143. //yield return StartCoroutine(loadingObject.StartLoading("Now Loading"));
  144. if (ContinuousController.instance.BattleDeckData != null)
  145. {
  146. deckInfoPanel.SetUpDeckInfoPanel(ContinuousController.instance.BattleDeckData);
  147. }
  148. if (ReturnButton != null)
  149. {
  150. ReturnButton.SetActive(false);
  151. }
  152. endLoadingText = true;
  153. yield return ContinuousController.instance.StartCoroutine(Init());
  154. yield return _waitForSeconds0_5;
  155. endLoadingText = false;
  156. ContinuousController.instance.LoadingTextCoroutine = ContinuousController.instance.StartCoroutine(SetWaitingText(
  157. LocalizeUtility.GetLocalizedString(
  158. EngMessage: "Connecting",
  159. JpnMessage: "接続中"
  160. )));
  161. MessageText.transform.localPosition = new Vector3(-180, -193, 0);
  162. yield return ContinuousController.instance.StartCoroutine(PhotonUtility.ConnectToLobbyCoroutine());
  163. yield return ContinuousController.instance.StartCoroutine(PhotonUtility.SignUpBattleDeckData());
  164. yield return _waitForSeconds0_5;
  165. StartRandomMatch();
  166. yield return new WaitWhile(() => !PhotonNetwork.InRoom);
  167. yield return _waitForSeconds0_1;
  168. yield return ContinuousController.instance.StartCoroutine(loadingObject.EndLoading());
  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 Callback when joining a room fails
  242. public override void OnJoinRoomFailed(short returnCode, string message)
  243. {
  244. if (this.gameObject.activeSelf && !DoneCompleteMatching)
  245. {
  246. Debug.Log($"[RandomMatch] Join room failed: [{returnCode}] {message}, retrying...");
  247. RandomRoomName = null;
  248. startJoin = false;
  249. m = false;
  250. n = false;
  251. if (!PhotonNetwork.InLobby)
  252. {
  253. PhotonNetwork.JoinLobby();
  254. }
  255. StartRandomMatch();
  256. }
  257. }
  258. #endregion
  259. #region Process to create a room
  260. public IEnumerator CreateRoomCoroutine(bool isRandomMatch)
  261. {
  262. yield return new WaitWhile(() => !PhotonNetwork.IsConnectedAndReady);
  263. yield return new WaitWhile(() => !PhotonNetwork.InLobby);
  264. //Setting up the room to be created
  265. RoomOptions roomOptions = new RoomOptions
  266. {
  267. IsVisible = true, //Make the room visible in the lobby.
  268. IsOpen = true, //Allow other players to enter the room
  269. PublishUserId = true,
  270. MaxPlayers = 2,
  271. //To display room creator in room custom properties, store creator's name
  272. CustomRoomProperties = new ExitGames.Client.Photon.Hashtable()
  273. {
  274. { "RoomCreator",PhotonNetwork.NickName },
  275. },
  276. //Display custom property information in the lobby
  277. CustomRoomPropertiesForLobby = new string[]
  278. {
  279. "RoomCreator",
  280. }
  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. Button ReturnButtonButton;
  366. private void Start()
  367. {
  368. ReturnButtonButton = ReturnButton.transform.GetChild(0).GetComponent<Button>();
  369. }
  370. private void LateUpdate()
  371. {
  372. if (PhotonNetwork.InRoom)
  373. {
  374. if (PhotonNetwork.CurrentRoom.PlayerCount == PhotonNetwork.CurrentRoom.MaxPlayers)
  375. {
  376. if (PhotonNetwork.IsMasterClient)
  377. {
  378. StartCoroutine(GoNextScene());
  379. }
  380. if (ReturnButtonButton != null)
  381. {
  382. ReturnButtonButton.enabled = false;
  383. }
  384. }
  385. else
  386. {
  387. if (ReturnButtonButton != null)
  388. {
  389. ReturnButtonButton.enabled = true;
  390. }
  391. }
  392. }
  393. else
  394. {
  395. ReturnButtonButton.enabled = true;
  396. }
  397. if (DoneCompleteMatching)
  398. {
  399. if (ReturnButtonButton != null)
  400. {
  401. ReturnButton.SetActive(false);
  402. }
  403. }
  404. }
  405. #region After matching is complete, transition to the battle scene.
  406. bool DoneCompleteMatching = false;
  407. bool once1 = false;
  408. IEnumerator GoNextScene()
  409. {
  410. if (DoneCompleteMatching || once1)
  411. {
  412. yield break;
  413. }
  414. once1 = true;
  415. yield return _waitForSeconds0_1;
  416. PhotonNetwork.CurrentRoom.IsVisible = false;
  417. photonView.RPC("GoToBattleScene", RpcTarget.All);
  418. }
  419. #endregion
  420. [PunRPC]
  421. public void GoToBattleScene()
  422. {
  423. if (DoneCompleteMatching)
  424. {
  425. return;
  426. }
  427. endLoadingText = true;
  428. DoneCompleteMatching = true;
  429. TimeText.gameObject.SetActive(false);
  430. MessageText.text = LocalizeUtility.GetLocalizedString(
  431. EngMessage: "Matching completed!",
  432. JpnMessage: "マッチングしました!"
  433. );
  434. MessageText.transform.localPosition = new Vector3(-390, -193, 0);
  435. ReturnButton.SetActive(false);
  436. ContinuousController.instance.StartCoroutine(GoToBattleSceneCoroutine());
  437. }
  438. IEnumerator GoToBattleSceneCoroutine()
  439. {
  440. ContinuousController.instance.StartCoroutine(Opening.instance.OpeningBGM.FadeOut(0.2f));
  441. Debug.Log("Matching completed!");
  442. yield return _waitForSeconds0_1;
  443. //Opening.instance.MainCamera.gameObject.SetActive(false);
  444. foreach (Camera camera in Opening.instance.openingCameras)
  445. {
  446. camera.gameObject.SetActive(false);
  447. }
  448. yield return _waitForSeconds0_1;
  449. SceneManager.LoadSceneAsync("BattleScene", LoadSceneMode.Additive);
  450. yield return null;
  451. }
  452. }
  453. #region Generation of random strings
  454. public static class StringUtils
  455. {
  456. public static string GeneratePassword_AlpahabetNum(int length)
  457. {
  458. string PASSWORD_CHARS =
  459. "0123456789abcdefghijklmnopqrstuvwxyz";
  460. var sb = new System.Text.StringBuilder(length);
  461. var r = new System.Random();
  462. for (int i = 0; i < length; i++)
  463. {
  464. int pos = r.Next(PASSWORD_CHARS.Length);
  465. char c = PASSWORD_CHARS[pos];
  466. sb.Append(c);
  467. }
  468. return sb.ToString();
  469. }
  470. public static string GeneratePassword_Num(int length)
  471. {
  472. string PASSWORD_CHARS =
  473. "0123456789";
  474. var sb = new System.Text.StringBuilder(length);
  475. var r = new System.Random();
  476. for (int i = 0; i < length; i++)
  477. {
  478. int pos = r.Next(PASSWORD_CHARS.Length);
  479. char c = PASSWORD_CHARS[pos];
  480. sb.Append(c);
  481. }
  482. return sb.ToString();
  483. }
  484. }
  485. #endregion