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
|
---Some more logic related to shop npc's
--@server sv_shop.lua
--@alias shop
local itm = nrequire("core/inventory/item.lua")
local shop = {}
for k,v in pairs({
"art_openshop",
"art_buyitem",
"art_sellitem",
}) do
util.AddNetworkString(v)
end
function shop.OpenShop(tbl, ply)
print("Called openshop!")
print("tbl was")
PrintTable(tbl)
if CLIENT then return end
net.Start("art_openshop")
net.WriteTable(tbl)
net.Send(ply)
end
---Literally the same thing as sv_npcsystem
--@todo Remove this? or the other?
function shop.CreateShop(npc)
print("Createing shop npc")
local npcent = ents.Create("npc_shop")
for k,v in pairs(npc) do
npcent[k] = v
end
npcent:Spawn()
print("Called spawn")
end
net.Receive("art_buyitem",function(len,ply)
local itemname = net.ReadString()
--Find the shop near the player
local es = ents.FindInSphere(ply:GetPos(),500)
local tshop
for k,v in pairs(es) do
if IsValid(v) and v:GetClass() == "npc_shop" then
tshop = v
break
end
end
print("Shop was", tshop)
print("Items:", tshop.shopitems)
PrintTable(tshop.shopitems)
--Find the price of the item we want to buy
local price
for k,v in pairs(tshop.shopitems) do
if v[1] == itemname then
price = v[2]
break
end
end
--Make sure we have enough credits to buy it
if ply:GetCredits() < price then
print(ply, " didn't have enough credits to buy a ", itemname, "(" .. price .. ")")
else
xpcall(function()
ply:GiveItem(itm.GetItemByName(itemname))
ply:SetCredits(ply:GetCredits() - price)
end,function(err)
print("Coulden't fit a " , itemname, " into ", ply, "'s inventory: ", err)
end)
end
end)
net.Receive("art_sellitem",function(len,ply)
local itemname = net.ReadString()
--Find the shop near the player
local es = ents.FindInSphere(ply:GetPos(),500)
local tshop
for k,v in pairs(es) do
if IsValid(v) and v:GetClass() == "npc_shop" then
tshop = v
break
end
end
print("Shop was", tshop)
print("Items:", tshop.shopitems)
PrintTable(tshop.shopitems)
--Find the price of the item we want to buy
local price
for k,v in pairs(tshop.shopitems) do
if v[1] == itemname then
price = math.floor(v[2] - math.log(v[2]))
break
end
end
local spot = ply:HasItem(itemname)
if spot then
print("Selling item at spot",spot)
PrintTable(spot)
ply:RemoveItem(spot)
ply:SetCredits(ply:GetCredits() + price)
end
end)
return shop
|