barter.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. if not minetest.global_exists("barter") then barter = {} end
  2. barter.chest = barter.chest or {}
  3. barter.reset_delay = 60*20 -- Number of seconds to reset after pressing start.
  4. barter.long_delay = 60*60*3
  5. barter.enable_logging = false
  6. -- Localize for performance.
  7. local math_floor = math.floor
  8. function barter.report(msg)
  9. if barter.enable_logging then
  10. minetest.chat_send_player("MustTest", msg)
  11. end
  12. end
  13. barter.chest.formspec = {
  14. main = "size[8,9]"..
  15. default.gui_bg ..
  16. default.gui_bg_img ..
  17. default.gui_slots ..
  18. "list[context;pl1;0,0;3,4;]"..
  19. "list[context;pl2;5,0;3,4;]"..
  20. "list[current_player;main;0,5;8,4;]",
  21. pl1 = {
  22. start = "button[3,1;1,1;pl1_start;Start]",
  23. player = function(name) return "label[3,0;" .. rename.gpn(name) .. "]" end,
  24. accept1 = "button[3,1;1,1;pl1_accept1;Confirm]"..
  25. "button[3,2;1,1;pl1_cancel;Cancel]",
  26. accept2 = "button[3,1;1,1;pl1_accept2;Exchange]"..
  27. "button[3,2;1,1;pl1_cancel;Cancel]",
  28. },
  29. pl2 = {
  30. start = "button[4,1;1,1;pl2_start;Start]",
  31. player = function(name) return "label[4,0;" .. rename.gpn(name) .. "]" end,
  32. accept1 = "button[4,1;1,1;pl2_accept1;Confirm]"..
  33. "button[4,2;1,1;pl2_cancel;Cancel]",
  34. accept2 = "button[4,1;1,1;pl2_accept2;Exchange]"..
  35. "button[4,2;1,1;pl2_cancel;Cancel]",
  36. },
  37. }
  38. barter.chest.check_privilege = function(listname,playername,meta)
  39. if listname == "pl1" then
  40. if playername ~= meta:get_string("pl1") then
  41. return false
  42. elseif meta:get_int("pl1step") ~= 1 then
  43. return false
  44. end
  45. end
  46. if listname == "pl2" then
  47. if playername ~= meta:get_string("pl2") then
  48. return false
  49. elseif meta:get_int("pl2step") ~= 1 then
  50. return false
  51. end
  52. end
  53. return true
  54. end
  55. barter.chest.update_formspec = function(meta)
  56. local formspec = barter.chest.formspec.main
  57. local pl_formspec = function(n)
  58. if meta:get_int(n.."step")==0 then
  59. formspec = formspec .. barter.chest.formspec[n].start
  60. else
  61. formspec = formspec .. barter.chest.formspec[n].player(meta:get_string(n))
  62. if meta:get_int(n.."step") == 1 then
  63. formspec = formspec .. barter.chest.formspec[n].accept1
  64. elseif meta:get_int(n.."step") == 2 then
  65. formspec = formspec .. barter.chest.formspec[n].accept2
  66. end
  67. end
  68. end
  69. pl_formspec("pl1")
  70. pl_formspec("pl2")
  71. meta:set_string("formspec", formspec)
  72. end
  73. barter.chest.give_inventory = function(inv, list, playername)
  74. barter.report("Giving list " .. list .. " to <" .. playername .. ">.")
  75. player = minetest.get_player_by_name(playername)
  76. if inv and player then
  77. local pinv = player:get_inventory()
  78. if pinv then
  79. barter.report("Moving items!")
  80. local from_list = inv:get_list(list)
  81. for k, v in ipairs(from_list) do
  82. pinv:add_item("main", v)
  83. inv:remove_item(list, v)
  84. end
  85. else
  86. barter.report("Couldn't get player inv!")
  87. end
  88. else
  89. barter.report("Missing inv or player!")
  90. end
  91. end
  92. barter.chest.cancel = function(meta)
  93. barter.chest.give_inventory(meta:get_inventory(),"pl1",meta:get_string("pl1"))
  94. barter.chest.give_inventory(meta:get_inventory(),"pl2",meta:get_string("pl2"))
  95. meta:set_string("pl1","")
  96. meta:set_string("pl2","")
  97. meta:set_int("pl1step",0)
  98. meta:set_int("pl2step",0)
  99. end
  100. -- This function expects to be called from minetest.after().
  101. -- Added by MustTest, to prevent players from locking these tables up so other players can't use them.
  102. barter.chest.hard_reset = function(pos, side, name)
  103. local node = minetest.get_node(pos)
  104. if node.name == "barter_table:barter" then
  105. local meta = minetest.get_meta(pos)
  106. if meta and meta:get_string(side) == name then
  107. barter.chest.give_inventory(meta:get_inventory(), side, meta:get_string(side))
  108. meta:set_string(side, "")
  109. meta:set_int(side .. "step", 0)
  110. barter.chest.update_formspec(meta)
  111. end
  112. end
  113. end
  114. -- Also need an hourly (or so) reset function, in case the server crashes before minetest.after() is run.
  115. barter.chest.reset2 = function(pos, elapsed)
  116. local node = minetest.get_node(pos)
  117. if node.name == "barter_table:barter" then
  118. local meta = minetest.get_meta(pos)
  119. barter.chest.give_inventory(meta:get_inventory(), "pl1", meta:get_string("pl1"))
  120. barter.chest.give_inventory(meta:get_inventory(), "pl2", meta:get_string("pl2"))
  121. meta:set_string("pl1", "")
  122. meta:set_string("pl2", "")
  123. meta:set_int("pl1step", 0)
  124. meta:set_int("pl2step", 0)
  125. barter.chest.update_formspec(meta)
  126. return true
  127. end
  128. end
  129. barter.chest.exchange = function(meta)
  130. barter.chest.give_inventory(meta:get_inventory(), "pl1", meta:get_string("pl2"))
  131. barter.chest.give_inventory(meta:get_inventory(), "pl2", meta:get_string("pl1"))
  132. meta:set_string("pl1","")
  133. meta:set_string("pl2","")
  134. meta:set_int("pl1step",0)
  135. meta:set_int("pl2step",0)
  136. end
  137. minetest.register_node("barter_table:barter", {
  138. drawtype = "nodebox",
  139. description = "Barter Table",
  140. paramtype = "light",
  141. paramtype2 = "facedir",
  142. tiles = {
  143. "barter_top.png",
  144. "barter_base.png",
  145. "barter_side.png",
  146. },
  147. inventory_image = "barter_top.png",
  148. node_box = {
  149. type = "fixed",
  150. fixed = {
  151. {-0.500000,0.312500,-0.500000,0.500000,0.500000,0.500000},
  152. {-0.437500,-0.500000,-0.437500,-0.250000,0.500000,-0.250000},
  153. {-0.437500,-0.500000,0.250000,-0.250000,0.500000,0.437500},
  154. {0.250000,-0.500000,-0.437500,0.437500,0.500000,-0.250000},
  155. {0.250000,-0.500000,0.250000,0.437500,0.500000,0.447500},
  156. },
  157. },
  158. groups = utility.dig_groups("furniture"),
  159. sounds = default.node_sound_wood_defaults(),
  160. on_timer = barter.chest.reset2,
  161. on_construct = function(pos)
  162. local meta = minetest.get_meta(pos)
  163. meta:set_string("infotext", "Trade/Barter Table")
  164. meta:set_string("pl1","")
  165. meta:set_string("pl2","")
  166. barter.chest.update_formspec(meta)
  167. local inv = meta:get_inventory()
  168. inv:set_size("pl1", 3*4)
  169. inv:set_size("pl2", 3*4)
  170. local timer = minetest.get_node_timer(pos)
  171. timer:start(barter.long_delay)
  172. end,
  173. on_receive_fields = function(pos, formname, fields, sender)
  174. local meta = minetest.get_meta(pos)
  175. local pl_receive_fields = function(n)
  176. if fields[n.."_start"] and meta:get_string(n) == "" then
  177. meta:set_string(n,sender:get_player_name())
  178. minetest.chat_send_player(sender:get_player_name(),
  179. "# Server: This barter table will reset in " ..
  180. barter.reset_delay/60 .. " minutes. Please complete your transaction before then.")
  181. minetest.after(barter.reset_delay, barter.chest.hard_reset, pos, n, sender:get_player_name())
  182. -- Make sure the janitor timer is running.
  183. local timer = minetest.get_node_timer(pos)
  184. if timer then
  185. if not timer:is_started() then
  186. timer:start(barter.long_delay)
  187. end
  188. -- Alert players how much time is left.
  189. local elasped = timer:get_elapsed()
  190. minetest.chat_send_player(sender:get_player_name(), "# Server: The periodic reset for this table is scheduled to occure in " .. math_floor((barter.long_delay - elasped)/60) .. " minutes.")
  191. minetest.chat_send_player(sender:get_player_name(), "# Server: WARNING: make sure you have enough space in your inventory to receive bartered goods.")
  192. end
  193. end
  194. if meta:get_string(n) == "" then
  195. meta:set_int(n.."step",0)
  196. elseif meta:get_int(n.."step")==0 then
  197. meta:set_int(n.."step",1)
  198. end
  199. if sender:get_player_name() == meta:get_string(n) then
  200. if meta:get_int(n.."step")==1 and fields[n.."_accept1"] then
  201. meta:set_int(n.."step",2)
  202. end
  203. if meta:get_int(n.."step")==2 and fields[n.."_accept2"] then
  204. meta:set_int(n.."step",3)
  205. if n == "pl1" and meta:get_int("pl2step") == 3 then barter.chest.exchange(meta) end
  206. if n == "pl2" and meta:get_int("pl1step") == 3 then barter.chest.exchange(meta) end
  207. end
  208. if fields[n.."_cancel"] then barter.chest.cancel(meta) end
  209. end
  210. end
  211. pl_receive_fields("pl1")
  212. pl_receive_fields("pl2")
  213. -- End
  214. barter.chest.update_formspec(meta)
  215. end,
  216. allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  217. local meta = minetest.get_meta(pos)
  218. if not barter.chest.check_privilege(from_list,player:get_player_name(),meta) then return 0 end
  219. if not barter.chest.check_privilege(to_list,player:get_player_name(),meta) then return 0 end
  220. return count
  221. end,
  222. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  223. local meta = minetest.get_meta(pos)
  224. if not barter.chest.check_privilege(listname,player:get_player_name(),meta) then return 0 end
  225. return stack:get_count()
  226. end,
  227. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  228. local meta = minetest.get_meta(pos)
  229. if not barter.chest.check_privilege(listname,player:get_player_name(),meta) then return 0 end
  230. return stack:get_count()
  231. end,
  232. })