aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/player_accountinventory.lua
blob: d606a83f0413a93febf5423c7099fce21379c7c1 (plain)
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
local meta = FindMetaTable("Player")

if (SERVER) then
	util.AddNetworkString("UpdateAccountInventory")

	function meta:UpdateAccountSlot(id)
		if (!self.AccountInv[id]) then return end

		net.Start("UpdateAccountInventory")
			net.WriteUInt(id,5)
			net.WriteString(self.AccountInv[id].Name)
			net.WriteUInt(self.AccountInv[id].Quantity,32)
		net.Send(self)
	end

	function meta:AddAccountItem(item,quantity)
		local Item = GetItemByName(item)

		if (!Item) then return end
		if (!self.AccountInv) then self.AccountInv = {} end

		--First search for existing items
		for i = 1,MAIN_MAX_SLOTS do
			if (self.AccountInv[i] and self.AccountInv[i].Name == item) then
				self.AccountInv[i].Quantity = self.AccountInv[i].Quantity+quantity
				self:UpdateAccountSlot(i)
				return
			end
		end

		--If it hasnt found any existing item, find an empty spot
		for i = 1,MAIN_MAX_SLOTS do
			if (!self.AccountInv[i]) then
				self.AccountInv[i] = {Name = item,Quantity = quantity}
				self:UpdateAccountSlot(i)
				return
			end
		end
	end

	function meta:RemoveAccountItem(item,quantity)
		for i = 1,MAIN_MAX_SLOTS do
			if (self.AccountInv[i] and self.AccountInv[i].Name == item) then
				if (self.AccountInv[i].Quantity > quantity) then
					self.AccountInv[i].Quantity = self.AccountInv[i].Quantity-quantity
					self:UpdateAccountSlot(i)
					return
				else
					quantity = quantity - self.AccountInv[i].Quantity

					self.AccountInv[i].Quantity = 0

					self:UpdateAccountSlot(i)

					self.AccountInv[i] = nil

					if (quantity > 0) then self:RemoveAccountItem(item,quantity) return end
				end
			end
		end
	end
else
	net.Receive("UpdateAccountInventory",function()
		local pl = LocalPlayer()

		if (not pl.AccountInv) then pl.AccountInv = {} end

		local ID = net.ReadUInt(5)
		if(not pl.AccountInv) then return end
		pl.AccountInv[ID] = {Name = net.ReadString(),Quantity = net.ReadUInt(32)}

		if (pl.AccountInv[ID].Quantity <= 0) then pl.AccountInv[ID] = nil end

		ReloadAccountMenu()
	end)
end

function meta:GetAccountInventory()
	return self.AccountInv or {}
end

function meta:HasAccountItem(name,quantity)
	if (!self.AccountInv) then return end

	quantity = quantity or 1

	for k,v in pairs(self.AccountInv) do
		if (v.Name == name and v.Quantity >= quantity) then
			return k
		end
	end

	return false
end

function meta:GetAccountSlot(id)
	return (self.AccountInv and self.AccountInv[id]) or nil
end