Exemple #1
0
        /// <summary>
        /// 抢任务
        /// </summary>
        /// <param name="user"></param>
        /// <param name="taskId"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ResponseMessage> KcoinTaskGrab(Models.UserInfo user, string taskId, CancellationToken cancellationToken = default(CancellationToken))
        {
            var response = new ResponseMessage();
            var taskInfo = await _iTaskStore.GetTaskInfos().Where(a => !a.IsDelete && a.Id == taskId).FirstOrDefaultAsync(cancellationToken);

            var nowTime = DateTime.Now;

            if (taskInfo == null)
            {
                response.Code    = ResponseCodeDefines.NotFound;
                response.Message = "任务未找到";
                return(response);
            }
            var periodInfo = await _iTaskStore.GetScorePeriods().Where(w => !w.IsDelete && w.Id == taskInfo.PeriodId).FirstOrDefaultAsync();

            if (periodInfo == null || periodInfo.State != 1)
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "只有进行中的赛季才能操作";
                return(response);
            }
            if (taskInfo.ExamineState != 2)
            {
                response.Code    = ResponseCodeDefines.NotAllow;
                response.Message = "任务未审核通过";
                return(response);
            }

            if (taskInfo.TaskState == 0 || nowTime < taskInfo.LootTime)
            {
                response.Code    = ResponseCodeDefines.NotAllow;
                response.Message = "未到开抢时间";
                return(response);
            }
            if (taskInfo.TaskState == 2 || taskInfo.TaskState == 3)
            {
                response.Code    = ResponseCodeDefines.NotAllow;
                response.Message = "任务已被领取";
                return(response);
            }
            var taskUser = new TaskUser
            {
                UserId  = user.Id,
                TaskId  = taskId,
                GetTime = nowTime,
            };

            //事务保存
            using (var trans = await _transaction.BeginTransaction())
            {
                try
                {
                    var oldTaskUser = await _iTaskStore.GetTaskUsers().Where(w => w.TaskId == taskInfo.Id).ToListAsync();

                    if (oldTaskUser.Any(fi => fi.UserId == user.Id))
                    {
                        response.Code    = ResponseCodeDefines.NotAllow;
                        response.Message = "您已抢到该任务!";
                        trans.Rollback();
                        return(response);
                    }
                    if (taskInfo.UserNumber < oldTaskUser.Count + 1)
                    {
                        response.Code    = ResponseCodeDefines.NotAllow;
                        response.Message = "当前任务所需人数已满";
                        trans.Rollback();
                        return(response);
                    }
                    await _iTaskStore.AddTaskUserAsync(taskUser, cancellationToken);

                    var taskUserQuery = _iTaskStore.GetTaskUsers().Where(a => a.TaskId == taskId);
                    var taskUserCount = await taskUserQuery.CountAsync(cancellationToken);

                    if (taskUserCount >= taskInfo.UserNumber)
                    {
                        taskInfo.TaskState = 2;
                        await _iTaskStore.UpdateTaskInfoAsync(taskInfo, cancellationToken);
                    }
                    // 抢任务成功添加动态
                    await _dynamicHelper.AddDynamicContent(
                        DynamicType.TaskGet,
                        taskInfo.PeriodId,
                        taskInfo.Id,
                        "",
                        "",
                        user.UserName,
                        user.GroupName,
                        user.Id,
                        taskInfo.TaskName,
                        taskInfo.Score,
                        null,
                        null);

                    trans.Commit();
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw new Exception("保存事务失败", e);
                }
            }
            return(response);
        }