---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