added ability for statistics on shops (itemcount being sold, average price)
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Thu, 10 May 2018 19:47:46 +0000 (21:47 +0200)
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>
Thu, 10 May 2018 19:47:46 +0000 (21:47 +0200)
init.lua

index ce6cce0..a9cd800 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -5,6 +5,56 @@ smartshop={user={},tmp={},dir={{x=0,y=0,z=-1},{x=-1,y=0,z=0},{x=0,y=0,z=1},{x=1,
 {{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}}}\r
 }\r
 \r
+-- table with itemname: number of items being traded\r
+smartshop.itemstats = {}\r
+smartshop.itemprices = {}\r
+\r
+\r
+\r
+smartshop.itemsatpos = function(pos, item, count)\r
+   -- set number of items of type 'item' sold at position 'pos'\r
+   if smartshop.itemstats[item] == nil then\r
+      smartshop.itemstats[item] = {}\r
+   end\r
+   smartshop.itemstats[item][pos] = count\r
+   local file = io.open(minetest.get_worldpath().."/smartshop_itemcounts.txt", "w")\r
+   if file then\r
+      file:write(minetest.serialize(smartshop.itemstats))\r
+      file:close()\r
+   end\r
+end\r
+\r
+smartshop.itempriceatpos = function(pos, item, price)\r
+   -- set number of items of type 'item' sold at position 'pos'\r
+   if smartshop.itemprices[item] == nil then\r
+      smartshop.itemprices[item] = {}\r
+   end\r
+   local file = io.open(minetest.get_worldpath().."/smartshop_itemprices.txt", "w")\r
+   if file then\r
+      file:write(minetest.serialize(smartshop.itemprices))\r
+      file:close()\r
+   end\r
+   smartshop.itemprices[item][pos] = price\r
+end\r
+\r
+smartshop.minegeldtonumber = function(stack)\r
+   -- return number of minegeld in stack, returns nil if stack is not composed of minegeld\r
+   count = stack:get_count()\r
+   if count == 0 then\r
+      return 0\r
+   end\r
+   if stack:get_name() == "currency:minegeld" then\r
+      return count\r
+   elseif stack:get_name() == "currency:minegeld_5" then\r
+      return count * 5\r
+   elseif stack:get_name() == "currency:minegeld_10" then\r
+      return count * 10\r
+   else\r
+      return nil\r
+   end\r
+end\r
+\r
+\r
 minetest.register_craft({\r
        output = "smartshop:shop",\r
        recipe = {\r
@@ -28,8 +78,6 @@ smartshop.use_offer=function(pos,player,n)
        smartshop.receive_fields(player,pressed)\r
        smartshop.user[player:get_player_name()]=nil\r
        smartshop.update(pos)\r
-\r
-\r
 end\r
 \r
 smartshop.get_offer=function(pos)\r
@@ -152,6 +200,7 @@ smartshop.update_info=function(pos)
           return\r
         end\r
        local meta=minetest.get_meta(pos)\r
+       local spos=minetest.pos_to_string(pos)\r
        local inv = meta:get_inventory()\r
        local owner=meta:get_string("owner")\r
        if meta:get_int("type")==0 then\r
@@ -165,6 +214,7 @@ 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
                stuff["buy" ..i]=0\r
                for ii=1,32,1 do\r
                        name=inv:get_stack("main",ii):get_name()\r
@@ -176,12 +226,16 @@ 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
-\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
                else\r
-\r
+                  smartshop.itemsatpos(spos, stuff["name"..i], stuff["buy"..i]*stuff["count" ..i])\r
+                  smartshop.itempriceatpos(spos, stuff["name"..i], stuff["pay"..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
@@ -336,6 +390,7 @@ minetest.register_node("smartshop:shop", {
                        local meta = minetest.get_meta(pos)\r
                        local inv = meta:get_inventory()\r
                        local added = inv:add_item("main", stack)\r
+                       smartshop.update_info(pos)\r
                        return added\r
                end,\r
                can_insert = function(pos, node, stack, direction)\r
@@ -442,3 +497,47 @@ can_dig = function(pos, player)
                end\r
        end,\r
 })\r
+\r
+minetest.register_chatcommand("smstats", {\r
+       description = "Get number of items sold",\r
+       params = "<item_name>",\r
+       func = function(plname, params)\r
+               local name = params:match("(%S+)")\r
+               if not (name) then\r
+                       return false, "Usage: /smstats <itemname>"\r
+               end\r
+               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
+               minetest.chat_send_player(plname, "Number of items: "..sum)\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
+               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
+\r
+-- load itemstats\r
+local file = io.open(minetest.get_worldpath().."/smartshop_itemcounts.txt", "r")\r
+if file then\r
+   local table = minetest.deserialize(file:read("*all"))\r
+   if type(table) == "table" then\r
+      smartshop.itemstats = table\r
+   end\r
+end\r
+local file = io.open(minetest.get_worldpath().."/smartshop_itemprices.txt", "r")\r
+if file then\r
+   local table = minetest.deserialize(file:read("*all"))\r
+   if type(table) == "table" then\r
+      smartshop.itemprices = table\r
+   end\r
+end\r