protected override async Task ExecuteCommand(string parameters = "")
        {
            var updateResult = await _client.RunUpdateUserInfo(_travianUser);

            if (updateResult.Errors == null || !updateResult.Errors.Any())
            {
                var update = updateResult.Villages.Select(x => _mapper.Map <VillageModel>(x));
                await _villageRepository.UpdateBaseInfos(update);

                if (_travianUser.PlayerData == null)
                {
                    _travianUser.PlayerData = new PlayerDataModel
                    {
                        Alliance    = updateResult.Player.Alliance,
                        TimeZone    = updateResult.Player.TimeZone,
                        Tribe       = (Tribe)updateResult.Player.Tribe,
                        UserName    = _travianUser.UserName,
                        VillagesIds = update.Select(x => x.VillageId).ToList(),
                        Status      = PlayerStatus.ALL_QUIET
                    };
                }
                else
                {
                    _travianUser.PlayerData.Alliance = updateResult.Player.Alliance;
                    _travianUser.PlayerData.TimeZone = updateResult.Player.TimeZone;
                    _travianUser.PlayerData.Tribe    = (Tribe)updateResult.Player.Tribe;
                }

                await _travianUserRepository.ReplacePlayerData(_travianUser);

                await _botService.SendTextMessageAsync(_chatId, $"User info updated");
            }
            else
            {
                await _botService.SendTextMessageAsync(_chatId, $"Unable to update user info: [{string.Join("], [", updateResult.Errors)}]");
            }
        }
示例#2
0
        protected sealed override async Task ExecuteCommand(string parameters = "")
        {
            var jobData = new JobExecutionData
            {
                Cron        = Cron,
                JobType     = JobType,
                Start       = Start,
                TravianUser = _travianUser
            };

            var commandKey = SchedulerService.BuildJobKey(jobData);

            await CleanContext(commandKey);

            var isRunning = await _schedulerService.IsJobRunning(commandKey);

            if (_travianUser.ExecutionContext == null)
            {
                _travianUser.ExecutionContext = new ExecutionContextModel
                {
                    Commands       = new List <CommandModel>(),
                    IsUserNotified = false
                };
            }
            else if (_travianUser.ExecutionContext.Commands == null)
            {
                _travianUser.ExecutionContext.Commands = new List <CommandModel>();
            }
            if (_travianUser.PlayerData == null || string.IsNullOrEmpty(_travianUser.PlayerData?.TimeZone) || _travianUser.PlayerData.Tribe == Tribe.NOT_SPECIFIED)
            {
                var updateInfoResult = await _gameplayClient.RunUpdateUserInfo(_travianUser);

                _travianUser.PlayerData = new PlayerDataModel
                {
                    Status   = PlayerStatus.ALL_QUIET,
                    Tribe    = (Tribe)updateInfoResult.Player.Tribe,
                    TimeZone = updateInfoResult.Player.TimeZone,
                    // TODO: add alliance
                    UserName = _travianUser.UserName
                };
            }

            string msg;

            if (parameters.Contains("stop"))
            {
                if (isRunning)
                {
                    var stopResult = await _schedulerService.InterruptCommandAsync(commandKey);

                    if (stopResult)
                    {
                        var i = _travianUser.ExecutionContext.Commands.FindIndex(
                            x => x.Name == Name &&
                            x.KeyGroup == commandKey.Group &&
                            x.KeyName == commandKey.Name);

                        if (i > -1)
                        {
                            _travianUser.ExecutionContext.Commands[i].IsRunning = false;
                            await _travianUserRepository.Update(_travianUser);
                        }
                    }

                    msg = stopResult
                        ? $"The {Name} command for player {_travianUser.UserName} has been stopped."
                        : $"Can not stop the {Name} command for player {_travianUser.UserName}.";
                }
                else
                {
                    msg = $"The {Name} command is not running for player {_travianUser.UserName}.";
                }
            }
            else
            {
                if (isRunning)
                {
                    msg = $"The {Name} command is already being executing for player {_travianUser.UserName}.";
                }
                else
                {
                    msg = $"Starting the {Name} command for player {_travianUser.UserName}.";

                    await _schedulerService.ScheduleCommandAsync(jobData, new CancellationToken());

                    var i = _travianUser.ExecutionContext?.Commands?.FindIndex(
                        x => x.Name == Name &&
                        x.KeyGroup == commandKey.Group &&
                        x.KeyName == commandKey.Name &&
                        x.StartDateTime == Start);
                    if (i.HasValue && i > -1)
                    {
                        _travianUser.ExecutionContext.Commands[i.Value].IsRunning = true;
                    }
                    else
                    {
                        _travianUser.ExecutionContext.Commands.Add(new CommandModel
                        {
                            Name          = Name,
                            IsRunning     = true,
                            KeyName       = commandKey.Name,
                            KeyGroup      = commandKey.Group,
                            StartDateTime = Start
                        });
                    }

                    await _travianUserRepository.Update(_travianUser);
                }
            }

            await _bot.SendTextMessageAsync(_chatId, msg);
        }