123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- -- INTERACTIONS.LUA (CLI)
- -- User interaction functions for Modular Politics
- -- Called by modpol.lua
- modpol.interactions = {}
- -- DASHBOARDS
- -- ==========
- -- Function: modpol.interactions.dashboard(user)
- -- Params: user (string)
- -- Q: Should this return a menu of commands relevant to the specific user?
- -- Output: Displays a menu of commands to the user
- -- TKTK currently just prints all of modpol---needs major improvement
- function modpol.interactions.dashboard(user)
- -- adds user to root org if not already in it
- if not modpol.instance:has_member(user) then
- modpol.instance:add_member(user)
- end
- local all_users = modpol.instance:list_members()
- print('All orgs: (user orgs indicated by *)')
- for id, org in ipairs(modpol.orgs.array) do
- if type(org) == "table" then
- local indicator = ""
- if org:has_member(user) then indicator = "*" end
- print('['..id..'] '..indicator..org.name)
- end
- end
- -- pending list
- local user_pending = {}
- local user_pending_count = 0
- for k,v in ipairs(modpol.orgs.array) do
- if v.pending and v.pending[user] then
- modpol.msg(v.name)
- table.insert(user_pending, v.name)
- user_pending_count = user_pending_count + 1
- end
- end
-
- print('Pending actions in: '..table.concat(user_pending,', '))
- print('All users: ' .. table.concat(all_users, ', '))
- print()
- print('Access which org id?')
- local sel = io.read()
- print()
- if modpol.orgs.array[tonumber(sel)] then
- local sel_org = modpol.orgs.array[tonumber(sel)].name
- modpol.interactions.org_dashboard(user, sel_org)
- else
- print("Org id not found.")
- end
- end
- -- Function: modpol.interactions.org_dashboard
- -- Params: user (string), org_string (string or id)
- -- Output: Displays a menu of org-specific commands to the user
- function modpol.interactions.org_dashboard(user, org_string)
- local org = modpol.orgs.get_org(org_string)
- if not org then return nil end
- -- identify parent
- local parent = ""
- if org.id == 1 then
- parent = "none"
- else
- parent = modpol.orgs.get_org(org.parent).name
- end
- -- identify children
- local children = {}
- for k,v in ipairs(org.children) do
- local this_child = modpol.orgs.get_org(v)
- table.insert(children, this_child.name)
- end
- -- list available modules
- local org_modules = {}
- for k,v in pairs(org.modules) do
- if not v.hide then
- table.insert(org_modules, v.slug)
- end
- end
- -- list pending
- local process_msg = #org.processes .. " total processes"
- if org.pending[user] then
- process_msg = process_msg .. " (" .. #org.pending[user] .. " pending)"
- else
- process_msg = process_msg .. " (0 pending)"
- end
-
- -- set up output
- print("Org: " .. org.name)
- print("Parent: " .. parent)
- print("Members: " .. table.concat(org.members, ", "))
- print("Children: " .. table.concat(children, ", "))
- print("Modules: " .. table.concat(org_modules, ", "))
- print("Pending: " .. process_msg)
- print()
- print("Commands: (M)odules, (P)ending")
-
- local sel = io.read()
- print()
- if sel == 'm' or sel == 'M' then
- print("Type module name: ")
- local module_sel = io.read()
- print()
- local module_result = false
- for k,v in ipairs(org_modules) do
- if v == module_sel then
- module_result = true
- end
- end
- if module_result then
- org:call_module(module_sel, user)
- else
- print("Error: Module not found.")
- end
-
- elseif sel == 'a' or sel == 'A' then
- local processes = {}
- print("All processes: (* indicates pending)")
- for k,v in ipairs(org.processes) do
- local active = ''
- if org.pending[user] then
- if org.pending[user][v.id] then
- active = '*'
- end
- end
- end
- print()
- print("Interact with which one?")
- local to_interact = io.read()
- local process = org.processes[tonumber(to_interact)]
- if not process then return end
- if org:has_pending_actions(user) then
- if org.pending[user][process.id] then
- org:interact(process.id, user)
- end
- end
- else
- print("Command not found")
- modpol.interactions.org_dashboard(user, org.name)
- end
- end
- -- Function: modpol.interactions.policy_dashboard
- -- input: user (string), org_id (int), policy (string)
- -- if policy is nil, enables creating a new policy
- -- output: opens a dashboard for viewing/editing policy details
- -- TODO
- -- Function: modpol.interactions.message
- -- input: user (string), message (string)
- -- output: prints message to CLI
- function modpol.interactions.message(user, message)
- print(user .. ": " .. message)
- end
- -- Function: modpol.interactions.text_query
- -- input: User (string), Query (string), func (function)
- -- func input: user input (string)
- -- output: Applies "func" to user input
- function modpol.interactions.text_query(user, query, func)
- print(user .. ": " .. query)
- answer = io.read()
- func(answer)
- end
- -- Function: dropdown_query
- -- input: user (string), label (string), options (table of strings), func(choice) (function)
- -- func input: choice (string)
- -- output: calls func on choice
- function modpol.interactions.dropdown_query(user, label, options, func)
- -- set up options
- local options_display = ""
- local options_number = 0
- for k,v in ipairs(options) do
- options_display = options_display .. k .. ". " ..
- options[k] .. "\n"
- options_number = options_number + 1
- end
- options_display = options_display .. "Select number:"
- if options_number == 0 then
- print("Error: No options given for dropdown")
- return nil
- end
- -- begin displaying
- print(user .. ": " .. label)
- print(options_display)
- -- read input and produce output
- local answer
- answer = io.read()
- answer = tonumber(answer)
- if answer then
- if answer >= 1 and answer <= options_number then
- print("Selection: " .. options[answer])
- func(options[answer])
- else
- print("Error: Not in dropdown range")
- return nil
- end
- else
- print("Error: Must be a number")
- return nil
- end
- end
- -- Function: modpol.binary_poll_user(user, question)
- -- Params: user (string), question (string), func (function)
- -- func input: user input (string: y/n)
- -- Output: Applies "func" to user input
- function modpol.interactions.binary_poll_user(user, question, func)
- local query = "Poll for " .. user .. " (y/n): ".. question
- local answer
- repeat
- print(query)
- answer = io.read()
- until answer == "y" or answer == "n"
- if answer == "y" then
- modpol.interactions.message(user, "Response recorded")
- func("Yes")
- elseif answer == "n" then
- modpol.interactions.message(user, "Response recorded")
- func("No")
- else
- modpol.interactions.message(user, "Error: invalid response")
- end
- end
- -- COMPLEX INTERACTIONS
- -- ====================
- -- Function: modpol.interactions.message_org
- -- input: initiator (string), org (number or string), message (string)
- -- output: broadcasts message to all org members
- function modpol.interactions.message_org(initiator, org, message)
- local this_org = modpol.orgs.get_org(org)
- local users = this_org:list_members()
- for k,v in ipairs(users) do
- modpol.interactions.message(v, message)
- end
- end
- -- Function: modpol.interactions.binary_poll_org
- -- input: initator (user string), org_id (number)
- -- output: gets question from initiator, asks all org members, broadcasts answers
|