Esempio n. 1
0
        public bool Handle(DomainEventHandlerData <DefendedSiegeEvent> @event)
        {
            var snap   = @event.Snapshot as GameAggregate;
            var castle = snap?.Castles.FirstOrDefault(e => e.Id == @event.EventObject.CastleId);

            if (castle?.Siege == null)
            {
                return(false);
            }
            castle.Siege.Soldiers = castle.Siege.Soldiers.Where(e => e.CastleTroopType.Health > 0).ToList();
            _gameDomainService.AddSiegeEvent(snap, castle, castle.Siege.Soldiers);
            //_domain.AddEvent(snap.Id, new SiegeCastleEvent(castle.Siege.OwnerUserId,
            //    castle.Id,
            //    castle.Siege.Soldiers,
            //    DateTime.UtcNow,
            //    DateTime.UtcNow.AddMinutes(_gameSettings.SiegeTime)));
            return(true);
        }
        public bool Handle(DomainEventHandlerData <OccupiedSiegeInCastleEvent> @event)
        {
            var snap   = @event.Snapshot as GameAggregate;
            var castle = snap?.Castles.FirstOrDefault(e => e.Id == @event.EventObject.CastleId);

            if (castle == null)
            {
                return(false);
            }
            castle.Siege = new SiegeAggregate()
            {
                Id          = Guid.NewGuid(),
                OwnerUserId = @event.EventObject.CreatedBy,
                Soldiers    = @event.EventObject.Soldiers,
                BattleAt    = _gameDomainService.GetBattleTime(snap.Speed)
            };
            // add event
            _gameDomainService.AddSiegeEvent(snap, castle, @event.EventObject.Soldiers);
            return(true);
        }
        public bool Handle(DomainEventHandlerData <BattalionMovementEvent> @event)
        {
            var snap   = @event.Snapshot as GameAggregate;
            var castle = snap?.Castles?.FirstOrDefault(e => e.Id == @event.EventObject.CastleId);

            if (castle == null)
            {
                return(false);
            }
            var destination = snap.Castles?.FirstOrDefault(e => e.Id == @event.EventObject.DestinationCastleId);

            if (destination == null)
            {
                return(false);
            }
            if (castle.Army == destination.Army)
            {
                // just add soldier
                if (destination.Soldiers == null)
                {
                    destination.Soldiers = new List <SoldierAggregate>();
                }
                var soldierCanAdd = destination.MaxResourceLimit - destination.Soldiers.Count;
                if (soldierCanAdd > 0)
                {
                    var canAddSoldiers = @event.EventObject.Soldiers
                                         .Where(e => destination.Soldiers.All(f => f.Id != e.Id))
                                         .OrderByDescending(e => e.CastleTroopType.Health)
                                         .Take(
                        soldierCanAdd);
                    destination.Soldiers.AddRange(canAddSoldiers);
                    destination.UpdateSoldierAmount();
                }
                if (@event.EventObject.Soldiers.Count > soldierCanAdd)
                {
                    //todo: fill all soldier still in battalion to closest castle
                }
                return(true);
            }
            // check siege of destination castle
            if (destination.Siege == null)
            {
                _gameDomainService.AddSiegeEvent(snap, castle, destination, @event.EventObject.Soldiers);
            }
            else if (destination.Siege.OwnerUserId != castle.OwnerUserId)
            {
                // start battle
                _domain.AddEvent(snap.Id,
                                 new BattleVersusSiegeEvent(
                                     castle.OwnerUserId,
                                     destination.Id,
                                     @event.EventObject.Soldiers,
                                     DateTime.UtcNow));
            }
            else
            {
                // add more soldier to siege
                if (destination.Siege.Soldiers == null)
                {
                    destination.Siege.Soldiers = new List <SoldierAggregate>();
                }
                var canAddSoldiers = @event.EventObject.Soldiers.Where(e => destination.Siege.Soldiers.All(f => f.Id != e.Id)).ToList();
                destination.Siege.Soldiers.AddRange(canAddSoldiers);
                castle.Soldiers.RemoveAll(e => canAddSoldiers.Any(f => f.Id == e.Id));
                destination.UpdateSoldierAmount();
                castle.UpdateSoldierAmount();
            }
            return(true);
        }