interactions.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. -- INTERACTIONS.LUA (for Minetest)
  2. -- CONTEXTUAL STUFF
  3. -- ================
  4. -- First, set up contexts to enable passing across formspecs
  5. -- https://rubenwardy.com/minetest_modding_book/en/players/formspecs.html#contexts
  6. local _contexts = {}
  7. local function get_context(name)
  8. local context = _contexts[name] or {}
  9. _contexts[name] = context
  10. return context
  11. end
  12. minetest.register_on_leaveplayer(function(player)
  13. _contexts[player:get_player_name()] = nil
  14. end)
  15. -- table of formspec field responses
  16. local formspec_fields = {}
  17. -- UTILITIES
  18. -- =========
  19. -- Function: formspec_list
  20. -- for use generating option lists in formspecs from tables
  21. -- input: table of strings
  22. -- output: a formspec-ready list of the strings
  23. local function formspec_list(array)
  24. local escaped = {}
  25. if not array then
  26. return ""
  27. end
  28. for i = 1, #array do
  29. escaped[i] = minetest.formspec_escape(array[i])
  30. end
  31. return table.concat(escaped,",")
  32. end
  33. -- DASHBOARDS
  34. -- ==========
  35. -- Function: modpol.interactions.dashboard(user)
  36. -- Params: user (string)
  37. -- Q: Should this return a menu of commands relevant to the specific user?
  38. -- Output: Displays a menu of commands to the user
  39. -- TODO currently a manually curated list---needs major improvement
  40. function modpol.interactions.dashboard(user)
  41. -- prepare data
  42. -- to add: nested orgs map
  43. local all_orgs = modpol.orgs.list_all()
  44. local user_orgs = modpol.orgs.user_orgs(user)
  45. local all_users = modpol.list_users()
  46. -- set up formspec
  47. local formspec = {
  48. "formspec_version[4]",
  49. "size[10,8]",
  50. "label[0.5,0.5;M O D U L A R P O L I T I C S]",
  51. "label[0.5,2;All orgs:]",
  52. "dropdown[2,1.5;5,0.8;all_orgs;"..formspec_list(all_orgs)..";;]",
  53. "label[0.5,3;Your orgs:]",
  54. "dropdown[2,2.5;5,0.8;user_orgs;"..formspec_list(user_orgs)..";;]",
  55. "label[0.5,4;All users:]",
  56. "dropdown[2,3.5;5,0.8;all_users;"..formspec_list(all_users)..";;]",
  57. "label[0.5,5;Processes:]",
  58. "dropdown[2,4.5;5,0.8;processes;TBA;;]",
  59. "button[0.5,7;1,0.8;test_poll;Test poll]",
  60. "button[2,7;1,0.8;add_org;Add org]",
  61. "button[3.5,7;1.5,0.8;remove_org;Remove org]",
  62. "button_exit[8.5,7;1,0.8;close;Close]",
  63. }
  64. local formspec_string = table.concat(formspec, "")
  65. -- present to player
  66. minetest.show_formspec(user, "modpol:dashboard", formspec_string)
  67. end
  68. -- receive input
  69. minetest.register_on_player_receive_fields(function (player, formname, fields)
  70. if formname == "modpol:dashboard" then
  71. local pname = player:get_player_name()
  72. if nil then
  73. -- buttons first
  74. elseif fields.test_poll then
  75. modpol.interactions.binary_poll_org(pname, 1, "Poll question (yes/no):")
  76. elseif fields.add_org then
  77. modpol.interactions.add_org(pname, 1)
  78. elseif fields.remove_org then
  79. modpol.interactions.remove_org(pname)
  80. -- dropdowns need to go last
  81. elseif fields.close then
  82. minetest.close_formspec(pname, formname)
  83. elseif fields.all_orgs or fields.user_orgs then
  84. local org_name = fields.all_orgs or fields.user_orgs
  85. modpol.interactions.org_dashboard(pname, org_name)
  86. end
  87. end
  88. end)
  89. -- Function: modpol.interactions.org_dashboard
  90. -- Params: user (string), org_name (string)
  91. -- Output: Displays a menu of org-specific commands to the user
  92. function modpol.interactions.org_dashboard(user, org_name)
  93. -- prepare data
  94. local org = modpol.orgs.get_org(org_name)
  95. if not org then return nil end
  96. local is_member = org:has_member(user)
  97. local membership_toggle = function()
  98. local toggle_code = ""
  99. if is_member then
  100. toggle_code = toggle_code
  101. ..minetest.formspec_escape("leave")..";"
  102. ..minetest.formspec_escape("Leave").."]"
  103. else
  104. toggle_code = toggle_code
  105. ..minetest.formspec_escape("join")..";"
  106. ..minetest.formspec_escape("Join").."]"
  107. end
  108. return toggle_code
  109. end
  110. local children = {}
  111. for k,v in ipairs(org.children) do
  112. local this_child = modpol.orgs.get_org(v)
  113. table.insert(children, this_child.name)
  114. end
  115. -- set player context
  116. local user_context = {}
  117. user_context["current_org"] = org_name
  118. _contexts[user] = user_context
  119. -- set up formspec
  120. local formspec = {
  121. "formspec_version[4]",
  122. "size[10,8]",
  123. "label[0.5,0.5;Org: "..
  124. minetest.formspec_escape(org_name).."]",
  125. "label[0.5,1;Parent: TODO]",
  126. "button[8.5,0.5;1,0.8;"..membership_toggle(),
  127. "label[0.5,2;Members:]",
  128. "dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(org.members)..";;]",
  129. "label[0.5,3;Children:]",
  130. "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]",
  131. "label[0.5,4;Policies:]",
  132. "dropdown[2,3.5;5,0.8;policies;"..formspec_list(org.policies)..";;]",
  133. "label[0.5,5;Processes:]",
  134. "dropdown[2,4.5;5,0.8;processes;"..formspec_list(org.processes)..";;]",
  135. "button[0.5,7;1,0.8;test_poll;Test poll]",
  136. "button[2,7;1,0.8;add_child;Add child]",
  137. "button[3.5,7;1.5,0.8;remove_org;Remove org]",
  138. "button[8.5,7;1,0.8;back;Back]",
  139. }
  140. local formspec_string = table.concat(formspec, "")
  141. -- present to player
  142. minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
  143. end
  144. -- receive input
  145. minetest.register_on_player_receive_fields(function (player, formname, fields)
  146. if formname == "modpol:org_dashboard" then
  147. local pname = player:get_player_name()
  148. local org = modpol.orgs.get_org(_contexts[pname].current_org)
  149. if nil then
  150. elseif fields.join then
  151. org:add_member(pname)
  152. modpol.interactions.org_dashboard(pname,org.name)
  153. elseif fields.leave then
  154. org:remove_member(pname)
  155. modpol.interactions.dashboard(pname)
  156. elseif fields.test_poll then
  157. modpol.interactions.binary_poll_org(pname, _contexts.pname.current_org.id, "Poll question (yes/no):")
  158. elseif fields.add_child then
  159. modpol.interactions.add_org(pname, org.id)
  160. elseif fields.remove_org then
  161. modpol.interactions.remove_org(pname)
  162. elseif fields.back then
  163. modpol.interactions.dashboard(pname)
  164. end
  165. end
  166. end)
  167. -- BASIC INTERACTION FUNCTIONS
  168. -- ===========================
  169. -- Function: modpol.interactions.message
  170. -- input: message (string)
  171. -- output
  172. modpol.interactions.message = function(user, message)
  173. minetest.chat_send_player(user, message)
  174. end
  175. -- Function: modpol.interactions.text_query
  176. -- Overrides function at modpol/interactions.lua
  177. -- input: Query (string), User (string)
  178. -- output: User response (string)
  179. -- TODO Need to switch "user" to index not name
  180. function modpol.interactions.text_query(user, query)
  181. -- set up formspec
  182. local formspec = {
  183. "formspec_version[4]",
  184. "size[10,4]",
  185. "label[0.5,1;", minetest.formspec_escape(query), "]",
  186. "field[0.5,1.25;9,0.8;input;;]",
  187. "button[0.5,2.5;1,0.8;yes;OK]",
  188. }
  189. local formspec_string = table.concat(formspec, "")
  190. -- present to players
  191. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  192. end
  193. -- receive fields
  194. minetest.register_on_player_receive_fields(function (player, formname, fields)
  195. if formname == "modpol:text_query" then
  196. local pname = player:get_player_name()
  197. if _contexts[pname] then
  198. _contexts[pname](fields.input)
  199. end
  200. minetest.close_formspec(pname, formname)
  201. end
  202. end)
  203. -- Function: dropdown_query
  204. -- input: user (string), label (string), options (table of strings)
  205. function modpol.interactions.dropdown_query(user, label, options)
  206. -- set up formspec
  207. local formspec = {
  208. "formspec_version[4]",
  209. "size[10,4]",
  210. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  211. "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]",
  212. "button[0.5,2.5;1,0.8;yes;OK]",
  213. }
  214. local formspec_string = table.concat(formspec, "")
  215. -- present to players
  216. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  217. end
  218. -- receive fields
  219. minetest.register_on_player_receive_fields(function (player, formname, fields)
  220. if formname == "modpol:dropdown_query" then
  221. local pname = player:get_player_name()
  222. if _contexts[pname] then
  223. _contexts[pname](fields.input)
  224. end
  225. minetest.close_formspec(pname, formname)
  226. end
  227. end)
  228. -- SECONDARY INTERACTIONS
  229. -- ======================
  230. -- Function: modpol.binary_poll_user(user, question)
  231. -- Overrides function at modpol/interactions.lua
  232. -- presents a yes/no poll to a user, returns answer
  233. --
  234. function modpol.interactions.binary_poll_user(user, question)
  235. -- set up formspec
  236. local text = "Poll: " .. question
  237. local formspec = {
  238. "formspec_version[4]",
  239. "size[5,3]",
  240. "label[0.375,0.5;", minetest.formspec_escape(text), "]",
  241. "button[1,1.5;1,0.8;yes;Yes]",
  242. "button[2,1.5;1,0.8;no;No]",
  243. --TKTK can we enable text wrapping?
  244. --TKTK we could use scroll boxes to contain the text
  245. }
  246. local formspec_string = table.concat(formspec, "")
  247. -- present to player
  248. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  249. end
  250. minetest.register_on_player_receive_fields(function (player, formname, fields)
  251. local pname = player:get_player_name()
  252. -- modpol:binary_poll
  253. if formname == "modpol:binary_poll_user" then
  254. local vote = nil
  255. if fields.yes then vote = fields.yes
  256. elseif fields.no then vote = fields.no
  257. elseif fields.abstain then vote = fields.abstain
  258. end
  259. if vote then
  260. modpol.interactions.message(pname, "Vote recorded")
  261. minetest.chat_send_all(pname .. " voted " .. vote)
  262. --TODO : we should send the message to all in that org, not to all players
  263. end
  264. minetest.close_formspec(pname, formname)
  265. return vote
  266. end
  267. end)
  268. -- COMPLEX INTERACTIONS
  269. -- ====================
  270. -- Function: modpol.interactions.binary_poll_org
  271. -- input: initator (user string), org (number)
  272. -- output: interaction begins
  273. function modpol.interactions.binary_poll_org(initiator, org)
  274. -- start formspec
  275. modpol.interactions.text_query(initiator, "Poll question (yes/no):")
  276. -- set user's context to followup function
  277. _contexts[initiator] =
  278. function(input)
  279. local users = modpol.list_users()
  280. for k,v in ipairs(users) do
  281. modpol.interactions.binary_poll_user(v, input)
  282. end
  283. _contexts[initiator] = nil
  284. end
  285. end
  286. -- Function: modpol.interactions.add_org
  287. -- input: initator (user string), base_org_id (ID)
  288. -- output: interaction begins
  289. function modpol.interactions.add_org(initiator, base_org_id)
  290. -- start formspec
  291. modpol.interactions.text_query(initiator, "Org name:")
  292. -- set user's context to followup function
  293. _contexts[initiator] = function(input)
  294. if input then
  295. local base_org = modpol.orgs.get_org(base_org_id)
  296. local result = base_org:add_org(input, initiator)
  297. if result then
  298. local message = input .. " created"
  299. modpol.interactions.message(initiator, message)
  300. end
  301. end
  302. _contexts[initiator] = nil
  303. modpol.interactions.dashboard(initiator)
  304. end
  305. end
  306. -- Function: modpol.interactions.remove_org
  307. -- input: initator (user string)
  308. -- output: interaction begins
  309. function modpol.interactions.remove_org(initiator)
  310. -- start formspec
  311. local orgs_list = modpol.orgs.list_all()
  312. local label = "Choose an org to remove:"
  313. modpol.interactions.dropdown_query(initiator, label, orgs_list)
  314. -- set user's context to followup function
  315. _contexts[initiator] = function(input)
  316. if input then
  317. local target_org = modpol.orgs.get_org(input)
  318. local result = target_org:delete()
  319. if result then
  320. local message = input .. " deleted"
  321. modpol.interactions.message(initiator, message)
  322. end
  323. end
  324. _contexts[initiator] = nil
  325. modpol.interactions.dashboard(initiator)
  326. end
  327. end