예제 #1
0
    internal void Init(NetworkManager.ChatMessage e, PlayerHandler player, ChatManager manager)
    {
        id                = e.id;
        type              = e.type;
        this.player       = player;
        this.manager      = manager;
        ChatAvatar.sprite = GD.Avatar(player.Avatar);
        participants      = new List <ChatParticipant>();
        bool found = false;

        foreach (ChatParticipant p in e.participants)
        {
            participants.Add(p);
            if (p.id == player.ID)
            {
                found = true;
            }
        }
        // Add the current player if missing
        if (!found)
        {
            participants.Add(new ChatParticipant(player.Name, player.Avatar, player.ID));
        }
        ChatTitle.text = e.chatname;
        ChatTxt.text   = "";
        gameObject.SetActive(true);
        MaxButton.gameObject.SetActive(false);
        ChatContainer.gameObject.SetActive(true);
        GetComponent <RectTransform>().sizeDelta = new Vector2(500, 360);
    }
예제 #2
0
    public void StartChatWith(ulong targetid, string targetname, int targetavatar)
    {
        // Generate the ID of the chat and the name
        string name   = "Chat with <sprite=" + targetavatar + "> " + targetname;
        ChatID chatid = new ChatID(player.ID, targetid);

        if (!chats.ContainsKey(chatid))
        {
            GameObject chat              = Instantiate(ChatTemplate, ChatsGrid);
            Chat       chatScript        = chat.GetComponent <Chat>();
            NetworkManager.ChatMessage e = new NetworkManager.ChatMessage {
                id           = chatid,
                type         = ChatType.OneToOne,
                chatname     = name,
                senderid     = player.ID,
                senderavatar = player.Avatar,
                message      = "",
                participants = new List <ChatParticipant> {
                    new ChatParticipant(player.Name, player.Avatar, player.ID),
                    new ChatParticipant(targetname, targetavatar, targetid)
                }
            };
            chatScript.Init(e, player, this);
            chats[chatid] = chatScript;
            // Send the chat init to the participant (FIXME we should do something similar for the game group chat)
            GD.instance.networkManager.SendChat(chatid, ChatType.OneToOne, player, e.participants, "");
        }
        Chats.SetActive(true);
    }
예제 #3
0
    private void Update()
    {
        if (eventsQueue == null || eventsQueue.Count == 0)
        {
            return;
        }

        NetworkManager.ChatMessage e = eventsQueue.Dequeue();

        if (e.type == ChatType.Error && !chats.ContainsKey(e.id))
        {
            return;
        }
        Chats.SetActive(true);

        // Pick the chat ID, if it does not exist just initialize it
        if (!chats.ContainsKey(e.id))
        {
            GameObject chat       = Instantiate(ChatTemplate, ChatsGrid);
            Chat       chatScript = chat.GetComponent <Chat>();
            chatScript.Init(e, player, this);
            chats[e.id] = chatScript;
        }
        try {
            chats[e.id].ShowMessage(e);
        } catch (System.Exception ex) {
            GD.DebugLog("Problems in visualizing a chat: " + ex.Message + " --> " + e, GD.LT.Warning);
        }
    }
예제 #4
0
 private NetworkManager.ChatMessage ReceiveChat(byte[] data)
 {
     /* Chat format
      * 16 bytes for chatid
      * 1 byte for type
      * 2 bytes msg len
      * n bytes msg
      * 8 bytes sender ID
      * 1 byte sender avatar
      * 1 byte num destinations
      * { serialization of each participant }
      */
     NetworkManager.ChatMessage e = new NetworkManager.ChatMessage();
     e.FromBytes(data);
     return(e);
 }
예제 #5
0
    public void StartChatWith(string gamename, List <ChatParticipant> targets)
    {
        // Generate the ID of the chat and the name
        string name      = "Game Chat <b>" + gamename + "</b>";
        ChatID chatid    = new ChatID(gamename);
        string introText = "<color=blue>Game chat:</color> ";
        int    num       = 1;

        foreach (ChatParticipant p in targets)
        {
            introText += " <sprite=" + p.avatar + "><b>" + p.name + "</b>";
            if (num < targets.Count)
            {
                introText += ", ";
            }
            num++;
        }
        if (!chats.ContainsKey(chatid))
        {
            GameObject chat              = Instantiate(ChatTemplate, ChatsGrid);
            Chat       chatScript        = chat.GetComponent <Chat>();
            NetworkManager.ChatMessage e = new NetworkManager.ChatMessage {
                id           = chatid,
                type         = ChatType.Game,
                chatname     = name,
                senderid     = player.ID,
                senderavatar = player.Avatar,
                message      = "",
                participants = targets
            };
            chatScript.Init(e, player, this);
            chats[chatid] = chatScript;
            // Send the chat init to the participant
            GD.instance.networkManager.SendChat(chatid, ChatType.Game, player, e.participants, introText);
        }
        Chats.SetActive(true);
    }
예제 #6
0
    internal void ShowMessage(NetworkManager.ChatMessage e)
    {
        if (string.IsNullOrEmpty(e.message))
        {
            return;
        }

        // Count the lines of the chat, trim if necessary
        int numlines = 1;

        foreach (char c in ChatTxt.text)
        {
            if (c == '\n')
            {
                numlines++;
            }
        }
        if (numlines > 11)
        {
            int pos = ChatTxt.text.Length - 1;
            numlines = 1;
            while (pos > 0 && numlines < 12)
            {
                if (ChatTxt.text[pos] == '\n')
                {
                    numlines++;
                }
                pos--;
            }
            ChatTxt.text = ChatTxt.text.Substring(pos + 1);
        }

        string msg = "";

        if (e.type == ChatType.ParticipantGone)
        {
            ChatParticipant goner = null;
            foreach (ChatParticipant cp in participants)
            {
                if (cp.id == e.senderid)
                {
                    goner = cp;
                    break;
                }
            }
            if (goner != null)
            {
                participants.Remove(goner);
                msg = (ChatTxt.text.Length > 0 ? "\n" : "") + e.message;
            }
        }
        else if (e.type == ChatType.Error)
        {
            msg = "<color=red>" + msg + "</color>";
        }
        else
        {
            // Add the avatar and name of the sender if in the list
            ChatParticipant sender = null;
            foreach (ChatParticipant cp in participants)
            {
                if (cp.id == e.senderid)
                {
                    sender = cp;
                    break;
                }
            }

            if (sender != null)
            {
                msg = (ChatTxt.text.Length > 0 ? "\n" : "") + "<sprite=" + sender.avatar + "><b><color=#101000>" + sender.name + "</color></b> " + e.message;
            }
            else
            {
                msg = (ChatTxt.text.Length > 0 ? "\n" : "") + e.message;
            }
            // Merge the participants
            foreach (ChatParticipant pe in e.participants)
            {
                bool found = false;
                foreach (ChatParticipant pc in participants)
                {
                    if (pe.id == pc.id)
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    participants.Add(pe);
                }
            }

            // Replace emojis
            msg = msg
                  .Replace(":)", "<sprite=75>").Replace(":smile:", "<sprite=75>")
                  .Replace(":D", "<sprite=76>").Replace(":happy:", "<sprite=76>")
                  .Replace(":love:", "<sprite=77>")
                  .Replace("8)", "<sprite=78>").Replace(":glasses:", "<sprite=78>")
                  .Replace(":lol:", "<sprite=79>").Replace(":joy:", "<sprite=79>")
                  .Replace(":p", "<sprite=80>").Replace(":P", "<sprite=80>").Replace(":tongue:", "<sprite=80>")
                  .Replace(":cry:", "<sprite=81>").Replace(">(", "<sprite=81>").Replace(":sad:", "<sprite=81>")
                  .Replace(":(", "<sprite=82>");
        }

        // Add the message
        ChatTxt.text += msg;
    }
예제 #7
0
 private void OnChatReceived(object sender, NetworkManager.ChatMessage e)
 {
     // We are not inside the MonoBehavior thread here, we need to store the event and process it inside the Update
     eventsQueue.Enqueue(e);
 }