Ejemplo n.º 1
0
        public void RunTaskItem(IDbConnection connection, int taskId)
        {
            if (taskId == 0)
            {
                return;
            }
            var taskItem = _uTaskService.GetByTaskId(connection, taskId, true);

            if (taskItem == null)
            {
                _demonLog.CrateAndSave("UserTaskDataModel", "TaskRunner.RunTaskItem.taskItemIsNull", new { TaskId = taskId }, taskId);

                return;
            }

            if (taskItem.TaskEnd)
            {
                return;
            }
            lock (_runTaskItemLocker)
            {
                var currUtcTime = DateTime.UtcNow;
                var curTime     = UnixTime.ToTimestamp(currUtcTime);
                var endTime     = taskItem.DateActivate + taskItem.Duration;
                var timers      = TimerExecutor.GetTaskTimersByTaskId(taskItem.Id);
                if (timers.Count > 1)
                {
                    _demonLog.CrateAndSave("UserTaskDataModel", "TaskRunner.RunTaskItem.multipleTimers_", new { TaskId = taskId }, taskId);
                    return;
                }
                var timer = timers.FirstOrDefault();
                if (timer == null)
                {
                    var delay = endTime - curTime;
                    if (delay > 0)
                    {
                        _onUserTaskCreated(delay, taskItem.Id, taskItem.SourceUserId, taskItem.TargetPlanetId);
                        return;
                    }
                }
                else if (!timer.InProgress || timer.IsDisposed)
                {
                    return;
                }

                taskItem = _uTaskService.SetUpdateInProgress(connection, taskItem);

                #region Body

                try
                {
                    var targetPlanetId  = taskItem.TargetPlanetId;
                    var targetPlanetSrc = _gDetailPlanetService.GetPlanet(connection, targetPlanetId);

                    var defenderUser    = _gameUserService.GetGameUser(connection, targetPlanetSrc.UserId);
                    var defendorPremium = _storeService.GetPremiumWorkModel(connection, defenderUser.Id);
                    var defendorPlanet  = _planetRunner.RunSinglePlanet(connection, targetPlanetSrc, defendorPremium, _gDetailPlanetService);


                    //result.TimeOver = true;
                    //var user = taskItem.user;
                    UserDataModel sourceUser = null;
                    var           npc        = NpcHelper.GetNpcByName(Npc.SkagyName);
                    if (taskItem.SourceUserId == defenderUser.Id)
                    {
                        taskItem.IsAtack    = false;
                        taskItem.IsTransfer = true;
                        sourceUser          = defenderUser;
                    }
                    else
                    {
                        taskItem.IsAtack    = true;
                        taskItem.IsTransfer = false;
                        if (taskItem.SourceUserId == npc.NpcUser.Id)
                        {
                            sourceUser = npc.NpcUser;
                        }
                        else
                        {
                            sourceUser = _gameUserService.GetGameUser(connection, taskItem.SourceUserId);
                        }
                    }

                    #region IsAtack

                    if (taskItem.IsAtack)
                    {
                        var atackModel = new TmpAtackModel
                        {
                            TaskItem   = taskItem,
                            SourceUser = new TmpAtackItem
                            {
                                User = sourceUser
                            },
                            DefendorUser = new TmpAtackItem
                            {
                                User    = defenderUser,
                                Premium = defendorPremium
                            },
                            DefendorPlanet = defendorPlanet,
                            Npc            = npc,
                            BattleTime     = curTime,
                            BattleDtTime   = currUtcTime
                        };
                        _atack(connection, atackModel);
                        var ti     = atackModel.TaskItem.CloneDeep();
                        var report = atackModel.ReportDataModel.CloneDeep();
                        int?newTotalWinnerUserCc = atackModel.NewTotalWinnerUserCc;

                        Task.Factory.StartNew(() => { _uTaskService.NotyfyTaskFinished(ti, report, newTotalWinnerUserCc); });

                        return;
                    }

                    #endregion

                    #region IsTransfer

                    defendorPlanet.Hangar = UnitList.CalculateNewUnits(taskItem.SourceFleet, defendorPlanet.Hangar, true);
                    _gDetailPlanetService.AddOrUpdate(connection, defendorPlanet);
                    taskItem.TaskEnd = true;
                    _uTaskService.UnlockUpdateInProgress(connection, taskItem.Id);
                    var updatedTask = _uTaskService.AddOrUpdate(connection, taskItem);

                    Task.Factory.StartNew(() => { _uTaskService.NotyfyTaskFinished(updatedTask); });



                    #endregion
                }
                catch (Exception e)
                {
                    _demonLog.CrateAndSave("UserTaskDataModel", "TaskRunner.RunTaskItem.ExceptionTaskItemData", taskItem, taskItem.Id);
                    _demonLog.CrateAndSave("Exception", "TaskRunner.RunTaskItem.ExceptionData", e, taskItem.Id);
                    _uTaskService.UnlockUpdateInProgress(connection, taskItem.Id);
                    throw;
                }
            }

            #endregion
        }
Ejemplo n.º 2
0
        //item

        public int CreateTaskItem(IDbConnection connection, int currentUserId, string currentUserNickname, TaskFleet inputData)
        {
            var curretnUserPremium = _storeService.GetPremiumWorkModel(connection, currentUserId);
            //todo Check current User fleet

            //todo тип для мазера  byte srcType = 20;

            var isMother = inputData.SourceId == 0;


            var targetPlanet         = _gDetailPlanetService.GetPlanet(connection, inputData.TargetName);
            var targetPlanetGeometry = _geometryPlanetService.GetGeometryPlanetById(connection, targetPlanet.Id);
            var targetPlanetSystem   = _systemService.GetDetailSystemBySystemId(connection, targetPlanetGeometry.SystemId);

            var newTaskItem = new UserTaskDataModel {
                SourceTypeId = (byte)(isMother ? 20 : 0),
                // canselation = false,
                SourceOwnType    = !isMother,
                SourceOwnName    = MapTypes.Mother.ToString(),
                SourceUserId     = currentUserId,
                SourceOwnId      = inputData.SourceId,
                TargetPlanetName = inputData.TargetName,
                DateActivate     = UnixTime.UtcNow(),
                Duration         = 100,
                //sourceFleet = inputData.FixUnitCount(),
                IsAtack            = true,
                IsTransfer         = false,
                TargetPlanetId     = targetPlanet.Id,
                TargetPlanetTypeId = targetPlanetGeometry.TypeId,
                TargetSystemName   = targetPlanetSystem.Name
            };


            //todo  проверить наличие юнитов у пользователя
            if (inputData.IsTransfer)
            {
                newTaskItem.IsAtack    = false;
                newTaskItem.IsTransfer = true;
            }



            if (newTaskItem.SourceOwnType)
            {
                var sourcePlanet = _gDetailPlanetService.GetUserPlanet(connection, inputData.SourceId, currentUserId);
                sourcePlanet = _synchronizer.UserPlanet(connection, sourcePlanet, curretnUserPremium, _gDetailPlanetService);
                var sourcePlanetGeometry = _geometryPlanetService.GetGeometryPlanetById(connection, sourcePlanet.Id);
                var sourcePlanetSystem   =
                    _systemService.GetDetailSystemBySystemId(connection, sourcePlanetGeometry.SystemId);
                var duration =
                    MapDistanceHelper.CalculatePlanetTransferFleet(connection, sourcePlanet.Id, newTaskItem.TargetPlanetId,
                                                                   _mapAdressService, curretnUserPremium.IsActive);

                var baseUnits = sourcePlanet.Hangar;
                inputData.FixUnitCount(sourcePlanet.Hangar);
                var planetFixedUnits = inputData.Units;


                newTaskItem.SourceFleet      = planetFixedUnits;
                newTaskItem.SourceTypeId     = sourcePlanetGeometry.TypeId;
                newTaskItem.SourceOwnName    = sourcePlanet.Name;
                newTaskItem.SourceSystemName = sourcePlanetSystem.Name;
                newTaskItem.Duration         = duration.Sec;

                var calculatedUnitsToSaveToSource = UnitList.CalculateNewUnits(baseUnits, planetFixedUnits, false);
                sourcePlanet.Hangar = calculatedUnitsToSaveToSource;
                _gDetailPlanetService.AddOrUpdate(connection, sourcePlanet);
            }
            else
            {
                var mother = _mothershipService.GetMother(connection, currentUserId);
                mother = _synchronizer.UserMothership(connection, mother, curretnUserPremium, _mothershipService, _motherJumpService);
                var motherSystem = _systemService.GetDetailSystemBySystemId(connection, mother.StartSystemId);
                var duration     =
                    MapDistanceHelper.CalculateMotherTransferFleet(connection, mother.StartSystemId, newTaskItem.TargetPlanetId,
                                                                   _mapAdressService, curretnUserPremium.IsActive);

                var baseUnits = mother.Hangar;
                inputData.FixUnitCount(mother.Hangar);
                var motherfixedUnits = inputData.Units;

                newTaskItem.SourceFleet      = motherfixedUnits;
                newTaskItem.SourceSystemName = motherSystem.Name;
                newTaskItem.Duration         = duration.Sec; //25;


                // почему то небыло указано....
                newTaskItem.SourceTypeId  = 20;
                newTaskItem.SourceOwnName = currentUserNickname;

                //todo calc  оставшихся юнитов
                var calculatedUnitsToSaveToSource = UnitList.CalculateNewUnits(baseUnits, motherfixedUnits, false);
                mother.Hangar = calculatedUnitsToSaveToSource;
                _mothershipService.AddOrUpdate(connection, mother);
            }

            newTaskItem = _uTaskService.AddOrUpdate(connection, newTaskItem);

            var clonedTaskItem = newTaskItem.CloneDeep();
            var userId         = targetPlanet.UserId;

            _synchronizer.GetTaskRunner().OnUserTaskCreated(newTaskItem.CloneDeep(), userId);
            Task.Delay(50).ContinueWith(t => {
                _uTaskService.NotyfyTaskCreated(clonedTaskItem, userId);
            });
            return(newTaskItem.Id);
        }