예제 #1
0
        /// <summary>
        /// Shows the next dialogue line if there is any and option buttons if we hit the end of dialogue.
        /// </summary>
        private void ShowNextDialogueLine()
        {
            if (lines.Count == 0)
            {
                Debug.LogWarning("There is no dialogue line left!");
                return;
            }

            DialogueLine dl = lines.Dequeue();

            SetDialogueWindow(dl);

            if (dl.actions != null)
            {
                ExecuteAllDialogueActions(dl.actions);
            }

            if (lines.Count != 0)
            {
                nextButton.SetActive(true);
            }
            //we are at the end of the dialogue, shows options
            else
            {
                nextButton.SetActive(false);
                if (currentDialogue.dialogueOptions == null || currentDialogue.dialogueOptions.Length == 0)
                {
                    ConversationEnded?.Invoke();
                    return;
                }

                //show each option in dialogue
                for (int i = 0; i < currentDialogue.dialogueOptions.Length; i++)
                {
                    DialogueOption opt = currentDialogue.dialogueOptions[i];

                    if (opt.prerequisities != null && !DoesMeetPrerequisities(opt.prerequisities))
                    {
                        continue;
                    }

                    GameObject go = Instantiate(dialogueOptionPrefab) as GameObject;
                    go.transform.SetParent(dialogueWindow, false);
                    DialogueOptionButton button = go.gameObject.GetComponent <DialogueOptionButton>();
                    button.dialogueOptionID = i;
                    button.optionText.text  = opt.optionText;
                    optionButtons.Add(go);
                }
            }
        }
예제 #2
0
        private async Task EnterConversationLoop()
        {
            $"{nameof(EnterConversationLoop)}({this})".Log();
            // NOTE: in terms of networking, this task is the only 'receiver'
            // and it is the same logic regardless of who initiated the conversation.
            try
            {
                while (!_cancellationTokenSource.IsCancellationRequested)
                {
                    var expectedSize = await ReadMessageLength(); //

                    var data = await ReadMessagePayload(
                        _inputStream,
                        expectedSize,
                        _cancellationTokenSource.Token);

                    var message = Encoding.UTF8.GetString(data);
                    try
                    {
                        MessageReceived?.Invoke(this, new MessageReceivedEventArgs
                        {
                            Peer    = Peer,
                            Message = message
                        });
                    }
                    catch (Exception ex)
                    {
                        ex.Log();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                // NOP: the reasons for a failed read are all valid, and
                // should result in a disconnect sequence, only logged as a warning
                ex.Log(System.Diagnostics.TraceEventType.Warning);
            }
            finally
            {
                try
                {
                    ConversationEnded?.Invoke(this, new EventArgs());
                }
                catch (Exception ex)
                {
                    ex.Log();
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Disconnects from currently connected stranger.
        /// </summary>
        public void DisconnectStranger()
        {
            if (IsReady && IsOpen && IsStrangerConnected)
            {
                SendPacket(
                    new DisconnectPacket(CurrentContactUID)
                    );
                IsStrangerConnected = false;

                var di        = new DisconnectInfo(false, -2);
                var eventArgs = new ConversationEndedEventArgs(di);
                ConversationEnded?.Invoke(this, eventArgs);

                ActionID++;
            }
        }
예제 #4
0
        private void WebsocketPacketHandler_SocketMessageReceived(List <Packet> packets)
        {
            // Server may send more than one packet.
            // -------------------------------------
            foreach (var packet in packets)
            {
                if (packet.Header == ConnectionAcceptedPacket.ToString())
                {
                    if (packet.Data == null)
                    {
                        throw new Exception("Invalid packet received, packet data is null.");
                    }

                    SendPacket(new ClientInfoPacket(false, UserAgent, packet.Data["hash"].ToString(), 0, false));
                    SendPacket(new OpenAcknowledgedPacket());

                    var eventArgs = new ConnectionAcceptedEventArgs(packet.Data["conn_id"].ToString(), packet.Data["hash"].ToString());
                    ConnectionAccepted?.Invoke(this, eventArgs);
                    continue;
                }

                if (packet.Header == ConversationEndedPacket.ToString())
                {
                    // Unusual behavior, server sends "convended" without any data
                    // if "flag stranger" packet is sent and no conversation have
                    // been started before.
                    //
                    // Hence, we have to handle it like this.
                    // -----------------------------------------------------------
                    IsStrangerConnected = false;
                    if (packet.Data != null)
                    {
                        var di        = new DisconnectInfo(true, int.Parse(packet.Data.ToString()));
                        var eventArgs = new ConversationEndedEventArgs(di);

                        ConversationEnded?.Invoke(this, eventArgs);
                    }
                    else
                    {
                        var di        = new DisconnectInfo(true, -1);
                        var eventArgs = new ConversationEndedEventArgs(di);

                        ConversationEnded?.Invoke(this, eventArgs);
                    }
                    continue;
                }

                if (packet.Header == StrangerDisconnectedPacket.ToString())
                {
                    if (CurrentCID != packet.Data.ToString() && EncounteredClientIDs.Contains(packet.Data.ToString()))
                    {
                        EncounteredClientIDs.Remove(packet.Data.ToString());
                        continue;
                    }

                    IsStrangerConnected = false;

                    if (packet.Data == null)
                    {
                        throw new Exception("Invalid packet received, packet data is null.");
                    }

                    var di        = new DisconnectInfo(false, int.Parse(packet.Data.ToString()));
                    var eventArgs = new ConversationEndedEventArgs(di);

                    ConversationEnded?.Invoke(this, eventArgs);
                    continue;
                }

                if (packet.Header == MessageReceivedPacket.ToString())
                {
                    if (packet.Data == null)
                    {
                        throw new Exception("Invalid packet received, packet data is null.");
                    }

                    int postId = -1;
                    if (packet.AdditionalFields.ContainsKey("post_id"))
                    {
                        postId = int.Parse(packet.AdditionalFields["post_id"].ToString());
                    }

                    var message = new Message(
                        packet.Data["msg"].ToString(),
                        int.Parse(packet.Data["cid"].ToString()),
                        postId,
                        MessageType.Chat
                        );
                    var eventArgs = new MessageEventArgs(message);
                    MessageReceived?.Invoke(this, eventArgs);
                    continue;
                }

                if (packet.Header == OnlinePeopleCountPacket.ToString())
                {
                    if (packet.Data == null)
                    {
                        throw new Exception("Invalid packet received, packet data is null.");
                    }

                    int number;
                    if (!int.TryParse(packet.Data.ToString(), out number))
                    {
                        number = -1;
                    }

                    var eventArgs = new OnlineCountEventArgs(number);
                    OnlinePeopleCountChanged?.Invoke(this, eventArgs);
                    continue;
                }

                if (packet.Header == PingPacket.ToString())
                {
                    if (KeepAlive)
                    {
                        PongResponse();
                    }

                    var eventArgs = new PingEventArgs(DateTime.Now);
                    PingReceived?.Invoke(this, eventArgs);
                    continue;
                }

                if (packet.Header == RandomTopicReceivedPacket.ToString())
                {
                    if (packet.Data == null)
                    {
                        throw new Exception("Invalid packet received, packet data is null.");
                    }

                    var message = new Message(
                        packet.Data["topic"].ToString(),
                        int.Parse(packet.Data["cid"].ToString()),
                        int.Parse(packet.AdditionalFields["post_id"].ToString()),
                        MessageType.Topic
                        );
                    var eventArgs = new MessageEventArgs(message);
                    MessageReceived?.Invoke(this, eventArgs);
                    continue;
                }

                if (packet.Header == ServiceMessageReceivedPacket.ToString())
                {
                    if (packet.Data == null)
                    {
                        throw new Exception("Invalid packet received, packet data is null.");
                    }

                    var message   = new Message(packet.Data.ToString(), -1, -1, MessageType.Service);
                    var eventArgs = new MessageEventArgs(message);
                    MessageReceived?.Invoke(this, eventArgs);
                    continue;
                }

                if (packet.Header == StrangerChatstatePacket.ToString())
                {
                    if (packet.Data == null)
                    {
                        throw new Exception("Invalid packet received, packet data is null.");
                    }

                    bool writing;
                    if (!bool.TryParse(packet.Data.ToString(), out writing))
                    {
                        writing = false;
                    }

                    var chatState = writing ? ChatState.Writing : ChatState.Idle;
                    var eventArgs = new ChatstateEventArgs(chatState);

                    StrangerChatstateChanged?.Invoke(this, eventArgs);
                    continue;
                }

                if (packet.Header == StrangerFoundPacket.ToString())
                {
                    if (packet.Data == null)
                    {
                        throw new Exception("Invalid packet received, packet data is null.");
                    }

                    CurrentContactUID = packet.Data["ckey"].ToString();

                    SendPacket(new ConversationStartAcknowledged(CurrentContactUID));
                    ActionID++;

                    EncounteredClientIDs.Add(packet.Data["cid"].ToString());

                    IsSearchingForStranger = false;
                    IsStrangerConnected    = true;

                    var si = new StrangerInfo(
                        int.Parse(packet.Data["cid"].ToString()),
                        packet.Data["ckey"].ToString(),
                        bool.Parse(packet.Data["flaged"].ToString()),
                        packet.Data["info"]
                        );

                    var eventArgs = new StrangerFoundEventArgs(si);
                    StrangerFound?.Invoke(this, eventArgs);
                }
            }
        }
 // Closes the conversation, can be used manually or as a dialogue action
 public void StopCurrentConversation()
 {
     _uiController.Close();
     _inConversation = false;
     ConversationEnded?.Invoke();
 }