Esempio n. 1
0
        /// <summary>
        /// Save data to database
        /// </summary>
        /// <param name="geoPoint">GeoPoint value</param>
        /// <param name="item">OpenStreetMapItem value</param>
        /// <returns>Save database task</returns>
        public async Task <bool> SaveDataAsync(GeoPoint geoPoint, OpenStreetMapItem item)
        {
            using (var context = new GeoContext())
            {
                var entity = await context.GeoData.FindAsync(geoPoint.Uuid);

                if (entity != null)
                {
                    return(false);
                }

                entity = new GeoEntity
                {
                    Uuid        = geoPoint.Uuid,
                    Latitude    = geoPoint.Latitude,
                    Longitude   = geoPoint.Longitude,
                    PlaceId     = item.PlaceId,
                    DisplayName = item.DisplayName,
                    City        = item.Address?.City,
                    Country     = item.Address?.Country,
                    CountryCode = item.Address?.CountryCode,
                    County      = item.Address?.County,
                    State       = item.Address?.State
                };

                await context.GeoData.AddAsync(entity);

                return(await context.SaveChangesAsync() > 0);
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,lat,lng")] GeoEntity geoEntity)
        {
            if (id != geoEntity.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(geoEntity);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!GeoEntityExists(geoEntity.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(geoEntity));
        }
Esempio n. 3
0
        public override void Process(Entity entity, InfluencePropagationCenter component1)
        {
            GeoEntity geoEntity = entity.GetComponent <GeoEntity>();
            var       result    = _dijkstraPathFinder.GetHexesInMovementRange(geoEntity.Position, 8);

            result.traversableTiles.ForEach(hex => { }
                                            );
        }
Esempio n. 4
0
        public void Think(Entity thinker, JEventBus eventBus)
        {
            _eventBus = eventBus;
            GeoEntity geoEntity = thinker.GetComponent <GeoEntity>();
            ArmyAi    armyAi    = thinker.GetComponent <ArmyAi>();
            Army      army      = thinker.GetComponent <Army>();

            FindStructureInArea findStructureInArea = new FindStructureInArea(geoEntity.Position, armyAi.SearchRadius);

            JEventBus.GetDefault().Post(findStructureInArea);

            Entity nearestStructure = null;

            foreach (var structureEntity in findStructureInArea.Results)
            {
                Structure structure    = structureEntity.GetComponent <Structure>();
                GeoEntity structureGeo = structureEntity.GetComponent <GeoEntity>();
                long      geoIndex     = GetGeoIndex(structureGeo.Position);
                if (StructureIsVisited(geoIndex, armyAi) || structure.Fraction == army.Fraction)
                {
                    continue;
                }
                //Check accessibility
                FindPathEvent findPathEvent = new FindPathEvent(geoEntity.Position, structureGeo.Position);
                _eventBus.Post(findPathEvent);
                if (findPathEvent.CalculatedPath == null)
                {
                    continue;
                }

                nearestStructure = structureEntity;
                break;
            }

            if (nearestStructure == null)
            {
                Debug.WriteLine(army + " Skip to FindResources");
                armyAi.ArmyStateMachine.Fire(ArmyTrigger.FindResources);
                return;
            }

            armyAi.SearchRadius = 10;

            GeoEntity resourcePosition = nearestStructure.GetComponent <GeoEntity>();

            GoToEvent goToEvent = new GoToEvent(thinker, resourcePosition.Position);

            Debug.WriteLine(army + " Go For Structure: " + goToEvent.Goal);
            JEventBus.GetDefault().Post(goToEvent);

            if (goToEvent.Complete)
            {
                armyAi.VisitedStructures.Add(GetGeoIndex(resourcePosition.Position));
            }

            armyAi.ArmyStateMachine.Fire(ArmyTrigger.FinishAction);
        }
Esempio n. 5
0
        public async Task <IActionResult> Create([Bind("Id,lat,lng")] GeoEntity geoEntity)
        {
            if (ModelState.IsValid)
            {
                _context.Add(geoEntity);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(geoEntity));
        }
        public void FindStructureInAreaListener(FindStructureInArea findStructureInArea)
        {
            Point middle = findStructureInArea.Location;

            foreach (var entity in _structures)
            {
                GeoEntity geoEntity = entity.GetComponent <GeoEntity>();
                float     distance  = Point.Distance(middle, geoEntity.Position);
                if (distance <= findStructureInArea.MaxDistance)
                {
                    findStructureInArea.Results.Add(entity);
                }
            }
        }
        public Entity BuildEntity(Entity e, EntityWorld entityWorld, params object[] args)
        {
            GeoEntity geoEntity = entityWorld.GetComponentFromPool <GeoEntity>();

            geoEntity.Position = args[1] as Point;
            e.AddComponent(geoEntity);

            Obstacle argObstacle = args[0] as Obstacle;
            Obstacle obstacle    = entityWorld.GetComponentFromPool <Obstacle>();

            obstacle.Definition = argObstacle.Definition;

            e.AddComponent(obstacle);
            return(e);
        }
        public Entity BuildEntity(Entity e, EntityWorld entityWorld, params object[] args)
        {
            GeoEntity geoEntity = entityWorld.GetComponentFromPool <GeoEntity>();

            geoEntity.Position = args[1] as Point;
            e.AddComponent(geoEntity);

            Structure argStructure = args[0] as Structure;
            Structure structure    = entityWorld.GetComponentFromPool <Structure>();

            structure.Definition = argStructure.Definition;

            e.AddComponent(structure);

            return(e);
        }
        public Entity BuildEntity(Entity e, EntityWorld entityWorld, params object[] args)
        {
            GeoEntity geoEntity = entityWorld.GetComponentFromPool <GeoEntity>();

            geoEntity.Position = args[1] as Point;
            e.AddComponent(geoEntity);

            Resource argResource = args[0] as Resource;
            Resource resource    = entityWorld.GetComponentFromPool <Resource>();

            resource.Amount     = argResource.Amount;
            resource.Definition = argResource.Definition;

            e.AddComponent(args[0] as Resource);
            return(e);
        }
Esempio n. 10
0
        public void GoToListener(GoToEvent goToEvent)
        {
            Entity entity = goToEvent.Entity;

            if (!entity.HasComponent <GeoEntity>())
            {
                Error("GoTo ERROR: Missing GeoEntity");
                return;
            }

            GeoEntity geoEntity = entity.GetComponent <GeoEntity>();
            Army      army      = entity.GetComponent <Army>();

            FindPathEvent findPathEvent = new FindPathEvent(geoEntity.Position, goToEvent.Goal);

            JEventBus.GetDefault().Post(findPathEvent);
            if (findPathEvent.CalculatedPath == null || findPathEvent.CalculatedPath.Count == 0)
            {
                Warning("GoTo ERROR: Path not found");
                return;
            }

            //Go to the same place (SPACE)
            if (findPathEvent.CalculatedPath.Count == 1)
            {
                findPathEvent.CalculatedPath.Add(findPathEvent.CalculatedPath[0]);
            }

            int i = 0;

            while (army.MovementPoints > 0)
            {
                if (i >= findPathEvent.CalculatedPath.Count - 1)
                {
                    break;
                }
                MoveToNextEvent moveToNextEvent = new MoveToNextEvent(findPathEvent.CalculatedPath, entity, i);
                JEventBus.GetDefault().Post(moveToNextEvent);
                i++;
            }

            Debug("GoTo OK: " + geoEntity.Position);
            if (geoEntity.Position.Equals(goToEvent.Goal))
            {
                goToEvent.Complete = true;
            }
        }
Esempio n. 11
0
        public void Think(Entity thinker, JEventBus eventBus)
        {
            _eventBus = eventBus;
            GeoEntity geoEntity = thinker.GetComponent <GeoEntity>();
            ArmyAi    armyAi    = thinker.GetComponent <ArmyAi>();
            Army      army      = thinker.GetComponent <Army>();

            FindResourceInArea findResourceInArea = new FindResourceInArea(geoEntity.Position, armyAi.SearchRadius);

            JEventBus.GetDefault().Post(findResourceInArea);

            Entity nearestResource = null;

            foreach (var resourceEntity in findResourceInArea.Results)
            {
                Resource  resource    = resourceEntity.GetComponent <Resource>();
                GeoEntity resourceGeo = resourceEntity.GetComponent <GeoEntity>();

                FindPathEvent findPathEvent = new FindPathEvent(geoEntity.Position, resourceGeo.Position);
                _eventBus.Post(findPathEvent);
                if (findPathEvent.CalculatedPath == null)
                {
                    continue;
                }

                nearestResource = resourceEntity;
                break;
            }
            if (nearestResource == null)
            {
                armyAi.SearchRadius += 5;
                Debug.WriteLine(army + " Skip to IDLE");
                armyAi.ArmyStateMachine.Fire(ArmyTrigger.FinishAction);
                return;
            }

            armyAi.SearchRadius = 10;

            GeoEntity resourcePosition = nearestResource.GetComponent <GeoEntity>();

            GoToEvent goToEvent = new GoToEvent(thinker, resourcePosition.Position);

            Debug.WriteLine(army + " Go For Resource: " + goToEvent.Goal);
            JEventBus.GetDefault().Post(goToEvent);

            armyAi.ArmyStateMachine.Fire(ArmyTrigger.FinishAction);
        }
        public Entity BuildEntity(Entity e, EntityWorld entityWorld, params object[] args)
        {
            Point     position      = args[0] as Point;
            float     influenceBase = (float)args[1];
            GeoEntity geoEntity     = entityWorld.GetComponentFromPool <GeoEntity>();

            geoEntity.Position = position;

            InfluencePropagationCenter influencePropagationCenter = entityWorld.GetComponentFromPool <InfluencePropagationCenter>();

            influencePropagationCenter.Position      = position;
            influencePropagationCenter.InfluenceBase = influenceBase;

            e.AddComponent(geoEntity);
            e.AddComponent(influencePropagationCenter);
            return(e);
        }
Esempio n. 13
0
        public static GeoEntity AddParents(GeoEntity entity, PolygonIndex index)
        {
            if (entity.Location == null)
            {
                return(entity);
            }

            var entities = index.Search(entity.Location);

            entity.Parents = entities.Select(x => new GeoEntityId
            {
                DataType  = x.DataType,
                Name      = x.Name,
                Reference = x.Reference
            }).ToList();

            return(entity);
        }
Esempio n. 14
0
        public Entity BuildEntity(Entity e, EntityWorld entityWorld, params object[] args)
        {
            Point     position   = args[0] as Point;
            int       population = (int)args[1];
            float     birdsRate  = (float)args[2];
            GeoEntity geoEntity  = entityWorld.GetComponentFromPool <GeoEntity>();

            geoEntity.Position = position;

            Urban urban = entityWorld.GetComponentFromPool <Urban>();

            urban.Population = population;
            urban.BirdsRate  = birdsRate;

            e.AddComponent(geoEntity);
            e.AddComponent(urban);
            return(e);
        }
Esempio n. 15
0
        public Entity BuildEntity(Entity e, EntityWorld entityWorld, params object[] args)
        {
            Point     position  = args[1] as Point;
            GeoEntity geoEntity = entityWorld.GetComponentFromPool <GeoEntity>();

            geoEntity.Position = position;

            Army argArmy = args[0] as Army;
            bool isAi    = args.Length <= 2 || (bool)args[2];

            e.AddComponent(geoEntity);
            e.AddComponent(argArmy);
            if (isAi)
            {
                e.AddComponent(PrepareArmyAi(entityWorld));
            }
            return(e);
        }
        public void FindNearestStructureListener(FindNearestStructure findNearestStructure)
        {
            Point middle = findNearestStructure.Location;

            foreach (var entity in _structures)
            {
                GeoEntity geoEntity = entity.GetComponent <GeoEntity>();
                float     distance  = Point.Distance(middle, geoEntity.Position);
                if (distance <= findNearestStructure.MaxDistance)
                {
                    if (findNearestStructure.Nearest == null || findNearestStructure.Distance > distance)
                    {
                        findNearestStructure.Nearest  = entity;
                        findNearestStructure.Distance = distance;
                    }
                }
            }
        }
Esempio n. 17
0
        public void AddStructureListener(AddStructureToFractionEvent addStructureToFractionEvent)
        {
            Fraction  newFraction = addStructureToFractionEvent.Fraction;
            Structure structure   = addStructureToFractionEvent.Structure;
            Fraction  oldFraction = structure.Fraction;
            GeoEntity geoEntity   = addStructureToFractionEvent.Entity.GetComponent <GeoEntity>();
            long      geoIndex    = _grid.GetNodeIndex(geoEntity.Position);

            if (newFraction == oldFraction)
            {
                return;
            }


            oldFraction?.Structures.Remove(geoIndex);
            newFraction?.Structures.Add(geoIndex, structure);
            structure.Fraction = newFraction;

            Debug("Mines captured: " + newFraction?.Structures[geoIndex]);
        }
Esempio n. 18
0
        public void Think(Entity thinker, JEventBus eventBus)
        {
            _eventBus = eventBus;
            GeoEntity geoEntity = thinker.GetComponent <GeoEntity>();
            ArmyAi    armyAi    = thinker.GetComponent <ArmyAi>();
            Army      army      = thinker.GetComponent <Army>();

            FindArmyInArea findStructureInArea = new FindArmyInArea(geoEntity.Position, armyAi.SearchRadius * 3);

            JEventBus.GetDefault().Post(findStructureInArea);

            Entity nearestEnemyArmy = null;

            foreach (var structureEntity in findStructureInArea.Results)
            {
                Army encounteredArmy = structureEntity.GetComponent <Army>();
                if (encounteredArmy.Fraction == army.Fraction)
                {
                    continue;
                }
                nearestEnemyArmy = structureEntity;
                break;
            }

            if (nearestEnemyArmy == null)
            {
                Debug.WriteLine(army + " Skip to FindStructure");
                armyAi.ArmyStateMachine.Fire(ArmyTrigger.FindStructure);
                return;
            }

            GeoEntity armyPosition = nearestEnemyArmy.GetComponent <GeoEntity>();

            GoToEvent goToEvent = new GoToEvent(thinker, armyPosition.Position);

            Debug.WriteLine(army + " Go For Battle!: " + goToEvent.Goal);
            JEventBus.GetDefault().Post(goToEvent);

            armyAi.ArmyStateMachine.Fire(ArmyTrigger.FinishAction);
        }
Esempio n. 19
0
        public Entity BuildEntity(Entity e, EntityWorld entityWorld, params object[] args)
        {
            Dictionary <string, object> paramsDictionary = (Dictionary <string, object>)args[2];

            GeoEntity geoEntity = entityWorld.GetComponentFromPool <GeoEntity>();

            geoEntity.Position = args[1] as Point;
            e.AddComponent(geoEntity);

            Structure argStructure = args[0] as Structure;
            Structure structure    = entityWorld.GetComponentFromPool <Structure>();

            structure.Definition = argStructure.Definition;
            e.AddComponent(structure);

            Habitat habitat = entityWorld.GetComponentFromPool <Habitat>();

            habitat.CreatureDefinition = paramsDictionary["definition"] as CreatureDefinition;
            habitat.Production         = (int)paramsDictionary["production"];
            e.AddComponent(habitat);

            return(e);
        }
Esempio n. 20
0
        public Entity BuildEntity(Entity e, EntityWorld entityWorld, params object[] args)
        {
            Dictionary <string, object> paramsDictionary = (Dictionary <string, object>)args[2];

            GeoEntity geoEntity = entityWorld.GetComponentFromPool <GeoEntity>();

            geoEntity.Position = args[1] as Point;
            e.AddComponent(geoEntity);

            Structure argStructure = args[0] as Structure;
            Structure structure    = entityWorld.GetComponentFromPool <Structure>();

            structure.Definition = argStructure.Definition;
            e.AddComponent(structure);

            Mine mine = entityWorld.GetComponentFromPool <Mine>();

            mine.ResourceDefinition = paramsDictionary["definition"] as ResourceDefinition;
            mine.Production         = (int)paramsDictionary["production"];
            e.AddComponent(mine);

            return(e);
        }
Esempio n. 21
0
        public static async void ProcessClicksForGeo(
            [QueueTrigger(QueueNames.ProcessClicksGeo)] string queuedHttpRequestString,
            [Table(TableNames.Redirects)] CloudTable redirectTable,
            [Table(TableNames.Domains)] CloudTable domainTable,
            [Table(TableNames.Geos)] CloudTable geoTable,
            ILogger log,
            ExecutionContext context)
        {
            HttpRequestEntity queuedHttpRequest = JsonConvert.DeserializeObject <HttpRequestEntity>(queuedHttpRequestString);

            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            string ipLookupUrl = $"{config["FREEGEOIP_HOST"] ??= "https://freegeoip.app"}/json/{queuedHttpRequest.RemoteIpAddress}";

            log.LogInformation($"Looking up freegeoip: {ipLookupUrl}.");


            var client      = new HttpClient();
            var getResponse = await client.GetAsync(ipLookupUrl);

            if (getResponse.StatusCode == HttpStatusCode.OK)
            {
                List <DomainEntity> domains = await DomainEntity.get(domainTable, queuedHttpRequest.Host);

                if (domains == null)
                {
                    throw new Exception($"Unable to process Geo lookup - domain {queuedHttpRequest.Host} wasn't found");
                }

                string path = queuedHttpRequest.Path.Value.Substring(1);

                RedirectEntity redirect = await RedirectEntity.get(redirectTable, domains.First().Account, path);

                if (redirect != null)
                {
                    string ipResponseString = await getResponse.Content.ReadAsStringAsync();

                    dynamic   ipResponse = JsonConvert.DeserializeObject <dynamic>(ipResponseString);
                    GeoEntity geoEntity  = new GeoEntity(ipResponse);
                    await GeoEntity.put(geoTable, geoEntity);

                    Dictionary <string, int> _geoCount = JsonConvert.DeserializeObject <Dictionary <string, int> >(redirect.GeoCount ??= "{}");
                    if (_geoCount.ContainsKey(geoEntity.RowKey))
                    {
                        log.LogInformation($"Incrementing GeoCount for redirect entity {queuedHttpRequest.Path}");

                        _geoCount[geoEntity.RowKey] = _geoCount[geoEntity.RowKey] + 1;
                    }
                    else
                    {
                        log.LogInformation($"Creating GeoCount for redirect entity {queuedHttpRequest.Path}");
                        _geoCount.Add(geoEntity.RowKey, 1);
                    }

                    redirect.GeoCount = JsonConvert.SerializeObject(_geoCount);
                    await RedirectEntity.put(redirectTable, redirect);

                    return;
                }


                log.LogError($"Http request {queuedHttpRequest.Path} for click handling doesn't match handled path");
                throw new System.Exception($"Http request {queuedHttpRequest.Path} for click handling doesn't match handled path");
            }

            log.LogError($"Free geo ip lookup for IP {queuedHttpRequest.RemoteIpAddress} failed with status code {getResponse.StatusCode}");
            throw new System.Exception($"Free geo ip lookup for IP {queuedHttpRequest.RemoteIpAddress} failed with status code {getResponse.StatusCode}");
        }
Esempio n. 22
0
        public static async void ProcessRedirectClicksForGeo(
            [QueueTrigger(QueueNames.ProcessRedirectClicksForGeo)] string queuedHttpRequestString,
            [Table(TableNames.RedirectSessions)] CloudTable redirectTable,
            ILogger log,
            ExecutionContext context)
        {
            HttpRequestEntity queuedHttpRequest = JsonConvert.DeserializeObject <HttpRequestEntity>(queuedHttpRequestString);

            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            string ipLookupUrl = $"{config["FREEGEOIP_HOST"] ??= "https://freegeoip.app"}/json/{queuedHttpRequest.RemoteIpAddress}";

            log.LogInformation($"Looking up freegeoip: {ipLookupUrl}.");

            var client      = new HttpClient();
            var getResponse = await client.GetAsync(ipLookupUrl);

            if (getResponse.StatusCode == HttpStatusCode.OK)
            {
                MatchCollection matches = Regex.Matches(queuedHttpRequest.Path, "/redirect/session/(\\d+)/", RegexOptions.IgnoreCase);
                if (matches.Count > 0)
                {
                    string ipResponseString = await getResponse.Content.ReadAsStringAsync();

                    dynamic   ipResponse      = JsonConvert.DeserializeObject(ipResponseString);
                    GeoEntity geoEntity       = new GeoEntity(ipResponse);
                    string    geoEntityString = JsonConvert.SerializeObject(geoEntity);

                    RedirectEntity redirectEntity = await RedirectEntity.get(redirectTable, matches[0].Groups[1].Value);

                    if (redirectEntity != null)
                    {
                        if (redirectEntity.GeoCount == null)
                        {
                            log.LogInformation($"Adding GeoCount property to redirect entity {queuedHttpRequest.Path}");
                            // redirectEntity.GeoCount = new Dictionary<string, int>();
                        }

                        Dictionary <string, int> _geoCount = JsonConvert.DeserializeObject <Dictionary <string, int> >(redirectEntity.GeoCount ??= "{}");
                        if (_geoCount.ContainsKey(geoEntityString))
                        {
                            log.LogInformation($"Incrementing GeoCount for redirect entity {queuedHttpRequest.Path}");

                            _geoCount[geoEntityString] = _geoCount[geoEntityString] + 1;
                        }
                        else
                        {
                            log.LogInformation($"Creating GeoCount for redirect entity {queuedHttpRequest.Path}");
                            _geoCount.Add(geoEntityString, 1);
                        }

                        log.LogInformation($" GeoCount property value: {JsonConvert.SerializeObject(redirectEntity.GeoCount)}");

                        await RedirectEntity.put(redirectTable, redirectEntity.RowKey, redirectEntity.RedirectTo, redirectEntity.VideoLink, redirectEntity.StartRedirectingMinutes ??= 0, redirectEntity.ClickCount, redirectEntity.CalendarClickCount, redirectEntity.VideoClickCount, JsonConvert.SerializeObject(_geoCount), redirectEntity.CalendarGeoCount, redirectEntity.VideoGeoCount);

                        log.LogInformation($"Successfully processed geo ip click for redirect query {queuedHttpRequest.Path} from {queuedHttpRequest.RemoteIpAddress}");

                        return;
                    }
                    else
                    {
                        log.LogError($"Http request {queuedHttpRequest.Path} for click geo handling failed to match a redirect session");
                        throw new System.Exception($"Http request {queuedHttpRequest.Path} for click geo handling failed to match a redirect session");
                    }
                }

                MatchCollection calendarMatches = Regex.Matches(queuedHttpRequest.Path, "/calendar/session/(\\d+)", RegexOptions.IgnoreCase);
                if (calendarMatches.Count > 0)
                {
                    string ipResponseString = await getResponse.Content.ReadAsStringAsync();

                    dynamic   ipResponse      = JsonConvert.DeserializeObject(ipResponseString);
                    GeoEntity geoEntity       = new GeoEntity(ipResponse);
                    string    geoEntityString = JsonConvert.SerializeObject(geoEntity);

                    RedirectEntity redirectEntity = await RedirectEntity.get(redirectTable, calendarMatches[0].Groups[1].Value);

                    if (redirectEntity != null)
                    {
                        Dictionary <string, int> _calendarGeoCount = JsonConvert.DeserializeObject <Dictionary <string, int> >(redirectEntity.CalendarGeoCount ??= "{}");
                        if (_calendarGeoCount.ContainsKey(geoEntityString))
                        {
                            log.LogInformation($"Incrementing CalendarGeoCount for redirect entity {queuedHttpRequest.Path}");

                            _calendarGeoCount[geoEntityString] = _calendarGeoCount[geoEntityString] + 1;
                        }
                        else
                        {
                            log.LogInformation($"Creating CalendarGeoCount for redirect entity {queuedHttpRequest.Path}");
                            _calendarGeoCount.Add(geoEntityString, 1);
                        }

                        log.LogInformation($" CalendarGeoCount property value: {JsonConvert.SerializeObject(redirectEntity.GeoCount)}");

                        await RedirectEntity.put(redirectTable, redirectEntity.RowKey, redirectEntity.RedirectTo, redirectEntity.VideoLink, redirectEntity.StartRedirectingMinutes ??= 0, redirectEntity.ClickCount, redirectEntity.CalendarClickCount, redirectEntity.VideoClickCount, redirectEntity.GeoCount, JsonConvert.SerializeObject(_calendarGeoCount), redirectEntity.VideoGeoCount);

                        log.LogInformation($"Successfully processed calendar geo ip click for redirect query {queuedHttpRequest.Path} from {queuedHttpRequest.RemoteIpAddress}");

                        return;
                    }
                    else
                    {
                        log.LogError($"Http request {queuedHttpRequest.Path} for click geo handling failed to match a redirect session");
                        throw new System.Exception($"Http request {queuedHttpRequest.Path} for click geo handling failed to match a redirect session");
                    }
                }

                MatchCollection videoMatches = Regex.Matches(queuedHttpRequest.Path, "/redirect/video/(\\d+)/", RegexOptions.IgnoreCase);
                if (videoMatches.Count > 0)
                {
                    string ipResponseString = await getResponse.Content.ReadAsStringAsync();

                    dynamic   ipResponse      = JsonConvert.DeserializeObject(ipResponseString);
                    GeoEntity geoEntity       = new GeoEntity(ipResponse);
                    string    geoEntityString = JsonConvert.SerializeObject(geoEntity);

                    RedirectEntity redirectEntity = await RedirectEntity.get(redirectTable, videoMatches[0].Groups[1].Value);

                    if (redirectEntity != null)
                    {
                        Dictionary <string, int> _videoGeoCount = JsonConvert.DeserializeObject <Dictionary <string, int> >(redirectEntity.VideoGeoCount ??= "{}");
                        if (_videoGeoCount.ContainsKey(geoEntityString))
                        {
                            log.LogInformation($"Incrementing VideoGeoCount for redirect entity {queuedHttpRequest.Path}");

                            _videoGeoCount[geoEntityString] = _videoGeoCount[geoEntityString] + 1;
                        }
                        else
                        {
                            log.LogInformation($"Creating VideoGeoCount for redirect entity {queuedHttpRequest.Path}");
                            _videoGeoCount.Add(geoEntityString, 1);
                        }

                        log.LogInformation($" VideoGeoCount property value: {JsonConvert.SerializeObject(redirectEntity.GeoCount)}");

                        await RedirectEntity.put(redirectTable, redirectEntity.RowKey, redirectEntity.RedirectTo, redirectEntity.VideoLink, redirectEntity.StartRedirectingMinutes ??= 0, redirectEntity.ClickCount, redirectEntity.CalendarClickCount, redirectEntity.VideoClickCount, redirectEntity.GeoCount, redirectEntity.CalendarGeoCount, JsonConvert.SerializeObject(_videoGeoCount));

                        log.LogInformation($"Successfully processed video geo ip click for redirect query {queuedHttpRequest.Path} from {queuedHttpRequest.RemoteIpAddress}");

                        return;
                    }
                    else
                    {
                        log.LogError($"Http request {queuedHttpRequest.Path} for click geo handling failed to match a redirect session");
                        throw new System.Exception($"Http request {queuedHttpRequest.Path} for click geo handling failed to match a redirect session");
                    }
                }

                log.LogError($"Http request {queuedHttpRequest.Path} for click handling doesn't match handled paths");
                throw new System.Exception($"Http request {queuedHttpRequest.Path} for click handling doesn't match handled paths");
            }

            log.LogError($"Free geo ip lookup for IP {queuedHttpRequest.RemoteIpAddress} failed with status code {getResponse.StatusCode}");
            throw new System.Exception($"Free geo ip lookup for IP {queuedHttpRequest.RemoteIpAddress} failed with status code {getResponse.StatusCode}");
        }
 public static GeoEntity AddParents(GeoEntity entity, IEnumerable <GeoEntity> sourceData, List <CrossReference> crossreferences)
 {
     entity.Parents = FindParents(entity.GeoId, sourceData, crossreferences);
     return(entity);
 }
Esempio n. 24
0
 public ArmyChangeEvent(Army army, GeoEntity geoEntity)
 {
     Army      = army;
     GeoEntity = geoEntity;
 }