Пример #1
0
        public MsgInformation WaitForInformation(MsgCommand command, Predicate <MsgInformation> judgeFunc, int timeout = 7)
        {
            var signal = new AutoResetEvent(false);
            var unit   = new WaiterUnit {
                JudgePredicate = judgeFunc, Signal = signal
            };

            lock (_lockObj)
            {
                if (UnitsDic.ContainsKey(command.ToGroup))
                {
                    UnitsDic[command.ToGroup].Add(unit);
                }
                else
                {
                    UnitsDic.Add(command.ToGroup, new List <WaiterUnit>()
                    {
                        unit
                    });
                }
            }

            MsgSender.PushMsg(command);
            signal.WaitOne(timeout * 1000);

            lock (_lockObj)
            {
                unit = UnitsDic[command.ToGroup].FirstOrDefault(u => u.Id == unit.Id);
                UnitsDic[command.ToGroup].Remove(unit);
            }

            return(unit?.ResultInfos.FirstOrDefault());
        }
Пример #2
0
        public MsgInformation WaitForInformation(MsgInformationEx MsgDTO, string msg, Predicate <MsgInformation> judgeFunc, int timeout = 7, bool isNeedAt = false)
        {
            var signal = new AutoResetEvent(false);
            var unit   = new WaiterUnit {
                JudgePredicate = judgeFunc, Signal = signal
            };

            lock (_lockObj)
            {
                if (UnitsDic.ContainsKey(MsgDTO.FromGroup))
                {
                    UnitsDic[MsgDTO.FromGroup].Add(unit);
                }
                else
                {
                    UnitsDic.Add(MsgDTO.FromGroup, new List <WaiterUnit>()
                    {
                        unit
                    });
                }
            }

            MsgSender.PushMsg(MsgDTO, msg, isNeedAt);
            signal.WaitOne(timeout * 1000);

            lock (_lockObj)
            {
                unit = UnitsDic[MsgDTO.FromGroup].FirstOrDefault(u => u.Id == unit.Id);
                UnitsDic[MsgDTO.FromGroup].Remove(unit);
            }

            return(unit?.ResultInfos.FirstOrDefault());
        }
Пример #3
0
        private void ListenCallBack(MsgInformation info)
        {
            var source = "未知";

            if (info.FromGroup == 0)
            {
                source = "私聊";
            }
            else
            {
                var groupSetting = GroupSettingSvc[info.FromGroup];
                if (groupSetting != null)
                {
                    source = groupSetting.Name;
                }
            }

            if (info.FromGroup != 0)
            {
                var setting = GroupSettingSvc[info.FromGroup];
                if (setting == null || setting.BindAi != info.BindAi)
                {
                    return;
                }
            }

            var msg = $"[Info] {info.BindAi} {source} {QqNumReflectSvc[info.FromQQ]} {info.Msg}";

            Global.MsgPublish(msg);

            if (BindAiSvc[info.FromQQ] != null)
            {
                return;
            }

            switch (info.Information)
            {
            case InformationType.Message:
            case InformationType.CommandBack:
                WaiterUnit waitUnit = null;
                lock (_lockObj)
                {
                    if (UnitsDic.ContainsKey(info.FromGroup) && !UnitsDic[info.FromGroup].IsNullOrEmpty())
                    {
                        waitUnit = UnitsDic[info.FromGroup].FirstOrDefault(u => u.JudgePredicate(info));
                    }
                }

                if (waitUnit == null)
                {
                    CrossWorldAiSvc.OnMsgReceived(info);
                    break;
                }

                waitUnit.ResultInfos.Add(info);
                waitUnit.Signal.Set();
                AIAnalyzer.AddCommandCount(new CmdRec()
                {
                    FunctionalAi = "Waiter",
                    Command      = "WaiterCallBack",
                    GroupNum     = info.FromGroup,
                    BindAi       = info.BindAi
                });

                break;

            case InformationType.AuthCode:
                break;

            case InformationType.Error:
                AIAnalyzer.AddError(info.Msg);
                break;

            case InformationType.ReceiveMoney:
                CrossWorldAiSvc.OnMoneyReceived(JsonConvert.DeserializeObject <ChargeModel>(info.Msg));
                break;

            case InformationType.GroupMemberIncrease:
            case InformationType.GroupMemberDecrease:
                CrossWorldAiSvc.OnGroupMemberChanged(JsonConvert.DeserializeObject <GroupMemberChangeModel>(info.Msg));
                break;

            case InformationType.ConnectStateChange:
                var bindai = BindAiSvc[info.BindAi];
                bindai.IsConnected = bool.Parse(info.Msg);
                break;
            }
        }