public void Apply(SubmitMovesCommand command) { if (!ActiveMatch) { ApplyDomainEvent(new MoveSubmittedToInactiveMatchEvent(Id)); } else if (ActiveTurnMoves.Any(m => m.Player == command.Player)) { ApplyDomainEvent(new PlayerDoubleSubmittedMoveEvent(Id, command.Player)); } else { foreach (var move in command.Deployments) { ApplyDomainEvent(new MoveSubmittedEvent(Id, CurrentTurn, command.Player, move.Type)); } foreach (var move in command.Attacks) { MakeAllPlayerUnitsOfTypeAttackTargetType(command, move); } ApplyDomainEvent(new AllPlayerMovesSubmittedForTurnEvent(Id, command.Player, CurrentTurn)); } }
private void MakeAllPlayerUnitsOfTypeAttackTargetType(SubmitMovesCommand command, Attack move) { var numberOfAttacksAllowed = Units.Count(m => m.Value.OwnedByPlayer == command.Player && m.Value.Type == move.Source); var numberOfOpposingPlayerUnits = Units.Count(m => m.Value.OwnedByPlayer != command.Player && m.Value.Type == move.Target); if (numberOfOpposingPlayerUnits > 0) { for (var i = 0; i < numberOfAttacksAllowed; i++) { ApplyDomainEvent(new MoveSubmittedEvent(Id, CurrentTurn, command.Player, move.Source, move.Target)); } } }
static void Main(string[] args) { var container = BuildContainer(); var commandBus = container.GetService <CommandDelegator>(); var queryBus = container.GetService <IQueryAsyncDispatcher>(); System.Console.WriteLine("Game initialized..."); var matchId = Guid.NewGuid(); var newGame = new StartNewGameCommand(matchId); commandBus.SendAsync(newGame).Wait(); System.Console.WriteLine("New game started..."); var activeMatch = queryBus.DispatchAsync <QueryActiveMatch, MatchReadModel>(new QueryActiveMatch()).Result; System.Console.WriteLine($"The active match is: {activeMatch.Id}"); var p1move = new SubmitMovesCommand() { Player = "player1", Match = matchId, Attacks = new List <Attack>() { new Attack() { Source = UnitType.PlaneBomber, Target = UnitType.TankHeavy }, new Attack() { Source = UnitType.PlaneFighter, Target = UnitType.PlaneFighter }, new Attack() { Source = UnitType.TankLight, Target = UnitType.Infantry }, new Attack() { Source = UnitType.TankHeavy, Target = UnitType.TankHeavy }, new Attack() { Source = UnitType.Infantry, Target = UnitType.TankHeavy } }, Deployments = new List <Deploy> { new Deploy() { Type = UnitType.TankHeavy }, new Deploy() { Type = UnitType.PlaneBomber } } }; var p2move = new SubmitMovesCommand() { Player = "player2", Match = matchId, Attacks = new List <Attack>() { new Attack() { Source = UnitType.PlaneBomber, Target = UnitType.PlaneBomber }, new Attack() { Source = UnitType.PlaneFighter, Target = UnitType.PlaneBomber }, new Attack() { Source = UnitType.TankLight, Target = UnitType.TankLight }, new Attack() { Source = UnitType.TankHeavy, Target = UnitType.TankLight }, new Attack() { Source = UnitType.Infantry, Target = UnitType.TankHeavy } }, Deployments = new List <Deploy> { new Deploy() { Type = UnitType.TankLight }, new Deploy() { Type = UnitType.Infantry }, new Deploy() { Type = UnitType.Infantry } } }; commandBus.SendAsync(p1move).Wait(); commandBus.SendAsync(p2move).Wait(); activeMatch = queryBus.DispatchAsync <QueryActiveMatch, MatchReadModel>(new QueryActiveMatch()).Result; System.Console.WriteLine($"The active match is: {activeMatch.Id}"); System.Console.WriteLine("Game Finished. Press any key to exit.."); System.Console.Read(); }