Esempio n. 1
0
        private async Task <OcrResult <IGym> > GetGym(Image <Rgba32> imageFragment, RaidImageConfiguration imageConfiguration, FenceConfiguration[] fences, int interactiveLimit)
        {
            imageFragment = imageConfiguration.PreProcessGymNameFragment(imageFragment);

            var ocrResult = await GetOcrResultAsync(imageFragment);

            if (!(ocrResult.Value > 0))
            {
                return(new OcrResult <IGym>(false, ocrResult.Key));
            }
            var similarGyms = await GymService.GetSimilarGymsByNameAsync(ocrResult.Key, fences, interactiveLimit * 2);

            if (similarGyms.Count == 0)
            {
                return(new OcrResult <IGym>(false, ocrResult.Key));
            }

            // Check if there are exact matches
            var exactMatches = similarGyms.Where(e => e.Value == 1).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            if (exactMatches.Count > 0)
            {
                similarGyms = exactMatches;
            }

            var results = similarGyms.Select(kvp => new KeyValuePair <IGym, double>(kvp.Key, kvp.Value)).ToArray();

            return(new OcrResult <IGym>(true, ocrResult.Key, results));
        }
        private async Task <ServiceResponse> AddResolveGymAsync(Type textResource, ZonedDateTime requestStartInUtc, DateTimeZone userZone, string gymName, byte level, IPokemon pokemon, IRaidboss raidboss, TimeSpan timeSpan, int interactiveLimit, FenceConfiguration[] fences)
        {
            var gymResponse = await GymService.GetGymAsync(textResource, gymName, interactiveLimit, (selectedGymId) => AddResolveGymAsync(textResource, requestStartInUtc, userZone, selectedGymId, level, pokemon, raidboss, timeSpan, interactiveLimit, fences), fences);

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

            return(await AddSaveAsync(textResource, requestStartInUtc, userZone, gymResponse.Result, level, pokemon, raidboss, timeSpan));
        }
        public async Task <ServiceResponse> HatchResolveGymAsync(Type textResource, string gymName, IPokemon pokemon, IRaidboss raidboss, int interactiveLimit, FenceConfiguration[] fences)
        {
            var gymResponse = await GymService.GetGymAsync(textResource, gymName, interactiveLimit, (selectedGymId) => HatchResolveGymWithIdAsync(textResource, selectedGymId, pokemon, raidboss, interactiveLimit), fences);

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

            return(await HatchSaveAsync(textResource, gymResponse.Result, pokemon, raidboss, interactiveLimit));
        }
        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();

            await GymService.UpdateGymAsync(gym);

            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> HatchResolveGymWithIdAsync(Type textResource, int gymId, IPokemon pokemon, IRaidboss raidboss, int interactiveLimit)
        {
            var gym = await GymService.GetGymByIdAsync(gymId);

            return(await HatchSaveAsync(textResource, gym, pokemon, raidboss, interactiveLimit));
        }
        public async Task <ServiceResponse> AddResolveGymAsync(Type textResource, ZonedDateTime requestStartInUtc, DateTimeZone userZone, int gymId, byte level, IPokemon pokemon, IRaidboss raidboss, TimeSpan timeSpan, int interactiveLimit, FenceConfiguration[] fences)
        {
            var gym = await GymService.GetGymByIdAsync(gymId);

            return(await AddSaveAsync(textResource, requestStartInUtc, userZone, gym, level, pokemon, raidboss, timeSpan));
        }
Esempio n. 8
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);
        }
Esempio n. 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 (!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);
        }