Пример #1
0
        public void NotyfyTaskFinished(UserTaskDataModel finishedTask, UserReportDataModel report = null, int?newTotalWinnerUserCc = null)
        {
            IDbConnection connection = null; // коннекшен для этого метода не нужен, это часть интерфейса

            if (!finishedTask.TaskEnd)
            {
                throw new NotImplementedException("NotyfyTaskFinished: finishedTask.TaskEnd = false");
            }
            IList <ConnectionUser> hubUsers;

            if (finishedTask.IsTransfer)
            {
                hubUsers = _hubCache.LocalFind(connection, (key, val) => val.UserId == finishedTask.SourceUserId);
                if (hubUsers == null || hubUsers.Count != 1 || !hubUsers[0].Connected)
                {
                    return;
                }
                _hub.Clients.Client(hubUsers[0].ConnectionId).InvokeAsync("journalOnTaskFinished", new
                {
                    Task         = finishedTask,
                    TabReportOut = 0
                });
                return;
            }
            if (report == null)
            {
                return;
            }

            hubUsers = _hubCache.LocalFind(connection, (key, val) =>
                                           val.UserId == report.AtackerUserId || val.UserId == report.DefenderUserId);
            if (hubUsers == null || hubUsers.All(i => !i.Connected))
            {
                return;
            }
            var            atakerUsers   = hubUsers.Where(i => i.UserId == report.AtackerUserId).ToList();
            var            defendorUsers = hubUsers.Where(i => i.UserId == report.DefenderUserId).ToList();
            ConnectionUser ataker        = null;
            ConnectionUser defendor      = null;

            if (atakerUsers.Any() && atakerUsers.Count == 1 && atakerUsers[0].Connected)
            {
                ataker = atakerUsers[0];
            }
            if (defendorUsers.Any() && defendorUsers.Count == 1 && defendorUsers[0].Connected)
            {
                defendor = defendorUsers[0];
            }


            if (ataker != null)
            {
                var client = _hub.Clients.Client(ataker.ConnectionId);
                if (client != null)
                {
                    L10N.SetCulture(ataker.Lang);
                    var atakerReport =
                        _journalOut.SetReportItem(report.AtackerUserId, new List <UserTaskDataModel> {
                        finishedTask
                    })(
                            report.CloneDeep());
                    TabReportOut.InitComplexBtnItem(atakerReport);
                    client.InvokeAsync("journalOnTaskFinished", new
                    {
                        Task                 = finishedTask,
                        TabReportOut         = atakerReport,
                        NewTotalWinnerUserCc = newTotalWinnerUserCc != null && !atakerReport.IsLose
                            ? newTotalWinnerUserCc
                            : null
                    });
                }
            }
            if (defendor != null)
            {
                var client = _hub.Clients.Client(defendor.ConnectionId);
                if (client != null)
                {
                    L10N.SetCulture(defendor.Lang);
                    var defendorReport = _journalOut.SetReportItem(report.DefenderUserId,
                                                                   new List <UserTaskDataModel> {
                        finishedTask
                    })(report.CloneDeep());
                    TabReportOut.InitComplexBtnItem(defendorReport);

                    client.InvokeAsync("journalOnTaskFinished", new
                    {
                        Task                 = finishedTask,
                        TabReportOut         = defendorReport,
                        NewTotalWinnerUserCc = newTotalWinnerUserCc != null && !defendorReport.IsLose
                            ? newTotalWinnerUserCc
                            : null
                    });
                }
            }
        }
Пример #2
0
        public void NotyfyTaskCreated(UserTaskDataModel createdTask, int targetUserId)
        {
            IDbConnection          connection = null; // коннекшен для этого метода не нужен, это часть интерфейса
            IList <ConnectionUser> hubUsers;
            object data = null;

            if (createdTask.IsTransfer)
            {
                hubUsers = _hubCache.LocalFind(connection, (key, val) => val.UserId == createdTask.SourceUserId);
                if (hubUsers == null || hubUsers.Count != 1 && !hubUsers[0].Connected)
                {
                    return;
                }
                L10N.SetCulture(hubUsers[0].Lang);
                data = SetTaskItem(createdTask);
                _hub.Clients.Client(hubUsers[0].ConnectionId).InvokeAsync("journalOnTaskCreated", data);
                return;
            }
            if (createdTask.SourceUserId == targetUserId)
            {
                throw new NotImplementedException("createdTask.SourceUserId == targetUserId");
            }
            if (createdTask.SourceUserId == Npc.SkagryGameUserId)
            {
                hubUsers = _hubCache.LocalFind(connection, (key, val) => val.UserId == targetUserId);
                if (hubUsers == null || hubUsers.Count != 1 || !hubUsers[0].Connected)
                {
                    return;
                }
                data = SetTaskItem(createdTask);
                _hub.Clients.Client(hubUsers[0].ConnectionId).InvokeAsync("journalOnTaskCreated", data);
                return;
            }
            hubUsers = _hubCache.LocalFind(connection, (key, val) =>
                                           val.UserId == createdTask.SourceUserId || val.UserId == targetUserId);

            if (hubUsers == null || !hubUsers.Any() || hubUsers.All(i => !i.Connected))
            {
                return;
            }
            var atakerUsers   = hubUsers.Where(i => i.UserId == createdTask.SourceUserId).ToList();
            var defendorUsers = hubUsers.Where(i => i.UserId == targetUserId).ToList();
            var connectionIds = new List <string>();

            if (atakerUsers.Any() && atakerUsers.Count == 1 && atakerUsers[0].Connected)
            {
                connectionIds.Add(atakerUsers[0].ConnectionId);
            }
            if (defendorUsers.Any() && defendorUsers.Count == 1 && defendorUsers[0].Connected)
            {
                connectionIds.Add(defendorUsers[0].ConnectionId);
            }
            if (connectionIds.Any())
            {
                data = SetTaskItem(createdTask);
                var clients = _hub.Clients;
                foreach (var cid in connectionIds)
                {
                    clients.Client(cid).InvokeAsync("journalOnTaskCreated", data);
                }
            }
        }