| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using Photon.Pun;
- using Photon.Realtime;
- using UnityEngine.SceneManagement;
- using Hashtable = ExitGames.Client.Photon.Hashtable;
- using System.Linq;
- using System;
- public class LobbyManager_FriendMatch : MonoBehaviourPunCallbacks
- {
- public GameObject RoomParent;//ScrolView content object
- public GameObject RoomElementPrefab;//Room Information Prefab
- public GameObject CreateRoomButton;
- public Text MessageText;
- public static LobbyManager_FriendMatch instance = null;
- string RandomKey = "ランダムマッチ部屋";
- public OptionPanel optionPanel;
- public void ReturnToTitle()
- {
- SceneManager.LoadSceneAsync("Opening");
- }
- private void Awake()
- {
- MessageText.text = "Connecting...";
- instance = this;
- optionPanel.Init();
- }
- private void Update()
- {
- if (PrivateLobby.activeSelf)
- {
- if (PhotonNetwork.InLobby)
- {
- CreateRoomButton.SetActive(true);
- ReturnButton.SetActive(true);
- }
- else
- {
- CreateRoomButton.SetActive(false);
- ReturnButton.SetActive(false);
- }
- }
- }
- #region GetRooms
- public void GetRooms(List<RoomInfo> roomInfo)
- {
- if (startJoin)
- {
- return;
- }
- m = false;
- n = false;
- if (roomInfo == null || roomInfo.Count == 0)
- {
- MessageText.text = "There is no room";
- return;
- }
- int roomCount = 0;
- for (int i = 0; i < roomInfo.Count; i++)
- {
- int p = roomInfo[i].PlayerCount;
- string n = roomInfo[i].Name;
- int m = roomInfo[i].MaxPlayers;
- object c = roomInfo[i].CustomProperties["RoomCreator"];
- if (p != 0 && m != 0 && c != null)
- {
- if (!n.Contains(RandomKey))
- {
- roomCount++;
- GameObject RoomElement = Instantiate(RoomElementPrefab, RoomParent.transform);
- RoomElement.GetComponent<CRoomElement>().SetRoomInfo(roomInfo[i].Name, roomInfo[i].PlayerCount, roomInfo[i].CustomProperties["RoomCreator"].ToString());
- RoomElement.GetComponent<CRoomElement>().OnClick = EnterRoom;
- void EnterRoom()
- {
- if (!PhotonNetwork.InLobby)
- {
- return;
- }
- PrivateRoom.SetActive(true);
- PrivateLobby.SetActive(false);
- StartCoroutine(wait());
- //Enter the room with roomname
- PhotonNetwork.JoinRoom(RoomElement.GetComponent<CRoomElement>().RoomName);
- MessageText.text = "When the two are ready, it begins.";
- }
- IEnumerator wait()
- {
- ReturnButton.SetActive(false);
- yield return new WaitWhile(() => !PhotonNetwork.InRoom);
- ReturnButton.SetActive(true);
- }
- }
- }
- }
- if (roomCount > 0)
- {
- MessageText.text = "There are " + roomCount + " rooms.";
- }
- }
- //Batch deletion of RoomElement
- public static void DestroyChildObject(Transform parent_trans)
- {
- for (int i = 0; i < parent_trans.childCount; ++i)
- {
- Destroy(parent_trans.GetChild(i).gameObject);
- }
- }
- #endregion
- bool m;
- bool n;
- public override void OnRoomListUpdate(List<RoomInfo> roomList)
- {
- if (!PhotonNetwork.InRoom && PhotonNetwork.InLobby && !m)
- {
- m = true;
- PhotonNetwork.LeaveLobby();
- }
- if (!PhotonNetwork.InRoom && PhotonNetwork.InLobby && n)
- {
- DestroyChildObject(RoomParent.transform); //Delete RoomElement
- GetRooms(roomList);
- }
- }
- public override void OnLeftLobby()
- {
- if (m)
- {
- n = true;
- PhotonNetwork.JoinLobby();
- }
- }
- // Callback called when connection to master server succeeds
- public override void OnConnectedToMaster()
- {
- PhotonNetwork.JoinLobby();
- }
- public override void OnJoinRoomFailed(short returnCode, string message)
- {
- if (isWaitingRandom)
- {
- startJoin = false;
- PrivateLobby.SetActive(false);
- PrivateRoom.SetActive(false);
- }
- }
- public IEnumerator CreateRoomCoroutine()
- {
- yield return new WaitWhile(() => !PhotonNetwork.InLobby);
- //Setting up the room to be created
- RoomOptions roomOptions = new RoomOptions();
- roomOptions.IsVisible = true; //Make the room visible in the lobby
- roomOptions.IsOpen = true; //Allow other players to enter the room
- roomOptions.PublishUserId = true;
- roomOptions.MaxPlayers = 2;
- //To display room creator in room custom properties, store creator's name
- roomOptions.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable()
- {
- { "RoomCreator",ContinuousController.instance.PlayerName },
- };
- //Display custom property information in the lobby
- roomOptions.CustomRoomPropertiesForLobby = new string[]
- {
- "RoomCreator",
- };
- string RoomName = StringUtils.GeneratePassword(8);
- //Create Room
- PhotonNetwork.CreateRoom(RoomName, roomOptions, null);
- }
- bool OnceCreatedPrivateRoom = false;
- public void OnClick_CreateRoomButton()
- {
- if (OnceCreatedPrivateRoom)
- {
- return;
- }
- OnceCreatedPrivateRoom = true;
- StartCoroutine(CreateRoomCoroutine());
- StartCoroutine(Wait_CreatePrivateRoom());
- }
- IEnumerator Wait_CreatePrivateRoom()
- {
- PrivateLobby.SetActive(false);
- ReturnButton.SetActive(false);
- MessageText.text = "Creating Room...";
- yield return new WaitWhile(() => !PhotonNetwork.InRoom);
- PrivateRoom.SetActive(true);
- ReturnButton.SetActive(true);
- MessageText.text = "When the two are ready, it begins.";
- OnceCreatedPrivateRoom = false;
- }
- public static class StringUtils
- {
- private const string PASSWORD_CHARS =
- "0123456789abcdefghijklmnopqrstuvwxyz";
- public static string GeneratePassword(int length)
- {
- var sb = new System.Text.StringBuilder(length);
- var r = new System.Random();
- for (int i = 0; i < length; i++)
- {
- int pos = r.Next(PASSWORD_CHARS.Length);
- char c = PASSWORD_CHARS[pos];
- sb.Append(c);
- }
- return sb.ToString();
- }
- }
- public GameObject PrivateLobby;
- public GameObject PrivateRoom;
- bool isWaitingRandom = false;
- bool startJoin = false;
- public GameObject ReturnButton;
- private void Start()
- {
- PrivateRoom.SetActive(false);
- PrivateLobby.SetActive(false);
- StartCoroutine(ConnectCoroutine());
- }
- IEnumerator ConnectCoroutine()
- {
- if (!PhotonNetwork.IsConnected)
- {
- yield return ContinuousController.instance.StartCoroutine(PhotonUtility.ConnectToMasterServerCoroutine());
- }
- MessageText.text = "Connecting...";
- yield return new WaitWhile(() => !PhotonNetwork.IsConnected);
- #region Save deck data to custom properties
- Hashtable hash = PhotonNetwork.LocalPlayer.CustomProperties;
- if (hash.TryGetValue(ContinuousController.DeckDataPropertyKey, out object value))
- {
- hash[ContinuousController.DeckDataPropertyKey] = ContinuousController.instance.BattleDeckData.GetThisDeckCode();
- }
- else
- {
- hash.Add(ContinuousController.DeckDataPropertyKey, ContinuousController.instance.BattleDeckData.GetThisDeckCode());
- }
- PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
- while (true)
- {
- Hashtable _hash = PhotonNetwork.LocalPlayer.CustomProperties;
- if (_hash.TryGetValue(ContinuousController.DeckDataPropertyKey, out value))
- {
- if ((string)value == ContinuousController.instance.BattleDeckData.GetThisDeckCode())
- {
- break;
- }
- }
- yield return null;
- }
- #endregion
- #region Save player name to custom properties
- hash = PhotonNetwork.LocalPlayer.CustomProperties;
- if (hash.TryGetValue(ContinuousController.PlayerNameKey, out value))
- {
- hash[ContinuousController.PlayerNameKey] = ContinuousController.instance.PlayerName;
- }
- else
- {
- hash.Add(ContinuousController.PlayerNameKey, ContinuousController.instance.PlayerName);
- }
- PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
- while (true)
- {
- Hashtable _hash = PhotonNetwork.LocalPlayer.CustomProperties;
- if (_hash.TryGetValue(ContinuousController.PlayerNameKey, out value))
- {
- if ((string)value == ContinuousController.instance.PlayerName)
- {
- break;
- }
- }
- yield return null;
- }
- #endregion
- #region Save the number of wins to a custom property
- hash = PhotonNetwork.LocalPlayer.CustomProperties;
- if (hash.TryGetValue(ContinuousController.WinCountKey, out value))
- {
- hash[ContinuousController.WinCountKey] = ContinuousController.instance.WinCount;
- }
- else
- {
- hash.Add(ContinuousController.WinCountKey, ContinuousController.instance.WinCount);
- }
- PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
- while (true)
- {
- Hashtable _hash = PhotonNetwork.LocalPlayer.CustomProperties;
- if (_hash.TryGetValue(ContinuousController.WinCountKey, out value))
- {
- if ((int)value == ContinuousController.instance.WinCount)
- {
- break;
- }
- }
- yield return null;
- }
- #endregion
- yield return new WaitWhile(() => !PhotonNetwork.InLobby);
- MessageText.text = "";
- PrivateRoom.SetActive(false);
- PrivateLobby.SetActive(true);
- }
- }
|