commit 8330bd0244cffa7506425b670c12ebf88b44adf8 Author: Brandon Date: Fri Feb 14 22:46:44 2014 -0600 first commit diff --git a/config.lua b/config.lua new file mode 100644 index 0000000..fc0e84c --- /dev/null +++ b/config.lua @@ -0,0 +1,37 @@ +--- +--money 2.00 +--Copyright (C) 2012 Bad_Command +-- +--This library is free software; you can redistribute it and/or +--modify it under the terms of the GNU Lesser General Public +--License as published by the Free Software Foundation; either +--version 2.1 of the License, or (at your option) any later version. +-- +--This program is distributed in the hope that it will be useful, +--but WITHOUT ANY WARRANTY; without even the implied warranty of +--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +--GNU General Public License for more details. +-- +--You should have received a copy of the GNU Lesser General Public +--License along with this library; if not, write to the Free Software +--Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +--- + +money.initial_amount = 2000 +money.currency_name = "cr" + + +money.convert_items = { + gold = { item = "default:gold_ingot", dig_block="default:stone_with_gold", desc='Gold', amount=75, minval=25 }, + silver = { item = "moreores:silver_ingot", dig_block="moreores:mineral_silver", desc='Silver', amount = 27, minval=7} + } + +money.stats = money.load_stats() +if ( money.stats == false ) then + money.stats = { } + + for key,val in pairs(money.convert_items) do + minetest.log("action","Initial Convert Stats Setup for "..money.convert_items[key].desc) + money.stats[key] = { running_time = 0, running_dug = 0, running_converted = 0, running_value = money.convert_items[key].amount } + end +end \ No newline at end of file diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..45ac7f8 --- /dev/null +++ b/init.lua @@ -0,0 +1,293 @@ +--- +--money 2.00 +--Copyright (C) 2012 Bad_Command +--Copyright (C) 2012 kotolegokot +-- +--This library is free software; you can redistribute it and/or +--modify it under the terms of the GNU Lesser General Public +--License as published by the Free Software Foundation; either +--version 2.1 of the License, or (at your option) any later version. +-- +--This program is distributed in the hope that it will be useful, +--but WITHOUT ANY WARRANTY; without even the implied warranty of +--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +--GNU General Public License for more details. +-- +--You should have received a copy of the GNU Lesser General Public +--License along with this library; if not, write to the Free Software +--Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +--- + +money = {} +money.version = 2.00 + +stat_file = minetest.get_worldpath() .. "/convert_stats" + +dofile(minetest.get_modpath("money2") .. "/lockedsign.lua") + +money.set = function(name, value) + local output = io.open(minetest.get_worldpath() .. "/money_" .. name .. ".txt", "w") + output:write(value) + io.close(output) +end + +money.get = function(name) + local input = io.open(minetest.get_worldpath() .. "/money_" .. name .. ".txt", "r") + if not input then + return nil + end + credit = input:read("*n") + io.close(input) + return credit +end + +money.has_credit = function(name) + local privs = minetest.get_player_privs(name) + if ( privs == nil or not privs["money"] ) then + return false + end + return true +end + +money.add = function(name, amount) + if ( amount < 0 ) then + return "must specify positive amount" + end + + local credit = money.get(name) + if ( credit == nil ) then + return name .. " does not have a credit account." + end + + money.set(name, credit + amount) + return nil +end + +money.dec = function(name, amount) + if ( amount < 0 ) then + return "must specify positive amount" + end + + local credit = money.get(name) + if ( credit == nil ) then + return name .. " does not have a credit account." + end + if ( credit < amount ) then + return name .. " does not have enough credit." + end + money.set(name, credit - amount) + return nil +end + +money.transfer = function(from, to, amount) + if ( from == to ) then + return + end + + amount = math.ceil(amount) + + if ( not money.has_credit(from) ) then + return from .. " does not have a credit account" + end + if ( not money.has_credit(to) ) then + return to .. " does not have a credit account" + end + + if ( amount < 0 ) then + return "negative transfers not allowed" + end + local from_credit = money.get(from) + if ( from_credit == nil or from_credit < amount ) then + return "not enough credit" + end + local to_credit = money.get(to) + if ( to_credit == nil ) then + return to .. " does not have a credit account." + end + money.set(from, from_credit - amount) + money.set(to, to_credit + amount) + minetest.log('action',"Credit transfer of "..tostring(amount).." from "..from.." to "..to) + return nil +end + +minetest.register_on_joinplayer(function(player) + name = player:get_player_name() + if not money.get(name) then + money.set(name, tostring(money.initial_amount)) + end +end) + +minetest.register_privilege("money", "Can use /money [pay ] command") +minetest.register_privilege("money_admin", { + description = "Can use /money [ | pay/set/add/dec ] command", + give_to_singleplayer = false, +}) + +minetest.register_chatcommand("money", { + privs = {money=true}, + params = "[ | pay/set/add/dec ]", + description = "Operations with credit", + func = function(name, param) + if param == "" then + minetest.chat_send_player(name, money.get(name) .. money.currency_name) + else + local param1, reciever, amount = string.match(param, "([^ ]+) ([^ ]+) (.+)") + if not reciever and not amount then + if minetest.get_player_privs(name)["money_admin"] then + if not money.get(param) then + minetest.chat_send_player(name, "money: Player named \"" .. param .. "\" does not exist or does not have an account.") + return true + end + minetest.chat_send_player(name, money.get(param) .. money.currency_name) + return true + else + minetest.chat_send_player(name, "money: You don't have permission to run this command (missing privileges: money_admin)") + end + end + if (param1 ~= "pay") and (param1 ~= "set") and (param1 ~= "add") and (param1 ~= "dec") or not reciever or not amount then + minetest.chat_send_player(name, "money: Invalid parameters (see /help money)") + return true + elseif not money.get(reciever) then + minetest.chat_send_player(name, "money: Player named \"" .. reciever .. "\" does not exist or does not have account.") + return true + elseif not tonumber(amount) then + minetest.chat_send_player(name, "money: amount .. " .. "is not a number.") + return true + elseif tonumber(amount) < 0 then + minetest.chat_send_player(name, "money: The amount must be greater than 0.") + return true + end + amount = tonumber(amount) + if param1 == "pay" then + local err = money.transfer(name ,reciever, amount) + if ( err ~= nil ) then + minetest.chat_send_player(name, "money: Error: "..err..".") + else + minetest.chat_send_player(name, "money: You paid " .. reciever .. " " .. amount .. money.currency_name) + minetest.chat_send_player(reciever, "money: " .. name .. " paid you " .. amount .. money.currency_name) + end + elseif minetest.get_player_privs(name)["money_admin"] then + if param1 == "add" then + local err = money.add(reciever, amount) + if ( err ~= nil ) then + minetest.chat_send_player(name, "money: Error"..err..".") + end + elseif param1 == "dec" then + local err = money.dec(reciever, amount) + if ( err ~= nil ) then + minetest.chat_send_player(name, "money: Error"..err..".") + end + elseif param1 == "set" then + local err = money.set(reciever, amount) + if ( err ~= nil ) then + minetest.chat_send_player(name, "money: Error"..err..".") + end + end + else + minetest.chat_send_player(name, "money: You don't have permission to run this command (missing privileges: money_admin)") + end + end + end, +}) + + +minetest.register_chatcommand("convertval",{ + privs = {money=true}, + params = "none", + description = "Shows the current value of convertable items", + func = function(name, param) + for k,v in pairs(money.stats) do + minetest.chat_send_player(name,money.convert_items[k].desc..": "..tostring(money.stats[k].running_value)) + end + end +}) + +function money.dignode(pos, node, player) + for k,v in pairs(money.convert_items) do + if ( node.name == money.convert_items[k].dig_block ) then + money.stats[k].running_dug = money.stats[k].running_dug + 1 + end + end +end + +function money.calcConvertValues() + for k,v in pairs(money.stats) do + money.stats[k].running_time = money.stats[k].running_time + 1 + money.stats[k].running_value = math.floor(money.convert_items[k].amount + ( (money.stats[k].running_time / 960) + (money.stats[k].running_dug / 22) - ( money.stats[k].running_converted / 11 ) )) + + if ( money.stats[k].running_value < money.convert_items[k].minval ) then + money.stats[k].running_val = money.convert_items[k].minval + end + + minetest.log("action","Calculated "..money.convert_items[k].desc.." value at "..tostring(money.stats[k].running_value)) + end + money.save_stats() +minetest.after(60,money.calcConvertValues) +end + +function money.save_stats() +minetest.log("action","Saving convert stats") + local f = io.open(stat_file, "w") + f:write(minetest.serialize(money.stats)) + f:close() +end + +function money.load_stats() +minetest.log("action","Loading convert stats") +local f = io.open(stat_file, "r") + if f==nil then + minetest.log("action","file not found") + return false + end + local t = f:read("*all") + f:close() + if t=="" or t==nil then + minetest.log("action","File blank") + return false + end + return minetest.deserialize(t) +end + +minetest.register_on_dignode( money.dignode ) + +minetest.after(60, money.calcConvertValues) + +dofile(minetest.get_modpath("money2") .. "/config.lua") + +local convert_options = "<" +for k,v in pairs(money.convert_items) do + convert_options = convert_options..k.."," +end +convert_options = convert_options..">" + +minetest.register_chatcommand("convert",{ + privs = {money=true}, + params = convert_options, + description="Converts certain ores to credits", + func = function(name, param) + -- check the parameters + if ( money.convert_items[param] ~= nil ) then + local item = money.convert_items[param].item + local amount = money.stats[param].running_value + local totalAmount = 0 + local totalItems = 0 + -- look through their inventory for the item they chose + local player = minetest.get_player_by_name(name) + local inventory = player:get_inventory() + + for i=1,inventory:get_size("main") do + --player_inv:set_stack("main", i, nil) + local inv_item = inventory:get_stack("main",i) + if ( inv_item:get_name() == item ) then + local add_amount = inv_item:get_count() * amount + money.add(name,add_amount) + totalAmount = totalAmount + add_amount + totalItems = totalItems + inv_item:get_count() + inventory:set_stack("main",i,nil) + end + end + money.stats[param].running_converted = money.stats[param].running_converted + totalItems + minetest.chat_send_player(name,"You converted "..tostring(totalItems).." "..param.." into "..tostring(totalAmount)..money.currency_name) + end + end +}) diff --git a/lockedsign.lua b/lockedsign.lua new file mode 100644 index 0000000..bba2bc9 --- /dev/null +++ b/lockedsign.lua @@ -0,0 +1,81 @@ +--- +--money 2.00 +--Copyright (C) 2012 kotolegokot +--Copyright (C) 2012 Bad_Command +-- +--This library is free software; you can redistribute it and/or +--modify it under the terms of the GNU Lesser General Public +--License as published by the Free Software Foundation; either +--version 2.1 of the License, or (at your option) any later version. +-- +--This program is distributed in the hope that it will be useful, +--but WITHOUT ANY WARRANTY; without even the implied warranty of +--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +--GNU General Public License for more details. +-- +--You should have received a copy of the GNU Lesser General Public +--License along with this library; if not, write to the Free Software +--Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +--- + +minetest.register_on_punchnode(function(pos, node, puncher) + bottom_pos = {x=pos.x, y=pos.y - 1, z=pos.z} + bottom_node = minetest.env:get_node(bottom_pos) + if (node.name == "locked_sign:sign_wall_locked") and (bottom_node.name == "default:chest_locked") and + minetest.env:get_meta(pos):get_string("owner") == minetest.env:get_meta(bottom_pos):get_string("owner") then + local sign_text = minetest.env:get_meta(pos):get_string("text") + local shop_name, shop_type, nodename, amount, cost = string.match(sign_text, "([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+)") + local owner_name = minetest.env:get_meta(pos):get_string("owner") + local puncher_name = puncher:get_player_name() + if (shop_type ~= "B") and (shop_type ~= "S") or (not minetest.registered_items[nodename]) or (not tonumber(amount)) or + (not tonumber(cost)) then + return true + end + + if ( not money.has_credit(owner_name) ) then + minetest.chat_send_player(puncher_name, "Owner does not have a credit account.") + end + if ( not money.has_credit(puncher_name) ) then + minetest.chat_send_player(puncher_name, "You do not have a credit account.") + end + + + local chest_inv = minetest.env:get_meta({x=pos.x, y=pos.y - 1, z = pos.z}):get_inventory() + local puncher_inv = puncher:get_inventory() + --BUY + if shop_type == "B" then + if not chest_inv:contains_item("main", nodename .. " " .. amount) then + minetest.chat_send_player(puncher_name, "In the chest is not enough goods.") + return true + elseif not puncher_inv:room_for_item("main", nodename .. " " .. amount) then + minetest.chat_send_player(puncher_name, "In your inventory is not enough space.") + return true + elseif money.get(puncher_name) - cost < 0 then + minetest.chat_send_player(puncher_name, "You do not have enough money.") + return true + end + money.set(puncher_name, money.get(puncher_name) - cost) + money.set(owner_name, money.get(owner_name) + cost) + puncher_inv:add_item("main", nodename .. " " .. amount) + chest_inv:remove_item("main", nodename .. " " .. amount) + minetest.chat_send_player(puncher_name, "You bought " .. amount .. " " .. nodename .. " at a price of " .. cost .. money.currency_name .. ".") + --SELL + elseif shop_type == "S" then + if not puncher_inv:contains_item("main", nodename .. " " .. amount) then + minetest.chat_send_player(puncher_name, "You do not have enough product.") + return true + elseif not chest_inv:room_for_item("main", nodename .. " " .. amount) then + minetest.chat_send_player(puncher_name, "In the chest is not enough space.") + return true + elseif money.get(owner_name) - cost < 0 then + minetest.chat_send_player(puncher_name, "The buyer is not enough money.") + return true + end + money.set(puncher:get_player_name(), money.get(puncher:get_player_name()) + cost) + money.set(owner_name, money.get(owner_name) - cost) + puncher_inv:remove_item("main", nodename .. " " .. amount) + chest_inv:add_item("main", nodename .. " " .. amount) + minetest.chat_send_player(puncher_name, "You sold " .. amount .. " " .. nodename .. " at a price of " .. cost .. money.currency_name .. ".") + end + end +end)