From 5a4bc0242d61e2b89594ffd55e916e7150fcea1d Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Thu, 21 Sep 2017 20:37:30 +0300 Subject: [PATCH] Voting UI fixes: - The vote count texts are removed when voting is disabled. - Server sends vote status to joining clients (-> clients see the number of votes immediately, not after someone votes something). - Server refreshes vote count texts when a client disconnects. --- .../Source/Networking/Voting.cs | 21 ++++++++------ .../Source/Networking/GameServer.cs | 29 ++++++++++++------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Networking/Voting.cs b/Barotrauma/BarotraumaClient/Source/Networking/Voting.cs index 3dbde1d8d..4852a7057 100644 --- a/Barotrauma/BarotraumaClient/Source/Networking/Voting.cs +++ b/Barotrauma/BarotraumaClient/Source/Networking/Voting.cs @@ -19,11 +19,12 @@ namespace Barotrauma if (GameMain.Server != null) { - UpdateVoteTexts(GameMain.Server.ConnectedClients, VoteType.Sub); + UpdateVoteTexts(value ? GameMain.Server.ConnectedClients : null, VoteType.Sub); GameMain.Server.UpdateVoteStatus(); } else { + UpdateVoteTexts(null, VoteType.Sub); GameMain.NetLobbyScreen.SubList.Deselect(); } } @@ -39,11 +40,12 @@ namespace Barotrauma GameMain.NetLobbyScreen.InfoFrame.FindChild("modevotes", true).Visible = value; if (GameMain.Server != null) { - UpdateVoteTexts(GameMain.Server.ConnectedClients, VoteType.Mode); + UpdateVoteTexts(value ? GameMain.Server.ConnectedClients : null, VoteType.Mode); GameMain.Server.UpdateVoteStatus(); } else { + UpdateVoteTexts(null, VoteType.Mode); GameMain.NetLobbyScreen.ModeList.Deselect(); } } @@ -60,10 +62,13 @@ namespace Barotrauma if (voteText != null) comp.RemoveChild(voteText); } - List> voteList = GetVoteList(voteType, clients); - foreach (Pair votable in voteList) + if (clients != null) { - SetVoteText(listBox, votable.First, votable.Second); + List> voteList = GetVoteList(voteType, clients); + foreach (Pair votable in voteList) + { + SetVoteText(listBox, votable.First, votable.Second); + } } } @@ -127,10 +132,7 @@ namespace Barotrauma AllowSubVoting = inc.ReadBoolean(); if (allowSubVoting) { - foreach (Submarine sub in Submarine.SavedSubmarines) - { - SetVoteText(GameMain.NetLobbyScreen.SubList, sub, 0); - } + UpdateVoteTexts(null, VoteType.Sub); int votableCount = inc.ReadByte(); for (int i = 0; i < votableCount; i++) { @@ -143,6 +145,7 @@ namespace Barotrauma AllowModeVoting = inc.ReadBoolean(); if (allowModeVoting) { + UpdateVoteTexts(null, VoteType.Mode); int votableCount = inc.ReadByte(); for (int i = 0; i < votableCount; i++) { diff --git a/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs b/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs index f34ccc704..1a9e07eec 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs @@ -1001,6 +1001,8 @@ namespace Barotrauma.Networking //and assume the message was received, so we don't have to keep resending //these large initial messages until the client acknowledges receiving them c.lastRecvGeneralUpdate++; + + SendVoteStatus(new List() { c }); } else { @@ -1559,11 +1561,13 @@ namespace Barotrauma.Networking Log(msg, ServerLog.MessageType.ServerMessage); client.Connection.Disconnect(targetmsg); + connectedClients.Remove(client); #if CLIENT - GameMain.NetLobbyScreen.RemovePlayer(client.name); + GameMain.NetLobbyScreen.RemovePlayer(client.name); + Voting.UpdateVoteTexts(connectedClients, VoteType.Sub); + Voting.UpdateVoteTexts(connectedClients, VoteType.Mode); #endif - connectedClients.Remove(client); UpdateVoteStatus(); @@ -1851,14 +1855,8 @@ namespace Barotrauma.Networking } GameMain.NetLobbyScreen.LastUpdateID++; - - NetOutgoingMessage msg = server.CreateMessage(); - msg.Write((byte)ServerPacketHeader.UPDATE_LOBBY); - msg.Write((byte)ServerNetObject.VOTE); - Voting.ServerWrite(msg); - msg.Write((byte)ServerNetObject.END_OF_MESSAGE); - - server.SendMessage(msg, connectedClients.Select(c => c.Connection).ToList(), NetDeliveryMethod.ReliableUnordered, 0); + + SendVoteStatus(connectedClients); if (Voting.AllowEndVoting && EndVoteMax > 0 && ((float)EndVoteCount / (float)EndVoteMax) >= EndVoteRequiredRatio) @@ -1868,6 +1866,17 @@ namespace Barotrauma.Networking } } + public void SendVoteStatus(List recipients) + { + NetOutgoingMessage msg = server.CreateMessage(); + msg.Write((byte)ServerPacketHeader.UPDATE_LOBBY); + msg.Write((byte)ServerNetObject.VOTE); + Voting.ServerWrite(msg); + msg.Write((byte)ServerNetObject.END_OF_MESSAGE); + + server.SendMessage(msg, recipients.Select(c => c.Connection).ToList(), NetDeliveryMethod.ReliableUnordered, 0); + } + public void UpdateClientPermissions(Client client) { clientPermissions.RemoveAll(cp => cp.IP == client.Connection.RemoteEndPoint.Address.ToString());