private static void OnFactionMessageRequest(MySyncCharacter sync, ref SendNewFactionMessageMsg msg, MyNetworkClient sender)
        {
            //Ignore messages that have improper lengths
            if (msg.ChatItem.Text.Length == 0 || msg.ChatItem.Text.Length > MyChatConstants.MAX_CHAT_STRING_LENGTH)
            {
                return;
            }

            long currentSenderId = MyEntityIdentifier.ConstructId(MyEntityIdentifier.ID_OBJECT_TYPE.IDENTITY, msg.ChatItem.IdentityIdUniqueNumber);
            var  senderId        = MySession.Static.Players.TryGetIdentity(currentSenderId);

            var chatItem = new MyFactionChatItem();

            chatItem.Init(msg.ChatItem);

            //Find all members that can receive this messages
            m_tempValidIds.Clear();
            for (int i = 0; i < msg.ChatItem.PlayersToSendToUniqueNumber.Count; i++)
            {
                if (!msg.ChatItem.IsAlreadySentTo[i])
                {
                    long receiverIdentityId = MyEntityIdentifier.ConstructId(MyEntityIdentifier.ID_OBJECT_TYPE.IDENTITY, msg.ChatItem.PlayersToSendToUniqueNumber[i]);
                    var  receiverId         = MySession.Static.Players.TryGetIdentity(receiverIdentityId);
                    if (receiverId != null && receiverId.Character != null && MyAntennaSystem.CheckConnection(senderId, receiverId))
                    {
                        m_tempValidIds.Add(receiverIdentityId);
                    }
                }
            }

            //Set their sent flag to true, so that everyone knows they already got it (no need for confirm message)
            foreach (var id in m_tempValidIds)
            {
                chatItem.PlayersToSendTo[id] = true;
            }

            //Save the flags back in the message
            msg.ChatItem = chatItem.GetObjectBuilder();

            //Send success message back to all recepient members
            foreach (var id in m_tempValidIds)
            {
                MyPlayer.PlayerId receiverPlayerId;
                MySession.Static.Players.TryGetPlayerId(id, out receiverPlayerId);
                ulong steamId = receiverPlayerId.SteamId;

                Sync.Layer.SendMessage(ref msg, steamId, MyTransportMessageEnum.Success);
            }

            //Save chat history on server for non-server players
            if (senderId.Character != MySession.LocalCharacter)
            {
                MyChatSystem.AddFactionChatItem(senderId.IdentityId, msg.FactionId1, msg.FactionId2, chatItem);
            }
        }
        public void SendNewFactionMessage(long factionId1, long factionId2, MyFactionChatItem chatItem)
        {
            var msg = new SendNewFactionMessageMsg();

            msg.CharacterEntityId = Entity.EntityId;
            msg.FactionId1        = factionId1;
            msg.FactionId2        = factionId2;
            msg.ChatItem          = chatItem.GetObjectBuilder();

            Sync.Layer.SendMessageToServer(ref msg, MyTransportMessageEnum.Request);
        }
        private static void OnFactionMessageSuccess(MySyncCharacter sync, ref SendNewFactionMessageMsg msg, MyNetworkClient sender)
        {
            long senderIdentityId = MyEntityIdentifier.ConstructId(MyEntityIdentifier.ID_OBJECT_TYPE.IDENTITY, msg.ChatItem.IdentityIdUniqueNumber);

            var factionChatItem = new MyFactionChatItem();

            factionChatItem.Init(msg.ChatItem);
            if (!(Sync.IsServer && senderIdentityId != MySession.LocalPlayerId))
            {
                MyChatSystem.AddFactionChatItem(MySession.LocalPlayerId, msg.FactionId1, msg.FactionId2, factionChatItem);
            }
            if (senderIdentityId != MySession.LocalPlayerId)
            {
                MySession.Static.Gpss.ScanText(factionChatItem.Text, MyTexts.GetString(MySpaceTexts.TerminalTab_GPS_NewFromFactionComms));
            }
            MySession.Static.ChatSystem.OnNewFactionMessage(msg.FactionId1, msg.FactionId2, senderIdentityId, true);
        }
        public static bool RetryFactionMessage(long factionId1, long factionId2, MyFactionChatItem chatItem, MyIdentity currentSenderIdentity)
        {
            Debug.Assert(Sync.IsServer, "Faction message retries should only be done on server");

            if (currentSenderIdentity.Character == null)
            {
                return(false);
            }

            m_tempValidIds.Clear();
            foreach (var playerToSendTo in chatItem.PlayersToSendTo)
            {
                if (!playerToSendTo.Value)
                {
                    long receiverIdentityId = playerToSendTo.Key;
                    var  receiverId         = MySession.Static.Players.TryGetIdentity(receiverIdentityId);
                    if (receiverId != null && receiverId.Character != null && MyAntennaSystem.CheckConnection(currentSenderIdentity, receiverId))
                    {
                        m_tempValidIds.Add(receiverIdentityId);
                    }
                }
            }

            if (m_tempValidIds.Count == 0)
            {
                return(false);
            }

            foreach (var id in m_tempValidIds)
            {
                chatItem.PlayersToSendTo[id] = true;
            }

            var msg = new SendNewFactionMessageMsg();

            msg.FactionId1        = factionId1;
            msg.FactionId2        = factionId2;
            msg.CharacterEntityId = currentSenderIdentity.Character.EntityId;
            msg.ChatItem          = chatItem.GetObjectBuilder();

            var confirmMessage = new ConfirmFactionMessageMsg();

            confirmMessage.CharacterEntityId = currentSenderIdentity.Character.EntityId;

            confirmMessage.FactionId1       = msg.FactionId1;
            confirmMessage.FactionId2       = msg.FactionId2;
            confirmMessage.OriginalSenderId = chatItem.IdentityId;
            confirmMessage.Timestamp        = chatItem.Timestamp.Ticks;
            confirmMessage.Text             = msg.ChatItem.Text;

            foreach (var id in m_tempValidIds)
            {
                MyPlayer.PlayerId receiverPlayerId;
                MySession.Static.Players.TryGetPlayerId(id, out receiverPlayerId);
                ulong steamId = receiverPlayerId.SteamId;

                Sync.Layer.SendMessage(ref msg, steamId, MyTransportMessageEnum.Success);
            }
            foreach (var id in m_tempValidIds)
            {
                confirmMessage.ReceiverId = id;

                //Send confimation to members of both factions
                SendConfirmMessageToFaction(confirmMessage.FactionId1, chatItem.PlayersToSendTo, ref confirmMessage);
                if (confirmMessage.FactionId1 != confirmMessage.FactionId2)
                {
                    SendConfirmMessageToFaction(confirmMessage.FactionId2, chatItem.PlayersToSendTo, ref confirmMessage);
                }
            }

            return(true);
        }
        public static bool RetryFactionMessage(long factionId1, long factionId2, MyFactionChatItem chatItem, MyIdentity currentSenderIdentity)
        {
            Debug.Assert(Sync.IsServer, "Faction message retries should only be done on server");

            if (currentSenderIdentity.Character == null)
            {
                return false;
            }

            m_tempValidIds.Clear();
            foreach (var playerToSendTo in chatItem.PlayersToSendTo)
            {
                if (!playerToSendTo.Value)
                {
                    long receiverIdentityId = playerToSendTo.Key;
                    var receiverId = MySession.Static.Players.TryGetIdentity(receiverIdentityId);
                    if (receiverId != null && receiverId.Character != null && MyAntennaSystem.CheckConnection(currentSenderIdentity, receiverId))
                    {
                        m_tempValidIds.Add(receiverIdentityId);
                    }
                }
            }

            if (m_tempValidIds.Count == 0)
            {
                return false;
            }

            foreach (var id in m_tempValidIds)
            {
                chatItem.PlayersToSendTo[id] = true;
            }

            var msg = new SendNewFactionMessageMsg();

            msg.FactionId1 = factionId1;
            msg.FactionId2 = factionId2;
            msg.CharacterEntityId = currentSenderIdentity.Character.EntityId;
            msg.ChatItem = chatItem.GetObjectBuilder();

            var confirmMessage = new ConfirmFactionMessageMsg();
            confirmMessage.CharacterEntityId = currentSenderIdentity.Character.EntityId;

            confirmMessage.FactionId1 = msg.FactionId1;
            confirmMessage.FactionId2 = msg.FactionId2;
            confirmMessage.OriginalSenderId = chatItem.IdentityId;
            confirmMessage.Timestamp = chatItem.Timestamp.Ticks;
            confirmMessage.Text = msg.ChatItem.Text;

            foreach (var id in m_tempValidIds)
            {
                MyPlayer.PlayerId receiverPlayerId;
                MySession.Static.Players.TryGetPlayerId(id, out receiverPlayerId);
                ulong steamId = receiverPlayerId.SteamId;

                Sync.Layer.SendMessage(ref msg, steamId, MyTransportMessageEnum.Success);
            }
            foreach (var id in m_tempValidIds)
            {
                confirmMessage.ReceiverId = id;

                //Send confimation to members of both factions
                SendConfirmMessageToFaction(confirmMessage.FactionId1, chatItem.PlayersToSendTo, ref confirmMessage);
                if (confirmMessage.FactionId1 != confirmMessage.FactionId2)
                {
                    SendConfirmMessageToFaction(confirmMessage.FactionId2, chatItem.PlayersToSendTo, ref confirmMessage);
                }
            }

            return true;
        }
        private static void OnFactionMessageSuccess(MySyncCharacter sync, ref SendNewFactionMessageMsg msg, MyNetworkClient sender)
        {
            long senderIdentityId = MyEntityIdentifier.ConstructId(MyEntityIdentifier.ID_OBJECT_TYPE.IDENTITY, msg.ChatItem.IdentityIdUniqueNumber);

            var factionChatItem = new MyFactionChatItem();
            factionChatItem.Init(msg.ChatItem);
            if (!(Sync.IsServer && senderIdentityId != MySession.LocalPlayerId))
            {
                MyChatSystem.AddFactionChatItem(MySession.LocalPlayerId, msg.FactionId1, msg.FactionId2, factionChatItem);
            }
            if (senderIdentityId != MySession.LocalPlayerId)
            {
                MySession.Static.Gpss.ScanText(factionChatItem.Text, MyTexts.GetString(MySpaceTexts.TerminalTab_GPS_NewFromFactionComms));
            }
            MySession.Static.ChatSystem.OnNewFactionMessage(msg.FactionId1, msg.FactionId2, senderIdentityId, true);
        }
        private static void OnFactionMessageRequest(MySyncCharacter sync, ref SendNewFactionMessageMsg msg, MyNetworkClient sender)
        {
            //Ignore messages that have improper lengths
            if (msg.ChatItem.Text.Length == 0 || msg.ChatItem.Text.Length > MyChatConstants.MAX_CHAT_STRING_LENGTH)
            {
                return;
            }

            long currentSenderId = MyEntityIdentifier.ConstructId(MyEntityIdentifier.ID_OBJECT_TYPE.IDENTITY, msg.ChatItem.IdentityIdUniqueNumber);
            var senderId = MySession.Static.Players.TryGetIdentity(currentSenderId);

            var chatItem = new MyFactionChatItem();
            chatItem.Init(msg.ChatItem);

            //Find all members that can receive this messages
            m_tempValidIds.Clear();
            for (int i = 0; i < msg.ChatItem.PlayersToSendToUniqueNumber.Count; i++)
            {
                if (!msg.ChatItem.IsAlreadySentTo[i])
                {
                    long receiverIdentityId = MyEntityIdentifier.ConstructId(MyEntityIdentifier.ID_OBJECT_TYPE.IDENTITY, msg.ChatItem.PlayersToSendToUniqueNumber[i]);
                    var receiverId = MySession.Static.Players.TryGetIdentity(receiverIdentityId);
                    if (receiverId != null && receiverId.Character != null && MyAntennaSystem.CheckConnection(senderId, receiverId))
                    {
                        m_tempValidIds.Add(receiverIdentityId);
                    }
                }
            }

            //Set their sent flag to true, so that everyone knows they already got it (no need for confirm message)
            foreach (var id in m_tempValidIds)
            {
                chatItem.PlayersToSendTo[id] = true;
            }

            //Save the flags back in the message
            msg.ChatItem = chatItem.GetObjectBuilder();

            //Send success message back to all recepient members
            foreach (var id in m_tempValidIds)
            {
                MyPlayer.PlayerId receiverPlayerId;
                MySession.Static.Players.TryGetPlayerId(id, out receiverPlayerId);
                ulong steamId = receiverPlayerId.SteamId;

                Sync.Layer.SendMessage(ref msg, steamId, MyTransportMessageEnum.Success);
            }

            //Save chat history on server for non-server players
            if (senderId.Character != MySession.LocalCharacter)
            {
                MyChatSystem.AddFactionChatItem(senderId.IdentityId, msg.FactionId1, msg.FactionId2, chatItem);
            }
        }
        public void SendNewFactionMessage(long factionId1, long factionId2, MyFactionChatItem chatItem)
        {
            var msg = new SendNewFactionMessageMsg();

            msg.CharacterEntityId = Entity.EntityId;
            msg.FactionId1 = factionId1;
            msg.FactionId2 = factionId2;
            msg.ChatItem = chatItem.GetObjectBuilder();

            Sync.Layer.SendMessageToServer(ref msg, MyTransportMessageEnum.Request);
        }