private async Task PlanetSelected(IPlanet selectedPlanet)
        {
            var result = await _navigationService.Navigate<PlanetViewModel, IPlanet, DestructionResult<IPlanet>>(selectedPlanet);

            if (result != null && result.Destroyed)
            {
                var planet = Planets.FirstOrDefault(p => p.Name == result.Entity.Name);
                if (planet != null)
                    Planets.Remove(planet);
            }
        }
        public MoveResult MoveFleet(MoveRequest request)
        {
            var result = new MoveResult();

            // non-zero ships
            if (request.NumberOfShips <= 0)
            {
                result.Success = false;
                result.Message = "Can't send a zero fleet";
                return(result);
            }

            var validSourcePlanets = _getPlanetsForPlayer(request.AuthToken);

            // A planet of the requested source ID exists, belongs to that planer, AND it has enough ships
            var sourceValid = validSourcePlanets.FirstOrDefault(p => p.Id == request.SourcePlanetId && request.NumberOfShips <= p.NumberOfShips);

            // A planet of the requested destination ID exists
            var destinationValid = Planets.FirstOrDefault(p => p.Id == request.DestinationPlanetId);

            if (sourceValid != null && destinationValid != null)
            {
                lock (synclock)
                {
                    // Subtract ships from planet
                    sourceValid.NumberOfShips -= request.NumberOfShips;

                    // Build fleet
                    var newFleet = new Fleet()
                    {
                        Id            = _MAXFLEETID++,
                        OwnerId       = _authTokenToId(request.AuthToken),
                        Source        = sourceValid,
                        Destination   = destinationValid,
                        NumberOfShips = request.NumberOfShips,
                        NumberOfTurnsToDestination = (int)Math.Ceiling(sourceValid.Position.Distance(destinationValid.Position))
                    };

                    _fleets.Add(newFleet);

                    result.Fleet   = Mapper.Map <Shared.Fleet>(newFleet);
                    result.Success = true;
                }
            }
            else
            {
                result.Success = false;
                result.Message = "Invalid move command, check if the planet of the requested source/dest ID exists, belongs to that player, AND it has enough ships.";
            }

            return(result);
        }
示例#3
0
        private Order ConstructAndValidateOrder(string input, int playerId)
        {
            string error;
            Order  order = Order.TryParse(input, playerId, out error);

            if (order == null)
            {
                SetErrorMessage(playerId, error);
                return(null);
            }

            Planet sourcePlanet      = Planets.FirstOrDefault(x => x.Identifier == order.Source);
            Planet destinationPlanet = Planets.FirstOrDefault(x => x.Identifier == order.Destination);

            if (sourcePlanet == null || destinationPlanet == null)
            {
                SetErrorMessage(playerId, $"Sending ships to or from a nonexistent planet.  Order = {input}");
            }

            if (order.Source == order.Destination)
            {
                SetErrorMessage(playerId, $"Sending ships from one planet to identical planet.  Order = {input}");
                return(null);
            }

            if (sourcePlanet == null || destinationPlanet == null)
            {
                SetErrorMessage(playerId, $"Tried to reference an unknown planet.  Order = {input}");
                return(null);
            }

            if (sourcePlanet.Ships < order.Ships)
            {
                SetErrorMessage(playerId, "Tried to send more ships than planet had.  " +
                                $"Order = {input}. Planet ship count = {sourcePlanet.Ships}");
                return(null);
            }

            if (order.Ships <= 0)
            {
                SetErrorMessage(playerId, $"Tried to send 0 (or fewer) ships in a fleet. Order = {input}");
            }

            if (sourcePlanet.Owner != playerId)
            {
                SetErrorMessage(playerId, $"Tried to send ships from another player's planet! Order = {input}");
                return(null);
            }

            return(order);
        }
示例#4
0
        public void TurnInit()
        {
            Planets.ForEach(p => p.Friendlyness = DetermineFriendlyness(p.Owner));

            for (var i = 0; i < Ships.Count; i++)
            {
                Ships[i].Friendlyness = DetermineFriendlyness(Ships[i].Owner);
                Ships[i].Target       = Planets.FirstOrDefault(p => p.Id == Ships[i].TargetId);
//                Ships.ForEach(s => s.Friendlyness = DetermineFriendlyness(s.Owner));
//                Ships.ForEach(s => s.Target = Planets.Single(p => p.Id == s.TargetId));
            }

            var shipsByTarget = Ships.GroupBy(s => s.TargetId).ToDictionary(s => s.Key, s => s.AsEnumerable());

            Planets.ForEach(p => p.SetInboundShips(shipsByTarget.GetValueOrDefault(p.Id, new Ship[0])));
        }