LobbyManager_RandomMatch.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 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. StartRandomMatch();
  252. }
  253. }
  254. #endregion
  255. #region Process to create a room
  256. public IEnumerator CreateRoomCoroutine(bool isRandomMatch)
  257. {
  258. yield return new WaitWhile(() => !PhotonNetwork.IsConnectedAndReady);
  259. yield return new WaitWhile(() => !PhotonNetwork.InLobby);
  260. //Setting up the room to be created
  261. RoomOptions roomOptions = new RoomOptions();
  262. roomOptions.IsVisible = true; //Make the room visible in the lobby.
  263. roomOptions.IsOpen = true; //Allow other players to enter the room
  264. roomOptions.PublishUserId = true;
  265. roomOptions.MaxPlayers = 2;
  266. //To display room creator in room custom properties, store creator's name
  267. roomOptions.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable()
  268. {
  269. { "RoomCreator",PhotonNetwork.NickName },
  270. };
  271. //Display custom property information in the lobby
  272. roomOptions.CustomRoomPropertiesForLobby = new string[]
  273. {
  274. "RoomCreator",
  275. };
  276. string RoomName = StringUtils.GeneratePassword_AlpahabetNum(8);
  277. if (isRandomMatch)
  278. {
  279. RoomName += RandomKey;
  280. }
  281. //Room Creation
  282. PhotonNetwork.CreateRoom(RoomName, roomOptions, null);
  283. while (!PhotonNetwork.InRoom)
  284. {
  285. yield return null;
  286. }
  287. endLoadingText = true;
  288. yield return new WaitForSeconds(0.2f);
  289. endLoadingText = false;
  290. ContinuousController.instance.LoadingTextCoroutine = StartCoroutine(SetWaitingText(
  291. LocalizeUtility.GetLocalizedString(
  292. EngMessage: "Matching",
  293. JpnMessage: "マッチング中"
  294. )));
  295. MessageText.transform.localPosition = new Vector3(-148, -193, 0);
  296. if (ReturnButton != null)
  297. {
  298. ReturnButton.SetActive(true);
  299. }
  300. }
  301. #endregion
  302. #region Random match starts after entering the lobby
  303. public void StartRandomMatch()
  304. {
  305. if (ReturnButton != null)
  306. {
  307. ReturnButton.SetActive(true);
  308. }
  309. //If there is no room, make room.
  310. if (String.IsNullOrEmpty(RandomRoomName))
  311. {
  312. StartCoroutine(CreateRoomCoroutine(true));
  313. }
  314. //If there's room, I'll go in.
  315. else
  316. {
  317. startJoin = true;
  318. PhotonNetwork.JoinRoom(RandomRoomName);
  319. }
  320. }
  321. #endregion
  322. private IEnumerator SetWaitingText(string defaultString)
  323. {
  324. if (isCoroutineRunning)
  325. {
  326. yield break; // Exit if already running
  327. }
  328. isCoroutineRunning = true;
  329. float waitTime = 0.18f;
  330. int count = 0;
  331. while (!endLoadingText)
  332. {
  333. count++;
  334. if (count >= 4)
  335. {
  336. count = 0;
  337. }
  338. MessageText.text = defaultString;
  339. for (int i = 0; i < count; i++)
  340. {
  341. MessageText.text += ".";
  342. }
  343. yield return new WaitForSeconds(waitTime);
  344. }
  345. isCoroutineRunning = false;
  346. }
  347. public void StartLoadingText(string defaultString)
  348. {
  349. if (!isCoroutineRunning)
  350. {
  351. endLoadingText = false; // Ensure the flag is reset when starting
  352. StartCoroutine(SetWaitingText(defaultString));
  353. }
  354. }
  355. public void StopLoadingText()
  356. {
  357. endLoadingText = true;
  358. }
  359. bool count = true;
  360. float timer = 0;
  361. Button ReturnButtonButton;
  362. private void Start()
  363. {
  364. ReturnButtonButton = ReturnButton.transform.GetChild(0).GetComponent<Button>();
  365. }
  366. private void LateUpdate()
  367. {
  368. if (PhotonNetwork.InRoom)
  369. {
  370. if (PhotonNetwork.CurrentRoom.PlayerCount == PhotonNetwork.CurrentRoom.MaxPlayers)
  371. {
  372. if (PhotonNetwork.IsMasterClient)
  373. {
  374. StartCoroutine(GoNextScene());
  375. }
  376. if (ReturnButtonButton != null)
  377. {
  378. ReturnButtonButton.enabled = false;
  379. }
  380. }
  381. else
  382. {
  383. if (ReturnButtonButton != null)
  384. {
  385. ReturnButtonButton.enabled = true;
  386. }
  387. }
  388. }
  389. else
  390. {
  391. ReturnButtonButton.enabled = true;
  392. }
  393. if (DoneCompleteMatching)
  394. {
  395. if (ReturnButtonButton != null)
  396. {
  397. ReturnButton.SetActive(false);
  398. }
  399. }
  400. }
  401. #region After matching is complete, transition to the battle scene.
  402. bool DoneCompleteMatching = false;
  403. bool once1 = false;
  404. IEnumerator GoNextScene()
  405. {
  406. if (DoneCompleteMatching || once1)
  407. {
  408. yield break;
  409. }
  410. once1 = true;
  411. yield return new WaitForSeconds(0.1f);
  412. PhotonNetwork.CurrentRoom.IsVisible = false;
  413. photonView.RPC("GoToBattleScene", RpcTarget.All);
  414. }
  415. #endregion
  416. [PunRPC]
  417. public void GoToBattleScene()
  418. {
  419. if (DoneCompleteMatching)
  420. {
  421. return;
  422. }
  423. endLoadingText = true;
  424. DoneCompleteMatching = true;
  425. TimeText.gameObject.SetActive(false);
  426. MessageText.text = LocalizeUtility.GetLocalizedString(
  427. EngMessage: "Matching completed!",
  428. JpnMessage: "マッチングしました!"
  429. );
  430. MessageText.transform.localPosition = new Vector3(-390, -193, 0);
  431. ReturnButton.SetActive(false);
  432. ContinuousController.instance.StartCoroutine(GoToBattleSceneCoroutine());
  433. }
  434. IEnumerator GoToBattleSceneCoroutine()
  435. {
  436. ContinuousController.instance.StartCoroutine(Opening.instance.OpeningBGM.FadeOut(0.2f));
  437. Debug.Log("Matching complted!");
  438. yield return new WaitForSeconds(0.1f);
  439. //Opening.instance.MainCamera.gameObject.SetActive(false);
  440. foreach (Camera camera in Opening.instance.openingCameras)
  441. {
  442. camera.gameObject.SetActive(false);
  443. }
  444. yield return new WaitForSeconds(0.1f);
  445. SceneManager.LoadSceneAsync("BattleScene", LoadSceneMode.Additive);
  446. yield return null;
  447. }
  448. }
  449. #region Generation of random strings
  450. public static class StringUtils
  451. {
  452. public static string GeneratePassword_AlpahabetNum(int length)
  453. {
  454. string PASSWORD_CHARS =
  455. "0123456789abcdefghijklmnopqrstuvwxyz";
  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. public static string GeneratePassword_Num(int length)
  467. {
  468. string PASSWORD_CHARS =
  469. "0123456789";
  470. var sb = new System.Text.StringBuilder(length);
  471. var r = new System.Random();
  472. for (int i = 0; i < length; i++)
  473. {
  474. int pos = r.Next(PASSWORD_CHARS.Length);
  475. char c = PASSWORD_CHARS[pos];
  476. sb.Append(c);
  477. }
  478. return sb.ToString();
  479. }
  480. }
  481. #endregion