Added lobby chat syncing

This commit is contained in:
juanjp600
2016-09-01 20:45:43 -03:00
parent fc457e0f18
commit 028c3a8bc1
7 changed files with 366 additions and 209 deletions
@@ -39,6 +39,9 @@ namespace Barotrauma.Networking
private set; private set;
} }
public static UInt32 LastID = 0;
public UInt32 ID = 0;
private ChatMessage(string senderName, string text, ChatMessageType type, Character sender) private ChatMessage(string senderName, string text, ChatMessageType type, Character sender)
{ {
Text = text; Text = text;
@@ -49,6 +52,9 @@ namespace Barotrauma.Networking
SenderName = senderName; SenderName = senderName;
TextWithSender = string.IsNullOrWhiteSpace(senderName) ? text : senderName + ": " + text; TextWithSender = string.IsNullOrWhiteSpace(senderName) ? text : senderName + ": " + text;
LastID++;
ID = LastID;
} }
public static ChatMessage Create(string senderName, string text, ChatMessageType type, Character sender) public static ChatMessage Create(string senderName, string text, ChatMessageType type, Character sender)
+3
View File
@@ -29,6 +29,9 @@ namespace Barotrauma.Networking
public string version; public string version;
public bool inGame; public bool inGame;
public UInt32 lastSentChatMsgID = 0; //last msg this client said
public UInt32 lastRecvChatMsgID = 0; //last msg this client knows about
public List<string> ChatMessages = new List<string>(); public List<string> ChatMessages = new List<string>();
public float ChatSpamSpeed; public float ChatSpamSpeed;
public float ChatSpamTimer; public float ChatSpamTimer;
+70 -1
View File
@@ -35,6 +35,9 @@ namespace Barotrauma.Networking
private int nonce; private int nonce;
private string saltedPw; private string saltedPw;
private UInt32 lastRecvChatMsgID = 0; //last message the server received from this client
private List<ChatMessage> chatMsgQueue = new List<ChatMessage>();
public byte ID public byte ID
{ {
get { return myID; } get { return myID; }
@@ -475,6 +478,11 @@ namespace Barotrauma.Networking
} }
} }
if (!gameStarted)
{
SendLobbyUpdate();
}
// Update current time // Update current time
updateTimer = DateTime.Now + updateInterval; updateTimer = DateTime.Now + updateInterval;
} }
@@ -493,10 +501,71 @@ namespace Barotrauma.Networking
while ((inc = client.ReadMessage()) != null) while ((inc = client.ReadMessage()) != null)
{ {
//TODO: read message data switch (inc.MessageType)
{
case NetIncomingMessageType.Data:
ServerPacketHeader header = (ServerPacketHeader)inc.ReadByte();
switch (header)
{
case ServerPacketHeader.UPDATE_LOBBY:
ReadLobbyUpdate(inc);
break;
}
break;
}
} }
} }
private void ReadLobbyUpdate(NetIncomingMessage inc)
{
lastRecvChatMsgID = inc.ReadUInt32();
ServerNetObject objHeader;
while ((objHeader = (ServerNetObject)inc.ReadByte()) != ServerNetObject.END_OF_MESSAGE)
{
switch (objHeader)
{
case ServerNetObject.CHAT_MESSAGE:
//TODO: READ CHAT MESSAGES FROM SERVER
break;
}
}
}
private void SendLobbyUpdate()
{
NetOutgoingMessage outmsg = client.CreateMessage();
outmsg.Write((byte)ClientPacketHeader.UPDATE_LOBBY);
ChatMessage removeMsg;
while ((removeMsg=chatMsgQueue.Find(cMsg => cMsg.ID <= lastRecvChatMsgID)) != null)
{
chatMsgQueue.Remove(removeMsg);
}
foreach (ChatMessage cMsg in chatMsgQueue)
{
outmsg.Write((byte)ClientNetObject.CHAT_MESSAGE);
outmsg.Write(cMsg.ID);
outmsg.Write(cMsg.Text);
}
outmsg.Write((byte)ClientNetObject.END_OF_MESSAGE);
client.SendMessage(outmsg, NetDeliveryMethod.Unreliable);
}
public override void SendChatMessage(string message, ChatMessageType? type = null)
{
if (client.ServerConnection == null) return;
type = ChatMessageType.Default;
ChatMessage chatMessage = ChatMessage.Create(
gameStarted && myCharacter != null ? myCharacter.Name : name,
message, (ChatMessageType)type, gameStarted ? myCharacter : null);
chatMsgQueue.Add(chatMessage);
}
public bool HasPermission(ClientPermissions permission) public bool HasPermission(ClientPermissions permission)
{ {
return false;// permissions.HasFlag(permission); return false;// permissions.HasFlag(permission);
+77 -7
View File
@@ -354,7 +354,21 @@ namespace Barotrauma.Networking
foreach (Client c in connectedClients) foreach (Client c in connectedClients)
{ {
//c.ReliableChannel.Update(deltaTime); if (gameStarted)
{
if (c.inGame)
{
ClientWriteIngame(c);
}
else
{
}
}
else
{
ClientWriteLobby(c);
}
//slowly reset spam timers //slowly reset spam timers
c.ChatSpamTimer = Math.Max(0.0f, c.ChatSpamTimer - deltaTime); c.ChatSpamTimer = Math.Max(0.0f, c.ChatSpamTimer - deltaTime);
@@ -382,15 +396,12 @@ namespace Barotrauma.Networking
ClientAuthRequest(inc.SenderConnection); ClientAuthRequest(inc.SenderConnection);
break; break;
case ClientPacketHeader.REQUEST_INIT: case ClientPacketHeader.REQUEST_INIT:
ClientInitialize(inc); ClientInitRequest(inc);
break; break;
case ClientPacketHeader.UPDATE_LOBBY: case ClientPacketHeader.UPDATE_LOBBY:
//TODO ClientReadLobby(inc);
break; break;
case ClientPacketHeader.UPDATE_INGAME_ALIVE: case ClientPacketHeader.UPDATE_INGAME:
//TODO
break;
case ClientPacketHeader.UPDATE_INGAME_SPECTATING:
//TODO //TODO
break; break;
} }
@@ -524,6 +535,65 @@ namespace Barotrauma.Networking
return userID; return userID;
} }
private void ClientReadLobby(NetIncomingMessage inc)
{
Client c = ConnectedClients.Find(x => x.Connection == inc.SenderConnection);
if (c == null)
{
inc.SenderConnection.Disconnect("You're not a connected client.");
return;
}
ClientNetObject objHeader;
while ((objHeader=(ClientNetObject)inc.ReadByte()) != ClientNetObject.END_OF_MESSAGE)
{
switch (objHeader)
{
case ClientNetObject.CHAT_MESSAGE:
UInt32 ID = inc.ReadUInt32();
string msg = inc.ReadString();
if (c.lastSentChatMsgID<ID)
{
//this chat message is new to the server
AddChatMessage(msg, ChatMessageType.Default, c.name);
c.lastSentChatMsgID = ID;
}
break;
}
}
}
private void ClientReadIngame(NetIncomingMessage inc)
{
Client c = ConnectedClients.Find(x => x.Connection == inc.SenderConnection);
if (c == null)
{
inc.SenderConnection.Disconnect("You're not a connected client.");
return;
}
}
private void ClientWriteIngame(Client c)
{
if (c.Character != null && !c.Character.IsDead)
{
}
else
{
}
}
private void ClientWriteLobby(Client c)
{
NetOutgoingMessage outmsg = server.CreateMessage();
outmsg.Write((byte)ServerPacketHeader.UPDATE_LOBBY);
outmsg.Write(c.lastSentChatMsgID); //send this to client so they know which messages weren't received by the server
server.SendMessage(outmsg,c.Connection,NetDeliveryMethod.Unreliable);
}
public bool StartGameClicked(GUIButton button, object obj) public bool StartGameClicked(GUIButton button, object obj)
{ {
Submarine selectedSub = null; Submarine selectedSub = null;
@@ -72,7 +72,7 @@ namespace Barotrauma.Networking
server.SendMessage(nonceMsg, conn, NetDeliveryMethod.Unreliable); server.SendMessage(nonceMsg, conn, NetDeliveryMethod.Unreliable);
} }
private void ClientInitialize(NetIncomingMessage inc) private void ClientInitRequest(NetIncomingMessage inc)
{ {
if (ConnectedClients.Find(c => c.Connection == inc.SenderConnection) != null) if (ConnectedClients.Find(c => c.Connection == inc.SenderConnection) != null)
{ {
@@ -190,13 +190,7 @@ namespace Barotrauma.Networking
newClient.Connection = unauthClient.Connection; newClient.Connection = unauthClient.Connection;
unauthenticatedClients.Remove(unauthClient); unauthenticatedClients.Remove(unauthClient);
unauthClient = null; unauthClient = null;
ConnectedClients.Add(newClient);
//TEMPORARY TEST CODE; MUST REMOVE
NetOutgoingMessage testMsg = server.CreateMessage();
testMsg.Write((byte)ServerPacketHeader.UPDATE_LOBBY);
server.SendMessage(testMsg, newClient.Connection, NetDeliveryMethod.Unreliable);
//END TEMPORARY TEST CODE
return; return;
} }
} }
+19 -4
View File
@@ -14,16 +14,31 @@ namespace Barotrauma.Networking
REQUEST_AUTH, //ask the server if a password is needed, if so we'll get nonce for encryption REQUEST_AUTH, //ask the server if a password is needed, if so we'll get nonce for encryption
REQUEST_INIT, //ask the server to give you initialization REQUEST_INIT, //ask the server to give you initialization
UPDATE_LOBBY, //update state in lobby UPDATE_LOBBY, //update state in lobby
UPDATE_INGAME_ALIVE, //update state ingame while alive (allow character input) UPDATE_INGAME, //update state ingame while alive
UPDATE_INGAME_SPECTATING, //update state ingame while spectating/dead
} }
enum ClientNetObject
{
END_OF_MESSAGE, //self-explanatory
CHAT_MESSAGE, //also self-explanatory
VOTE, //you get the idea
CHARACTER_INPUT,
ITEM_INTERACTION
}
enum ServerPacketHeader enum ServerPacketHeader
{ {
AUTH_RESPONSE, //tell the player if they require a password to log in AUTH_RESPONSE, //tell the player if they require a password to log in
AUTH_FAILURE, //the server won't authorize player yet, however connection is still alive AUTH_FAILURE, //the server won't authorize player yet, however connection is still alive
UPDATE_LOBBY, //update state in lobby (votes and chat messages) UPDATE_LOBBY, //update state in lobby (votes and chat messages)
UPDATE_INGAME_ALIVE, //update state ingame while alive (character input and chat messages) UPDATE_INGAME, //update state ingame while alive (character input and chat messages)
UPDATE_INGAME_SPECTATING, //update state ingame while spectating/dead (chat messages) }
enum ServerNetObject
{
END_OF_MESSAGE,
CHAT_MESSAGE,
VOTE,
CHARACTER_POSITION,
ITEM_STATE
} }
enum VoteType enum VoteType
+1 -1
View File
@@ -989,7 +989,7 @@ namespace Barotrauma
((chatBox.CountChildren % 2) == 0) ? Color.Transparent : Color.Black*0.1f, message.Color, ((chatBox.CountChildren % 2) == 0) ? Color.Transparent : Color.Black*0.1f, message.Color,
Alignment.Left, GUI.Style, null, true); Alignment.Left, GUI.Style, null, true);
msg.Font = GUI.SmallFont; msg.Font = GUI.SmallFont;
msg.UserData = message.SenderName; msg.UserData = message;
msg.CanBeFocused = false; msg.CanBeFocused = false;
msg.Padding = new Vector4(20, 0, 0, 0); msg.Padding = new Vector4(20, 0, 0, 0);