Added mail sending when stock is empty, only accept items that get sold by the shop
[smartshop.git] / init.lua
1 smartshop={user={},tmp={},dir={{x=0,y=0,z=-1},{x=-1,y=0,z=0},{x=0,y=0,z=1},{x=1,y=0,z=0}},dpos={
2 {{x=0.2,y=0.2,z=0},{x=-0.2,y=0.2,z=0},{x=0.2,y=-0.2,z=0},{x=-0.2,y=-0.2,z=0}},
3 {{x=0,y=0.2,z=0.2},{x=0,y=0.2,z=-0.2},{x=0,y=-0.2,z=0.2},{x=0,y=-0.2,z=-0.2}},
4 {{x=-0.2,y=0.2,z=0},{x=0.2,y=0.2,z=0},{x=-0.2,y=-0.2,z=0},{x=0.2,y=-0.2,z=0}},
5 {{x=0,y=0.2,z=-0.2},{x=0,y=0.2,z=0.2},{x=0,y=-0.2,z=-0.2},{x=0,y=-0.2,z=0.2}}}
6 }
7
8 minetest.register_craft({
9 output = "smartshop:shop",
10 recipe = {
11 {"default:chest_locked", "default:chest_locked", "default:chest_locked"},
12 {"default:sign_wall_wood", "default:chest_locked", "default:sign_wall_wood"},
13 {"default:sign_wall_wood", "default:torch", "default:sign_wall_wood"},
14 }
15 })
16
17 smartshop.use_offer=function(pos,player,n)
18 local pressed={}
19 pressed["buy" .. n]=true
20 smartshop.user[player:get_player_name()]=pos
21 smartshop.receive_fields(player,pressed)
22 smartshop.user[player:get_player_name()]=nil
23 smartshop.update(pos)
24
25
26 end
27
28 smartshop.get_offer=function(pos)
29 if not pos or not minetest.get_node(pos) then return end
30 if minetest.get_node(pos).name~="smartshop:shop" then return end
31 local meta=minetest.get_meta(pos)
32 local inv=meta:get_inventory()
33 local offer={}
34 for i=1,4,1 do
35 offer[i]={
36 give=inv:get_stack("give" .. i,1):get_name(),
37 give_count=inv:get_stack("give" .. i,1):get_count(),
38 pay=inv:get_stack("pay" .. i,1):get_name(),
39 pay_count=inv:get_stack("pay" .. i,1):get_count(),
40 }
41 end
42 return offer
43 end
44
45 smartshop.send_mail=function(owner, pos, item)
46 if not minetest.get_modpath( "mail" ) then
47 return
48 end
49 item = ItemStack(item)
50 local spos = "("..pos.x..", "..pos.y..", "..pos.z..")"
51 mail.send("DO NOT REPLY", owner, "Out of stock at "..spos, "Your smartshop at "..spos.." is out of "..core.registered_items[item:get_name()].description..". Please restock")
52 end
53
54
55 smartshop.receive_fields=function(player,pressed)
56 if pressed.customer then
57 return smartshop.showform(smartshop.user[player:get_player_name()],player,true)
58 elseif pressed.sellall then
59 local pos=smartshop.user[player:get_player_name()]
60 local meta=minetest.get_meta(pos)
61 local pname=player:get_player_name()
62 if meta:get_int("sellall")==0 then
63 meta:set_int("sellall",1)
64 minetest.chat_send_player(pname, "Sell your stock and give line")
65 else
66 meta:set_int("sellall",0)
67 minetest.chat_send_player(pname, "Sell your stock only")
68 end
69 elseif pressed.tooglelime then
70 local pos=smartshop.user[player:get_player_name()]
71 local meta=minetest.get_meta(pos)
72 local pname=player:get_player_name()
73 if meta:get_int("type")==0 then
74 meta:set_int("type",1)
75 minetest.chat_send_player(pname, "Your stock is limited")
76 else
77 meta:set_int("type",0)
78 minetest.chat_send_player(pname, "Your stock is unlimited")
79 end
80 elseif not pressed.quit then
81 local n=1
82 for i=1,4,1 do
83 n=i
84 if pressed["buy" .. i] then break end
85 end
86 local pos=smartshop.user[player:get_player_name()]
87 local meta=minetest.get_meta(pos)
88 local type=meta:get_int("type")
89 local sellall=meta:get_int("sellall")
90 local inv=meta:get_inventory()
91 local pinv=player:get_inventory()
92 local pname=player:get_player_name()
93 if pressed["buy" .. n] then
94 local name=inv:get_stack("give" .. n,1):get_name()
95 local stack=name .." ".. inv:get_stack("give" .. n,1):get_count()
96 local pay=inv:get_stack("pay" .. n,1):get_name() .." ".. inv:get_stack("pay" .. n,1):get_count()
97 if name~="" then
98 if type==1 and inv:room_for_item("main", pay)==false then minetest.chat_send_player(pname, "Error: The owners stock is full, cant receive, exchange aborted.") return end
99 if type==1 and sellall==1 and inv:contains_item("main", stack)==false and inv:contains_item("give" .. n, stack)==true then
100 inv:add_item("main", stack)
101 inv:remove_item("give" .. n, stack)
102 end
103 if type==1 and inv:contains_item("main", stack)==false then
104 minetest.chat_send_player(pname, "Error: The owners stock is end.")
105 if not meta:get_int("alerted") or meta:get_int("alerted") == 0 then
106 meta:set_int("alerted",1) -- Do not alert twice
107 smartshop.send_mail(meta:get_string("owner"), pos, stack)
108 end
109 return
110 end
111 if not pinv:contains_item("main", pay) then minetest.chat_send_player(pname, "Error: You dont have enough in your inventory to buy this, exchange aborted.") return end
112 if not pinv:room_for_item("main", stack) then minetest.chat_send_player(pname, "Error: Your inventory is full, exchange aborted.") return end
113 pinv:remove_item("main", pay)
114 pinv:add_item("main", stack)
115 if type==1 then
116 inv:remove_item("main", stack)
117 inv:add_item("main", pay)
118 end
119 end
120 end
121 else
122 local pos=smartshop.user[player:get_player_name()]
123 smartshop.update_info(pos)
124 if smartshop.user[player:get_player_name()] or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
125 local meta=minetest.get_meta(smartshop.user[player:get_player_name()])
126 if meta:get_string("owner")==player:get_player_name() then
127 smartshop.update(smartshop.user[player:get_player_name()],"update")
128 end
129 end
130 smartshop.user[player:get_player_name()]=nil
131 end
132 end
133
134 minetest.register_on_player_receive_fields(function(player, form, pressed)
135 if form=="smartshop.showform" then
136 smartshop.receive_fields(player,pressed)
137 end
138 end)
139
140
141
142
143 smartshop.update_info=function(pos)
144 local meta=minetest.get_meta(pos)
145 local inv = meta:get_inventory()
146 local owner=meta:get_string("owner")
147 local gve=0
148 if meta:get_int("sellall")==1 then gve=1 end
149 if meta:get_int("type")==0 then
150 meta:set_string("infotext","(Smartshop by " .. owner ..") Stock is unlimited")
151 return false
152 end
153 local name=""
154 local count=0
155 local stuff={}
156 for i=1,4,1 do
157 stuff["count" ..i]=inv:get_stack("give" .. i,1):get_count()
158 stuff["name" ..i]=inv:get_stack("give" .. i,1):get_name()
159 stuff["stock" ..i]=gve*stuff["count" ..i]
160 stuff["buy" ..i]=0
161 for ii=1,32,1 do
162 name=inv:get_stack("main",ii):get_name()
163 count=inv:get_stack("main",ii):get_count()
164 if name==stuff["name" ..i] then
165 stuff["stock" ..i]=stuff["stock" ..i]+count
166 end
167 end
168 local nstr=(stuff["stock" ..i]/stuff["count" ..i]) ..""
169 nstr=nstr.split(nstr, ".")
170 stuff["buy" ..i]=tonumber(nstr[1])
171
172 if stuff["name" ..i]=="" or stuff["buy" ..i]==0 then
173 stuff["buy" ..i]=""
174 stuff["name" ..i]=""
175 else
176 --if string.find(stuff["name" ..i],":")~=nil then
177 -- stuff["name" ..i]=stuff["name" ..i].split(stuff["name" ..i],":")[2]
178 --end
179 stuff["name"..i] = core.registered_items[stuff["name"..i]].description
180 stuff["buy" ..i]="(" ..stuff["buy" ..i] ..") "
181 stuff["name" ..i]=stuff["name" ..i] .."\n"
182 end
183 end
184 meta:set_string("infotext",
185 "(Smartshop by " .. owner ..") Purchases left:\n"
186 .. stuff.buy1 .. stuff.name1
187 .. stuff.buy2 .. stuff.name2
188 .. stuff.buy3 .. stuff.name3
189 .. stuff.buy4 .. stuff.name4
190 )
191 end
192
193
194
195
196 smartshop.update=function(pos,stat)
197 --clear
198 local spos=minetest.pos_to_string(pos)
199 for _, ob in ipairs(minetest.env:get_objects_inside_radius(pos, 2)) do
200 if ob and ob:get_luaentity() and ob:get_luaentity().smartshop and ob:get_luaentity().pos==spos then
201 ob:remove()
202 end
203 end
204 if stat=="clear" then return end
205 --update
206 local meta=minetest.get_meta(pos)
207 local inv = meta:get_inventory()
208 local node=minetest.get_node(pos)
209 local dp = smartshop.dir[node.param2+1]
210 if not dp then return end
211 pos.x = pos.x + dp.x*0.01
212 pos.y = pos.y + dp.y*6.5/16
213 pos.z = pos.z + dp.z*0.01
214 for i=1,4,1 do
215 local item=inv:get_stack("give" .. i,1):get_name()
216 local pos2=smartshop.dpos[node.param2+1][i]
217 if item~="" then
218 smartshop.tmp.item=item
219 smartshop.tmp.pos=spos
220 local e = minetest.env:add_entity({x=pos.x+pos2.x,y=pos.y+pos2.y,z=pos.z+pos2.z},"smartshop:item")
221 e:setyaw(math.pi*2 - node.param2 * math.pi/2)
222 end
223 end
224 end
225
226
227 minetest.register_entity("smartshop:item",{
228 hp_max = 1,
229 visual="wielditem",
230 visual_size={x=.20,y=.20},
231 collisionbox = {0,0,0,0,0,0},
232 physical=false,
233 textures={"air"},
234 smartshop=true,
235 type="",
236 on_activate = function(self, staticdata)
237 if smartshop.tmp.item ~= nil then
238 self.item=smartshop.tmp.item
239 self.pos=smartshop.tmp.pos
240 smartshop.tmp={}
241 else
242 if staticdata ~= nil and staticdata ~= "" then
243 local data = staticdata:split(';')
244 if data and data[1] and data[2] then
245 self.item = data[1]
246 self.pos = data[2]
247 end
248 end
249 end
250 if self.item ~= nil then
251 self.object:set_properties({textures={self.item}})
252 else
253 self.object:remove()
254 end
255 end,
256 get_staticdata = function(self)
257 if self.item ~= nil and self.pos ~= nil then
258 return self.item .. ';' .. self.pos
259 end
260 return ""
261 end,
262 })
263
264
265 smartshop.showform=function(pos,player,re)
266 local meta=minetest.get_meta(pos)
267 local creative=meta:get_int("creative")
268 local inv = meta:get_inventory()
269 local gui=""
270 local spos=pos.x .. "," .. pos.y .. "," .. pos.z
271 local owner=meta:get_string("owner")==player:get_player_name()
272 if minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then owner=true end
273 if re then owner=false end
274 smartshop.user[player:get_player_name()]=pos
275 if owner then
276 meta:set_int("alerted",0) -- Player has been there to refill
277 gui=""
278 .."size[8,10]"
279 .."button_exit[6,0;1.5,1;customer;Customer]"
280 .."button[7.2,0;1,1;sellall;All]"
281 .."label[0,0.2;Item:]"
282 .."label[0,1.2;Price:]"
283 .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
284 .."list[nodemeta:" .. spos .. ";pay1;2,1;1,1;]"
285 .."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
286 .."list[nodemeta:" .. spos .. ";pay2;3,1;1,1;]"
287 .."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
288 .."list[nodemeta:" .. spos .. ";pay3;4,1;1,1;]"
289 .."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
290 .."list[nodemeta:" .. spos .. ";pay4;5,1;1,1;]"
291 if creative==1 then
292 gui=gui .."label[0.5,-0.4;Your stock is unlimited becaouse you have creative or give]"
293 .."button[6,1;2.2,1;tooglelime;Toggle limit]"
294 end
295 gui=gui
296 .."list[nodemeta:" .. spos .. ";main;0,2;8,4;]"
297 .."list[current_player;main;0,6.2;8,4;]"
298 .."listring[nodemeta:" .. spos .. ";main]"
299 .."listring[current_player;main]"
300 else
301 gui=""
302 .."size[8,6]"
303 .."list[current_player;main;0,2.2;8,4;]"
304 .."label[0,0.2;Item:]"
305 .."label[0,1.2;Price:]"
306 .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
307 .."item_image_button[2,1;1,1;".. inv:get_stack("pay1",1):get_name() ..";buy1;\n\n\b\b\b\b\b" .. inv:get_stack("pay1",1):get_count() .."]"
308 .."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
309 .."item_image_button[3,1;1,1;".. inv:get_stack("pay2",1):get_name() ..";buy2;\n\n\b\b\b\b\b" .. inv:get_stack("pay2",1):get_count() .."]"
310 .."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
311 .."item_image_button[4,1;1,1;".. inv:get_stack("pay3",1):get_name() ..";buy3;\n\n\b\b\b\b\b" .. inv:get_stack("pay3",1):get_count() .."]"
312 .."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
313 .."item_image_button[5,1;1,1;".. inv:get_stack("pay4",1):get_name() ..";buy4;\n\n\b\b\b\b\b" .. inv:get_stack("pay4",1):get_count() .."]"
314 end
315 minetest.after((0.1), function(gui)
316 return minetest.show_formspec(player:get_player_name(), "smartshop.showform",gui)
317 end, gui)
318 end
319
320 minetest.register_node("smartshop:shop", {
321 description = "Smartshop",
322 tiles = {"default_chest_top.png^[colorize:#ffffff77^default_obsidian_glass.png"},
323 groups = {choppy = 2, oddly_breakable_by_hand = 1,tubedevice = 1, tubedevice_receiver = 1},
324 drawtype="nodebox",
325 node_box = {type="fixed",fixed={-0.5,-0.5,-0.0,0.5,0.5,0.5}},
326 paramtype2="facedir",
327 paramtype = "light",
328 sunlight_propagates = true,
329 light_source = 10,
330 tube = {insert_object = function(pos, node, stack, direction)
331 local meta = minetest.get_meta(pos)
332 local inv = meta:get_inventory()
333 local added = inv:add_item("main", stack)
334 return added
335 end,
336 can_insert = function(pos, node, stack, direction)
337 local meta = minetest.get_meta(pos)
338 local inv = meta:get_inventory()
339 for i=1,4 do
340 local sellitem = inv:get_stack("give"..i,1):get_name()
341 if sellitem == stack:get_name() then
342 return inv:room_for_item("main", stack)
343 end
344 -- minetest.chat_send_all(sellitem)
345 end
346 --
347 return false
348 end,
349 input_inventory = "main",
350 connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}},
351 after_place_node = function(pos, placer)
352 local meta=minetest.get_meta(pos)
353 meta:set_string("owner",placer:get_player_name())
354 meta:set_string("infotext", "Shop by: " .. placer:get_player_name())
355 meta:set_int("type",1)
356 meta:set_int("sellall",1)
357 if minetest.check_player_privs(placer:get_player_name(), {creative=true}) or minetest.check_player_privs(placer:get_player_name(), {give=true}) then
358 meta:set_int("creative",1)
359 meta:set_int("type",0)
360 meta:set_int("sellall",0)
361 end
362 end,
363 on_construct = function(pos)
364 local meta=minetest.get_meta(pos)
365 meta:set_int("state", 0)
366 meta:get_inventory():set_size("main", 32)
367 meta:get_inventory():set_size("give1", 1)
368 meta:get_inventory():set_size("pay1", 1)
369 meta:get_inventory():set_size("give2", 1)
370 meta:get_inventory():set_size("pay2", 1)
371 meta:get_inventory():set_size("give3", 1)
372 meta:get_inventory():set_size("pay3", 1)
373 meta:get_inventory():set_size("give4", 1)
374 meta:get_inventory():set_size("pay4", 1)
375 end,
376 on_rightclick = function(pos, node, player, itemstack, pointed_thing)
377 smartshop.showform(pos,player)
378 end,
379 allow_metadata_inventory_put = function(pos, listname, index, stack, player)
380 if minetest.get_meta(pos):get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
381 return stack:get_count()
382 end
383 return 0
384 end,
385 allow_metadata_inventory_take = function(pos, listname, index, stack, player)
386 if minetest.get_meta(pos):get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
387 return stack:get_count()
388 end
389 return 0
390 end,
391 allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
392 if minetest.get_meta(pos):get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then
393 return count
394 end
395 return 0
396 end,
397 can_dig = function(pos, player)
398 local meta=minetest.get_meta(pos)
399 local inv=meta:get_inventory()
400 if ((meta:get_string("owner")==player:get_player_name() or minetest.check_player_privs(player:get_player_name(), {protection_bypass=true})) and inv:is_empty("main") and inv:is_empty("pay1") and inv:is_empty("pay2") and inv:is_empty("pay3") and inv:is_empty("pay4") and inv:is_empty("give1") and inv:is_empty("give2") and inv:is_empty("give3") and inv:is_empty("give4")) or meta:get_string("owner")=="" then
401 smartshop.update(pos,"clear")
402 return true
403 end
404 end,
405 })