You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.9KB

  1. ---
  2. --money 2.00
  3. --Copyright (C) 2012 kotolegokot
  4. --Copyright (C) 2012 Bad_Command
  5. --
  6. --This library is free software; you can redistribute it and/or
  7. --modify it under the terms of the GNU Lesser General Public
  8. --License as published by the Free Software Foundation; either
  9. --version 2.1 of the License, or (at your option) any later version.
  10. --
  11. --This program is distributed in the hope that it will be useful,
  12. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. --GNU General Public License for more details.
  15. --
  16. --You should have received a copy of the GNU Lesser General Public
  17. --License along with this library; if not, write to the Free Software
  18. --Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. ---
  20. minetest.register_on_punchnode(function(pos, node, puncher)
  21. bottom_pos = {x=pos.x, y=pos.y - 1, z=pos.z}
  22. bottom_node = minetest.env:get_node(bottom_pos)
  23. if (node.name == "locked_sign:sign_wall_locked") and (bottom_node.name == "default:chest_locked") and
  24. minetest.env:get_meta(pos):get_string("owner") == minetest.env:get_meta(bottom_pos):get_string("owner") then
  25. local sign_text = minetest.env:get_meta(pos):get_string("text")
  26. local shop_name, shop_type, nodename, amount, cost = string.match(sign_text, "([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+) ([^ ]+)")
  27. local owner_name = minetest.env:get_meta(pos):get_string("owner")
  28. local puncher_name = puncher:get_player_name()
  29. if (shop_type ~= "B") and (shop_type ~= "S") or (not minetest.registered_items[nodename]) or (not tonumber(amount)) or
  30. (not tonumber(cost)) then
  31. return true
  32. end
  33. if ( not money.has_credit(owner_name) ) then
  34. minetest.chat_send_player(puncher_name, "Owner does not have a credit account.")
  35. end
  36. if ( not money.has_credit(puncher_name) ) then
  37. minetest.chat_send_player(puncher_name, "You do not have a credit account.")
  38. end
  39. local chest_inv = minetest.env:get_meta({x=pos.x, y=pos.y - 1, z = pos.z}):get_inventory()
  40. local puncher_inv = puncher:get_inventory()
  41. --BUY
  42. if shop_type == "B" then
  43. if not chest_inv:contains_item("main", nodename .. " " .. amount) then
  44. minetest.chat_send_player(puncher_name, "In the chest is not enough goods.")
  45. return true
  46. elseif not puncher_inv:room_for_item("main", nodename .. " " .. amount) then
  47. minetest.chat_send_player(puncher_name, "In your inventory is not enough space.")
  48. return true
  49. elseif money.get(puncher_name) - cost < 0 then
  50. minetest.chat_send_player(puncher_name, "You do not have enough money.")
  51. return true
  52. end
  53. money.set(puncher_name, money.get(puncher_name) - cost)
  54. money.set(owner_name, money.get(owner_name) + cost)
  55. puncher_inv:add_item("main", nodename .. " " .. amount)
  56. chest_inv:remove_item("main", nodename .. " " .. amount)
  57. minetest.chat_send_player(puncher_name, "You bought " .. amount .. " " .. nodename .. " at a price of " .. cost .. money.currency_name .. ".")
  58. --SELL
  59. elseif shop_type == "S" then
  60. if not puncher_inv:contains_item("main", nodename .. " " .. amount) then
  61. minetest.chat_send_player(puncher_name, "You do not have enough product.")
  62. return true
  63. elseif not chest_inv:room_for_item("main", nodename .. " " .. amount) then
  64. minetest.chat_send_player(puncher_name, "In the chest is not enough space.")
  65. return true
  66. elseif money.get(owner_name) - cost < 0 then
  67. minetest.chat_send_player(puncher_name, "The buyer is not enough money.")
  68. return true
  69. end
  70. money.set(puncher:get_player_name(), money.get(puncher:get_player_name()) + cost)
  71. money.set(owner_name, money.get(owner_name) - cost)
  72. puncher_inv:remove_item("main", nodename .. " " .. amount)
  73. chest_inv:add_item("main", nodename .. " " .. amount)
  74. minetest.chat_send_player(puncher_name, "You sold " .. amount .. " " .. nodename .. " at a price of " .. cost .. money.currency_name .. ".")
  75. end
  76. end
  77. end)