private void DefendCity(Session session, Packet packet) { uint cityId; uint targetCityId; ISimpleStub simpleStub; AttackMode mode; try { cityId = packet.GetUInt32(); targetCityId = packet.GetUInt32(); simpleStub = PacketHelper.ReadStub(packet, FormationType.Defense); mode = (AttackMode)packet.GetByte(); } catch (Exception) { ReplyError(session, packet, Error.Unexpected); return; } if (cityId == targetCityId) { ReplyError(session, packet, Error.DefendSelf); return; } var cityNotFound = locker.Lock(session.Player).Do(() => session.Player.GetCity(cityId) == null); if (cityNotFound) { ReplyError(session, packet, Error.Unexpected); return; } Dictionary <uint, ICity> cities; locker.Lock(out cities, cityId, targetCityId).Do(() => { ICity city = cities[cityId]; var troopInitializer = troopObjectInitializerFactory.CreateCityTroopObjectInitializer(cityId, simpleStub, TroopBattleGroup.Defense, mode); var da = actionFactory.CreateCityDefenseChainAction(cityId, troopInitializer, targetCityId); var result = city.Worker.DoPassive(city, da, true); ReplyWithResult(session, packet, result); }); }
/// <summary> /// Called to dispatch a unit /// </summary> /// <param name="stub"></param> /// <returns></returns> private bool Dispatch(ITroopStub stub) { // Create troop object ITroopObject troopObject; procedure.TroopObjectCreate(stub.City, stub, out troopObject); PassiveAction action; if (Target.LocationType == LocationType.City) { IStructure structure = (IStructure)gameObjectLocator.Regions.GetObjectsInTile(X, Y).FirstOrDefault(z => z is IStructure); if (structure == null) { procedure.TroopObjectDelete(troopObject, true); stub.City.Owner.SendSystemMessage(null, "Assignment Failed", string.Format( @"Assigned target({0},{1}) has already been destroyed. The reserved troops have been returned to the city.", X, Y)); return(false); } if (IsAttack) { var troopObjectInitializer = troopObjectInitializerFactory.CreateAssignmentTroopObjectInitializer(troopObject, TroopBattleGroup.Attack, AttackMode); action = actionFactory.CreateCityAttackChainAction(stub.City.Id, troopObjectInitializer, structure.City.Id, new Position(X, Y)); } else { var troopObjectInitializer = troopObjectInitializerFactory.CreateAssignmentTroopObjectInitializer(troopObject, TroopBattleGroup.Defense, AttackMode); action = actionFactory.CreateCityDefenseChainAction(stub.City.Id, troopObjectInitializer, structure.City.Id); } } else if (Target.LocationType == LocationType.Stronghold) { if (IsAttack) { var troopObjectInitializer = troopObjectInitializerFactory.CreateAssignmentTroopObjectInitializer(troopObject, TroopBattleGroup.Attack, AttackMode); action = actionFactory.CreateStrongholdAttackChainAction(stub.City.Id, troopObjectInitializer, Target.LocationId, forceAttack: true); } else { var troopObjectInitializer = troopObjectInitializerFactory.CreateAssignmentTroopObjectInitializer(troopObject, TroopBattleGroup.Defense, AttackMode); action = actionFactory.CreateStrongholdDefenseChainAction(stub.City.Id, troopObjectInitializer, Target.LocationId); } } else { procedure.TroopObjectDelete(troopObject, true); return(false); } if (stub.City.Worker.DoPassive(stub.City, action, true) != Error.Ok) { procedure.TroopObjectDelete(troopObject, true); return(false); } return(true); }