interactions.lua 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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.instance:list_members()
  44. -- pending list
  45. local user_pending_count = 0
  46. local user_pending = {}
  47. for k,v in ipairs(modpol.orgs.array) do
  48. if v.pending and v.pending[user] then
  49. if modpol.util.num_pairs(v.pending[user]) ~= 0 then
  50. table.insert(user_pending, v.name)
  51. user_pending_count = user_pending_count + 1
  52. end
  53. end
  54. end
  55. -- set up formspec
  56. local formspec = {
  57. "formspec_version[4]",
  58. "size[10,8]",
  59. "hypertext[0.5,0.5;9,1;title;<big>Org dashboard</big>]",
  60. "label[0.5,2;All orgs:]",
  61. "dropdown[2.5,1.5;7,0.8;all_orgs;View...,"..formspec_list(all_orgs)..";;]",
  62. "label[0.5,3;Your orgs:]",
  63. "dropdown[2.5,2.5;7,0.8;user_orgs;View...,"..formspec_list(user_orgs)..";;]",
  64. "label[0.5,4;All users:]",
  65. "dropdown[2.5,3.5;7,0.8;all_users;View...,"..formspec_list(all_users)..";;]",
  66. "label[0.5,5;Pending ("..user_pending_count.."):]",
  67. "dropdown[2.5,4.5;7,0.8;pending;View...,"..formspec_list(user_pending)..";;]",
  68. "button[0.5,7;1,0.8;refresh;Refresh]",
  69. "button_exit[8.5,7;1,0.8;close;Close]",
  70. }
  71. local formspec_string = table.concat(formspec, "")
  72. -- present to player
  73. minetest.show_formspec(user, "modpol:dashboard", formspec_string)
  74. end
  75. -- receive input
  76. minetest.register_on_player_receive_fields(function (player, formname, fields)
  77. if formname == "modpol:dashboard" then
  78. local pname = player:get_player_name()
  79. if nil then
  80. elseif fields.close then
  81. minetest.close_formspec(pname, formname)
  82. elseif fields.refresh then
  83. modpol.interactions.dashboard(pname)
  84. -- Put all dropdowns at the end
  85. elseif fields.all_users
  86. and fields.all_users ~= "View..." then
  87. modpol.interactions.user_dashboard(
  88. pname,
  89. fields.all_users,
  90. function()
  91. modpol.interactions.dashboard(pname)
  92. end
  93. )
  94. elseif fields.all_orgs or fields.user_orgs or fields.pending then
  95. local org_name = fields.all_orgs or fields.user_orgs or fields.pending
  96. if org_name ~= "View..." then
  97. modpol.interactions.org_dashboard(
  98. pname, org_name)
  99. end
  100. end
  101. end
  102. end)
  103. -- Function: modpol.interactions.org_dashboard
  104. -- Params: user (string), org_string (string or num)
  105. -- Output: Displays a menu of org-specific commands to the user
  106. function modpol.interactions.org_dashboard(user, org_string)
  107. -- prepare data
  108. local org = modpol.orgs.get_org(org_string)
  109. if not org then return nil end
  110. local function membership_toggle(org_display)
  111. local current_org = modpol.orgs.get_org(org_display)
  112. if current_org then
  113. if current_org:has_member(user) then
  114. return " (member)"
  115. end
  116. end
  117. return ""
  118. end
  119. -- identify parent
  120. local parent = modpol.orgs.get_org(org.parent)
  121. if parent then parent = parent.name
  122. else parent = "none" end
  123. -- prepare members menu
  124. local members = org.members
  125. -- prepare children menu
  126. local children = {}
  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. table.sort(children)
  132. -- prepare modules menu
  133. local modules = {}
  134. if org.modules then
  135. for k,v in pairs(org.modules) do
  136. if not v.hide then -- hide utility modules
  137. local module_entry = v.name
  138. table.insert(modules, module_entry)
  139. end
  140. end
  141. end
  142. table.sort(modules)
  143. -- prepare pending menu
  144. local pending = {}
  145. local num_pending = 0
  146. if org.pending[user] then
  147. for k,v in pairs(org.pending[user]) do
  148. if org.processes[k] ~= "deleted" then
  149. local pending_string = org.processes[k].name
  150. .." ["..k.."]"
  151. table.insert(pending, pending_string)
  152. num_pending = num_pending + 1
  153. end
  154. end
  155. end
  156. table.sort(pending)
  157. -- set player context
  158. local user_context = {}
  159. user_context["current_org"] = org.name
  160. _contexts[user] = user_context
  161. -- set up formspec
  162. local formspec = {
  163. "formspec_version[4]",
  164. "size[10,8]",
  165. "hypertext[0.5,0.5;9,1;title;<big>Org: <b>"..
  166. minetest.formspec_escape(org.name).."</b>"..membership_toggle(org.name).."</big>]",
  167. "label[0.5,1.25;Parent: "..parent..membership_toggle(parent).."]",
  168. "label[0.5,2;Members:]",
  169. "dropdown[2.5,1.5;7,0.8;members;View...,"..formspec_list(members)..";;]",
  170. "label[0.5,3;Child orgs:]",
  171. "dropdown[2.5,2.5;7,0.8;children;View...,"..formspec_list(children)..";;]",
  172. "label[0.5,4;Modules:]",
  173. "dropdown[2.5,3.5;7,0.8;modules;View...,"..formspec_list(modules)..";;]",
  174. "label[0.5,5;Pending ("..num_pending.."):]",
  175. "dropdown[2.5,4.5;7,0.8;pending;View...,"..formspec_list(pending)..";;]",
  176. "button[0.5,7;1,0.8;refresh;Refresh]",
  177. "button[8.5,7;1,0.8;back;Back]",
  178. }
  179. local formspec_string = table.concat(formspec, "")
  180. -- present to player
  181. minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
  182. end
  183. -- receive input
  184. minetest.register_on_player_receive_fields(function (player, formname, fields)
  185. if formname == "modpol:org_dashboard" then
  186. local pname = player:get_player_name()
  187. local org = modpol.orgs.get_org(_contexts[pname].current_org)
  188. -- just confirm the org still exists:
  189. if not org then
  190. modpol.interactions.message(pname, "Org no longer exists")
  191. modpol.interactions.dashboard(pname)
  192. return end
  193. -- okay, onward
  194. if nil then
  195. elseif fields.back then
  196. modpol.interactions.dashboard(pname)
  197. elseif fields.refresh then
  198. modpol.interactions.org_dashboard(pname, org.name)
  199. -- Put all dropdowns at the end
  200. -- Receiving modules
  201. elseif fields.members
  202. and fields.members ~= "View..." then
  203. modpol.interactions.user_dashboard(
  204. pname,
  205. fields.members,
  206. function()
  207. modpol.interactions.org_dashboard(
  208. pname, org.name)
  209. end
  210. )
  211. elseif fields.modules
  212. and fields.modules ~= "View..." then
  213. local module = nil
  214. for k,v in pairs(org.modules) do
  215. if fields.modules == v.name then
  216. module = v
  217. end end
  218. if module then
  219. modpol.interactions.binary_poll_user(
  220. pname,
  221. module.name..":\n"..
  222. module.desc.."\n"..
  223. modpol.interactions.get_policy_string(
  224. org.name, module.slug, ", ")..
  225. "\n".."Proceed?",
  226. function(input)
  227. if input == "Yes" then
  228. org:call_module(module.slug, pname)
  229. elseif input == "No" then
  230. modpol.interactions.org_dashboard(
  231. pname, org.id)
  232. end
  233. end)
  234. end
  235. -- Receiving pending
  236. elseif fields.pending
  237. and fields.pending ~= "View..." then
  238. local pending = string.match(
  239. fields.pending,"%[(%d)%]")
  240. local process = org.processes[tonumber(pending)]
  241. if process then
  242. org:interact(process.id, pname)
  243. end
  244. -- Children
  245. elseif fields.children
  246. and fields.children ~= "View..." then
  247. local org_name = fields.children
  248. modpol.interactions.org_dashboard(pname, org_name)
  249. end
  250. end
  251. end)
  252. --- Function: modpol.interactions.user_dashboard
  253. -- Displays a dashboard about a particular user
  254. -- @param viewer Name of user viewing the dashboard (string)
  255. -- @param user Name of user being viewed (string)
  256. -- @param completion Optional function to call on Done button
  257. function modpol.interactions.user_dashboard(viewer, user, completion)
  258. local user_orgs = modpol.orgs.user_orgs(user)
  259. -- set player context
  260. local user_context = {}
  261. user_context["viewer"] = viewer
  262. user_context["user"] = user
  263. user_context["completion"] = completion
  264. _contexts[viewer] = user_context
  265. -- set up formspec
  266. local formspec = {
  267. "formspec_version[4]",
  268. "size[10,8]",
  269. "hypertext[0.5,0.5;9,1;title;<big>User: <b>"..user.."</b></big>]",
  270. "label[0.5,2;User's orgs:]",
  271. "dropdown[2.5,1.5;7,0.8;user_orgs;View...,"..formspec_list(user_orgs)..";;]",
  272. "button[0.5,7;1.5,0.8;message;Message]",
  273. "button_exit[8.5,7;1,0.8;close;Close]",
  274. }
  275. local formspec_string = table.concat(formspec, "")
  276. -- present to player
  277. minetest.show_formspec(viewer, "modpol:user_dashboard", formspec_string)
  278. end
  279. -- receive input
  280. minetest.register_on_player_receive_fields(function (player, formname, fields)
  281. if formname == "modpol:user_dashboard" then
  282. local contexts = _contexts[player:get_player_name()]
  283. -- check fields
  284. if nil then
  285. elseif fields.message then
  286. modpol.interactions.message_user(
  287. contexts.viewer, contexts.user
  288. )
  289. elseif fields.back then
  290. if contexts.completion then
  291. completion()
  292. else
  293. modpol.interactions.dashboard(
  294. contexts.viewer)
  295. end
  296. -- dropdown fields
  297. elseif fields.user_orgs
  298. and fields.user_orgs ~= "View..." then
  299. modpol.interactions.org_dashboard(
  300. contexts.viewer, fields.user_orgs)
  301. end
  302. end
  303. end)
  304. -- INTERACTION PRIMITIVES
  305. -- ======================
  306. -- Function: modpol.interactions.message
  307. -- Produces a brief message to a user
  308. -- input: user (string), message (string)
  309. -- output: displays message to specified user
  310. function modpol.interactions.message(user, message)
  311. if message then
  312. minetest.chat_send_player(user, message)
  313. end
  314. end
  315. --- Function: modpol.interactions.message_user
  316. -- Gets and sends a message from one user to another
  317. -- @param sender Name of user sending (string)
  318. -- @param recipient Name of user receiving (string)
  319. function modpol.interactions.message_user(sender, recipient)
  320. modpol.interactions.text_query(
  321. sender,
  322. "Message for "..recipient..":",
  323. function(input)
  324. modpol.interactions.message(
  325. recipient,
  326. input.." [from "..sender.."]")
  327. end
  328. )
  329. end
  330. --- Function: modpol.interactions.display
  331. -- Displays complex data to a user
  332. -- @param user Name of target user (string)
  333. -- @param title Title of display (string)
  334. -- @param message Content of message (string or table of strings)
  335. -- @param completion Optional function for what happens when user is done
  336. function modpol.interactions.display(
  337. user, title, message, completion)
  338. -- set up contexts
  339. _contexts[user]["completion"] = completion
  340. -- set up output
  341. local output = ""
  342. if type(message) == "table" then
  343. output = table.concat(message,"\n")
  344. elseif type(message) == "string" then
  345. output = message
  346. elseif type(message) == "number" then
  347. output = message
  348. else
  349. modpol.interactions.message(
  350. self.initiator, "Error: message not typed for display")
  351. if completion then completion() else
  352. modpol.intereactions.dashboard(user)
  353. end
  354. return
  355. end
  356. -- set up formspec
  357. local formspec = {
  358. "formspec_version[4]",
  359. "size[10,8]",
  360. "label[0.5,0.5;"..title.."]",
  361. "hypertext[0.5,1;9,5.5;display;<global background=black margin=10>"..output.."]",
  362. "button_exit[8.5,7;1,0.8;done;Done]",
  363. }
  364. local formspec_string = table.concat(formspec, "")
  365. -- present to player
  366. minetest.show_formspec(user, "modpol:display", formspec_string)
  367. end
  368. -- receive fields
  369. minetest.register_on_player_receive_fields(function (player, formname, fields)
  370. local pname = player:get_player_name()
  371. if formname == "modpol:display" then
  372. if fields.done and _contexts[pname].completion then
  373. minetest.close_formspec(pname, formname)
  374. _contexts[pname].completion()
  375. end
  376. end
  377. end)
  378. -- Function: modpol.interactions.text_query
  379. -- Overrides function at modpol/interactions.lua
  380. -- input: user (string), query (string), func (function)
  381. -- output: Applies "func" to user input
  382. function modpol.interactions.text_query(user, query, func)
  383. -- set up formspec
  384. local formspec = {
  385. "formspec_version[4]",
  386. "size[10,4]",
  387. "label[0.5,1;", minetest.formspec_escape(query), "]",
  388. "field[0.5,1.25;9,0.8;input;;]",
  389. "button[0.5,2.5;1,0.8;yes;OK]",
  390. }
  391. local formspec_string = table.concat(formspec, "")
  392. -- present to player
  393. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  394. -- put func in _contexts
  395. if _contexts[user] == nil then _contexts[user] = {} end
  396. _contexts[user]["text_query_func"] = func
  397. end
  398. -- receive fields
  399. minetest.register_on_player_receive_fields(function (player, formname, fields)
  400. if formname == "modpol:text_query" then
  401. local pname = player:get_player_name()
  402. local input = fields.input
  403. if not input then
  404. -- no input, do nothing
  405. else
  406. local func = _contexts[pname]["text_query_func"]
  407. if func then
  408. func(input)
  409. else
  410. modpol.interactions.message(pname, "text_query: " .. input)
  411. end
  412. end
  413. minetest.close_formspec(pname, formname)
  414. end
  415. end)
  416. -- Function: dropdown_query
  417. -- input: user (string), label (string), options (table of strings), func (function)
  418. -- func input: choice (string)
  419. -- output: calls func on user choice
  420. function modpol.interactions.dropdown_query(user, label, options, func)
  421. -- set up formspec
  422. local formspec = {
  423. "formspec_version[4]",
  424. "size[10,4]",
  425. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  426. "dropdown[0.5,1.25;9,0.8;input;View...,"..formspec_list(options)..";;]",
  427. "button[0.5,2.5;1,0.8;cancel;Cancel]",
  428. }
  429. local formspec_string = table.concat(formspec, "")
  430. -- present to players
  431. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  432. -- put func in _contexts
  433. if _contexts[user] == nil then _contexts[user] = {} end
  434. _contexts[user]["dropdown_query_func"] = func
  435. end
  436. -- receive fields
  437. minetest.register_on_player_receive_fields(function (player, formname, fields)
  438. if formname == "modpol:dropdown_query" then
  439. local pname = player:get_player_name()
  440. if fields.cancel then
  441. minetest.close_formspec(pname, formname)
  442. elseif fields.input == "View..." then
  443. -- "View...", do nothing
  444. else
  445. local choice = fields.input
  446. local func = _contexts[pname]["dropdown_query_func"]
  447. if not choice then
  448. -- empty, do nothing
  449. elseif func then
  450. --causes issues with sequential dropdowns
  451. --minetest.close_formspec(pname, formname)
  452. func(choice)
  453. else
  454. minetest.close_formspec(pname, formname)
  455. modpol.interactions.message(pname, "dropdown_query: " .. choice)
  456. end
  457. end
  458. end
  459. end)
  460. --- Function: modpol.interactions.checkbox_query
  461. -- Allows user to select from a set of options
  462. -- @param user Name of user (string)
  463. -- @param label Query for user before options (string)
  464. -- @param options table of options and their checked status in the form {{"option_1_string", true}, {"option_2_string", false}}
  465. -- @param func function to be called with param "input", made up of the corrected table in the same format as the param options
  466. function modpol.interactions.checkbox_query(
  467. user, label, options, func)
  468. -- set up formspec
  469. -- prepare options
  470. local vertical = 0
  471. local checkbox_options = {}
  472. for i,v in ipairs(options) do
  473. local fs_line = ""
  474. vertical = i * .5
  475. fs_line = "checkbox[0,"..vertical..";checkbox_"..i..";"..
  476. minetest.formspec_escape(v[1])..";"..
  477. tostring(v[2]).."]"
  478. table.insert(checkbox_options, fs_line)
  479. end
  480. local max = vertical * 4
  481. local bar_height = vertical / 2
  482. local formspec = {
  483. "formspec_version[4]",
  484. "size[10,8]",
  485. "label[0.5,0.5;"..label.."]",
  486. "scrollbaroptions[arrows=default;max="..max..";smallstep=10;largestep=100;thumbsize="..bar_height.."]",
  487. "scrollbar[9,1;0.3,5.5;vertical;scroller;0]",
  488. "scroll_container[0.5,1;9,5.5;scroller;vertical]",
  489. }
  490. -- insert options
  491. for i,v in ipairs(checkbox_options) do
  492. table.insert(formspec, v)
  493. end
  494. table.insert(formspec,"scroll_container_end[]")
  495. table.insert(formspec,"button[0.5,7;1.5,0.8;submit;Submit]")
  496. table.insert(
  497. formspec,"button_exit[8,7;1.5,0.8;cancel;Cancel]")
  498. local formspec_string = table.concat(formspec, "")
  499. -- present to players
  500. minetest.show_formspec(user, "modpol:checkbox_query", formspec_string)
  501. -- put func in _contexts
  502. if _contexts[user] == nil then _contexts[user] = {} end
  503. _contexts[user]["checkbox_query_func"] = func
  504. _contexts[user]["checkbox_query_result"] =
  505. modpol.util.copy_table(options)
  506. end
  507. -- receive fields
  508. minetest.register_on_player_receive_fields(function (player, formname, fields)
  509. if formname == "modpol:checkbox_query" then
  510. local pname = player:get_player_name()
  511. -- start checking fields
  512. if fields.cancel then
  513. minetest.close_formspec(pname, formname)
  514. elseif fields.submit then
  515. -- send in result
  516. minetest.close_formspec(pname, formname)
  517. _contexts[pname].checkbox_query_func(
  518. _contexts[pname].checkbox_query_result)
  519. else
  520. for k,v in pairs(fields) do
  521. -- identify checkbox actions and flip bool
  522. if string.find(k,"checkbox_") then
  523. local index = tonumber(
  524. string.match(k,"%d+"))
  525. _contexts[pname].checkbox_query_result[index][2] =
  526. not _contexts[pname].checkbox_query_result[index][2]
  527. end
  528. end
  529. end
  530. end
  531. end)
  532. -- Function: modpol.binary_poll_user(user, question, function)
  533. -- Overrides function at modpol/interactions.lua
  534. -- Params: user (string), question (string), func (function)
  535. -- func input: user input (string: y/n)
  536. -- Output: Applies "func" to user input
  537. function modpol.interactions.binary_poll_user(user, question, func)
  538. -- set up formspec
  539. local formspec = {
  540. "formspec_version[4]",
  541. "size[8,6]",
  542. "label[0.375,0.5;",minetest.formspec_escape(question), "]",
  543. "button[1,5;1,0.8;yes;Yes]",
  544. "button[2,5;1,0.8;no;No]",
  545. --TODO can we enable text wrapping?
  546. --TODO we could use scroll boxes to contain the text
  547. }
  548. local formspec_string = table.concat(formspec, "")
  549. if _contexts[user] == nil then _contexts[user] = {} end
  550. _contexts[user]["binary_poll_func"] = func
  551. -- present to player
  552. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  553. end
  554. minetest.register_on_player_receive_fields(function (player, formname, fields)
  555. local pname = player:get_player_name()
  556. -- modpol:binary_poll
  557. if formname == "modpol:binary_poll_user" then
  558. local vote = nil
  559. if fields.yes then vote = fields.yes
  560. elseif fields.no then vote = fields.no
  561. end
  562. if vote then
  563. local func = _contexts[pname]["binary_poll_func"]
  564. if func then func(vote) end
  565. end
  566. minetest.close_formspec(pname, formname)
  567. end
  568. end)
  569. -- TESTING
  570. --testing command for "singleplayer"
  571. function modpol.msg(text)
  572. modpol.interactions.message("singleplayer",text)
  573. end