From 52f28f98a76a435285423e1e358efeb384b10693 Mon Sep 17 00:00:00 2001 From: Regalis Date: Tue, 24 May 2016 19:10:11 +0300 Subject: [PATCH] - server asks the clients whether they're ready to start a round, giving some time to load the selected sub before the round starts - fixed joining clients not being informed about vote status or voting being enabled --- Subsurface/Source/Networking/GameClient.cs | 11 ++++ Subsurface/Source/Networking/GameServer.cs | 50 ++++++++++++++++--- Subsurface/Source/Networking/NetworkMember.cs | 2 +- Subsurface/Source/Screens/NetLobbyScreen.cs | 8 +-- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/Subsurface/Source/Networking/GameClient.cs b/Subsurface/Source/Networking/GameClient.cs index c1ad086d7..e4f459224 100644 --- a/Subsurface/Source/Networking/GameClient.cs +++ b/Subsurface/Source/Networking/GameClient.cs @@ -487,6 +487,17 @@ namespace Barotrauma.Networking switch (packetType) { + case (byte)PacketTypes.CanStartGame: + string subName = inc.ReadString(); + string subHash = inc.ReadString(); + + if (GameMain.NetLobbyScreen.TrySelectSub(subName,subHash)) + { + NetOutgoingMessage readyToStartMsg = client.CreateMessage(); + readyToStartMsg.Write((byte)PacketTypes.StartGame); + client.SendMessage(readyToStartMsg, NetDeliveryMethod.ReliableUnordered); + } + break; case (byte)PacketTypes.StartGame: if (Screen.Selected == GameMain.GameScreen) continue; diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index 66bed8f45..352369a62 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -474,6 +474,8 @@ namespace Barotrauma.Networking //send the message to everyone except the client who just logged in SendMessage(outmsg, NetDeliveryMethod.ReliableUnordered, inc.SenderConnection); + UpdateVoteStatus(); + AddChatMessage(sender.name + " has joined the server", ChatMessageType.Server); } } @@ -521,6 +523,9 @@ namespace Barotrauma.Networking case (byte)PacketTypes.PlayerLeft: DisconnectClient(inc.SenderConnection); break; + case (byte)PacketTypes.StartGame: + dataSender.ReadyToStart = true; + break; case (byte)PacketTypes.CharacterInfo: ReadCharacterData(inc); break; @@ -871,17 +876,38 @@ namespace Barotrauma.Networking { GameMain.NetLobbyScreen.StartButton.Enabled = false; - if (Voting.AllowSubVoting) yield return new WaitForSeconds(1.0f); + NetOutgoingMessage msg = server.CreateMessage(); + msg.Write((byte)PacketTypes.CanStartGame); + msg.Write(selectedSub.Name); + msg.Write(selectedSub.MD5Hash.Hash); - while (ConnectedClients.Any(c => c.FileStreamSender != null && c.FileStreamSender.FilePath == selectedSub.FilePath)) + SendMessage(msg, NetDeliveryMethod.ReliableUnordered); + + ConnectedClients.ForEach(c => c.ReadyToStart = false); + + float waitForResponseTimer = 5.0f; + while (ConnectedClients.Any(c => !c.ReadyToStart) && waitForResponseTimer > 0.0f) { + waitForResponseTimer -= CoroutineManager.UnscaledDeltaTime; + yield return CoroutineStatus.Running; + } + + float fileTransferTimeOut = 60.0f; + while (ConnectedClients.Any(c => c.FileStreamSender != null && c.FileStreamSender.FilePath == selectedSub.FilePath) && fileTransferTimeOut>0.0f) + { + fileTransferTimeOut -= CoroutineManager.UnscaledDeltaTime; + if (GUIMessageBox.MessageBoxes.Peek() == null) { - new GUIMessageBox("File transfer in progress", - "The round will be started after the submarine file has been sent to all players.", 400, 400); + var messageBox = new GUIMessageBox("File transfer in progress", + "The round will be started after the submarine file has been sent to all players.", new string[] {"Cancel transfer"}, 400, 400); + messageBox.Buttons[0].UserData = ConnectedClients.Find(c => c.FileStreamSender != null && c.FileStreamSender.FilePath == selectedSub.FilePath); + messageBox.Buttons[0].OnClicked = (button, obj) => + { + (button.UserData as Client).CancelTransfer(); + return true; + }; } - - yield return new WaitForSeconds(0.1f); } GameMain.ShowLoading(StartGame(selectedSub, selectedMode), false); @@ -1782,6 +1808,8 @@ namespace Barotrauma.Networking public string version; public bool inGame; + public bool ReadyToStart; + private object[] votes; public List jobPreferences; @@ -1828,5 +1856,15 @@ namespace Barotrauma.Networking votes[i] = null; } } + + public void CancelTransfer() + { + if (FileStreamSender == null) return; + + FileStreamSender.CancelTransfer(); + FileStreamSender.Dispose(); + + FileStreamSender = null; + } } } diff --git a/Subsurface/Source/Networking/NetworkMember.cs b/Subsurface/Source/Networking/NetworkMember.cs index dc78c8a48..6d9a3ea0d 100644 --- a/Subsurface/Source/Networking/NetworkMember.cs +++ b/Subsurface/Source/Networking/NetworkMember.cs @@ -19,7 +19,7 @@ namespace Barotrauma.Networking RequestNetLobbyUpdate, - StartGame, EndGame, + StartGame, EndGame, CanStartGame, NewItem, RemoveItem, diff --git a/Subsurface/Source/Screens/NetLobbyScreen.cs b/Subsurface/Source/Screens/NetLobbyScreen.cs index db8b14a99..e6bc0802b 100644 --- a/Subsurface/Source/Screens/NetLobbyScreen.cs +++ b/Subsurface/Source/Screens/NetLobbyScreen.cs @@ -1027,8 +1027,8 @@ namespace Barotrauma //already downloading the selected sub file if (GameMain.Client.ActiveFileTransferName == subName+".sub") return false; - var matchingListSub = subList.children.Find(c => c.UserData != null && (c.UserData as Submarine).Name == subName); - if (matchingListSub!=null) + var matchingListSub = subList.children.Find(c => c.UserData != null && (c.UserData as Submarine).Name == subName) as GUITextBlock; + if (matchingListSub != null) { subList.Select(subList.children.IndexOf(matchingListSub), true); } @@ -1043,7 +1043,9 @@ namespace Barotrauma } else if (sub.MD5Hash.Hash == null) { - errorMsg = "Couldn't load submarine ''" + subName + "''. The file may be corrupted. "; + errorMsg = "Couldn't load submarine ''" + subName + "''. The file may be corrupted. "; + + if (matchingListSub != null) matchingListSub.TextColor = Color.Red; } else {