From 34ce286360a4f1e412e889213bb7c9315dda6c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20P=C3=A9rez-Cerezo?= Date: Sun, 13 May 2018 13:04:24 +0200 Subject: added report on globalstep, add sample munin plugin --- Readme.txt | 7 ++- init.lua | 52 +++++++++++++-------- munin-grapher.pl | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 20 deletions(-) create mode 100755 munin-grapher.pl diff --git a/Readme.txt b/Readme.txt index 2ecdbfe..5c3d692 100644 --- a/Readme.txt +++ b/Readme.txt @@ -13,4 +13,9 @@ You can toogle it unlimited or limited if you have give or creative The "All" button will lime your stock to the inventory or and the giveline, its on as default. -It also works with pipeworks and aliveai \ No newline at end of file +It also works with pipeworks and aliveai + +Additions by gpcf: + - Mail support: Smartshops send mails out if stock is empty + - Statistics: Smartshops make statistics on prices and stock + - Smartshops only accept items they sell via pipeworks diff --git a/init.lua b/init.lua index c1b79ef..2c3fc8d 100644 --- a/init.lua +++ b/init.lua @@ -567,31 +567,45 @@ minetest.register_chatcommand("smstats", { end, }) +smartshop.report = function () + 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).." "..smartshop.get_shop_count(i).."\n") + end + file:close() +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).." "..smartshop.get_shop_count(i).."\n") - end - file:close() + smartshop.report() end, }) - - -minetest.register_lbm({ - name = "smartshop:update", - nodenames = {"smartshop:shop"}, - action = function(pos, node) - smartshop.update_info(pos) - end, -}) +local timer = 0 +minetest.register_globalstep(function(dtime) + timer = timer + dtime; + if timer >= 100 then + smartshop.report() + timer = 0 + end +end) + + +if false then -- This lbm is used to add pre-update smartshops to the price database. Activate with care! Warning: very slow. + minetest.register_lbm({ + name = "smartshop:update", + nodenames = {"smartshop:shop"}, + action = function(pos, node) + smartshop.update_info(pos) + end, + }) +end -- load itemstats local file = io.open(minetest.get_worldpath().."/smartshop_itemcounts.txt", "r") diff --git a/munin-grapher.pl b/munin-grapher.pl new file mode 100755 index 0000000..d70cc70 --- /dev/null +++ b/munin-grapher.pl @@ -0,0 +1,135 @@ +#!/usr/bin/perl + +# Sample script for monitoring smartshop prices using munin. + +# Copyright (C) 2018 Gabriel PĂ©rez-Cerezo, licensed under CC-0, like +# rest of the smartshop mod + +# adjust variable $file to the smartshop_report.txt file in your +# world, adjust %items to the items you want to have graphed. + + +use strict; +use warnings; +my $file = "/var/games/minetest-server/.minetest/worlds/world/smartshop_report.txt"; +my %items = ("ores" => {"gold" => ["default:gold_ingot", "Gold Ingot"], + "mese" => ["default:mese_crystal", "Mese Crystal"], + "diamond" => ["default:diamond", "Diamond"], + "steel" => ["default:steel_ingot", "Steel Ingot"], + "coal" => ["default:coal_lump", "Coal Lump"], + "copper" => ["default:copper_ingot", "Copper Ingot"], + "uranium" => ["technic:uranium_ingot", "Uranium Ingot"], + "chromium" => ["technic:chromium_ingot", "Chromium Ingot"], + "lead" => ["technic:lead_ingot", "Lead Ingot"], + "zinc" => ["technic:zinc_ingot", "Zinc Ingot"], + "tin" => ["default:tin_ingot", "Tin Ingot"], + "silver" => ["moreores:silver_ingot", "Silver Ingot"], + }, + "stones" => {"cobble" => ["default:cobble", "Cobblestone"], + "sandstone" => ["default:sandstone", "Sandstone"], + "desertstone" => ["default:desert_stone", "Desert Stone"], + "desertcobble" => ["default:desert_cobble", "Desert Stone"], + "obsidian" => ["default:obsidian", "Obsidian"], + "brick" => ["default:brick", "Brick"], + "marble" => ["technic:marble", "Marble"], + "graninte" => ["technic:granite", "Granite"], + "sand" => ["default:sand", "Sand"], + "gravel" => ["default:gravel", "Gravel"], + "dirt" => ["default:dirt", "Dirt"], + }, + "crops" => { + "carrot" => ["farming:carrot", "Carrot"], + "tomato" => ["farming:tomato", "Tomato"], + "melon" => ["farming:melon_slice", "Melon"], + "pumpkin" => ["farming:pumpkin_slice", "Pumpkin"], + "wheat" => ["farming:wheat", "Wheat"], + "rhubarb" => ["farming:rhubarb", "Rhubarb"], + "blueberries" => ["farming:blueberries", "Blueberries"], + "corn" => ["farming:corn", "Corn"], + "cucumber" => ["farming:cucumber", "Cucumber"], + "grapes" => ["farming:grapes", "Grapes"], + }, + ); + +sub print_legend { + my $cat = shift; + print "graph_category minetest\n"; + foreach (keys %{$items{$cat}}) { + print "$_.label $items{$cat}{$_}[1]\n"; + } + print "\n"; +} + +if ($_ = shift @ARGV and $_ eq "config") { +# print "config\n"; + ## initialize the graph + my $k; + foreach $k(keys %items) { + print "multigraph $k"."_supply\n"; + print "graph_title $k being sold\n"; + print "graph_vlabel Number of items for sale\n"; + print_legend $k; + print "multigraph $k"."_price\n"; + print "graph_title $k Prices\n"; + print "graph_vlabel Price in Minegeld\n"; + print_legend $k; + print "multigraph $k"."_shops\n"; + print "graph_title Number of shops selling $k\n"; + print "graph_vlabel Number of shops selling this item\n"; + print_legend $k; + } + exit 0; + +} + + + +my @report; + +#$file = "smartshop_report.txt"; + +open my $fh, "<", $file + or die "could not open $file: $!"; +chomp(@report = <$fh>); + +close $fh; + +sub vals { + my $cat = shift; + my $key = shift; + foreach (keys %{$items{$cat}}) { + my @s = @{$items{$cat}{$_}}; + print "$_.value $s[$key]\n"; + } + print "\n"; +} + +my @price; +foreach (keys %items) { + # print values for each category + my %cat = %{$items{$_}}; + foreach (keys %cat) { + my $ore = $cat{$_}[0]; + @price = grep(/$ore /, @report); + # @price[0] eq "" and + if (scalar @price == 0) { + $price[0] = "invalid 0 0 0"; + } + my @data = split(/ /, $price[0]); + shift @data; + my $pr = shift @data; + my $nu = shift @data; + my $f = shift @data; + # print $items{$_}[0]; + push @{$cat{$_}}, $pr, $nu, $f; + # print "$_ $items{$_}[2] $items{$_}[3]\n" + } + + print "multigraph $_"."_supply\n"; + vals $_,2; + print "multigraph $_"."_price\n"; + vals $_,3; + print "multigraph $_"."_shops\n"; + vals $_,4; +} + -- cgit v1.2.3