LobbyManager_RandomMatch.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. yield return ContinuousController.instance.StartCoroutine(PhotonUtility.DisconnectCoroutine());
  80. OffLobby();
  81. yield return ContinuousController.instance.StartCoroutine(disconnectLoadingObject.EndLoading());
  82. }
  83. #endregion
  84. #region 初期化
  85. public IEnumerator Init()
  86. {
  87. n = false;
  88. m = false;
  89. once1 = false;
  90. endLoadingText = false;
  91. time = 0;
  92. RandomRoomName = "";
  93. startJoin = false;
  94. DoneCompleteMatching = false;
  95. ContinuousController.instance.LoadingTextCoroutine = null;
  96. ReturnButton.SetActive(true);
  97. MessageText.text = "";
  98. TimeText.gameObject.SetActive(true);
  99. StartCoroutine(TimeCountUp());
  100. timer = 0;
  101. count = true;
  102. if (PhotonNetwork.InLobby)
  103. {
  104. PhotonNetwork.LeaveLobby();
  105. }
  106. yield return new WaitWhile(() => PhotonNetwork.InLobby);
  107. if (PhotonNetwork.IsConnected)
  108. {
  109. PhotonNetwork.Disconnect();
  110. }
  111. yield return new WaitWhile(() => PhotonNetwork.IsConnected);
  112. }
  113. #endregion
  114. #region Time text count up
  115. int time = 0;
  116. IEnumerator TimeCountUp()
  117. {
  118. time = 0;
  119. TimeText.gameObject.SetActive(true);
  120. while (!DoneCompleteMatching)
  121. {
  122. time += (int)Time.deltaTime;
  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 new WaitForSeconds(1f);
  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 new WaitForSeconds(0.5f);
  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 new WaitForSeconds(0.5f);
  165. StartRandomMatch();
  166. yield return new WaitWhile(() => !PhotonNetwork.InRoom);
  167. yield return new WaitForSeconds(0.1f);
  168. yield return ContinuousController.instance.StartCoroutine(loadingObject.EndLoading());
  169. ContinuousController.instance.StartCoroutine(TimeCountUp());
  170. ReturnButton.SetActive(true);
  171. }
  172. #endregion
  173. #region ReturnToTitle()
  174. public void ReturnToTitle()
  175. {
  176. SceneManager.LoadSceneAsync("Opening");
  177. DoneCompleteMatching = true;
  178. }
  179. #endregion
  180. #region Callback on room list update
  181. bool m;
  182. bool n;
  183. public override void OnRoomListUpdate(List<RoomInfo> roomList)
  184. {
  185. if (this.gameObject.activeSelf)
  186. {
  187. if (!PhotonNetwork.InRoom && PhotonNetwork.InLobby && !m)
  188. {
  189. m = true;
  190. PhotonNetwork.LeaveLobby();
  191. }
  192. if (!PhotonNetwork.InRoom && PhotonNetwork.InLobby && n)
  193. {
  194. GetRandomMatcingRoom(roomList);
  195. }
  196. }
  197. }
  198. #endregion
  199. #region Search for random matching rooms available
  200. public void GetRandomMatcingRoom(List<RoomInfo> roomInfo)
  201. {
  202. if (startJoin)
  203. {
  204. return;
  205. }
  206. m = false;
  207. n = false;
  208. if (roomInfo == null || roomInfo.Count == 0)
  209. {
  210. return;
  211. }
  212. RandomRoomName = null;
  213. for (int i = 0; i < roomInfo.Count; i++)
  214. {
  215. int p = roomInfo[i].PlayerCount;
  216. string n = roomInfo[i].Name;
  217. int m = roomInfo[i].MaxPlayers;
  218. object c = roomInfo[i].CustomProperties["RoomCreator"];
  219. if (p != 0 && m != 0 && c != null)
  220. {
  221. if (n.Contains(RandomKey))
  222. {
  223. RandomRoomName = n;
  224. }
  225. }
  226. }
  227. }
  228. #endregion
  229. #region Callback when leaving the lobby
  230. public override void OnLeftLobby()
  231. {
  232. if (this.gameObject.activeSelf)
  233. {
  234. if (m)
  235. {
  236. n = true;
  237. PhotonNetwork.JoinLobby();
  238. }
  239. }
  240. }
  241. #endregion
  242. #region Callback when joining a room fails
  243. public override void OnJoinRoomFailed(short returnCode, string message)
  244. {
  245. if (this.gameObject.activeSelf && !DoneCompleteMatching)
  246. {
  247. Debug.Log($"[RandomMatch] Join room failed: [{returnCode}] {message}, retrying...");
  248. RandomRoomName = null;
  249. startJoin = false;
  250. m = false;
  251. n = false;
  252. if (!PhotonNetwork.InLobby)
  253. {
  254. PhotonNetwork.JoinLobby();
  255. }
  256. StartRandomMatch();
  257. }
  258. }
  259. #endregion
  260. #region Process to create a room
  261. public IEnumerator CreateRoomCoroutine(bool isRandomMatch)
  262. {
  263. yield return new WaitWhile(() => !PhotonNetwork.IsConnectedAndReady);
  264. yield return new WaitWhile(() => !PhotonNetwork.InLobby);
  265. //Setting up the room to be created
  266. RoomOptions roomOptions = new RoomOptions();
  267. roomOptions.IsVisible = true; //Make the room visible in the lobby.
  268. roomOptions.IsOpen = true; //Allow other players to enter the room
  269. roomOptions.PublishUserId = true;
  270. roomOptions.MaxPlayers = 2;
  271. //To display room creator in room custom properties, store creator's name
  272. roomOptions.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable()
  273. {
  274. { "RoomCreator",PhotonNetwork.NickName },
  275. };
  276. //Display custom property information in the lobby
  277. roomOptions.CustomRoomPropertiesForLobby = new string[]
  278. {
  279. "RoomCreator",
  280. };
  281. string RoomName = StringUtils.GeneratePassword_AlpahabetNum(8);
  282. if (isRandomMatch)
  283. {
  284. RoomName += RandomKey;
  285. }
  286. //Room Creation
  287. PhotonNetwork.CreateRoom(RoomName, roomOptions, null);
  288. while (!PhotonNetwork.InRoom)
  289. {
  290. yield return null;
  291. }
  292. endLoadingText = true;
  293. yield return new WaitForSeconds(0.2f);
  294. endLoadingText = false;
  295. ContinuousController.instance.LoadingTextCoroutine = StartCoroutine(SetWaitingText(
  296. LocalizeUtility.GetLocalizedString(
  297. EngMessage: "Matching",
  298. JpnMessage: "マッチング中"
  299. )));
  300. MessageText.transform.localPosition = new Vector3(-148, -193, 0);
  301. if (ReturnButton != null)
  302. {
  303. ReturnButton.SetActive(true);
  304. }
  305. }
  306. #endregion
  307. #region Random match starts after entering the lobby
  308. public void StartRandomMatch()
  309. {
  310. if (ReturnButton != null)
  311. {
  312. ReturnButton.SetActive(true);
  313. }
  314. //If there is no room, make room.
  315. if (String.IsNullOrEmpty(RandomRoomName))
  316. {
  317. StartCoroutine(CreateRoomCoroutine(true));
  318. }
  319. //If there's room, I'll go in.
  320. else
  321. {
  322. startJoin = true;
  323. PhotonNetwork.JoinRoom(RandomRoomName);
  324. }
  325. }
  326. #endregion
  327. private IEnumerator SetWaitingText(string defaultString)
  328. {
  329. if (isCoroutineRunning)
  330. {
  331. yield break; // Exit if already running
  332. }
  333. isCoroutineRunning = true;
  334. float waitTime = 0.18f;
  335. int count = 0;
  336. while (!endLoadingText)
  337. {
  338. count++;
  339. if (count >= 4)
  340. {
  341. count = 0;
  342. }
  343. MessageText.text = defaultString;
  344. for (int i = 0; i < count; i++)
  345. {
  346. MessageText.text += ".";
  347. }
  348. yield return new WaitForSeconds(waitTime);
  349. }
  350. isCoroutineRunning = false;
  351. }
  352. public void StartLoadingText(string defaultString)
  353. {
  354. if (!isCoroutineRunning)
  355. {
  356. endLoadingText = false; // Ensure the flag is reset when starting
  357. StartCoroutine(SetWaitingText(defaultString));
  358. }
  359. }
  360. public void StopLoadingText()
  361. {
  362. endLoadingText = true;
  363. }
  364. bool count = true;
  365. float timer = 0;
  366. Button ReturnButtonButton;
  367. private void Start()
  368. {
  369. ReturnButtonButton = ReturnButton.transform.GetChild(0).GetComponent<Button>();
  370. }
  371. private void LateUpdate()
  372. {
  373. if (PhotonNetwork.InRoom)
  374. {
  375. if (PhotonNetwork.CurrentRoom.PlayerCount == PhotonNetwork.CurrentRoom.MaxPlayers)
  376. {
  377. if (PhotonNetwork.IsMasterClient)
  378. {
  379. StartCoroutine(GoNextScene());
  380. }
  381. if (ReturnButtonButton != null)
  382. {
  383. ReturnButtonButton.enabled = false;
  384. }
  385. }
  386. else
  387. {
  388. if (ReturnButtonButton != null)
  389. {
  390. ReturnButtonButton.enabled = true;
  391. }
  392. }
  393. }
  394. else
  395. {
  396. ReturnButtonButton.enabled = true;
  397. }
  398. if (DoneCompleteMatching)
  399. {
  400. if (ReturnButtonButton != null)
  401. {
  402. ReturnButton.SetActive(false);
  403. }
  404. }
  405. }
  406. #region After matching is complete, transition to the battle scene.
  407. bool DoneCompleteMatching = false;
  408. bool once1 = false;
  409. IEnumerator GoNextScene()
  410. {
  411. if (DoneCompleteMatching || once1)
  412. {
  413. yield break;
  414. }
  415. once1 = true;
  416. yield return new WaitForSeconds(0.1f);
  417. PhotonNetwork.CurrentRoom.IsVisible = false;
  418. photonView.RPC("GoToBattleScene", RpcTarget.All);
  419. }
  420. #endregion
  421. [PunRPC]
  422. public void GoToBattleScene()
  423. {
  424. if (DoneCompleteMatching)
  425. {
  426. return;
  427. }
  428. endLoadingText = true;
  429. DoneCompleteMatching = true;
  430. TimeText.gameObject.SetActive(false);
  431. MessageText.text = LocalizeUtility.GetLocalizedString(
  432. EngMessage: "Matching completed!",
  433. JpnMessage: "マッチングしました!"
  434. );
  435. MessageText.transform.localPosition = new Vector3(-390, -193, 0);
  436. ReturnButton.SetActive(false);
  437. ContinuousController.instance.StartCoroutine(GoToBattleSceneCoroutine());
  438. }
  439. IEnumerator GoToBattleSceneCoroutine()
  440. {
  441. ContinuousController.instance.StartCoroutine(Opening.instance.OpeningBGM.FadeOut(0.2f));
  442. Debug.Log("Matching complted!");
  443. yield return new WaitForSeconds(0.1f);
  444. //Opening.instance.MainCamera.gameObject.SetActive(false);
  445. foreach (Camera camera in Opening.instance.openingCameras)
  446. {
  447. camera.gameObject.SetActive(false);
  448. }
  449. yield return new WaitForSeconds(0.1f);
  450. SceneManager.LoadSceneAsync("BattleScene", LoadSceneMode.Additive);
  451. yield return null;
  452. }
  453. }
  454. #region Generation of random strings
  455. public static class StringUtils
  456. {
  457. public static string GeneratePassword_AlpahabetNum(int length)
  458. {
  459. string PASSWORD_CHARS =
  460. "0123456789abcdefghijklmnopqrstuvwxyz";
  461. var sb = new System.Text.StringBuilder(length);
  462. var r = new System.Random();
  463. for (int i = 0; i < length; i++)
  464. {
  465. int pos = r.Next(PASSWORD_CHARS.Length);
  466. char c = PASSWORD_CHARS[pos];
  467. sb.Append(c);
  468. }
  469. return sb.ToString();
  470. }
  471. public static string GeneratePassword_Num(int length)
  472. {
  473. string PASSWORD_CHARS =
  474. "0123456789";
  475. var sb = new System.Text.StringBuilder(length);
  476. var r = new System.Random();
  477. for (int i = 0; i < length; i++)
  478. {
  479. int pos = r.Next(PASSWORD_CHARS.Length);
  480. char c = PASSWORD_CHARS[pos];
  481. sb.Append(c);
  482. }
  483. return sb.ToString();
  484. }
  485. }
  486. #endregion