private async Task <ServiceResponse> AddResolvePokemonOrLevelAsync(Type textResource, ZonedDateTime requestStartInUtc, DateTimeZone userZone, string gymName, string pokemonNameOrLevel, TimeSpan timeSpan, int interactiveLimit, FenceConfiguration[] fences)
        {
            if (int.TryParse(pokemonNameOrLevel, out int raidLevel))
            {
                if (raidLevel < 1)
                {
                    return(new ServiceResponse(false, LocalizationService.Get(textResource, "Raids_Errors_LevelToLow")));
                }
                if (raidLevel > 5)
                {
                    return(new ServiceResponse(false, LocalizationService.Get(textResource, "Raids_Errors_LevelToHigh")));
                }
                return(await AddResolveGymAsync(textResource, requestStartInUtc, userZone, gymName, Convert.ToByte(raidLevel), null, null, timeSpan, interactiveLimit, fences));
            }

            var pokemonResponse = await PokemonService.GetPokemonAndRaidbossAsync(textResource, pokemonNameOrLevel, interactiveLimit, (selectedPokemonName) => AddResolvePokemonOrLevelAsync(textResource, requestStartInUtc, userZone, gymName, selectedPokemonName, timeSpan, interactiveLimit, fences));

            if (!pokemonResponse.IsSuccess)
            {
                return(pokemonResponse);
            }

            var pokemonAndRaidboss = pokemonResponse.Result;
            var pokemon            = pokemonAndRaidboss.Pokemon;
            var raidboss           = pokemonAndRaidboss.Raidboss;

            return(await AddResolveGymAsync(textResource, requestStartInUtc, userZone, gymName, Convert.ToByte(raidboss.Level), pokemon, raidboss, timeSpan, interactiveLimit, fences));
        }
示例#2
0
        public async Task <ServiceResponse> AddRaidAsync(Type textResource, ZonedDateTime requestStartInUtc, DateTimeZone userZone, string filePath, int interactiveLimit, FenceConfiguration[] fences, bool testMode)
        {
            SaveDebugImages = testMode;
            using (var image = Image.Load(filePath))
            {
                var configuration = GetConfiguration(image);
                configuration.PreProcessImage(image);
                if (SaveDebugImages)
                {
                    image.Save("_AfterPreprocess.png");
                }

                var raidOcrResult = await GetRaidOcrResultAsync(image, configuration, interactiveLimit, fences);

                if (!raidOcrResult.IsRaidImage)
                {
                    return(new ServiceResponse(false, LocalizationService.Get(textResource, "Raids_Errors_NotAnRaidImage")));
                }

                if (raidOcrResult.IsRaidBoss)
                {
                    var timeLeft = raidOcrResult.RaidTimer.GetFirst();
                    return(await InteractivePokemonResolve(textResource, requestStartInUtc, userZone, timeLeft, raidOcrResult, fences, interactiveLimit));
                }
                else
                {
                    var timeLeft = raidOcrResult.EggTimer.GetFirst();
                    var level    = raidOcrResult.EggLevel.GetFirst();
                    return(await InteractiveGymResolve(textResource, requestStartInUtc, userZone, timeLeft, level, null, raidOcrResult, fences, interactiveLimit));
                }
            }
        }
        public async Task <ServiceResponse> AddAsync(Type textResource, ZonedDateTime requestStartInUtc, DateTimeZone userZone, string gymName, string pokemonNameOrLevel, string timeLeft, int interactiveLimit, FenceConfiguration[] fences)
        {
            var startEndTime = GetTimeSpan(timeLeft);

            if (!startEndTime.HasValue)
            {
                return(new ServiceResponse(false, LocalizationService.Get(textResource, "Raids_Errors_TimeFormat")));
            }

            return(await AddResolvePokemonOrLevelAsync(textResource, requestStartInUtc, userZone, gymName, pokemonNameOrLevel, startEndTime.Value, interactiveLimit, fences));
        }
 public async Task <ServiceResponse <IGym> > GetGymAsync(Type textResource, string name, int interactiveLimit, Func <int, Task <ServiceResponse> > interactiveCallbackAction, FenceConfiguration[] fences = null)
 {
     return(await InteractiveServiceHelper.GenericGetEntityWithCallback(
                GetGymsByNameAsync(name, fences),
                list => list.Where(e => e.Name.ToLowerInvariant().Trim() == name.ToLowerInvariant().Trim()).ToList(),
                interactiveLimit,
                interactiveCallbackAction,
                gym => gym.Id,
                GetGymNameWithAdditionAsync,
                gym => gym.Name,
                () => LocalizationService.Get(textResource, "Gyms_Errors_NothingFound", name),
                list => LocalizationService.Get(textResource, "Gyms_Errors_ToManyFound", list.Count, name, interactiveLimit),
                list => LocalizationService.Get(textResource, "Gyms_Errors_InteractiveMode", list.Count, name)
                ));
 }
 public async Task <ServiceResponse <RaidbossPokemon> > GetPokemonAndRaidbossAsync(Type textResource, string name, int interactiveLimit, Func <string, Task <ServiceResponse> > interactiveCallback)
 {
     return(await InteractiveServiceHelper.GenericGetEntityWithCallback(
                GetPossibleRaidbossPokemonAsync(name),
                list => list.Where(e => e.Pokemon.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)).ToList(),
                interactiveLimit,
                interactiveCallback,
                pokemon => pokemon.Pokemon.Name,
                (pokemon, list) => Task.FromResult(pokemon.Pokemon.Name),
                pokemon => pokemon.Pokemon.Name,
                () => LocalizationService.Get(textResource, "Pokemon_Errors_NothingFound", name, "raidboss-"),
                list => LocalizationService.Get(textResource, "Pokemon_Errors_ToManyFound", list.Count, name, interactiveLimit, "raidboss-"),
                list => LocalizationService.Get(textResource, "Pokemon_Errors_InteractiveMode", list.Count, name, "raidboss-")
                ));
 }
示例#6
0
        public async Task <ServiceResponse> HatchSaveAsync(Type textResource, IGym gym, IPokemon pokemon, IRaidboss raidboss, int interactiveLimit)
        {
            var beforeSpawnTime = SystemClock.Instance.GetCurrentInstant().Minus(Duration.FromMinutes(90)).ToUnixTimeSeconds();
            var raid            = await RaidRepository.FindAsync(e => e.FortId == gym.Id && e.TimeSpawn > beforeSpawnTime);

            if (raid == null)
            {
                return(new ServiceResponse(false, LocalizationService.Get(textResource, "Raids_Errors_Hatch_NoEntryFound", gym.Name)));
            }

            raid.PokemonId = (short)raidboss.Id;
            await RaidRepository.SaveAsync();

            return(new ServiceResponse(true, LocalizationService.Get(textResource, "Raids_Messages_BossHatched", pokemon.Name, gym.Name)));
        }
        private async Task <ServiceResponse> AddSaveAsync(Type textResource, ZonedDateTime requestStartInUtc, DateTimeZone userZone, IGym gym, byte level, IPokemon pokemon, IRaidboss raidboss, TimeSpan timeSpan)
        {
            var utcNowAfterProcessing   = SystemClock.Instance.GetCurrentInstant().InUtc();
            var processingTime          = utcNowAfterProcessing.Minus(requestStartInUtc);
            var durationMinusProcessing = Duration.FromTimeSpan(timeSpan).Minus(processingTime);

            var expiry = utcNowAfterProcessing.Plus(durationMinusProcessing).ToInstant();

            // Create the raid entry
            var beforeSpawnTime = utcNowAfterProcessing.Minus(Duration.FromMinutes(105)).ToInstant().ToUnixTimeSeconds();
            var raid            = await RaidRepository.FindAsync(e => e.FortId == gym.Id && e.TimeSpawn > beforeSpawnTime);

            if (raid == null)
            {
                raid            = RaidRepository.CreateInstance();
                raid.ExternalId = ThreadLocalRandom.NextLong();
                raid.FortId     = gym.Id;
                RaidRepository.Add(raid);
            }

            string message;

            if (raidboss == null)
            {
                raid.Level      = level;
                raid.TimeSpawn  = (int)expiry.Minus(_eggDuration).ToUnixTimeSeconds();
                raid.TimeBattle = (int)expiry.ToUnixTimeSeconds();
                raid.TimeEnd    = (int)expiry.Plus(_raidDuration).ToUnixTimeSeconds();
                message         = LocalizationService.Get(textResource, "Raids_Messages_EggAdded", level, gym.Name, FormatExpiry(expiry, userZone));
            }
            else
            {
                raid.PokemonId  = (short)raidboss.Id;
                raid.Level      = level;
                raid.TimeSpawn  = (int)expiry.Minus(Duration.FromMinutes(105)).ToUnixTimeSeconds();
                raid.TimeBattle = (int)expiry.Minus(Duration.FromMinutes(45)).ToUnixTimeSeconds();
                raid.TimeEnd    = (int)expiry.ToUnixTimeSeconds();
                message         = LocalizationService.Get(textResource, "Raids_Messages_BossAdded", pokemon.Name, gym.Name, FormatExpiry(expiry, userZone));
            }

            await RaidRepository.SaveAsync();

            await GymService.UpdateGymAsync(gym);

            return(new ServiceResponse(true, message));
        }
        public async Task <ServiceResponse> GetRaidList(Type textResource, ZonedDateTime requestStartInUtc, DateTimeZone userZone, string pokemonNameOrLevel, FenceConfiguration[] fences, string orderType, int interactiveLimit, Func <RaidListInfo, string> formatResult)
        {
            var order     = GetRaidListOrder(orderType);
            var startTime = Instant.FromDateTimeUtc(new DateTime(2018, 5, 1, 0, 0, 0).ToUniversalTime()).ToUnixTimeSeconds();

            if (int.TryParse(pokemonNameOrLevel, out int raidLevel))
            {
                if (raidLevel < 1)
                {
                    return(new ServiceResponse <RaidListInfo>(false, LocalizationService.Get(textResource, "Raids_Errors_LevelToLow"), null));
                }
                if (raidLevel > 5)
                {
                    return(new ServiceResponse <RaidListInfo>(false, LocalizationService.Get(textResource, "Raids_Errors_LevelToHigh"), null));
                }

                // Query Level
                var raids = await RaidRepository.FindAllWithGymsAsync(e => e.TimeSpawn > startTime && e.Level == raidLevel);

                return(await GetListResult(raidLevel, null, raids, order, formatResult));
            }
            else
            {
                var pokemonResponse = await PokemonService.GetPokemonAndRaidbossAsync(textResource, pokemonNameOrLevel,
                                                                                      interactiveLimit,
                                                                                      (selectedPokemonName) =>
                                                                                      GetRaidList(textResource, requestStartInUtc, userZone, selectedPokemonName, fences, orderType, interactiveLimit, formatResult));

                if (!pokemonResponse.IsSuccess)
                {
                    return(pokemonResponse);
                }

                var pokemonAndRaidboss = pokemonResponse.Result;
                var raidboss           = pokemonAndRaidboss.Raidboss;
                // Query Raidboss
                var raids = await RaidRepository.FindAllWithGymsAsync(e => e.TimeSpawn > startTime && e.PokemonId == raidboss.Id);

                return(await GetListResult(raidboss.Level, pokemonAndRaidboss, raids, order, formatResult));
            }
        }
示例#9
0
        private async Task <ServiceResponse> InteractiveGymResolve(Type textResource, ZonedDateTime requestStartInUtc, DateTimeZone userZone, TimeSpan timeLeft, int level, RaidbossPokemon raidbossPokemon, RaidOcrResult raidOcrResult, FenceConfiguration[] fences, int interactiveLimit)
        {
            if (!UseInteractiveMode(raidOcrResult.Gym))
            {
                return(await AddRaidAsync(textResource, requestStartInUtc, userZone, raidOcrResult.Gym.GetFirst().Id, level, raidbossPokemon, timeLeft, raidOcrResult, fences, interactiveLimit));
            }

            if (raidOcrResult.Gym.Results == null || raidOcrResult.Gym.Results.Length == 0)
            {
                return(new ServiceResponse(false, LocalizationService.Get(textResource, "Gyms_Errors_NothingFound", raidOcrResult.Gym.OcrValue)));
            }

            // If the interactive response limit is reached try to select the all gyms with the same name as from the ocr result
            var gyms = raidOcrResult.Gym.Results.Select(e => e.Key).ToList();

            if (gyms.Count > interactiveLimit)
            {
                var ocrString = raidOcrResult.Gym.OcrValue.Trim().ToLowerInvariant();
                gyms = gyms.Where(e => e.Name.Trim().ToLowerInvariant() == ocrString).ToList();
                // If we didn't found any return to the precious state
                if (gyms.Count == 0)
                {
                    gyms = raidOcrResult.Gym.Results.Select(e => e.Key).ToList();
                }
            }

            var gymCallbacks = InteractiveServiceHelper.GenericCreateCallbackAsync(interactiveLimit,
                                                                                   (selectedGym) =>
                                                                                   AddRaidAsync(textResource, requestStartInUtc, userZone, selectedGym, level, raidbossPokemon,
                                                                                                timeLeft, raidOcrResult, fences, interactiveLimit),
                                                                                   gym => gym.Id,
                                                                                   (gym, list) => GymService.GetGymNameWithAdditionAsync(gym, list),
                                                                                   list => LocalizationService.Get(textResource, "Gyms_Errors_ToManyFound", list.Count, raidOcrResult.Gym.OcrValue, interactiveLimit),
                                                                                   list => LocalizationService.Get(textResource, "Gyms_Errors_InteractiveMode", list.Count, raidOcrResult.Gym.OcrValue), gyms);

            return(await gymCallbacks);
        }
示例#10
0
        private async Task <ServiceResponse> InteractiveGymResolve(Type textResource, ZonedDateTime requestStartInUtc, DateTimeZone userZone, TimeSpan timeLeft, int level, RaidbossPokemon raidbossPokemon, RaidOcrResult raidOcrResult, FenceConfiguration[] fences, int interactiveLimit)
        {
            if (!InteractiveServiceHelper.UseInteractiveMode(raidOcrResult.Gym.Results))
            {
                return(await AddRaidAsync(textResource, requestStartInUtc, userZone, raidOcrResult.Gym.GetFirst().Id, level, raidbossPokemon, timeLeft, raidOcrResult, fences, interactiveLimit));
            }

            if (raidOcrResult.Gym.Results == null || raidOcrResult.Gym.Results.Length == 0)
            {
                return(new ServiceResponse(false, LocalizationService.Get(textResource, "Gyms_Errors_NothingFound", raidOcrResult.Gym.OcrValue)));
            }

            var gymCallbacks = InteractiveServiceHelper.GenericCreateCallbackAsync(interactiveLimit,
                                                                                   (selectedGym) =>
                                                                                   AddRaidAsync(textResource, requestStartInUtc, userZone, selectedGym, level, raidbossPokemon,
                                                                                                timeLeft, raidOcrResult, fences, interactiveLimit),
                                                                                   gym => gym.Id,
                                                                                   (gym, list) => GymService.GetGymNameWithAdditionAsync(gym, list),
                                                                                   list => LocalizationService.Get(textResource, "Gyms_Errors_ToManyFound", list.Count, raidOcrResult.Gym.OcrValue, interactiveLimit),
                                                                                   list => LocalizationService.Get(textResource, "Gyms_Errors_InteractiveMode", list.Count, raidOcrResult.Gym.OcrValue),
                                                                                   raidOcrResult.Gym.Results.Select(e => e.Key).ToList());

            return(await gymCallbacks);
        }
示例#11
0
        private async Task <ServiceResponse> InteractivePokemonResolve(Type textResource, ZonedDateTime requestStartInUtc, DateTimeZone userZone, TimeSpan timeLeft, RaidOcrResult raidOcrResult, FenceConfiguration[] fences, int interactiveLimit)
        {
            if (UseInteractiveMode(raidOcrResult.Pokemon))
            {
                var pokemonCallbacks = InteractiveServiceHelper.GenericCreateCallbackAsync(interactiveLimit,
                                                                                           (selectedPokemon) =>
                                                                                           InteractiveGymResolve(textResource, requestStartInUtc, userZone, timeLeft, GetRaidbossPokemonById(selectedPokemon, raidOcrResult).Raidboss.Level, GetRaidbossPokemonById(selectedPokemon, raidOcrResult), raidOcrResult, fences, interactiveLimit),
                                                                                           pokemon => pokemon.Pokemon.Id,
                                                                                           (pokemon, list) => Task.FromResult(pokemon.Pokemon.Name),
                                                                                           list => LocalizationService.Get(textResource, "Pokemon_Errors_ToManyFound", list.Count, raidOcrResult.Pokemon.OcrValue, interactiveLimit, "raidboss-"),
                                                                                           list => LocalizationService.Get(textResource, "Pokemon_Errors_InteractiveMode", list.Count, raidOcrResult.Pokemon.OcrValue, "raidboss-"),
                                                                                           raidOcrResult.Pokemon.Results.Select(e => e.Key).ToList());
                return(await pokemonCallbacks);
            }

            var raidbossPokemon = raidOcrResult.Pokemon.GetFirst();

            return(await InteractiveGymResolve(textResource, requestStartInUtc, userZone, timeLeft, raidbossPokemon.Raidboss.Level, raidbossPokemon, raidOcrResult, fences,
                                               interactiveLimit));
        }