813ba6a7b30b20ad05449c9f375f2edfa9626e44
[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 name=inv:get_stack("give" .. n,1):get_name()
148 local stack=name .." ".. inv:get_stack("give" .. n,1):get_count()
149 local pay=inv:get_stack("pay" .. n,1):get_name() .." ".. inv:get_stack("pay" .. n,1):get_count()
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 pinv:remove_item("main", pay)
173 pinv:add_item("main", stack)
174 if type==1 then
175 inv:remove_item("main", stack)
176 inv:add_item("main", pay)
177 if not inv:contains_item("main", stack) and (not meta:get_int("alerted") or meta:get_int("alerted") == 0) then
178 meta:set_int("alerted",1) -- Do not alert twice
179 smartshop.send_mail(meta:get_string("owner"), pos, name)
180 end
181 end
182 end
183 end
184 else
185 smartshop.update_info(pos)
186 local meta = minetest.get_meta(pos)
187 if meta:get_string("owner") == pname then
188 smartshop.update(pos, "update")
189 end
190 smartshop.user[pname] = nil
191 end
192 end
193
194 minetest.register_on_player_receive_fields(function(player, form, pressed)
195 if form=="smartshop.showform" then
196 smartshop.receive_fields(player,pressed)
197 end
198 end)
199
200
201
202
203 smartshop.update_info=function(pos)
204 if not pos then
205 return
206 end
207 local meta=minetest.get_meta(pos)
208 local spos=minetest.pos_to_string(pos)
209 local inv = meta:get_inventory()
210 local owner=meta:get_string("owner")
211 if meta:get_int("type")==0 then
212 meta:set_string("infotext","(Smartshop by " .. owner ..") Stock is unlimited")
213 return false
214 end
215 local name=""
216 local count=0
217 local stuff={}
218 for i=1,4,1 do
219 stuff["count" ..i]=inv:get_stack("give" .. i,1):get_count()
220 stuff["name" ..i]=inv:get_stack("give" .. i,1):get_name()
221 stuff["stock" ..i]=0 -- stuff["count" ..i]
222 local mg_price = smartshop.minegeldtonumber(inv:get_stack("pay" .. i,1))
223 if mg_price ~= nil then
224 stuff["pay"..i] = mg_price/stuff["count" ..i]
225 end
226 stuff["buy" ..i]=0
227 for ii=1,32,1 do
228 name=inv:get_stack("main",ii):get_name()
229 count=inv:get_stack("main",ii):get_count()
230 if name==stuff["name" ..i] then
231 stuff["stock" ..i]=stuff["stock" ..i]+count
232 end
233 end
234 local nstr=(stuff["stock" ..i]/stuff["count" ..i]) ..""
235 nstr=nstr.split(nstr, ".")
236 stuff["buy" ..i]=tonumber(nstr[1])
237 if stuff["name" ..i]=="" or stuff["buy" ..i]==0 then
238 stuff["buy" ..i]=""
239 stuff["name" ..i]=""
240 if smartshop.stuffsold[spos..i] then
241 smartshop.itemsatpos(spos, smartshop.stuffsold[spos..i], 0)
242 smartshop.itempriceatpos(spos, smartshop.stuffsold[spos..i], nil)
243 smartshop.stuffsold[spos..i] = nil
244 end
245 else
246 smartshop.itemsatpos(spos, stuff["name"..i], stuff["buy"..i]*stuff["count" ..i])
247 smartshop.itempriceatpos(spos, stuff["name"..i], stuff["pay"..i])
248 smartshop.stuffsold[spos..i] = stuff["name"..i]
249 stuff["name"..i] = smartshop.get_human_name(stuff["name"..i])
250 stuff["buy" ..i]="(" ..stuff["buy" ..i] ..") "
251 stuff["name" ..i]=stuff["name" ..i] .."\n"
252 end
253 end
254 meta:set_string("infotext",
255 "(Smartshop by " .. owner ..") Purchases left:\n"
256 .. stuff.buy1 .. stuff.name1
257 .. stuff.buy2 .. stuff.name2
258 .. stuff.buy3 .. stuff.name3
259 .. stuff.buy4 .. stuff.name4
260 )
261 end
262
263
264
265
266 smartshop.update=function(pos,stat)
267 --clear
268 local spos=minetest.pos_to_string(pos)
269 for _, ob in ipairs(minetest.env:get_objects_inside_radius(pos, 2)) do
270 if ob and ob:get_luaentity() and ob:get_luaentity().smartshop and ob:get_luaentity().pos==spos then
271 ob:remove()
272 end
273 end
274 if stat=="clear" then return end
275 --update
276 local meta=minetest.get_meta(pos)
277 local inv = meta:get_inventory()
278 local node=minetest.get_node(pos)
279 local dp = smartshop.dir[node.param2+1]
280 if not dp then return end
281 pos.x = pos.x + dp.x*0.01
282 pos.y = pos.y + dp.y*6.5/16
283 pos.z = pos.z + dp.z*0.01
284 for i=1,4,1 do
285 local item=inv:get_stack("give" .. i,1):get_name()
286 local pos2=smartshop.dpos[node.param2+1][i]
287 if item~="" then
288 smartshop.tmp.item=item
289 smartshop.tmp.pos=spos
290 local e = minetest.env:add_entity({x=pos.x+pos2.x,y=pos.y+pos2.y,z=pos.z+pos2.z},"smartshop:item")
291 e:setyaw(math.pi*2 - node.param2 * math.pi/2)
292 end
293 end
294 end
295
296
297 minetest.register_entity("smartshop:item",{
298 hp_max = 1,
299 visual="wielditem",
300 visual_size={x=.20,y=.20},
301 collisionbox = {0,0,0,0,0,0},
302 physical=false,
303 textures={"air"},
304 smartshop=true,
305 type="",
306 on_activate = function(self, staticdata)
307 if smartshop.tmp.item ~= nil then
308 self.item=smartshop.tmp.item
309 self.pos=smartshop.tmp.pos
310 smartshop.tmp={}
311 else
312 if staticdata ~= nil and staticdata ~= "" then
313 local data = staticdata:split(';')
314 if data and data[1] and data[2] then
315 self.item = data[1]
316 self.pos = data[2]
317 end
318 end
319 end
320 if self.item ~= nil then
321 self.object:set_properties({textures={self.item}})
322 else
323 self.object:remove()
324 end
325 end,
326 get_staticdata = function(self)
327 if self.item ~= nil and self.pos ~= nil then
328 return self.item .. ';' .. self.pos
329 end
330 return ""
331 end,
332 })
333
334
335 smartshop.showform=function(pos,player,re)
336 local meta=minetest.get_meta(pos)
337 local creative=meta:get_int("creative")
338 local inv = meta:get_inventory()
339 local gui=""
340 local spos=pos.x .. "," .. pos.y .. "," .. pos.z
341 local owner=meta:get_string("owner")==player:get_player_name()
342 if minetest.check_player_privs(player:get_player_name(), {protection_bypass=true}) then owner=true end
343 if re then owner=false end
344 smartshop.user[player:get_player_name()]=pos
345 if owner then
346 meta:set_int("alerted",0) -- Player has been there to refill
347 gui=""
348 .."size[8,10]"
349 .."button_exit[6,0;1.5,1;customer;Customer]"
350 .."label[0,0.2;Item:]"
351 .."label[0,1.2;Price:]"
352 .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
353 .."list[nodemeta:" .. spos .. ";pay1;2,1;1,1;]"
354 .."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
355 .."list[nodemeta:" .. spos .. ";pay2;3,1;1,1;]"
356 .."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
357 .."list[nodemeta:" .. spos .. ";pay3;4,1;1,1;]"
358 .."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
359 .."list[nodemeta:" .. spos .. ";pay4;5,1;1,1;]"
360 if creative==1 then
361 gui=gui .."label[0.5,-0.4;Your stock is unlimited becaouse you have creative or give]"
362 .."button[6,1;2.2,1;tooglelime;Toggle limit]"
363 end
364 gui=gui
365 .."list[nodemeta:" .. spos .. ";main;0,2;8,4;]"
366 .."list[current_player;main;0,6.2;8,4;]"
367 .."listring[nodemeta:" .. spos .. ";main]"
368 .."listring[current_player;main]"
369 else
370 gui=""
371 .."size[8,6]"
372 .."list[current_player;main;0,2.2;8,4;]"
373 .."label[0,0.2;Item:]"
374 .."label[0,1.2;Price:]"
375 .."list[nodemeta:" .. spos .. ";give1;2,0;1,1;]"
376 .."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() .."]"
377 .."list[nodemeta:" .. spos .. ";give2;3,0;1,1;]"
378 .."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() .."]"
379 .."list[nodemeta:" .. spos .. ";give3;4,0;1,1;]"
380 .."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() .."]"
381 .."list[nodemeta:" .. spos .. ";give4;5,0;1,1;]"
382 .."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() .."]"
383 end
384 minetest.after((0.1), function(gui)
385 return minetest.show_formspec(player:get_player_name(), "smartshop.showform",gui)
386 end, gui)
387 end
388
389 minetest.register_node("smartshop:shop", {
390 description = "Smartshop",
391 tiles = {"default_chest_top.png^[colorize:#ffffff77^default_obsidian_glass.png"},
392 groups = {choppy = 2, oddly_breakable_by_hand = 1,tubedevice = 1, tubedevice_receiver = 1},
393 drawtype="nodebox",
394 node_box = {type="fixed",fixed={-0.5,-0.5,-0.0,0.5,0.5,0.5}},
395 paramtype2="facedir",
396 paramtype = "light",
397 sunlight_propagates = true,
398 light_source = 10,
399 tube = {insert_object = function(pos, node, stack, direction)
400 local meta = minetest.get_meta(pos)
401 local inv = meta:get_inventory()
402 local added = inv:add_item("main", stack)
403 smartshop.update_info(pos)
404 return added
405 end,
406 can_insert = function(pos, node, stack, direction)
407 local meta = minetest.get_meta(pos)
408 local inv = meta:get_inventory()
409 for i=1,4 do
410 local sellitem = inv:get_stack("give"..i,1):get_name()
411 if sellitem == stack:get_name() then
412 return inv:room_for_item("main", stack)
413 end
414 -- minetest.chat_send_all(sellitem)
415 end
416 --
417 return false
418 end,
419 input_inventory = "main",
420 connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}},
421 after_place_node = function(pos, placer)
422 local meta=minetest.get_meta(pos)
423 meta:set_string("owner",placer:get_player_name())
424 meta:set_string("infotext", "Shop by: " .. placer:get_player_name())
425 meta:set_int("type",1)
426 if is_creative(placer:get_player_name()) then
427 meta:set_int("creative",1)
428 meta:set_int("type",0)
429 end
430 end,
431 on_construct = function(pos)
432 local meta=minetest.get_meta(pos)
433 meta:set_int("state", 0)
434 meta:get_inventory():set_size("main", 32)
435 meta:get_inventory():set_size("give1", 1)
436 meta:get_inventory():set_size("pay1", 1)
437 meta:get_inventory():set_size("give2", 1)
438 meta:get_inventory():set_size("pay2", 1)
439 meta:get_inventory():set_size("give3", 1)
440 meta:get_inventory():set_size("pay3", 1)
441 meta:get_inventory():set_size("give4", 1)
442 meta:get_inventory():set_size("pay4", 1)
443 meta:set_int("ghost", 1)
444 end,
445 on_rightclick = function(pos, node, player, itemstack, pointed_thing)
446 smartshop.showform(pos,player)
447 end,
448 allow_metadata_inventory_put = function(pos, listname, index, stack, player)
449 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
450 local meta = minetest.get_meta(pos)
451 if meta:get_int("ghost") == 1 and (string.find(listname, "pay") or string.find(listname, "give")) then
452 local inv = minetest.get_inventory({type="node", pos=pos})
453 -- minetest.chat_send_all( inv:get_stack(listname, index):get_name()..stack:get_name())
454 if inv:get_stack(listname, index):get_name() == stack:get_name() then
455 inv:add_item(listname, stack)
456 else
457 inv:set_stack(listname, index, stack)
458 end
459 return 0
460 end
461 return stack:get_count()
462 end
463 return 0
464 end,
465 allow_metadata_inventory_take = function(pos, listname, index, stack, player)
466 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
467 local meta = minetest.get_meta(pos)
468 if meta:get_int("ghost") == 1 and (string.find(listname, "pay") or string.find(listname, "give")) then
469 local inv = minetest.get_inventory({type="node", pos=pos})
470 inv:set_stack(listname, index, ItemStack(""))
471 return 0
472 end
473 return stack:get_count()
474 end
475 return 0
476 end,
477 allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
478 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
479 local meta = minetest.get_meta(pos)
480 local inv = minetest.get_inventory({type="node", pos=pos})
481 if meta:get_int("ghost") ~= 1 then
482 return count
483 end
484 if (string.find(from_list, "pay") or string.find(from_list, "give")) and to_list == "main" then
485 inv:set_stack(from_list, from_index, ItemStack(""))
486 return 0
487 elseif (string.find(to_list, "pay") or string.find(to_list, "give")) and from_list == "main" then
488 if inv:get_stack(to_list, to_index):get_name() == inv:get_stack(from_list, from_index):get_name() then
489 inv:add_item(to_list, inv:get_stack(from_list, from_index))
490 else
491 inv:set_stack(to_list, to_index, inv:get_stack(from_list, from_index))
492 inv:set_stack(from_list, from_index, inv:get_stack(from_list, from_index))
493 end
494 return 0
495 else
496 return count
497 end
498 end
499 return 0
500 end,
501 can_dig = function(pos, player)
502 local meta=minetest.get_meta(pos)
503 local inv=meta:get_inventory()
504 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
505 smartshop.update(pos,"clear")
506 return true
507 end
508 end,
509 })
510
511 smartshop.get_item_count = function(name)
512 sum = 0
513 if smartshop.itemstats[name] == nil then
514 return 0
515 end
516 for i, k in pairs(smartshop.itemstats[name]) do
517 sum = sum + k
518 end
519 return sum
520 end
521
522 smartshop.get_shop_count = function(name)
523 sum = 0
524 if smartshop.itemstats[name] == nil then
525 return 0
526 end
527 for i, k in pairs(smartshop.itemstats[name]) do
528 sum = sum + 1
529 end
530 return sum
531 end
532
533 smartshop.get_item_price = function(name)
534 sum = smartshop.get_item_count(name)
535 if smartshop.itemprices[name] == nil then
536 return 0
537 end
538 if sum == 0 then
539 return 0
540 end
541 psum = 0
542 for i, k in pairs(smartshop.itemprices[name]) do
543 psum = psum + k*smartshop.itemstats[name][i]
544 end
545 return psum/sum
546 end
547
548
549 minetest.register_chatcommand("smstats", {
550 description = "Get number of items sold",
551 params = "<item_name>",
552 func = function(plname, params)
553 local name = params:match("(%S+)")
554 if not (name) then
555 return false, "Usage: /smstats <itemname>"
556 end
557 if not smartshop.itemstats[name] then
558 return false, "No stats on "..name
559 end
560 sum = smartshop.get_item_count(name)
561 minetest.chat_send_player(plname, "Number of items: "..sum)
562 minetest.chat_send_player(plname, "Number of shops offering item: "..smartshop.get_shop_count(name))
563 if sum == 0 then
564 return
565 end
566 price = smartshop.get_item_price(name)
567 minetest.chat_send_player(plname, "Average price: "..string.format("%.3f",price))
568 return true
569 -- local ok, e = xban.ban_player(plname, name, nil, reason)
570 -- return ok, ok and ("Banned %s."):format(plname) or e
571 end,
572 })
573
574 smartshop.report = function ()
575 local file = io.open(minetest.get_worldpath().."/smartshop_report.txt", "w")
576 if not file then
577 return false, "could not write to file"
578 end
579 for i,k in pairs(smartshop.itemstats) do
580 local count = smartshop.get_item_count(i)
581 local price = smartshop.get_item_price(i)
582 file:write(i.." "..count.." "..string.format("%.3f", price).." "..smartshop.get_shop_count(i).."\n")
583 end
584 file:close()
585 end
586
587 minetest.register_chatcommand("smreport", {
588 description = "Get number of items sold",
589 func = function(plname, params)
590 smartshop.report()
591 end,
592 })
593
594 local timer = 0
595 minetest.register_globalstep(function(dtime)
596 timer = timer + dtime;
597 if timer >= 100 then
598 smartshop.report()
599 timer = 0
600 end
601 end)
602
603
604 if false then -- This lbm is used to add pre-update smartshops to the price database. Activate with care! Warning: very slow.
605 minetest.register_lbm({
606 name = "smartshop:update",
607 nodenames = {"smartshop:shop"},
608 action = function(pos, node)
609 smartshop.update_info(pos)
610 end,
611 })
612 end
613
614 -- load itemstats
615 local file = io.open(minetest.get_worldpath().."/smartshop_itemcounts.txt", "r")
616 if file then
617 local table = minetest.deserialize(file:read("*all"))
618 if type(table) == "table" then
619 smartshop.itemstats = table
620 end
621 end
622 local file = io.open(minetest.get_worldpath().."/smartshop_itemprices.txt", "r")
623 if file then
624 local table = minetest.deserialize(file:read("*all"))
625 if type(table) == "table" then
626 smartshop.itemprices = table
627 end
628 end