interactions.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. -- ===================================================================
  2. -- /orgs.lua
  3. -- User interaction functions for Modular Politics
  4. -- Called by modpol.lua
  5. modpol.interactions = {}
  6. -- DASHBOARDS
  7. -- ==========
  8. -- Function: modpol.dashboard(user)
  9. -- Params: user (string)
  10. -- Q: Should this return a menu of commands relevant to the specific user?
  11. -- Output: Displays a menu of commands to the user
  12. -- TKTK currently just prints all of modpol---needs major improvement
  13. function modpol.dashboard(user)
  14. local output = ""
  15. -- Org status
  16. output = output .. "Orgs:\n" ..
  17. table.concat(modpol.orgs.list_all(),"\n")
  18. -- Command list (should be redone to be org-specific)
  19. output = output .. "\nCommand list:\n"
  20. for key,value in pairs(modpol) do
  21. output = output .. "modpol." .. key .. " "
  22. end
  23. print(output)
  24. end
  25. -- Function: modpol.interactions.org_dashboard
  26. -- Params: user (string), org_name (string)
  27. -- Output: Displays a menu of org-specific commands to the user
  28. function modpol.interactions.org_dashboard(user, org_name)
  29. local org = modpol.orgs.get_org(org_name)
  30. if not org then return nil end
  31. local is_member = org:has_member(user)
  32. local membership_toggle = function()
  33. local toggle_code = ""
  34. if is_member then
  35. toggle_code = "[Leave]"
  36. else
  37. toggle_code = "[Join]"
  38. end
  39. return toggle_code
  40. end
  41. local children = {}
  42. for k,v in ipairs(org.children) do
  43. local this_child = modpol.orgs.get_org(v)
  44. table.insert(children, this_child.name)
  45. end
  46. -- set up output
  47. local dashboard_table = {
  48. "Org: " .. org_name,
  49. membership_toggle(),
  50. "Members: " .. table.concat(org.members, ", "),
  51. "Children: " .. table.concat(children, ", "),
  52. "Policies: " .. table.concat(org.policies, ", "),
  53. "Processes: " .. table.concat(org.processes, ", "),
  54. "[Add child]",
  55. "[Remove org]",
  56. "[Dashboard: modpol.dashboard()]"
  57. }
  58. -- present to player
  59. print(table.concat(dashboard_table, "\n"))
  60. end
  61. -- ===================================================================
  62. -- Function: modpol.interactions.message
  63. -- input: user (string), message (string)
  64. -- output: prints message to CLI
  65. function modpol.interactions.message(user, message)
  66. print(user .. ": " .. message)
  67. end
  68. -- ===================================================================
  69. -- Function: modpol.interactions.text_query
  70. -- input: Query (string)
  71. -- output: User response (string)
  72. function modpol.interactions.text_query(query)
  73. -- TODO
  74. end
  75. -- ===================================================================
  76. -- Function: modpol.binary_poll_user(user, question)
  77. -- Params: user (string), question (string)
  78. -- Output:
  79. -- presents a yes/no/abstain poll to a user, returns answer
  80. function modpol.interactions.binary_poll_user(user, question)
  81. local query = "Poll for " .. user .. " (y/n): ".. question
  82. local answer
  83. repeat
  84. print(query)
  85. answer = io.read()
  86. until answer == "y" or answer == "n" or answer == "a"
  87. if answer == "y" then
  88. return "Yes"
  89. elseif answer == "n" then
  90. return "No"
  91. else
  92. return "Abstain"
  93. end
  94. end