From 85455f7bfa75ba5e57ebc5a70deffb1e0766d6f9 Mon Sep 17 00:00:00 2001 From: Gabriel PĂ©rez-Cerezo Date: Thu, 10 May 2018 22:36:34 +0200 Subject: added function to add report --- init.lua | 56 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/init.lua b/init.lua index 0deaef7..8c172ca 100644 --- a/init.lua +++ b/init.lua @@ -504,6 +504,33 @@ can_dig = function(pos, player) end, }) +smartshop.get_item_count = function(name) + sum = 0 + if smartshop.itemstats[name] == nil then + return 0 + end + for i, k in pairs(smartshop.itemstats[name]) do + sum = sum + k + end + return sum +end + +smartshop.get_item_price = function(name) + sum = smartshop.get_item_count(name) + if smartshop.itemprices[name] == nil then + return 0 + end + if sum == 0 then + return 0 + end + psum = 0 + for i, k in pairs(smartshop.itemprices[name]) do + psum = psum + k*smartshop.itemstats[name][i] + end + return psum/sum +end + + minetest.register_chatcommand("smstats", { description = "Get number of items sold", params = "", @@ -515,25 +542,36 @@ minetest.register_chatcommand("smstats", { if not smartshop.itemstats[name] then return false, "No stats on "..name end - sum = 0 - for i, k in pairs(smartshop.itemstats[name]) do - sum = sum + k - end + sum = smartshop.get_item_count(name) minetest.chat_send_player(plname, "Number of items: "..sum) if sum == 0 then return end - psum = 0 - for i, k in pairs(smartshop.itemprices[name]) do - psum = psum + k*smartshop.itemstats[name][i] - end - minetest.chat_send_player(plname, "Average price: "..string.format("%.3f",psum/sum)) + price = smartshop.get_item_price(name) + minetest.chat_send_player(plname, "Average price: "..string.format("%.3f",price)) return true -- local ok, e = xban.ban_player(plname, name, nil, reason) -- return ok, ok and ("Banned %s."):format(plname) or e end, }) +minetest.register_chatcommand("smreport", { + description = "Get number of items sold", + func = function(plname, params) + local file = io.open(minetest.get_worldpath().."/smartshop_report.txt", "w") + if not file then + return false, "could not write to file" + end + for i,k in pairs(smartshop.itemstats) do + local count = smartshop.get_item_count(i) + local price = smartshop.get_item_price(i) + file:write(i.." "..count.." "..string.format("%.3f", price).."\n") + end + file:close() + end, +}) + + minetest.register_lbm({ name = "smartshop:update", nodenames = {"smartshop:shop"}, -- cgit v1.2.3