LobbyManager_RandomMatch.cs 15 KB

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