Preserve metadata on sale
[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 -- table with itemname: number of items being traded
9 smartshop.itemstats = {}
10 smartshop.itemprices = {}
11 smartshop.stuffsold = {}
12
13
14
15 smartshop.itemsatpos = function(pos, item, count)
16 -- set number of items of type 'item' sold at position 'pos'
17 if smartshop.itemstats[item] == nil then
18 smartshop.itemstats[item] = {}
19 end
20 smartshop.itemstats[item][pos] = count
21 local file = io.open(minetest.get_worldpath().."/smartshop_itemcounts.txt", "w")
22 if file then
23 file:write(minetest.serialize(smartshop.itemstats))
24 file:close()
25 end
26 end
27
28 smartshop.itempriceatpos = function(pos, item, price)
29 -- set number of items of type 'item' sold at position 'pos'
30 if smartshop.itemprices[item] == nil then
31 smartshop.itemprices[item] = {}
32 end
33 local file = io.open(minetest.get_worldpath().."/smartshop_itemprices.txt", "w")
34 if file then
35 file:write(minetest.serialize(smartshop.itemprices))
36 file:close()
37 end
38 smartshop.itemprices[item][pos] = price
39 end
40
41 smartshop.minegeldtonumber = function(stack)
42 -- return number of minegeld in stack, returns nil if stack is not composed of minegeld
43 count = stack:get_count()
44 if count == 0 then
45 return 0
46 end
47 if stack:get_name() == "currency:minegeld" then
48 return count
49 elseif stack:get_name() == "currency:minegeld_5" then
50 return count * 5
51 elseif stack:get_name() == "currency:minegeld_10" then
52 return count * 10
53 else
54 return nil
55 end
56 end
57
58
59 minetest.register_craft({
60 output = "smartshop:shop",
61 recipe = {
62 {"default:chest_locked", "default:chest_locked", "default:chest_locked"},
63 {"default:sign_wall_wood", "default:chest_locked", "default:sign_wall_wood"},
64 {"default:sign_wall_wood", "default:torch", "default:sign_wall_wood"},
65 }
66 })
67 smartshop.get_human_name = function(item)
68 if core.registered_items[item] then
69 return core.registered_items[item].description
70 else
71 return "Unknown Item"
72 end
73 end
74
75 smartshop.use_offer=function(pos,player,n)
76 local pressed={}
77 pressed["buy" .. n]=true
78 smartshop.user[player:get_player_name()]=pos
79 smartshop.receive_fields(player,pressed)
80 smartshop.user[player:get_player_name()]=nil
81 smartshop.update(pos)
82 end
83
84 smartshop.get_offer=function(pos)
85 if not pos or not minetest.get_node(pos) then return end
86 if minetest.get_node(pos).name~="smartshop:shop" then return end
87 local meta=minetest.get_meta(pos)
88 local inv=meta:get_inventory()
89 local offer={}
90 for i=1,4,1 do
91 offer[i]={
92 give=inv:get_stack("give" .. i,1):get_name(),
93 give_count=inv:get_stack("give" .. i,1):get_count(),
94 pay=inv:get_stack("pay" .. i,1):get_name(),
95 pay_count=inv:get_stack("pay" .. i,1):get_count(),
96 }
97 end
98 return offer
99 end
100
101 smartshop.send_mail=function(owner, pos, item)
102 if not minetest.get_modpath( "mail" ) then
103 return
104 end
105 local spos = "("..pos.x..", "..pos.y..", "..pos.z..")"
106 mail.send("DO NOT REPLY", owner, "Out of "..smartshop.get_human_name(item).." at "..spos, "Your smartshop at "..spos.." is out of "..smartshop.get_human_name(item)..". Please restock")
107 end
108
109 local function is_creative(pname)
110 return minetest.check_player_privs(pname, {creative=true}) or minetest.check_player_privs(pname, {give=true})
111 end
112
113 smartshop.receive_fields=function(player,pressed)
114 local pname = player:get_player_name()
115 local pos = smartshop.user[pname]
116 if not pos then
117 return
118 end
119 if pressed.customer then
120 return smartshop.showform(pos, player, true)
121 elseif pressed.tooglelime then
122 local meta=minetest.get_meta(pos)
123 if not is_creative(pname) then
124 meta:set_int("type", 1)
125 meta:set_int("creative", 0)
126 minetest.chat_send_player(pname, "You are not allowed to make a creative shop!")
127 return
128 end
129 if meta:get_int("type")==0 then
130 meta:set_int("type",1)
131 minetest.chat_send_player(pname, "Your stock is limited")
132 else
133 meta:set_int("type",0)
134 minetest.chat_send_player(pname, "Your stock is unlimited")
135 end
136 elseif not pressed.quit then
137 local n=1
138 for i=1,4,1 do
139 n=i
140 if pressed["buy" .. i] then break end
141 end
142 local meta=minetest.get_meta(pos)
143 local type=meta:get_int("type")
144 local inv=meta:get_inventory()
145 local pinv=player:get_inventory()
146 if pressed["buy" .. n] then
147 local stack=inv:get_stack("give" .. n,1)
148 local name=stack:get_name()
149 local pay=inv:get_stack("pay" .. n,1)
150 if name~="" then
151 if type==1 and inv:room_for_item("main", pay)==false then minetest.chat_send_player(pname, "Error: The owner's stock is full, can't receive, exchange aborted.") return end
152 if meta:get_int("ghost") ~=1 then
153 -- transition shops to ghost inventory.
154 for i=1,4 do
155 if inv:room_for_item("main", "pay"..i) and inv:room_for_item("main", "give"..i) then
156 meta:set_int("ghost", 1)
157 inv:add_item("main", inv:get_stack("pay"..i,1))
158 inv:add_item("main", inv:get_stack("give"..i,1))
159 end
160 end
161 end
162 if type==1 and inv:contains_item("main", stack)==false then
163 minetest.chat_send_player(pname, "Error: "..smartshop.get_human_name(name).." is sold out.")
164 if not meta:get_int("alerted") or meta:get_int("alerted") == 0 then
165 meta:set_int("alerted",1) -- Do not alert twice
166 smartshop.send_mail(meta:get_string("owner"), pos, name)
167 end
168 return
169 end
170 if not pinv:contains_item("main", pay) then minetest.chat_send_player(pname, "Error: You don't have enough in your inventory to buy this, exchange aborted.") return end
171 if not pinv:room_for_item("main", stack) then minetest.chat_send_player(pname, "Error: Your inventory is full, exchange aborted.") return end
172 if type == 0 then
173 pinv:remove_item("main", pay)
174 pinv:add_item("main", stack)
175 else
176 local item = inv:remove_item("main", stack)
177 pinv:add_item("main", item)
178 item = pinv:remove_item("main",pay)
179 inv:add_item("main", item)
180 if not inv:contains_item("main", stack) and (not meta:get_int("alerted") or meta:get_int("alerted") == 0) then
181 meta:set_int("alerted",1) -- Do not alert twice
182 smartshop.send_mail(meta:get_string("owner"), pos, name)
183 end
184 end
185 end
186 end
187 else
188 smartshop.update_info(pos)
189 local meta = minetest.get_meta(pos)
190 if meta:get_string("owner") == pname then
191 smartshop.update(pos, "update")
192 end
193 smartshop.user[pname] = nil
194 end
195 end
196
197 minetest.register_on_player_receive_fields(function(player, form, pressed)
198 if form=="smartshop.showform" then
199 smartshop.receive_fields(player,pressed)
200 end
201 end)
202
203
204
205
206 smartshop.update_info=function(pos)
207 if not pos then
208 return
209 end
210 local meta=minetest.get_meta(pos)
211 local spos=minetest.pos_to_string(pos)
212 local inv = meta:get_inventory()
213 local owner=meta:get_string("owner")
214 if meta:get_int("type")==0 then
215 meta:set_string("infotext","(Smartshop by " .. owner ..") Stock is unlimited")
216 return false
217 end
218 local name=""
219 local count=0
220 local stuff={}
221 for i=1,4,1 do
222 stuff["count" ..i]=inv:get_stack("give" .. i,1):get_count()
223 stuff["name" ..i]=inv:get_stack("give" .. i,1):get_name()
224 stuff["stock" ..i]=0 -- stuff["count" ..i]
225 local mg_price = smartshop.minegeldtonumber(inv:get_stack("pay" .. i,1))
226 if mg_price ~= nil then
227 stuff["pay"..i] = mg_price/stuff["count" ..i]
228 end
229 stuff["buy" ..i]=0
230 for ii=1,32,1 do
231 name=inv:get_stack("main",ii):get_name()
232 count=inv:get_stack("main",ii):get_count()
233 if name==stuff["name" ..i] then
234 stuff["stock" ..i]=stuff["stock" ..i]+count
235 end
236 end
237 local nstr=(stuff["stock" ..i]/stuff["count" ..i]) ..""
238 nstr=nstr.split(nstr, ".")
239 stuff["buy" ..i]=tonumber(nstr[1])
240 if stuff["name" ..i]=="" or stuff["buy" ..i]==0 then
241 stuff["buy" ..i]=""
242 stuff["name" ..i]=""
243 if smartshop.stuffsold[spos..i] then
244 smartshop.itemsatpos(spos, smartshop.stuffsold[spos..i], 0)
245 smartshop.itempriceatpos(spos, smartshop.stuffsold[spos..i], nil)
246 smartshop.stuffsold[spos..i] = nil
247 end
248 else
249 smartshop.itemsatpos(spos, stuff["name"..i], stuff["buy"..i]*stuff["count" ..i])
250 smartshop.itempriceatpos(spos, stuff["name"..i], stuff["pay"..i])
251 smartshop.stuffsold[spos..i] = stuff["name"..i]
252 stuff["name"..i] = smartshop.get_human_name(stuff["name"..i])
253 stuff["buy" ..i]="(" ..stuff["buy" ..i] ..") "
254 stuff["name" ..i]=stuff["name" ..i] .."\n"
255 end
256 end
257 meta:set_string("infotext",
258 "(Smartshop by " .. owner ..") Purchases left:\n"
259 .. stuff.buy1 .. stuff.name1
260 .. stuff.buy2 .. stuff.name2
261 .. stuff.buy3 .. stuff.name3
262 .. stuff.buy4 .. stuff.name4
263 )
264 end
265
266
267
268
269 smartshop.update=function(pos,stat)
270 --clear
271 local spos=minetest.pos_to_string(pos)
272 for _, ob in ipairs(minetest.env:get_objects_inside_radius(pos, 2)) do
273 if ob and ob:get_luaentity() and ob:get_luaentity().smartshop and ob:get_luaentity().pos==spos then
274 ob:remove()
275 end
276 end
277 if stat=="clear" then return end
278 --update
279 local meta=minetest.get_meta(pos)
280 local inv = meta:get_inventory()
281 local node=minetest.get_node(pos)
282 local dp = smartshop.dir[node.param2+1]
283 if not dp then return end
284 pos.x = pos.x + dp.x*0.01
285 pos.y = pos.y + dp.y*6.5/16
286 pos.z = pos.z + dp.z*0.01
287 for i=1,4,1 do
288 local item=inv:get_stack("give" .. i,1):get_name()
289 local pos2=smartshop.dpos[node.param2+1][i]
290 if item~="" then
291 smartshop.tmp.item=item
292 smartshop.tmp.pos=spos
293 local e = minetest.env:add_entity({x=pos.x+pos2.x,y=pos.y+pos2.y,z=pos.z+pos2.z},"smartshop:item")
294 e:setyaw(math.pi*2 - node.param2 * math.pi/2)
295 end
296 end
297 end
298
299
300 minetest.register_entity("smartshop:item",{
301 hp_max = 1,
302 visual="wielditem",
303 visual_size={x=.20,y=.20},
304 collisionbox = {0,0,0,0,0,0},
305 physical=false,
306 textures={"air"},
307 smartshop=true,
308 type="",
309 on_activate = function(self, staticdata)
310 if smartshop.tmp.item ~= nil then
311 self.item=smartshop.tmp.item
312 self.pos=smartshop.tmp.pos
313 smartshop.tmp={}
314 else
315 if staticdata ~= nil and staticdata ~= "" then
316 local data = staticdata:split(';')
317 if data and data[1] and data[2] then
318 self.item = data[1]
319 self.pos = data[2]
320 end
321 end
322 end
323 if self.item ~= nil then
324 self.object:set_properties({textures={self.item}})
325 else
326 self.object:remove()
327 end
328 end,
329 get_staticdata = function(self)
330 if self.item ~= nil and self.pos ~= nil then
331 return self.item .. ';' .. self.pos
332 end
333 return ""
334 end,
335 })
336
337
338 smartshop.showform=function(pos,player,re)
339 local meta=minetest.get_meta(pos)
340 local creative=meta:get_int("creative")
341 local inv = meta:get_inventory()
342 local gui=""
343 local spos=pos.x .. "," .. pos.y .. "," .. pos.z
344 local owner=meta:get_string("owner")==player:get_player_name()
345 if minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then owner=true end
346 if re then owner=false end
347 smartshop.user[player:get_player_name()]=pos
348 if owner then
349 meta:set_int("alerted",0) -- Player has been there to refill
350 gui=""
351 .."size[8,10]"
352 .."button_exit[6,0;1.5,1;customer;Customer]"
353 .."label[0,0.2;Item:]"
354 .."label[0,1.2;Price:]"
355 .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
356 .."list[nodemeta:" .. spos .. ";pay1;2,1;1,1;]"
357 .."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
358 .."list[nodemeta:" .. spos .. ";pay2;3,1;1,1;]"
359 .."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
360 .."list[nodemeta:" .. spos .. ";pay3;4,1;1,1;]"
361 .."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
362 .."list[nodemeta:" .. spos .. ";pay4;5,1;1,1;]"
363 if creative==1 then
364 gui=gui .."label[0.5,-0.4;Your stock is unlimited becaouse you have creative or give]"
365 .."button[6,1;2.2,1;tooglelime;Toggle limit]"
366 end
367 gui=gui
368 .."list[nodemeta:" .. spos .. ";main;0,2;8,4;]"
369 .."list[current_player;main;0,6.2;8,4;]"
370 .."listring[nodemeta:" .. spos .. ";main]"
371 .."listring[current_player;main]"
372 else
373 gui=""
374 .."size[8,6]"
375 .."list[current_player;main;0,2.2;8,4;]"
376 .."label[0,0.2;Item:]"
377 .."label[0,1.2;Price:]"
378 .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
379 .."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() .."]"
380 .."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
381 .."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() .."]"
382 .."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
383 .."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() .."]"
384 .."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
385 .."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() .."]"
386 end
387 minetest.after((0.1), function(gui)
388 return minetest.show_formspec(player:get_player_name(), "smartshop.showform",gui)
389 end, gui)
390 end
391
392 minetest.register_node("smartshop:shop", {
393 description = "Smartshop",
394 tiles = {"default_chest_top.png^[colorize:#ffffff77^default_obsidian_glass.png"},
395 groups = {choppy = 2, oddly_breakable_by_hand = 1,tubedevice = 1, tubedevice_receiver = 1},
396 drawtype="nodebox",
397 node_box = {type="fixed",fixed={-0.5,-0.5,-0.0,0.5,0.5,0.5}},
398 paramtype2="facedir",
399 paramtype = "light",
400 sunlight_propagates = true,
401 light_source = 10,
402 tube = {insert_object = function(pos, node, stack, direction)
403 local meta = minetest.get_meta(pos)
404 local inv = meta:get_inventory()
405 local added = inv:add_item("main", stack)
406 smartshop.update_info(pos)
407 return added
408 end,
409 can_insert = function(pos, node, stack, direction)
410 local meta = minetest.get_meta(pos)
411 local inv = meta:get_inventory()
412 for i=1,4 do
413 local sellitem = inv:get_stack("give"..i,1):get_name()
414 if sellitem == stack:get_name() then
415 return inv:room_for_item("main", stack)
416 end
417 -- minetest.chat_send_all(sellitem)
418 end
419 --
420 return false
421 end,
422 input_inventory = "main",
423 connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}},
424 after_place_node = function(pos, placer)
425 local meta=minetest.get_meta(pos)
426 meta:set_string("owner",placer:get_player_name())
427 meta:set_string("infotext", "Shop by: " .. placer:get_player_name())
428 meta:set_int("type",1)
429 if is_creative(placer:get_player_name()) then
430 meta:set_int("creative",1)
431 meta:set_int("type",0)
432 end
433 end,
434 on_construct = function(pos)
435 local meta=minetest.get_meta(pos)
436 meta:set_int("state", 0)
437 meta:get_inventory():set_size("main", 32)
438 meta:get_inventory():set_size("give1", 1)
439 meta:get_inventory():set_size("pay1", 1)
440 meta:get_inventory():set_size("give2", 1)
441 meta:get_inventory():set_size("pay2", 1)
442 meta:get_inventory():set_size("give3", 1)
443 meta:get_inventory():set_size("pay3", 1)
444 meta:get_inventory():set_size("give4", 1)
445 meta:get_inventory():set_size("pay4", 1)
446 meta:set_int("ghost", 1)
447 end,
448 on_rightclick = function(pos, node, player, itemstack, pointed_thing)
449 smartshop.showform(pos,player)
450 end,
451 allow_metadata_inventory_put = function(pos, listname, index, stack, player)
452 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
453 local meta = minetest.get_meta(pos)
454 if meta:get_int("ghost") == 1 and (string.find(listname, "pay") or string.find(listname, "give")) then
455 local inv = minetest.get_inventory({type="node", pos=pos})
456 -- minetest.chat_send_all( inv:get_stack(listname, index):get_name()..stack:get_name())
457 if inv:get_stack(listname, index):get_name() == stack:get_name() then
458 inv:add_item(listname, stack)
459 else
460 inv:set_stack(listname, index, stack)
461 end
462 return 0
463 end
464 return stack:get_count()
465 end
466 return 0
467 end,
468 allow_metadata_inventory_take = function(pos, listname, index, stack, player)
469 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
470 local meta = minetest.get_meta(pos)
471 if meta:get_int("ghost") == 1 and (string.find(listname, "pay") or string.find(listname, "give")) then
472 local inv = minetest.get_inventory({type="node", pos=pos})
473 inv:set_stack(listname, index, ItemStack(""))
474 return 0
475 end
476 return stack:get_count()
477 end
478 return 0
479 end,
480 allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
481 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
482 local meta = minetest.get_meta(pos)
483 local inv = minetest.get_inventory({type="node", pos=pos})
484 if meta:get_int("ghost") ~= 1 then
485 return count
486 end
487 if (string.find(from_list, "pay") or string.find(from_list, "give")) and to_list == "main" then
488 inv:set_stack(from_list, from_index, ItemStack(""))
489 return 0
490 elseif (string.find(to_list, "pay") or string.find(to_list, "give")) and from_list == "main" then
491 if inv:get_stack(to_list, to_index):get_name() == inv:get_stack(from_list, from_index):get_name() then
492 inv:add_item(to_list, inv:get_stack(from_list, from_index))
493 else
494 inv:set_stack(to_list, to_index, inv:get_stack(from_list, from_index))
495 inv:set_stack(from_list, from_index, inv:get_stack(from_list, from_index))
496 end
497 return 0
498 else
499 return count
500 end
501 end
502 return 0
503 end,
504 can_dig = function(pos, player)
505 local meta=minetest.get_meta(pos)
506 local inv=meta:get_inventory()
507 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
508 smartshop.update(pos,"clear")
509 return true
510 end
511 end,
512 })
513
514 smartshop.get_item_count = function(name)
515 sum = 0
516 if smartshop.itemstats[name] == nil then
517 return 0
518 end
519 for i, k in pairs(smartshop.itemstats[name]) do
520 sum = sum + k
521 end
522 return sum
523 end
524
525 smartshop.get_shop_count = function(name)
526 sum = 0
527 if smartshop.itemstats[name] == nil then
528 return 0
529 end
530 for i, k in pairs(smartshop.itemstats[name]) do
531 sum = sum + 1
532 end
533 return sum
534 end
535
536 smartshop.get_item_price = function(name)
537 sum = smartshop.get_item_count(name)
538 if smartshop.itemprices[name] == nil then
539 return 0
540 end
541 if sum == 0 then
542 return 0
543 end
544 psum = 0
545 for i, k in pairs(smartshop.itemprices[name]) do
546 psum = psum + k*smartshop.itemstats[name][i]
547 end
548 return psum/sum
549 end
550
551
552 minetest.register_chatcommand("smstats", {
553 description = "Get number of items sold",
554 params = "<item_name>",
555 func = function(plname, params)
556 local name = params:match("(%S+)")
557 if not (name) then
558 return false, "Usage: /smstats <itemname>"
559 end
560 if not smartshop.itemstats[name] then
561 return false, "No stats on "..name
562 end
563 sum = smartshop.get_item_count(name)
564 minetest.chat_send_player(plname, "Number of items: "..sum)
565 minetest.chat_send_player(plname, "Number of shops offering item: "..smartshop.get_shop_count(name))
566 if sum == 0 then
567 return
568 end
569 price = smartshop.get_item_price(name)
570 minetest.chat_send_player(plname, "Average price: "..string.format("%.3f",price))
571 return true
572 -- local ok, e = xban.ban_player(plname, name, nil, reason)
573 -- return ok, ok and ("Banned %s."):format(plname) or e
574 end,
575 })
576
577 smartshop.report = function ()
578 local file = io.open(minetest.get_worldpath().."/smartshop_report.txt", "w")
579 if not file then
580 return false, "could not write to file"
581 end
582 for i,k in pairs(smartshop.itemstats) do
583 local count = smartshop.get_item_count(i)
584 local price = smartshop.get_item_price(i)
585 file:write(i.." "..count.." "..string.format("%.3f", price).." "..smartshop.get_shop_count(i).."\n")
586 end
587 file:close()
588 end
589
590 minetest.register_chatcommand("smreport", {
591 description = "Get number of items sold",
592 func = function(plname, params)
593 smartshop.report()
594 end,
595 })
596
597 local timer = 0
598 minetest.register_globalstep(function(dtime)
599 timer = timer + dtime;
600 if timer >= 100 then
601 smartshop.report()
602 timer = 0
603 end
604 end)
605
606
607 if false then -- This lbm is used to add pre-update smartshops to the price database. Activate with care! Warning: very slow.
608 minetest.register_lbm({
609 name = "smartshop:update",
610 nodenames = {"smartshop:shop"},
611 action = function(pos, node)
612 smartshop.update_info(pos)
613 end,
614 })
615 end
616
617 -- load itemstats
618 local file = io.open(minetest.get_worldpath().."/smartshop_itemcounts.txt", "r")
619 if file then
620 local table = minetest.deserialize(file:read("*all"))
621 if type(table) == "table" then
622 smartshop.itemstats = table
623 end
624 end
625 local file = io.open(minetest.get_worldpath().."/smartshop_itemprices.txt", "r")
626 if file then
627 local table = minetest.deserialize(file:read("*all"))
628 if type(table) == "table" then
629 smartshop.itemprices = table
630 end
631 end