Пример #1
0
        private Task Bot_MessageReceived(
            SocketMessage arg)
        {
            lock (this.LogIDHistory)
            {
                if (this.LogIDHistory.Contains(arg.Id))
                {
                    return(Task.CompletedTask);
                }

                this.LogIDHistory.Add(arg.Id);
            }

            var activeChannels = this.GetActiveChannels();

            if (activeChannels == null)
            {
                return(Task.CompletedTask);
            }

            var ch = activeChannels
                     .FirstOrDefault(x =>
                                     x.DiscordChannelID == arg.Channel.Id.ToString());

            if (ch == null)
            {
                return(Task.CompletedTask);
            }

            var model = ChatLogModel.FromDiscordLog(arg);

            model.ChatCode = ch.ChatCode;
            model.IsMe     = string.Equals(
                model.OriginalSpeaker,
                SharlayanController.Instance.CurrentPlayer?.Name,
                StringComparison.OrdinalIgnoreCase);

            if (!model.IsMe ||
                model.DiscordLog.Attachments.Any())
            {
                WPFHelper.Dispatcher.Invoke(() =>
                {
                    ChatLogsModel.AddToBuffers(model);
                });

                var chName = !string.IsNullOrEmpty(ch.ChannelShortName) ?
                             ch.ChannelShortName :
                             ch.ChannelName;

                ChatLogger.Write(
                    chName,
                    model.Speaker,
                    model.SpeakerAlias,
                    model.Message);
            }

            return(Task.CompletedTask);
        }
Пример #2
0
        private void SubscribeChatLog()
        {
            Thread.Sleep(TimeSpan.FromSeconds(DetectProcessInterval));
            AppLogger.Write("FFXIV chat log subscriber started.");

            var previousPlayerName = string.Empty;

            while (true)
            {
                var interval    = TimeSpan.FromMilliseconds(Config.Instance.ChatLogPollingInterval);
                var isExistLogs = false;

                try
                {
                    // スレッドプライオリティを更新する
                    if (Thread.CurrentThread.Priority != Config.Instance.ChatLogSubscriberThreadPriority)
                    {
                        Thread.CurrentThread.Priority = Config.Instance.ChatLogSubscriberThreadPriority;
                    }

                    if (!this.IsAttached ||
                        !Reader.CanGetChatLog())
                    {
                        interval = TimeSpan.FromSeconds(DetectProcessInterval);
                        continue;
                    }

                    var targetLogs = default(IEnumerable <ChatLogItem>);

                    try
                    {
                        if (this.isWorking)
                        {
                            continue;
                        }

                        this.isWorking = true;

                        if (this.currentPlayer != null &&
                            !string.IsNullOrEmpty(this.currentPlayer.Name))
                        {
                            if (!string.IsNullOrEmpty(previousPlayerName) &&
                                previousPlayerName != this.currentPlayer.Name)
                            {
                                this.previousArrayIndex = 0;
                                this.previousOffset     = 0;
                            }

                            previousPlayerName = this.CurrentPlayer.Name;
                        }

                        var result = Reader.GetChatLog(this.previousArrayIndex, this.previousOffset);
                        if (result == null)
                        {
                            continue;
                        }

                        this.previousArrayIndex = result.PreviousArrayIndex;
                        this.previousOffset     = result.PreviousOffset;

                        if (!result.ChatLogItems.Any())
                        {
                            continue;
                        }

                        targetLogs = result.ChatLogItems
                                     .Where(x => ChatCodes.All.Contains(x.Code));

                        isExistLogs = targetLogs.Any();
                    }
                    finally
                    {
                        this.isWorking = false;
                    }

                    if (isExistLogs)
                    {
                        var models = targetLogs
                                     .Select(x => ChatLogModel.FromXIVLog(x, this.currentPlayerNames))
                                     .ToArray();

                        WPFHelper.Dispatcher.Invoke(() =>
                        {
                            ChatLogsModel.AddToBuffers(models);
                        });

                        foreach (var model in models)
                        {
                            if (model.IsMe)
                            {
                                var playerName = this.currentPlayer?.Name;
                                if (string.IsNullOrEmpty(playerName))
                                {
                                    playerName = previousPlayerName;

                                    if (string.IsNullOrEmpty(playerName))
                                    {
                                        playerName = Config.Instance.ActiveProfile?.CharacterName;
                                    }
                                }

                                DiscordBotController.Instance.SendMessage(
                                    model.ChatCode,
                                    playerName,
                                    Config.Instance.ActiveProfile?.Alias,
                                    model.Message);
                            }

                            var chName = !string.IsNullOrEmpty(model.ChannelShortName) ?
                                         model.ChannelShortName :
                                         model.ChannelName;

                            ChatLogger.Write(
                                chName,
                                model.Speaker,
                                model.SpeakerAlias,
                                model.Message);
                        }
                    }
                }
                catch (ThreadAbortException)
                {
                    return;
                }
                catch (Exception ex)
                {
                    AppLogger.Error("Happened exception from chat log subscriber.", ex);
                    interval = TimeSpan.FromSeconds(DetectProcessInterval * 2);
                }
                finally
                {
                    var now = DateTime.Now;

                    if (isExistLogs)
                    {
                        this.lastChatLogReceivedTimestamp = now;
                        Thread.Yield();
                    }
                    else
                    {
                        if ((now - this.lastChatLogReceivedTimestamp) > ChatIdelThreshold)
                        {
                            interval = ChatIdleInterval;
                        }

                        Thread.Sleep(interval);
                    }
                }
            }
        }