private async Task <RouteModel> GetCastleRoute(CastleModel fromCastle, CastleModel toCastle)
        {
            var direction = await _directionProvider.GetDirection(fromCastle.Position, toCastle.Position);

            if (direction.Status == DirectionsStatusCodes.OK && direction.Routes != null && direction.Routes.Any() &&
                direction.Routes.ElementAt(0).Legs != null && direction.Routes.ElementAt(0).Legs.Any())
            {
                var selectedRouteLeg = direction.Routes.ElementAt(0).Legs.ElementAt(0);
                return(new RouteModel
                {
                    Steps = selectedRouteLeg.Steps.Select(step => new RouteStepModel()
                    {
                        StartLocation =
                            new PositionModel
                        {
                            Lat = step.StartLocation.Latitude,
                            Lng = step.StartLocation.Longitude
                        },
                        EndLocation =
                            new PositionModel {
                            Lat = step.EndLocation.Latitude, Lng = step.EndLocation.Longitude
                        },
                        Distance = step.Distance.Value,
                        Duration = step.Duration.Value
                    }).ToList(),
                    Distance = selectedRouteLeg.Distance.Value,
                    Duration = selectedRouteLeg.Duration.Value
                });
            }
            return(null);
        }
Exemple #2
0
        // Inspired by Tessera's Castle scene
        public void CastleSetup()
        {
            var topology = new GridTopology(10, 10, 10, false);

            var model = CastleModel.Get();

            propagator3 = new TilePropagator(model, topology, new TilePropagatorOptions {
            });
        }
        private async Task <GenerateCastleData> GenerateCastleForSelfPlayingGame(List <PositionModel> result, GameModel game)
        {
            var cur = new PositionModel()
            {
                Lat = game.Position.Lat,
                Lng = game.Position.Lng
            };
            int total        = result.Count;
            var castleResult = new List <CastleModel>();
            Dictionary <Army, int> armies = new Dictionary <Army, int>()
            {
                { Army.Blue, 10 }, { Army.Red, 10 }
            };

            for (int i = 0; i < 20; ++i)
            {
                int index;
                do
                {
                    index = R.Next(total);
                }while (!CanBeACastle(castleResult, result[index], cur));
                Army army;
                do
                {
                    army = armies.ElementAt(R.Next(2)).Key;
                }while (armies[army] == 0);
                var castle = new CastleModel
                {
                    Index            = castleResult.Count,
                    Name             = GetCastleName(game, army, castleResult.Count(e => e.Army == army)),
                    Position         = result[index],
                    Army             = army,
                    MaxResourceLimit = 10,
                    OwnerId          = GetCastleHeroOwnerIdByArmy(army, game),
                    OwnerUserId      = GetCastleOwnerIdByArmy(army, game),

                    Strength = _gameSettings.WallStrength,
                    IsAdded  = true
                };

                castleResult.Add(castle);
                armies[army] = armies[army] - 1;
            }
            // generate route
            var routes = new List <CastleRouteModel>();
            List <CastleModel> remainingCastles = castleResult.Where(e => e.RouteCount <= 2).ToList();

            while (remainingCastles.Count >= 2)
            {
                var castle       = remainingCastles.First();
                var exceptCastle = routes.Where(e => e.FromCastle == castle || e.ToCastle == castle)
                                   .SelectMany(e => new[] { e.FromCastle.Index, e.ToCastle.Index }).ToList();
                exceptCastle.Add(castle.Index);
                exceptCastle = exceptCastle.Distinct().ToList();
                if (exceptCastle.Count == remainingCastles.Count)
                {
                    break;
                }
                var nearCastles    = remainingCastles.Where(e => !exceptCastle.Contains(e.Index));
                var nearestCastles = nearCastles.Select(e =>
                                                        new
                {
                    Castle   = e,
                    Distance = Helpers.DistanceBetween(castle.Position.Lat, castle.Position.Lng, e.Position.Lat,
                                                       e.Position.Lng) * 1.0
                }).OrderBy(e => e.Distance).Take(3 - castle.RouteCount);
                foreach (var nearestCastle in nearestCastles)
                {
                    routes.Add(new CastleRouteModel(castle, nearestCastle.Castle));
                    nearestCastle.Castle.RouteCount++;
                    castle.RouteCount++;
                }
                remainingCastles = castleResult.Where(e => e.RouteCount <= 2).ToList();
            }

            // get route
            foreach (var route in routes)
            {
                var r = await GetCastleRoute(route.FromCastle, route.ToCastle);

                if (r == null)
                {
                    throw new Exception($"Can not found route from castle {route.FromCastle.Index} to castle {route.ToCastle.Index}");
                }
                route.Route = r;
            }
            return(new GenerateCastleData()
            {
                Castles = castleResult,
                Routes = routes
            });
        }