public bool Handle(DomainEventHandlerData <StartBattalionEvent> @event) { var snap = @event.Snapshot as GameAggregate; var castle = snap?.Castles?.FirstOrDefault(e => e.Id == @event.EventObject.CastleId); if (castle == null) { return(false); } var movedSoldiers = castle.Soldiers.Where(s => @event.EventObject.Soldiers.Any(e => new Guid(e) == s.Id)).ToList(); castle.Soldiers.RemoveAll(s => @event.EventObject.Soldiers.Any(e => new Guid(e) == s.Id)); castle.UpdateSoldierAmount(); var e2 = new BattalionMovementEvent(castle.Id, @event.EventObject.DestinationCastleId, movedSoldiers, @event.EventObject.Route, @event.EventObject.CreatedBy, DateTime.UtcNow, DateTime.UtcNow.Add(@event.EventObject.Route.Duration)); e2.Id = @event.EventObject.MovementId; _domain.AddEvent(snap.Id, e2); _gameDomainService.CreateSoldierIfNeed(snap); return(true); }
public bool Handle(DomainEventHandlerData <BattleEvent> @event) { var snap = @event.Snapshot as GameAggregate; var castle = snap?.Castles.FirstOrDefault(e => e.Id == @event.EventObject.AtCastleId); if (castle?.Siege?.Soldiers == null) { return(false); } var attacking = castle.Siege.Soldiers; var defending = castle.Soldiers.Where(e => !e.IsDead).ToList(); // boost strength var hero = castle.Heroes?.FirstOrDefault(e => e.Army != castle.Army); var attackingBoostStrength = hero == null ? 0 : _repository.GetHeroById(hero.UserId, hero.Id.ToString())?.Leadership ?? 0; hero = castle.Heroes?.FirstOrDefault(e => e.Army == castle.Army); var defendingBoostStrength = hero == null ? 0 : _repository.GetHeroById(hero.UserId, hero.Id.ToString())?.Leadership ?? 0; var battle = _gameDomainService.AttackSequence(@event.EventObject.Id, castle, castle.Siege.OwnerUserId, castle.OwnerUserId, attacking, defending, attackingBooststrength: attackingBoostStrength, defendingBooststrength: defendingBoostStrength); // attacking win if (attacking.Count(e => !e.IsDead && e.CastleTroopType.Health > 0) > 0) { // add event // for user _domain.AddEvent(snap.Id, new OccupiedCastleEvent(castle.Siege.OwnerUserId, castle.Id, battle, DateTime.UtcNow)); // for old owner of castle if (!string.IsNullOrEmpty(castle.OwnerUserId)) { _domain.AddEvent(snap.Id, new CastleHasBeenOccupiedEvent(castle.OwnerUserId, castle.Id, battle, DateTime.UtcNow)); } } else { // defending win castle.Soldiers = castle.Soldiers.Where(e => e.CastleTroopType.Health > 0 && !e.IsDead).ToList(); castle.UpdateSoldierAmount(); _gameDomainService.CreateSoldierIfNeed(snap); // for depending if (!string.IsNullOrEmpty(castle.OwnerUserId)) { _domain.AddEvent(snap.Id, new DefendedCastleEvent(castle.OwnerUserId, castle.Id, battle, DateTime.UtcNow)); } // for attacking _domain.AddEvent(snap.Id, new FailedAttackCastleEvent(castle.Siege.OwnerUserId, castle.Id, battle, DateTime.UtcNow)); } return(true); }
public bool Handle(DomainEventHandlerData <CreateSoldierEvent> data) { GameAggregate snapshot = data.Snapshot as GameAggregate; if (snapshot == null) { return(false); } if (data.EventObject.Army == Army.Blue) { if (snapshot.UserSoldiers == null) { snapshot.UserSoldiers = new List <SoldierAggregate>(); } ++snapshot.UserSoldiersAmount; } else { if (snapshot.OpponentSoldiers == null) { snapshot.OpponentSoldiers = new List <SoldierAggregate>(); } ++snapshot.OpponentSoldierAmount; } SoldierAggregate soldierAggregate = new SoldierAggregate { Id = Guid.NewGuid() }; var castleTroopType = snapshot.GetTroopTypeData(data.EventObject.Army, data.EventObject.TroopType); if (castleTroopType != null) { soldierAggregate.CastleTroopType = castleTroopType; if (data.EventObject.Army == Army.Blue) { snapshot.UserSoldiers.Add(soldierAggregate); } else { snapshot.OpponentSoldiers.Add(soldierAggregate); } } _gameDomainService.CreateSoldierIfNeed(snapshot, data.EventObject.Army); return(true); }
public bool Handle(DomainEventHandlerData <OccupiedCastleEvent> @event) { var snap = @event.Snapshot as GameAggregate; var castle = snap?.Castles.FirstOrDefault(e => e.Id == @event.EventObject.CastleId); if (castle?.Siege == null) { return(false); } var liveSoldiers = castle.Siege.Soldiers?.Where(e => e.CastleTroopType.Health > 0).ToList() ?? new List <SoldierAggregate>(); castle.Soldiers = liveSoldiers; castle.UpdateSoldierAmount(); var army = snap.UserId == castle.Siege.OwnerUserId ? Army.Blue : Army.Red; castle.Siege = null; castle.Army = army; if (army == Army.Blue) { castle.OwnerId = snap.UserHeroId; castle.OwnerUserId = snap.UserId; } else { castle.OwnerId = snap.OpponentHeroId; castle.OwnerUserId = snap.OpponentId; } // check end game status bool isBlueWin = snap.Castles.All(e => e.Army == Army.Blue); var isRedWin = snap.Castles.All(e => e.Army == Army.Red); if (isBlueWin || isRedWin) { _domain.AddEvent(snap.Id, new EndGameEvent(isBlueWin ? Army.Blue : Army.Red, DateTime.UtcNow)); return(true); } _gameDomainService.CreateSoldierIfNeed(snap); return(true); }