interactions.lua 18 KB

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