interactions.lua 16 KB

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