public void HandleRequest(IRequest request)
        {
            using (var scope = Db.CreateTransaction())
            {
                var character = request.Session.Character;
                character.IsDocked.ThrowIfFalse(ErrorCodes.CharacterHasToBeDocked);

                var robotEid = request.Data.GetOrDefault <long>(k.robotEID);
                var robot    = _robotHelper.LoadRobotOrThrow(robotEid);
                robot.IsSingleAndUnpacked.ThrowIfFalse(ErrorCodes.RobotMustbeSingleAndNonRepacked);
                robot.EnlistTransaction();

                var componentType = request.Data.GetOrDefault <string>(k.sourceComponent).ToEnum <RobotComponentType>();
                var component     = robot.GetRobotComponentOrThrow(componentType);
                var sourceSlot    = request.Data.GetOrDefault <int>(k.source);
                var targetSlot    = request.Data.GetOrDefault <int>(k.target);
                component.ChangeModuleOrThrow(sourceSlot, targetSlot);

                robot.Initialize(character);
                robot.Save();

                Transaction.Current.OnCommited(() =>
                {
                    var result = new Dictionary <string, object>
                    {
                        { k.robot, robot.ToDictionary() }
                    };
                    Message.Builder.FromRequest(request).WithData(result).WrapToResult().Send();
                });

                scope.Complete();
            }
        }
        public void HandleRequest(IRequest request)
        {
            using (var scope = Db.CreateTransaction())
            {
                var character = request.Session.Character;
                character.IsDocked.ThrowIfFalse(ErrorCodes.CharacterHasToBeDocked);

                var robotEid = request.Data.GetOrDefault <long>(k.robotEID);
                var robot    = _robotHelper.LoadRobotOrThrow(robotEid);
                robot.IsSingleAndUnpacked.ThrowIfFalse(ErrorCodes.RobotMustbeSingleAndNonRepacked);
                robot.EnlistTransaction();

                var sourceModuleEid = request.Data.GetOrDefault <long>(k.sourceModuleEID);
                var sourceModule    = robot.GetModule(sourceModuleEid).ThrowIfNotType <ActiveModule>(ErrorCodes.ModuleNotFound);
                sourceModule.IsAmmoable.ThrowIfFalse(ErrorCodes.AmmoNotRequired);

                var sourceAmmo = sourceModule.GetAmmo().ThrowIfNull(ErrorCodes.AmmoNotFound);

                var targetModuleEid = request.Data.GetOrDefault <long>(k.targetModuleEID);
                var targetModule    = robot.GetModule(targetModuleEid).ThrowIfNotType <ActiveModule>(ErrorCodes.ModuleNotFound);
                targetModule.IsAmmoable.ThrowIfFalse(ErrorCodes.AmmoNotRequired);
                targetModule.CheckLoadableAmmo(sourceAmmo.Definition).ThrowIfFalse(ErrorCodes.InvalidAmmoDefinition);
                targetModule.AmmoCapacity.ThrowIfLess(sourceAmmo.Quantity, ErrorCodes.ModuleOutOfSpace);

                var targetAmmo = targetModule.GetAmmo();
                if (targetAmmo == null)
                {
                    //target module is empty
                    sourceModule.SetAmmo(null);
                }
                else
                {
                    //target module has ammo
                    sourceModule.CheckLoadableAmmo(targetAmmo.Definition).ThrowIfFalse(ErrorCodes.InvalidAmmoDefinition);
                    sourceModule.AmmoCapacity.ThrowIfLess(targetAmmo.Quantity, ErrorCodes.ModuleOutOfSpace);
                    sourceModule.SetAmmo(targetAmmo);
                }

                targetModule.SetAmmo(sourceAmmo);

                robot.Initialize(character);
                robot.Save();

                Transaction.Current.OnCompleted(completed =>
                {
                    var result = new Dictionary <string, object> {
                        { k.robot, robot.ToDictionary() }
                    };
                    Message.Builder.FromRequest(request).WithData(result).WrapToResult().Send();
                });

                scope.Complete();
            }
        }
        public void HandleRequest(IRequest request)
        {
            using (var scope = Db.CreateTransaction())
            {
                var character = request.Session.Character;
                character.IsDocked.ThrowIfFalse(ErrorCodes.CharacterHasToBeDocked);

                var containerEid = request.Data.GetOrDefault <long>(k.containerEID);
                var container    = Container.GetWithItems(containerEid, character);
                container.ThrowIfType <VolumeWrapperContainer>(ErrorCodes.AccessDenied);

                var robotEid = request.Data.GetOrDefault <long>(k.robotEID);
                var robot    = _robotHelper.LoadRobotOrThrow(robotEid);
                robot.IsSingleAndUnpacked.ThrowIfFalse(ErrorCodes.RobotMustbeSingleAndNonRepacked);

                var moduleEid = request.Data.GetOrDefault <long>(k.moduleEID);
                var module    = robot.GetModule(moduleEid).ThrowIfNotType <ActiveModule>(ErrorCodes.ModuleNotFound);
                module.IsAmmoable.ThrowIfFalse(ErrorCodes.AmmoNotRequired);

                var ammoEid = request.Data.GetOrDefault <long>(k.ammoEID);
                var ammo    = (Ammo)container.GetItemOrThrow(ammoEid);

                //check ammo type
                module.CheckLoadableAmmo(ammo.Definition).ThrowIfFalse(ErrorCodes.InvalidAmmoDefinition);
                module.UnequipAmmoToContainer(container);

                ammo = (Ammo)ammo.Unstack(module.AmmoCapacity);
                module.SetAmmo(ammo);

                robot.Initialize(character);
                module.Save();
                container.Save();

                Transaction.Current.OnCompleted(completed =>
                {
                    var result = new Dictionary <string, object>
                    {
                        { k.robot, robot.ToDictionary() },
                        { k.container, container.ToDictionary() }
                    };
                    Message.Builder.FromRequest(request).WithData(result).WrapToResult().Send();
                });

                scope.Complete();
            }
        }
Esempio n. 4
0
        public void HandleRequest(IRequest request)
        {
            using (var scope = Db.CreateTransaction())
            {
                var character = request.Session.Character;
                character.IsDocked.ThrowIfFalse(ErrorCodes.CharacterHasToBeDocked);

                var containerEid = request.Data.GetOrDefault <long>(k.containerEID);
                var container    = Container.GetWithItems(containerEid, character).ThrowIfNull(ErrorCodes.ContainerNotFound);
                container.ThrowIfType <VolumeWrapperContainer>(ErrorCodes.AccessDenied);

                var robotEid = request.Data.GetOrDefault <long>(k.robotEID);
                var robot    = _robotHelper.LoadRobotOrThrow(robotEid);
                robot.IsSingleAndUnpacked.ThrowIfFalse(ErrorCodes.RobotMustbeSingleAndNonRepacked);
                robot.Initialize(character);

                var slot          = request.Data.GetOrDefault <int>(k.slot);
                var componentType = request.Data.GetOrDefault <string>(k.robotComponent).ToEnum <RobotComponentType>();
                var component     = robot.GetRobotComponentOrThrow(componentType);
                component.MakeSlotFree(slot, container);
                var moduleEid = request.Data.GetOrDefault <long>(k.moduleEID);
                var module    = (Module)container.GetItemOrThrow(moduleEid).Unstack(1);
                component.EquipModuleOrThrow(module, slot);

                robot.Initialize(character);
                robot.Save();
                container.Save();

                Transaction.Current.OnCompleted(completed =>
                {
                    var result = new Dictionary <string, object>
                    {
                        { k.robot, robot.ToDictionary() },
                        { k.container, container.ToDictionary() }
                    };
                    Message.Builder.FromRequest(request).WithData(result).WrapToResult().Send();
                });

                scope.Complete();
            }
        }