fixed bug (division by nil, zero division, number of items not clearing); clip averag...
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Thu, 10 May 2018 20:04:16 +0000 (22:04 +0200)
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Thu, 10 May 2018 20:04:16 +0000 (22:04 +0200)
init.lua

index a9cd800..72264e5 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -8,6 +8,7 @@ smartshop={user={},tmp={},dir={{x=0,y=0,z=-1},{x=-1,y=0,z=0},{x=0,y=0,z=1},{x=1,
 -- table with itemname: number of items being traded\r
 smartshop.itemstats = {}\r
 smartshop.itemprices = {}\r
+smartshop.stuffsold = {}\r
 \r
 \r
 \r
@@ -214,7 +215,10 @@ smartshop.update_info=function(pos)
                stuff["count" ..i]=inv:get_stack("give" .. i,1):get_count()\r
                stuff["name" ..i]=inv:get_stack("give" .. i,1):get_name()\r
                stuff["stock" ..i]=0 -- stuff["count" ..i]\r
-               stuff["pay"..i] = smartshop.minegeldtonumber(inv:get_stack("pay" .. i,1))/stuff["count" ..i]\r
+               local mg_price = smartshop.minegeldtonumber(inv:get_stack("pay" .. i,1))\r
+               if mg_price ~= nil then\r
+                  stuff["pay"..i] = mg_price/stuff["count" ..i]\r
+               end\r
                stuff["buy" ..i]=0\r
                for ii=1,32,1 do\r
                        name=inv:get_stack("main",ii):get_name()\r
@@ -226,16 +230,18 @@ smartshop.update_info=function(pos)
                local nstr=(stuff["stock" ..i]/stuff["count" ..i]) ..""\r
                nstr=nstr.split(nstr, ".")\r
                stuff["buy" ..i]=tonumber(nstr[1])\r
-               if stuff["name" ..i]~="" and stuff["buy" ..i]==0 then\r
-                  smartshop.itemsatpos(spos, stuff["name"..i], stuff["buy"..i]*stuff["count" ..i])\r
-                  smartshop.itempriceatpos(spos, stuff["name"..i], nil)\r
-               end\r
                if stuff["name" ..i]=="" or stuff["buy" ..i]==0 then\r
                        stuff["buy" ..i]=""\r
                        stuff["name" ..i]=""\r
+                       if smartshop.stuffsold[spos..i] then\r
+                          smartshop.itemsatpos(spos, smartshop.stuffsold[spos..i], 0)\r
+                          smartshop.itempriceatpos(spos, smartshop.stuffsold[spos..i], nil)\r
+                          smartshop.stuffsold[spos..i] = nil\r
+                       end                                                \r
                else\r
                   smartshop.itemsatpos(spos, stuff["name"..i], stuff["buy"..i]*stuff["count" ..i])\r
                   smartshop.itempriceatpos(spos, stuff["name"..i], stuff["pay"..i])\r
+                  smartshop.stuffsold[spos..i] = stuff["name"..i]\r
                   stuff["name"..i] = smartshop.get_human_name(stuff["name"..i])\r
                   stuff["buy" ..i]="(" ..stuff["buy" ..i] ..") "\r
                   stuff["name" ..i]=stuff["name" ..i] .."\n"\r
@@ -514,11 +520,14 @@ minetest.register_chatcommand("smstats", {
                   sum = sum + k\r
                end\r
                minetest.chat_send_player(plname, "Number of items: "..sum)\r
+               if sum == 0 then\r
+                  return\r
+               end\r
                psum = 0\r
                for i, k in pairs(smartshop.itemprices[name]) do\r
                   psum = psum + k*smartshop.itemstats[name][i]\r
                end\r
-               minetest.chat_send_player(plname, "Average price: "..psum/sum)\r
+               minetest.chat_send_player(plname, "Average price: "..string.format("%.3f",psum/sum))\r
                return true\r
 --             local ok, e = xban.ban_player(plname, name, nil, reason)\r
 --             return ok, ok and ("Banned %s."):format(plname) or e\r