added function to add report
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Thu, 10 May 2018 20:36:34 +0000 (22:36 +0200)
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Thu, 10 May 2018 20:36:34 +0000 (22:36 +0200)
init.lua

index 0deaef7..8c172ca 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -504,6 +504,33 @@ can_dig = function(pos, player)
        end,\r
 })\r
 \r
+smartshop.get_item_count = function(name)\r
+   sum = 0\r
+   if smartshop.itemstats[name] == nil then\r
+      return 0\r
+   end\r
+   for i, k in pairs(smartshop.itemstats[name]) do\r
+      sum = sum + k\r
+   end\r
+   return sum\r
+end\r
+\r
+smartshop.get_item_price = function(name)\r
+   sum = smartshop.get_item_count(name)\r
+   if smartshop.itemprices[name] == nil then\r
+      return 0\r
+   end\r
+   if sum == 0 then\r
+      return 0\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
+   return psum/sum\r
+end\r
+\r
+\r
 minetest.register_chatcommand("smstats", {\r
        description = "Get number of items sold",\r
        params = "<item_name>",\r
@@ -515,25 +542,36 @@ minetest.register_chatcommand("smstats", {
                if not smartshop.itemstats[name] then\r
                   return false, "No stats on "..name\r
                end\r
-               sum = 0\r
-               for i, k in pairs(smartshop.itemstats[name]) do\r
-                  sum = sum + k\r
-               end\r
+               sum = smartshop.get_item_count(name)\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: "..string.format("%.3f",psum/sum))\r
+               price = smartshop.get_item_price(name)\r
+               minetest.chat_send_player(plname, "Average price: "..string.format("%.3f",price))\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
        end,\r
 })\r
 \r
+minetest.register_chatcommand("smreport", {                          \r
+       description = "Get number of items sold",\r
+       func = function(plname, params)\r
+          local file = io.open(minetest.get_worldpath().."/smartshop_report.txt", "w")\r
+          if not file then\r
+             return false, "could not write to file"\r
+          end\r
+          for i,k in pairs(smartshop.itemstats) do\r
+             local count = smartshop.get_item_count(i)\r
+             local price = smartshop.get_item_price(i)\r
+             file:write(i.." "..count.." "..string.format("%.3f", price).."\n")\r
+          end\r
+          file:close()\r
+       end,\r
+})\r
+                                   \r
+\r
 minetest.register_lbm({\r
       name = "smartshop:update",\r
       nodenames = {"smartshop:shop"},\r