interactions.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. -- INTERACTIONS.LUA (for Minetest)
  2. -- CONTEXTUAL STUFF
  3. -- ================
  4. -- _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. -- UTILITIES
  16. -- =========
  17. -- Function: formspec_list
  18. -- for use generating option lists in formspecs from tables
  19. -- input: table of strings
  20. -- output: a formspec-ready list of the strings
  21. local function formspec_list(array)
  22. local escaped = {}
  23. if not array then
  24. return ""
  25. end
  26. for i = 1, #array do
  27. escaped[i] = minetest.formspec_escape(array[i])
  28. end
  29. return table.concat(escaped,",")
  30. end
  31. -- DASHBOARDS
  32. -- ==========
  33. -- Function: modpol.interactions.dashboard(user)
  34. -- Params: user (string)
  35. -- Q: Should this return a menu of commands relevant to the specific user?
  36. -- Output: Displays a menu of commands to the user
  37. -- TODO currently a manually curated list---needs major improvement
  38. function modpol.interactions.dashboard(user)
  39. -- prepare data
  40. -- to add: nested orgs map
  41. local all_orgs = modpol.orgs.list_all()
  42. local user_orgs = modpol.orgs.user_orgs(user)
  43. local all_users = modpol.list_users()
  44. -- set up formspec
  45. local formspec = {
  46. "formspec_version[4]",
  47. "size[10,8]",
  48. "label[0.5,0.5;MODPOL DASHBOARD]",
  49. "label[0.5,2;All orgs:]",
  50. "dropdown[2,1.5;5,0.8;all_orgs;"..formspec_list(all_orgs)..";;]",
  51. "label[0.5,3;Your orgs:]",
  52. "dropdown[2,2.5;5,0.8;user_orgs;"..formspec_list(user_orgs)..";;]",
  53. "label[0.5,4;All users:]",
  54. "dropdown[2,3.5;5,0.8;all_users;"..formspec_list(all_users)..";;]",
  55. "button[0.5,7;1,0.8;test_poll;Test poll]",
  56. "button[2,7;1,0.8;add_org;Add org]",
  57. "button[3.5,7;1.5,0.8;remove_org;Remove org]",
  58. "button[5.5,7;1.5,0.8;reset_orgs;Reset orgs]",
  59. "button_exit[8.5,7;1,0.8;close;Close]",
  60. }
  61. local formspec_string = table.concat(formspec, "")
  62. -- present to player
  63. minetest.show_formspec(user, "modpol:dashboard", formspec_string)
  64. end
  65. -- receive input
  66. minetest.register_on_player_receive_fields(function (player, formname, fields)
  67. if formname == "modpol:dashboard" then
  68. local pname = player:get_player_name()
  69. if nil then
  70. -- buttons first
  71. elseif fields.test_poll then
  72. -- FOR TESTING PURPOSES ONLY
  73. modpol.interactions.text_query(
  74. pname,"Poll question:",
  75. function(input)
  76. modpol.interactions.binary_poll_user(
  77. pname, input,
  78. function(vote)
  79. modpol.interactions.message(
  80. pname, pname .. " voted " .. vote)
  81. end)
  82. end)
  83. elseif fields.add_org then
  84. modpol.interactions.add_org(pname, 1)
  85. elseif fields.remove_org then
  86. modpol.interactions.remove_org(pname)
  87. elseif fields.reset_orgs then
  88. modpol.orgs.reset()
  89. modpol.instance:add_member(pname)
  90. modpol.interactions.dashboard(pname)
  91. -- Put all dropdowns at the end
  92. elseif fields.close then
  93. minetest.close_formspec(pname, formname)
  94. elseif fields.all_orgs or fields.user_orgs then
  95. local org_name = fields.all_orgs or fields.user_orgs
  96. modpol.interactions.org_dashboard(pname, org_name)
  97. end
  98. end
  99. end)
  100. -- Function: modpol.interactions.org_dashboard
  101. -- Params: user (string), org_name (string)
  102. -- Output: Displays a menu of org-specific commands to the user
  103. function modpol.interactions.org_dashboard(user, org_name)
  104. -- prepare data
  105. local org = modpol.orgs.get_org(org_name)
  106. if not org then return nil end
  107. local is_member = org:has_member(user)
  108. local membership_toggle = function()
  109. local toggle_code = ""
  110. if is_member then
  111. toggle_code = toggle_code
  112. ..minetest.formspec_escape("leave")..";"
  113. ..minetest.formspec_escape("Leave").."]"
  114. else
  115. toggle_code = toggle_code
  116. ..minetest.formspec_escape("join")..";"
  117. ..minetest.formspec_escape("Join").."]"
  118. end
  119. return toggle_code
  120. end
  121. -- identify parent
  122. local parent = modpol.orgs.get_org(org.parent)
  123. if parent then parent = parent.name
  124. else parent = "none" end
  125. -- prepare children menu
  126. local children = {"View..."}
  127. for k,v in ipairs(org.children) do
  128. local this_child = modpol.orgs.get_org(v)
  129. table.insert(children, this_child.name)
  130. end
  131. -- prepare modules menu
  132. local modules = {"View..."}
  133. if org.modules then
  134. for k,v in ipairs(org.modules) do
  135. table.insert(modules, org.modules[k].slug)
  136. end
  137. end
  138. -- prepare actions menu
  139. local actions = {"View..."}
  140. if org.pending[user] then
  141. for k,v in pairs(org.pending[user]) do
  142. local action_string = "[" .. k .. "] " ..
  143. org.processes[k].name
  144. table.insert(actions, action_string)
  145. end
  146. end
  147. -- set player context
  148. local user_context = {}
  149. user_context["current_org"] = org_name
  150. _contexts[user] = user_context
  151. -- set up formspec
  152. local formspec = {
  153. "formspec_version[4]",
  154. "size[10,8]",
  155. "label[0.5,0.5;Org: "..
  156. minetest.formspec_escape(org_name).."]",
  157. "label[0.5,1;Parent: "..parent.."]",
  158. "button[8.5,0.5;1,0.8;"..membership_toggle(),
  159. "label[0.5,2;Members:]",
  160. "dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(org.members)..";;]",
  161. "label[0.5,3;Children:]",
  162. "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]",
  163. "label[0.5,4;Modules:]",
  164. "dropdown[2,3.5;5,0.8;modules;"..formspec_list(modules)..";;]",
  165. "label[0.5,5;Actions:]",
  166. "dropdown[2,4.5;5,0.8;actions;"..formspec_list(actions)..";;]",
  167. "button[0.5,7;1,0.8;test_poll;Test poll]",
  168. "button[2,7;1,0.8;add_child;Add child]",
  169. "button[3.5,7;1.5,0.8;remove_org;Remove org]",
  170. "button[8.5,7;1,0.8;back;Back]",
  171. }
  172. local formspec_string = table.concat(formspec, "")
  173. -- present to player
  174. minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
  175. end
  176. -- receive input
  177. minetest.register_on_player_receive_fields(function (player, formname, fields)
  178. if formname == "modpol:org_dashboard" then
  179. local pname = player:get_player_name()
  180. local org = modpol.orgs.get_org(_contexts[pname].current_org)
  181. if nil then
  182. elseif fields.join then
  183. local new_request = {
  184. user = pname,
  185. type = "add_member",
  186. params = {pname}
  187. }
  188. org:make_request(new_request)
  189. modpol.interactions.org_dashboard(pname,org.name)
  190. elseif fields.leave then
  191. org:remove_member(pname)
  192. modpol.interactions.dashboard(pname)
  193. elseif fields.test_poll then
  194. modpol.interactions.binary_poll_org(
  195. pname, org.id,
  196. function(input)
  197. modpol.interactions.message_org(
  198. pname,
  199. org.id,
  200. "New response: " .. input)
  201. end)
  202. elseif fields.add_child then
  203. modpol.interactions.text_query(
  204. pname, "Child org name:",
  205. function(input)
  206. local new_request = {
  207. user = pname,
  208. type = "add_org",
  209. params = {input}
  210. }
  211. org:make_request(new_request)
  212. modpol.interactions.message(pname,"requested")
  213. modpol.interactions.org_dashboard(
  214. pname,org.name)
  215. end)
  216. elseif fields.remove_org then
  217. local new_request = {
  218. user = pname,
  219. type = "delete",
  220. params = {}
  221. }
  222. org:make_request(new_request)
  223. modpol.interactions.org_dashboard(pname,org.name)
  224. elseif fields.back then
  225. modpol.interactions.dashboard(pname)
  226. -- Put all dropdowns at the end
  227. -- Receiving modules
  228. elseif fields.modules
  229. and fields.modules ~= "View..." then
  230. local module = fields.modules
  231. org:call_module(module, user)
  232. -- Receiving actions
  233. elseif fields.actions
  234. and fields.actions ~= "View..." then
  235. local action = string.match(
  236. fields.actions,"%[(%d)%]")
  237. local process = org.processes[tonumber(action)]
  238. if process then
  239. org:interact(process, user)
  240. end
  241. -- Children
  242. elseif fields.children
  243. and fields.children ~= "View..." then
  244. local org_name = fields.children
  245. modpol.interactions.org_dashboard(pname, org_name)
  246. end
  247. end
  248. end)
  249. -- Function: modpol.interactions.policy_dashboard
  250. -- input: user (string), org_id (int), policy (string)
  251. -- output: opens a dashboard for viewing/editing policy details
  252. -- TODO
  253. function modpol.interactions.policy_dashboard(
  254. user, org_id, policy)
  255. modpol.interactions.message(
  256. user,
  257. "Not yet implemented: " .. policy)
  258. end
  259. -- BASIC INTERACTION FUNCTIONS
  260. -- ===========================
  261. -- Function: modpol.interactions.message
  262. -- input: message (string)
  263. -- output
  264. function modpol.interactions.message(user, message)
  265. minetest.chat_send_player(user, message)
  266. end
  267. -- Function: modpol.interactions.text_query
  268. -- Overrides function at modpol/interactions.lua
  269. -- input: user (string), query (string), func (function)
  270. -- func input: user input (string)
  271. -- output: Applies "func" to user input
  272. function modpol.interactions.text_query(user, query, func)
  273. -- set up formspec
  274. local formspec = {
  275. "formspec_version[4]",
  276. "size[10,4]",
  277. "label[0.5,1;", minetest.formspec_escape(query), "]",
  278. "field[0.5,1.25;9,0.8;input;;]",
  279. "button[0.5,2.5;1,0.8;yes;OK]",
  280. }
  281. local formspec_string = table.concat(formspec, "")
  282. -- present to players
  283. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  284. -- put func in _contexts
  285. if _contexts[user] == nil then _contexts[user] = {} end
  286. _contexts[user]["text_query_func"] = func
  287. end
  288. -- receive fields
  289. minetest.register_on_player_receive_fields(function (player, formname, fields)
  290. if formname == "modpol:text_query" then
  291. local pname = player:get_player_name()
  292. local input = fields.input
  293. if not input then
  294. -- no input, do nothing
  295. else
  296. local func = _contexts[pname]["text_query_func"]
  297. if func then
  298. func(input)
  299. else
  300. modpol.interactions.message(pname, "text_query: " .. input)
  301. end
  302. end
  303. minetest.close_formspec(pname, formname)
  304. end
  305. end)
  306. -- Function: dropdown_query
  307. -- input: user (string), label (string), options (table of strings), func (function)
  308. -- func input: choice (string)
  309. -- output: calls func on user choice
  310. function modpol.interactions.dropdown_query(user, label, options, func)
  311. -- set up formspec
  312. local formspec = {
  313. "formspec_version[4]",
  314. "size[10,4]",
  315. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  316. "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]",
  317. "button[0.5,2.5;1,0.8;cancel;Cancel]",
  318. }
  319. local formspec_string = table.concat(formspec, "")
  320. -- present to players
  321. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  322. -- put func in _contexts
  323. if _contexts[user] == nil then _contexts[user] = {} end
  324. _contexts[user]["dropdown_query_func"] = func
  325. end
  326. -- receive fields
  327. minetest.register_on_player_receive_fields(function (player, formname, fields)
  328. if formname == "modpol:dropdown_query" then
  329. local pname = player:get_player_name()
  330. if fields.cancel ~= "cancel" then
  331. local choice = fields.input
  332. local func = _contexts[pname]["dropdown_query_func"]
  333. if not choice then
  334. -- no choice, do nothing
  335. elseif func then
  336. func(choice)
  337. else
  338. modpol.interactions.message(pname, "dropdown_query: " .. choice)
  339. end
  340. end
  341. minetest.close_formspec(pname, formname)
  342. end
  343. end)
  344. -- SECONDARY INTERACTIONS
  345. -- ======================
  346. -- Function: modpol.binary_poll_user(user, question, function)
  347. -- Overrides function at modpol/interactions.lua
  348. -- Params: user (string), question (string), func (function)
  349. -- func input: user input (string: y/n)
  350. -- Output: Applies "func" to user input
  351. function modpol.interactions.binary_poll_user(user, question, func)
  352. -- set up formspec
  353. local formspec = {
  354. "formspec_version[4]",
  355. "size[5,3]",
  356. "label[0.375,0.5;",minetest.formspec_escape(question), "]",
  357. "button[1,1.5;1,0.8;yes;Yes]",
  358. "button[2,1.5;1,0.8;no;No]",
  359. --TKTK can we enable text wrapping?
  360. --TKTK we could use scroll boxes to contain the text
  361. }
  362. local formspec_string = table.concat(formspec, "")
  363. if _contexts[user] == nil then _contexts[user] = {} end
  364. _contexts[user]["binary_poll_func"] = func
  365. -- present to player
  366. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  367. end
  368. minetest.register_on_player_receive_fields(function (player, formname, fields)
  369. local pname = player:get_player_name()
  370. -- modpol:binary_poll
  371. if formname == "modpol:binary_poll_user" then
  372. local vote = nil
  373. if fields.yes then vote = fields.yes
  374. elseif fields.no then vote = fields.no
  375. end
  376. if vote then
  377. modpol.interactions.message(pname, "Responded " .. vote)
  378. local func = _contexts[pname]["binary_poll_func"]
  379. if func then func(vote) end
  380. end
  381. minetest.close_formspec(pname, formname)
  382. end
  383. end)
  384. -- COMPLEX INTERACTIONS
  385. -- ====================
  386. -- Function: modpol.interactions.message_org
  387. -- input: initiator (string), org_id (number), message (string)
  388. -- output: broadcasts message to all org members
  389. function modpol.interactions.message_org(initiator, org_id, message)
  390. local org = modpol.orgs.get_org(org_id)
  391. local users = org:list_members()
  392. for k,v in ipairs(users) do
  393. modpol.interactions.message(v, message)
  394. end
  395. end
  396. -- Function: modpol.interactions.binary_poll_org
  397. -- input: initator (user string), org_id (number)
  398. -- output: gets question from initiator, asks all org members, broadcasts answers
  399. -- TODO for testing. This should be implemented as a request.
  400. function modpol.interactions.binary_poll_org(initiator, org_id, func)
  401. local org = modpol.orgs.get_org(org_id)
  402. local users = org:list_members()
  403. modpol.interactions.text_query(
  404. initiator, "Yes/no poll question:",
  405. function(input)
  406. for k,v in ipairs(users) do
  407. modpol.interactions.binary_poll_user(v, input, func)
  408. end
  409. end)
  410. end
  411. -- Function: modpol.interactions.add_org
  412. -- input: initator (user string), base_org_id (ID)
  413. -- output: interaction begins
  414. -- GODMODE
  415. function modpol.interactions.add_org(user, base_org_id)
  416. modpol.interactions.text_query(
  417. user,"Org name:",
  418. function(input)
  419. local base_org = modpol.orgs.get_org(1)
  420. local result = base_org:add_org(input, user)
  421. local message = input .. " created"
  422. modpol.interactions.message(user, message)
  423. modpol.interactions.dashboard(user)
  424. end)
  425. end
  426. -- Function: modpol.interactions.remove_org
  427. -- input: initator (user string)
  428. -- output: interaction begins
  429. -- GODMODE
  430. function modpol.interactions.remove_org(user)
  431. -- start formspec
  432. local orgs_list = modpol.orgs.list_all()
  433. local label = "Choose an org to remove:"
  434. modpol.interactions.dropdown_query(
  435. user, label, orgs_list,
  436. function(input)
  437. if input then
  438. local target_org = modpol.orgs.get_org(input)
  439. local result = target_org:delete()
  440. local message = input .. " deleted"
  441. modpol.interactions.message(user, message)
  442. end
  443. modpol.interactions.dashboard(user)
  444. end)
  445. end