Exemple #1
0
        public void TeamUserGradeAutoUpdate(long userId, IScope scope)
        {
            var parentMaps = scope.Resolve <ITeamService>().GetTeamMap(userId).ToList();
            var userIds    = parentMaps.Select(r => r.UserId);
            var users      = scope.Resolve <IUserService>().GetList(userIds);

            // 更新团队等级数量
            foreach (var item in parentMaps)
            {
                // 更新团队上级的等级数据
                // 从最新的会员开始更新,UserId大的在前面
                scope.Resolve <IGradeInfoService>().UpdateSingle(item.UserId);

                var itemGradeInfo =
                    scope.Resolve <IGradeInfoService>()
                    .GetSingle(r => r.UserId == item.UserId);     // 必须重新获取,因为如果下级会员更新成功,会自动更新
                var itemUser = users.FirstOrDefault(r => r.Id == item.UserId);
                if (itemGradeInfo == null || itemUser == null)
                {
                    continue;
                }

                var kpiResult = GetKpiResult(itemGradeInfo, itemUser, scope);
                if (kpiResult.Item1)
                {
                    var user          = scope.Resolve <IUserService>().GetSingle(r => r.Id == item.UserId);
                    var beforeGradeId = user.GradeId;
                    user.GradeId = kpiResult.Item2;
                    if (scope.Resolve <IUserService>().Update(user))
                    {
                        var upgradeRecord = new UpgradeRecord {
                            UserId        = item.UserId,
                            Type          = UpgradeType.TeamUserGradeChange,
                            QueueId       = 0,
                            BeforeGradeId = beforeGradeId,
                            AfterGradeId  = kpiResult.Item2
                        };
                        scope.Resolve <IUpgradeRecordService>().Add(upgradeRecord);

                        // 重新添加队列 // 触发上面的
                        UpgradeQueue queue = new UpgradeQueue {
                            UserId = user.Id,
                            Type   = UpgradeType.TeamUserGradeChange,
                            Status = QueueStatus.Pending
                        };
                        scope.Resolve <IUpgradeQueueService>().Add(queue);
                    }
                }
            }
        }
        public async Task <IActionResult> Upgrade(UpgradeSystem selected)
        {
            UpdateAll upengine = new UpdateAll();
            var       Town     = await _context.Towns.LastOrDefaultAsync(x => x.UserId == User.GetUserId());

            var Player = await _userManager.FindByIdAsync(User.GetUserId());

            UpgradeSystem buildings = new UpgradeSystem(_context, Player);

            buildings.BuildingPage(Town);
            var production = buildings.Upgrades.ElementAt(selected.UpgradeIndex);

            upengine.UpdateResource(Town);
            upengine.UpdateBuildings(Town);
            await _context.SaveChangesAsync();

            if (ModelState.IsValid && production.Cost_Iron <= Town.TownIRON &&
                production.Cost_Meat <= Town.TownMEAT && production.Cost_Wheat
                <= Town.TownWHEAT && production.Cost_Wood <= Town.TownWOOD)
            {
                Town.TownWHEAT = Town.TownWHEAT - production.Cost_Wheat;
                Town.TownMEAT  = Town.TownMEAT - production.Cost_Meat;
                Town.TownIRON  = Town.TownIRON - production.Cost_Iron;
                Town.TownWOOD  = Town.TownWOOD - production.Cost_Wood;

                UpgradeSystem Engine     = new UpgradeSystem(_context, Player);
                UpgradeQueue  NewUpgrade = new UpgradeQueue();

                NewUpgrade.BuildingINDEX = production.index;
                NewUpgrade.TownID        = Town.TownId;

                NewUpgrade.ProductionTime = DateTime.Now.AddSeconds(production.Buildtime);

                _context.upgradeQueue.Add(NewUpgrade);

                await _context.SaveChangesAsync();

                return(View("Buildings", buildings));
            }
            else
            {
                await _context.SaveChangesAsync();

                ModelState.AddModelError("", "Not Enough Resources");
                return(View("Buildings", buildings));
            }
        }