LobbyManager_RandomMatch.cs 14 KB

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