forked from PerpetuumOnline/PerpetuumServer
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathEquipModule.cs
More file actions
66 lines (57 loc) · 2.72 KB
/
EquipModule.cs
File metadata and controls
66 lines (57 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Perpetuum.Containers;
using Perpetuum.Data;
using Perpetuum.EntityFramework;
using Perpetuum.Host.Requests;
using Perpetuum.Items;
using Perpetuum.Modules;
using Perpetuum.Robots;
using System.Collections.Generic;
using System.Transactions;
namespace Perpetuum.RequestHandlers
{
public class EquipModule : IRequestHandler
{
private readonly IEntityRepository _entityRepository;
private readonly RobotHelper _robotHelper;
public EquipModule(IEntityRepository entityRepository, RobotHelper robotHelper)
{
_entityRepository = entityRepository;
_robotHelper = robotHelper;
}
public void HandleRequest(IRequest request)
{
using (TransactionScope scope = Db.CreateTransaction())
{
Accounting.Characters.Character character = request.Session.Character;
character.IsDocked.ThrowIfFalse(ErrorCodes.CharacterHasToBeDocked);
long containerEid = request.Data.GetOrDefault<long>(k.containerEID);
Container container = Container.GetWithItems(containerEid, character).ThrowIfNull(ErrorCodes.ContainerNotFound);
container.ThrowIfType<VolumeWrapperContainer>(ErrorCodes.AccessDenied);
long robotEid = request.Data.GetOrDefault<long>(k.robotEID);
Robot robot = _robotHelper.LoadRobotOrThrow(robotEid);
robot.IsSingleAndUnpacked.ThrowIfFalse(ErrorCodes.RobotMustbeSingleAndNonRepacked);
robot.Initialize(character);
int slot = request.Data.GetOrDefault<int>(k.slot);
RobotComponentType componentType = request.Data.GetOrDefault<string>(k.robotComponent).ToEnum<RobotComponentType>();
RobotComponent component = robot.GetRobotComponentOrThrow(componentType);
component.MakeSlotFree(slot, container);
long moduleEid = request.Data.GetOrDefault<long>(k.moduleEID);
Module module = (Module)container.GetItemOrThrow(moduleEid).Unstack(1);
component.EquipModuleOrThrow(module, slot, robot.Definition);
robot.Initialize(character);
robot.Save();
container.Save();
Transaction.Current.OnCompleted(completed =>
{
Dictionary<string, object> result = new Dictionary<string, object>
{
{k.robot, robot.ToDictionary()},
{k.container, container.ToDictionary()}
};
Message.Builder.FromRequest(request).WithData(result).WrapToResult().Send();
});
scope.Complete();
}
}
}
}