init.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. -- ===================================================================
  2. -- /init.lua
  3. -- Modular Politics (modpol) for Minetest
  4. -- TKTK Maybe make this just a quick ref file and locate MT files elsewhere?
  5. -- TKTK need to add player to orgs.instance with on_joinplayer
  6. -- ===================================================================
  7. -- Load modpol system
  8. dofile(minetest.get_modpath("modpol") .. "/modpol.lua")
  9. -- ===================================================================
  10. -- Modular Politics functions
  11. -- Overwriting default API functions with platform-specific ones
  12. -- ===================================================================
  13. -- ===================================================================
  14. -- Function: modpol.list_users(org)
  15. -- Overwrites function at /users.lua
  16. -- Params:
  17. -- if nil, lists instance members; if an org name, lists its members
  18. -- Output: a table with names of players currently in the game
  19. modpol.list_users = function(org)
  20. local users = {}
  21. if (org == nil) then -- no specified org; all players
  22. for _,player in ipairs(minetest.get_connected_players()) do
  23. local name = player:get_player_name()
  24. table.insert(users,name)
  25. end
  26. else -- if an org is specified
  27. if (modpol.orgs[org] ~= nil) then -- org exists
  28. users = modpol.orgs[org]["members"]
  29. end
  30. end
  31. return members
  32. end
  33. -- ===================================================================
  34. -- Function: modpol.binary_poll_user(user, question)
  35. -- Overwrites function at /interactions.lua
  36. -- presents a yes/no/abstain poll to a user, returns answer
  37. modpol.binary_poll_user = function(user, question)
  38. -- set up formspec
  39. local text = "Poll: " .. question
  40. local formspec = {
  41. "formspec_version[4]",
  42. "size[5,3]",
  43. "label[0.375,0.5;", minetest.formspec_escape(text), "]",
  44. "button[1,1.5;1,0.8;yes;Yes]",
  45. "button[2,1.5;1,0.8;no;No]",
  46. "button[3,1.5;1,0.8;abstain;Abstain]"
  47. --TKTK can we enable text wrapping?
  48. }
  49. local formspec_string = table.concat(formspec, "")
  50. -- present to player
  51. minetest.show_formspec(user, "modpol:binary_poll", formspec_string)
  52. end
  53. -- ===================================================================
  54. -- Minetest commands
  55. -- ===================================================================
  56. local chat_table -- MT chat command definitions table
  57. local regchat -- Chat-command registration function
  58. regchat = minetest.register_chatcommand
  59. -- ===================================================================
  60. -- /addorg /add_org
  61. -- This code defines a chat command which creates a new
  62. -- "org". Presently, the command makes the user the sole member of the
  63. -- "org".
  64. chat_table = {
  65. privs = {} ,
  66. func = function (user, param)
  67. local result = modpol.add_org (param, { user })
  68. return true, result
  69. end
  70. }
  71. regchat ("addorg" , chat_table)
  72. regchat ("add_org" , chat_table)
  73. -- ===================================================================
  74. -- /listorg /listorgs /list_org /list_orgs
  75. -- In Minetest mode, this code defines a chat command which lists the
  76. -- existing "orgs".
  77. -- The list shows one "org" per line in the following format:
  78. -- org_name (member, member, ...)
  79. chat_table = {
  80. privs = {} ,
  81. func = function (user, param)
  82. return true, "Orgs:\n" .. modpol.list_orgs()
  83. end
  84. }
  85. regchat ("listorg" , chat_table)
  86. regchat ("listorgs" , chat_table)
  87. regchat ("list_org" , chat_table)
  88. regchat ("list_orgs" , chat_table)
  89. -- ===================================================================
  90. -- /listplayers
  91. minetest.register_chatcommand(
  92. "listplayers", {
  93. privs = {},
  94. func = function(user)
  95. local result = table.concat(modpol.list_users(),", ")
  96. return true, "All players: " .. result
  97. end,
  98. })
  99. -- ===================================================================
  100. -- /joinorg
  101. minetest.register_chatcommand(
  102. "joinorg", {
  103. privs = {},
  104. func = function(user, param)
  105. local result = modpol.add_member(param, user)
  106. return true, result
  107. end,
  108. })
  109. -- ===================================================================
  110. -- /pollself [question]
  111. -- asks the user a question specified in param
  112. minetest.register_chatcommand(
  113. "pollself", {
  114. privs = {},
  115. func = function(user, param)
  116. modpol.binary_poll_user(user, param)
  117. return true, result
  118. end,
  119. })
  120. -- ===================================================================
  121. -- Minetest events
  122. -- ===================================================================
  123. -- ===================================================================
  124. -- Receiving fields
  125. minetest.register_on_player_receive_fields(function (player, formname, fields)
  126. -- modpol:poll
  127. if formname == "modpol:binary_poll" then
  128. local pname = player:get_player_name()
  129. local vote = nil
  130. if fields.yes then vote = fields.yes
  131. elseif fields.no then vote = fields.no
  132. elseif fields.abstain then vote = fields.abstain
  133. end
  134. if vote then
  135. minetest.chat_send_all(pname .. " voted " .. vote)
  136. end
  137. minetest.close_formspec(pname, formname)
  138. return vote
  139. else -- if the form is not a recognized name
  140. return
  141. end
  142. end)
  143. -- ===================================================================
  144. -- End of file.