forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemContainer.cs
More file actions
192 lines (156 loc) · 5.59 KB
/
Copy pathItemContainer.cs
File metadata and controls
192 lines (156 loc) · 5.59 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using L2dotNET.DataContracts;
using L2dotNET.DataContracts.Shared.Enums;
using L2dotNET.Models.Items;
using L2dotNET.Models.Player;
using L2dotNET.Services.Contracts;
using L2dotNET.Tables;
using L2dotNET.World;
using Microsoft.Extensions.DependencyInjection;
namespace L2dotNET.Models.Inventory
{
public abstract class ItemContainer
{
protected readonly ICrudService<ItemContract> _itemCrudService;
protected readonly IItemService _itemService;
protected readonly IdFactory _idFactory;
protected readonly ItemTable _itemTable;
public List<L2Item> Items { get; }
protected ItemContainer(L2Character owner)
{
Owner = owner;
_itemService = GameServer.ServiceProvider.GetService<IItemService>();
_itemTable = GameServer.ServiceProvider.GetService<ItemTable>();
_idFactory = GameServer.ServiceProvider.GetService<IdFactory>();
_itemCrudService = GameServer.ServiceProvider.GetService<ICrudService<ItemContract>>();
Items = new List<L2Item>();
}
protected L2Character Owner { get; set; }
protected ItemLocation BaseLocation { get; }
public int OwnerId => Owner?.ObjectId ?? 0;
public int Count =>Items.Count;
public string Name => "ItemContainer";
public bool HasAtLeaseOneItem(params int[] itemIds)
{
return itemIds.Any(itemId => GetItemsByItemId(itemId) != null);
}
public List<L2Item> GetItemsByItemId(int itemId)
{
return Items.Where(item => item.Template.ItemId == itemId).ToList();
}
public L2Item GetItemByItemId(int itemId)
{
return Items.FirstOrDefault(item => item.Template.ItemId == itemId);
}
public L2Item GetItemByObjectId(int objectId)
{
return Items.FirstOrDefault(item => item.ObjectId == objectId);
}
public virtual async Task Restore(L2Character owner)
{
IEnumerable<ItemContract> models = await _itemService.RestoreInventory(owner.ObjectId);
List<L2Item> items = RestoreFromDb(models.ToList());
foreach (L2Item item in items)
{
L2World.AddObject(item);
Owner = owner;
AddItem(item, (L2Player)Owner);
}
}
public List<L2Item> RestoreFromDb(List<ItemContract> models)
{
return models.Select(MapModelToItem).ToList();
}
private L2Item MapModelToItem(ItemContract contract)
{
L2Item item = new L2Item(_itemCrudService, _idFactory, _itemTable.GetItem(contract.ItemId), _idFactory.NextId())
{
ObjectId = contract.ObjectId,
Count = contract.Count,
CustomType1 = contract.CustomType1,
CustomType2 = contract.CustomType2,
Enchant = contract.Enchant,
SlotLocation = contract.LocationData,
OwnerId = contract.CharacterId,
};
return item;
}
public L2Item AddItem(L2Item item, L2Player player)
{
if (item != null)
{
item.OwnerId = player.ObjectId;
//item.SlotLocation = 0;
Items.Add(item);
item.UpdateDatabase();
}
return item;
}
public L2Item AddItem(int itemId, int count, L2Player player,bool ExistsInDb = false)
{
L2Item item = GetItemByItemId(itemId);
if ((item != null) && item.Template.Stackable)
{
item.ChangeCount(count,player);
item.UpdateDatabase();
}
else
{
//for (int i = 0; i < Count; i++)
// {
ItemTemplate template = _itemTable.GetItem(itemId);
if (template == null)
return null;
item = _itemTable.CreateItem(itemId, count, player);
item.OwnerId = player.ObjectId;
item.SlotLocation = 0;
item.ExistsInDb = ExistsInDb;
item.Location = ItemLocation.Inventory;
Items.Add(item);
item.UpdateDatabase();
// }
}
return item;
}
public L2Item DestroyItem(L2Item item, int count, L2Player player)
{
return null;
//to be implemented
}
public L2Item DestroyItemById(int itemId, int count, L2Player player)
{
return null;
//to be implemented
}
public void DestroyAllItems(L2Player player)
{
Items.ForEach(item => DestroyItem(item, item.Count, player));
}
public int AdenaCount()
{
return Items.Where(item => item.Template.ItemId == 57).Select(item => item.Count).FirstOrDefault();
}
public L2Item AddItem(L2Item item)
{
return null;
//to be implemented
}
protected bool RemoveItem(L2Item item)
{
return Items.Remove(item);
}
public void RestoreInv() { }
public void DeleteMe() { }
public void UpdateDatabase() { }
public bool ValidateCapacity(int slots)
{
return true;
}
public bool CalidateWeight(int weight)
{
return true;
}
}
}