LobbyManager_FriendMatch.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 Hashtable = ExitGames.Client.Photon.Hashtable;
  9. using System.Linq;
  10. using System;
  11. public class LobbyManager_FriendMatch : MonoBehaviourPunCallbacks
  12. {
  13. public GameObject RoomParent;//ScrolView content object
  14. public GameObject RoomElementPrefab;//Room Information Prefab
  15. public GameObject CreateRoomButton;
  16. public Text MessageText;
  17. public static LobbyManager_FriendMatch instance = null;
  18. string RandomKey = "ランダムマッチ部屋";
  19. public OptionPanel optionPanel;
  20. public void ReturnToTitle()
  21. {
  22. SceneManager.LoadSceneAsync("Opening");
  23. }
  24. private void Awake()
  25. {
  26. MessageText.text = "Connecting...";
  27. instance = this;
  28. optionPanel.Init();
  29. }
  30. private void Update()
  31. {
  32. if (PrivateLobby.activeSelf)
  33. {
  34. if (PhotonNetwork.InLobby)
  35. {
  36. CreateRoomButton.SetActive(true);
  37. ReturnButton.SetActive(true);
  38. }
  39. else
  40. {
  41. CreateRoomButton.SetActive(false);
  42. ReturnButton.SetActive(false);
  43. }
  44. }
  45. }
  46. #region GetRooms
  47. public void GetRooms(List<RoomInfo> roomInfo)
  48. {
  49. if (startJoin)
  50. {
  51. return;
  52. }
  53. m = false;
  54. n = false;
  55. if (roomInfo == null || roomInfo.Count == 0)
  56. {
  57. MessageText.text = "There is no room";
  58. return;
  59. }
  60. int roomCount = 0;
  61. for (int i = 0; i < roomInfo.Count; i++)
  62. {
  63. int p = roomInfo[i].PlayerCount;
  64. string n = roomInfo[i].Name;
  65. int m = roomInfo[i].MaxPlayers;
  66. object c = roomInfo[i].CustomProperties["RoomCreator"];
  67. if (p != 0 && m != 0 && c != null)
  68. {
  69. if (!n.Contains(RandomKey))
  70. {
  71. roomCount++;
  72. GameObject RoomElement = Instantiate(RoomElementPrefab, RoomParent.transform);
  73. RoomElement.GetComponent<CRoomElement>().SetRoomInfo(roomInfo[i].Name, roomInfo[i].PlayerCount, roomInfo[i].CustomProperties["RoomCreator"].ToString());
  74. RoomElement.GetComponent<CRoomElement>().OnClick = EnterRoom;
  75. void EnterRoom()
  76. {
  77. if (!PhotonNetwork.InLobby)
  78. {
  79. return;
  80. }
  81. PrivateRoom.SetActive(true);
  82. PrivateLobby.SetActive(false);
  83. StartCoroutine(wait());
  84. //Enter the room with roomname
  85. PhotonNetwork.JoinRoom(RoomElement.GetComponent<CRoomElement>().RoomName);
  86. MessageText.text = "When the two are ready, it begins.";
  87. }
  88. IEnumerator wait()
  89. {
  90. ReturnButton.SetActive(false);
  91. yield return new WaitWhile(() => !PhotonNetwork.InRoom);
  92. ReturnButton.SetActive(true);
  93. }
  94. }
  95. }
  96. }
  97. if (roomCount > 0)
  98. {
  99. MessageText.text = "There are " + roomCount + " rooms.";
  100. }
  101. }
  102. //Batch deletion of RoomElement
  103. public static void DestroyChildObject(Transform parent_trans)
  104. {
  105. for (int i = 0; i < parent_trans.childCount; ++i)
  106. {
  107. Destroy(parent_trans.GetChild(i).gameObject);
  108. }
  109. }
  110. #endregion
  111. bool m;
  112. bool n;
  113. public override void OnRoomListUpdate(List<RoomInfo> roomList)
  114. {
  115. if (!PhotonNetwork.InRoom && PhotonNetwork.InLobby && !m)
  116. {
  117. m = true;
  118. PhotonNetwork.LeaveLobby();
  119. }
  120. if (!PhotonNetwork.InRoom && PhotonNetwork.InLobby && n)
  121. {
  122. DestroyChildObject(RoomParent.transform); //Delete RoomElement
  123. GetRooms(roomList);
  124. }
  125. }
  126. public override void OnLeftLobby()
  127. {
  128. if (m)
  129. {
  130. n = true;
  131. PhotonNetwork.JoinLobby();
  132. }
  133. }
  134. // Callback called when connection to master server succeeds
  135. public override void OnConnectedToMaster()
  136. {
  137. PhotonNetwork.JoinLobby();
  138. }
  139. public override void OnJoinRoomFailed(short returnCode, string message)
  140. {
  141. if (isWaitingRandom)
  142. {
  143. startJoin = false;
  144. PrivateLobby.SetActive(false);
  145. PrivateRoom.SetActive(false);
  146. }
  147. }
  148. public IEnumerator CreateRoomCoroutine()
  149. {
  150. yield return new WaitWhile(() => !PhotonNetwork.InLobby);
  151. //Setting up the room to be created
  152. RoomOptions roomOptions = new RoomOptions();
  153. roomOptions.IsVisible = true; //Make the room visible in the lobby
  154. roomOptions.IsOpen = true; //Allow other players to enter the room
  155. roomOptions.PublishUserId = true;
  156. roomOptions.MaxPlayers = 2;
  157. //To display room creator in room custom properties, store creator's name
  158. roomOptions.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable()
  159. {
  160. { "RoomCreator",ContinuousController.instance.PlayerName },
  161. };
  162. //Display custom property information in the lobby
  163. roomOptions.CustomRoomPropertiesForLobby = new string[]
  164. {
  165. "RoomCreator",
  166. };
  167. string RoomName = StringUtils.GeneratePassword(8);
  168. //Create Room
  169. PhotonNetwork.CreateRoom(RoomName, roomOptions, null);
  170. }
  171. bool OnceCreatedPrivateRoom = false;
  172. public void OnClick_CreateRoomButton()
  173. {
  174. if (OnceCreatedPrivateRoom)
  175. {
  176. return;
  177. }
  178. OnceCreatedPrivateRoom = true;
  179. StartCoroutine(CreateRoomCoroutine());
  180. StartCoroutine(Wait_CreatePrivateRoom());
  181. }
  182. IEnumerator Wait_CreatePrivateRoom()
  183. {
  184. PrivateLobby.SetActive(false);
  185. ReturnButton.SetActive(false);
  186. MessageText.text = "Creating Room...";
  187. yield return new WaitWhile(() => !PhotonNetwork.InRoom);
  188. PrivateRoom.SetActive(true);
  189. ReturnButton.SetActive(true);
  190. MessageText.text = "When the two are ready, it begins.";
  191. OnceCreatedPrivateRoom = false;
  192. }
  193. public static class StringUtils
  194. {
  195. private const string PASSWORD_CHARS =
  196. "0123456789abcdefghijklmnopqrstuvwxyz";
  197. public static string GeneratePassword(int length)
  198. {
  199. var sb = new System.Text.StringBuilder(length);
  200. var r = new System.Random();
  201. for (int i = 0; i < length; i++)
  202. {
  203. int pos = r.Next(PASSWORD_CHARS.Length);
  204. char c = PASSWORD_CHARS[pos];
  205. sb.Append(c);
  206. }
  207. return sb.ToString();
  208. }
  209. }
  210. public GameObject PrivateLobby;
  211. public GameObject PrivateRoom;
  212. bool isWaitingRandom = false;
  213. bool startJoin = false;
  214. public GameObject ReturnButton;
  215. private void Start()
  216. {
  217. PrivateRoom.SetActive(false);
  218. PrivateLobby.SetActive(false);
  219. StartCoroutine(ConnectCoroutine());
  220. }
  221. IEnumerator ConnectCoroutine()
  222. {
  223. if (!PhotonNetwork.IsConnected)
  224. {
  225. yield return ContinuousController.instance.StartCoroutine(PhotonUtility.ConnectToMasterServerCoroutine());
  226. }
  227. MessageText.text = "Connecting...";
  228. yield return new WaitWhile(() => !PhotonNetwork.IsConnected);
  229. #region Save deck data to custom properties
  230. Hashtable hash = PhotonNetwork.LocalPlayer.CustomProperties;
  231. if (hash.TryGetValue(ContinuousController.DeckDataPropertyKey, out object value))
  232. {
  233. hash[ContinuousController.DeckDataPropertyKey] = ContinuousController.instance.BattleDeckData.GetThisDeckCode();
  234. }
  235. else
  236. {
  237. hash.Add(ContinuousController.DeckDataPropertyKey, ContinuousController.instance.BattleDeckData.GetThisDeckCode());
  238. }
  239. PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
  240. while (true)
  241. {
  242. Hashtable _hash = PhotonNetwork.LocalPlayer.CustomProperties;
  243. if (_hash.TryGetValue(ContinuousController.DeckDataPropertyKey, out value))
  244. {
  245. if ((string)value == ContinuousController.instance.BattleDeckData.GetThisDeckCode())
  246. {
  247. break;
  248. }
  249. }
  250. yield return null;
  251. }
  252. #endregion
  253. #region Save player name to custom properties
  254. hash = PhotonNetwork.LocalPlayer.CustomProperties;
  255. if (hash.TryGetValue(ContinuousController.PlayerNameKey, out value))
  256. {
  257. hash[ContinuousController.PlayerNameKey] = ContinuousController.instance.PlayerName;
  258. }
  259. else
  260. {
  261. hash.Add(ContinuousController.PlayerNameKey, ContinuousController.instance.PlayerName);
  262. }
  263. PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
  264. while (true)
  265. {
  266. Hashtable _hash = PhotonNetwork.LocalPlayer.CustomProperties;
  267. if (_hash.TryGetValue(ContinuousController.PlayerNameKey, out value))
  268. {
  269. if ((string)value == ContinuousController.instance.PlayerName)
  270. {
  271. break;
  272. }
  273. }
  274. yield return null;
  275. }
  276. #endregion
  277. #region Save the number of wins to a custom property
  278. hash = PhotonNetwork.LocalPlayer.CustomProperties;
  279. if (hash.TryGetValue(ContinuousController.WinCountKey, out value))
  280. {
  281. hash[ContinuousController.WinCountKey] = ContinuousController.instance.WinCount;
  282. }
  283. else
  284. {
  285. hash.Add(ContinuousController.WinCountKey, ContinuousController.instance.WinCount);
  286. }
  287. PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
  288. while (true)
  289. {
  290. Hashtable _hash = PhotonNetwork.LocalPlayer.CustomProperties;
  291. if (_hash.TryGetValue(ContinuousController.WinCountKey, out value))
  292. {
  293. if ((int)value == ContinuousController.instance.WinCount)
  294. {
  295. break;
  296. }
  297. }
  298. yield return null;
  299. }
  300. #endregion
  301. yield return new WaitWhile(() => !PhotonNetwork.InLobby);
  302. MessageText.text = "";
  303. PrivateRoom.SetActive(false);
  304. PrivateLobby.SetActive(true);
  305. }
  306. }