2021-05-08 12:54:07 +02:00
|
|
|
local Serializable = require "birb.classes.serializable"
|
|
|
|
local Pocket = Serializable:extend()
|
2020-09-13 10:03:43 +02:00
|
|
|
|
|
|
|
function Pocket:new(pocketdata)
|
|
|
|
self.name = pocketdata.name
|
|
|
|
self.fullname = pocketdata.fullname
|
|
|
|
self.inBattle = pocketdata.inBattle
|
|
|
|
self.description = pocketdata.description
|
2021-04-22 19:46:19 +02:00
|
|
|
self.isEquipement = pocketdata.isEquipement
|
2020-09-13 10:03:43 +02:00
|
|
|
self.list = {}
|
2021-05-08 12:54:07 +02:00
|
|
|
Pocket.super.new(self, {"list"})
|
2021-04-04 16:33:48 +02:00
|
|
|
end
|
|
|
|
|
2020-09-13 10:03:43 +02:00
|
|
|
function Pocket:addItem(item, number)
|
|
|
|
local success = false
|
|
|
|
|
|
|
|
for i,itemData in ipairs(self.list) do
|
|
|
|
if (itemData.name == item) then
|
|
|
|
itemData.number = itemData.number + number
|
|
|
|
success = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if (not success) then
|
2020-09-13 16:57:06 +02:00
|
|
|
local itemData = {}
|
2020-09-13 10:03:43 +02:00
|
|
|
itemData.name = item
|
|
|
|
itemData.number = number
|
|
|
|
table.insert(self.list, itemData)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-12 20:54:45 +01:00
|
|
|
function Pocket:removeItem(item, number)
|
|
|
|
for i,itemData in ipairs(self.list) do
|
|
|
|
if (itemData.name == item) then
|
|
|
|
if (itemData.number > number) then
|
|
|
|
itemData.number = itemData.number - number
|
|
|
|
else
|
|
|
|
table.remove(self.list, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-13 10:03:43 +02:00
|
|
|
function Pocket:getItem(name)
|
|
|
|
for i, itemData in ipairs(self.list) do
|
|
|
|
if (itemData.name == name) then
|
|
|
|
return itemData
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function Pocket:getItemNumber(name)
|
|
|
|
local item = self:getItem(name)
|
|
|
|
if (item == nil) then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return item.number
|
|
|
|
end
|
|
|
|
end
|
2020-09-13 13:15:12 +02:00
|
|
|
|
|
|
|
return Pocket
|