public void LeaveRoom() { if (joiningRoomId == -1 || !OnRoom) { return; } if (JoiningRoomData.Players.Count <= 1) { if (database.DeleteRoom(joiningRoomId)) { joiningRoomId = -1; JoiningRoomData = null; } return; } JoiningRoomData.Players.Remove(Player); if (JoiningRoomData.AdminPlayer.Equals(Player)) { JoiningRoomData.AdminPlayer = JoiningRoomData.Players[0]; } if (JoiningRoomData.HostPlayer != null && JoiningRoomData.HostPlayer.Equals(Player)) { //ホストが落ちる場合 } database.SetRoomData(joiningRoomId, MNRoomData.ToJson(JoiningRoomData)); OnRoom = false; }
public void JoinRoom(int roomId) { if (Player == null) { Debug.Log("player instance is null"); return; } MNRoomData roomData = MNRoomData.FromJson(database.GetRoomData(roomId)); if (roomData.Capacity != -1 && roomData.Capacity <= roomData.Players.Count) { Debug.Log("部屋が満員です!"); return; } roomData.JoinRequests.Add(Player); if (database.SetRoomData(roomId, MNRoomData.ToJson(roomData))) { joiningRoomId = roomId; JoiningRoomData = roomData; } else { Debug.Log("部屋への参加に失敗しました"); } }
public Dictionary <int, MNRoomData> GetRoomList() { Dictionary <int, MNRoomData> temp = new Dictionary <int, MNRoomData>(); foreach (KeyValuePair <int, string> pair in database.GetRoomList()) { temp.Add(pair.Key, MNRoomData.FromJson(pair.Value)); } return(temp); }
public void CreateRoom(string roomName) { if (Player == null) { Debug.Log("player instance is null"); return; } MNRoomData roomData = new MNRoomData(roomName, Player, GetServerCapacity()); string temp = MNRoomData.ToJson(roomData); Debug.Log("create room:\n" + temp); joiningRoomId = database.CreateRoom(temp); JoiningRoomData = roomData; OnRoom = true; }
public void StartSession() { JoiningRoomData.Status = MNRoomData.RoomStatus.DeterminingHost; database.SetRoomData(joiningRoomId, MNRoomData.ToJson(JoiningRoomData)); Debug.Log("send request that start session."); }
private void Database_OnRoomDataChangedEvent(int roomId) { if (roomId != joiningRoomId) { return; } string temp = database.GetRoomData(roomId); Debug.Log("datachanged:\n" + temp); MNRoomData roomData = MNRoomData.FromJson(temp); if (roomData.Status == MNRoomData.RoomStatus.Connecting) { if (roomData.HostPlayer.Equals(Player)) { Debug.Log("Starting host.."); OpenPort(); StartHost(); roomData.Status = MNRoomData.RoomStatus.InSession; database.SetRoomData(joiningRoomId, MNRoomData.ToJson(roomData)); } } if (OnRoom && roomData.Status == MNRoomData.RoomStatus.InSession && !OnRunningClient()) { if (!roomData.HostPlayer.Equals(Player)) { StartClient(GetProperAddress(roomData.HostPlayer)); } } //入室が承認された場合 if (!OnRoom && roomData.Players.Contains(Player)) { OnRoom = true; Debug.Log("入室が承認されました。"); } //自分がadminPlayerの場合にやること if (roomData.AdminPlayer.Equals(Player)) { //入室の承認 if (roomData.JoinRequests.Count > 0) { foreach (MNPlayer player in roomData.JoinRequests) { if (roomData.Capacity >= roomData.Players.Count) { roomData.Players.Add(player); } } roomData.JoinRequests.Clear(); database.SetRoomData(roomId, MNRoomData.ToJson(roomData)); } //セッションを開始する if (roomData.Status == MNRoomData.RoomStatus.DeterminingHost) { Debug.Log("ホスト決定を開始"); HostDeterminer determiner = new HostDeterminer(roomData.Players); determiner.Run(); if (determiner.GetHost() == null) { Debug.Log("ホスト可能なプレイヤーが存在しません!"); roomData.Status = MNRoomData.RoomStatus.WaitingPlayer; database.SetRoomData(roomId, MNRoomData.ToJson(roomData)); } else { roomData.HostPlayer = determiner.GetHost(); roomData.Status = MNRoomData.RoomStatus.Connecting; database.SetRoomData(roomId, MNRoomData.ToJson(roomData)); Debug.Log("Start Session!"); } } } JoiningRoomData = roomData; }
public static string ToJson(MNRoomData data) { //return JsonUtility.ToJson(data); return(JsonConvert.SerializeObject(data)); }