Browse Source

Have basic checkbox_query working in CLI core interactions, not yet attempted in Minetest

Nathan Schneider 2 years ago
parent
commit
c8d527dba8
2 changed files with 56 additions and 6 deletions
  1. 3 0
      modpol_core/api.lua
  2. 53 6
      modpol_core/interactions/interactions.lua

+ 3 - 0
modpol_core/api.lua

@@ -30,3 +30,6 @@ dofile (localdir .. "/modules/remove_process.lua")
 dofile (localdir .. "/modules/rename_org_consent.lua")
 dofile (localdir .. "/modules/send_token.lua")
 dofile (localdir .. "/modules/tokenomics.lua")
+
+
+dofile (localdir .. "/modules/checkbox_test.lua")

+ 53 - 6
modpol_core/interactions/interactions.lua

@@ -60,11 +60,11 @@ function modpol.interactions.dashboard(user)
       if modpol.orgs.array[tonumber(sel)] then
          local sel_org = modpol.orgs.array[tonumber(sel)].name
          modpol.interactions.org_dashboard(user, sel_org)
-      else 
+      else
          print("Org id not found")
          modpol.interactions.dashboard(user)
-      end   
-      
+      end
+
    elseif sel == "U" or sel == "u" then
       print("Access which user?")
       sel = io.read()
@@ -276,7 +276,7 @@ function modpol.interactions.display(user, title, message, completion)
       output = message
    else
       modpol.interactions.message(
-         self.initiator, "Error: message not typed for display")      
+         self.initiator, "Error: message not typed for display")
       modpol.interactions.message(
          self.initiator, "Error: input not typed for display")
       if completion then completion() else
@@ -318,7 +318,7 @@ function modpol.interactions.dropdown_query(user, label, options, func)
    options_display = options_display .. "Select number:"
    if options_number == 0 then
       print("Error: No options given for dropdown")
-      return nil      
+      return nil
    end
    -- begin displaying
    print(user .. ": " .. label)
@@ -341,7 +341,54 @@ function modpol.interactions.dropdown_query(user, label, options, func)
    end
 end
 
--- Function: modpol.binary_poll_user(user, question)
+--- Function: modpol.interactions.checkbox_query
+-- Allows user to select from a set of options
+-- @param user Name of user (string)
+-- @param label Query for user before options (string)
+-- @param options table of options and their checked status in the form {{"option_1_string", true}, {"option_2_string", false}}
+-- @param func function to be called with param "input", made up of the corrected table in the same format as the param options
+function modpol.interactions.checkbox_query(
+      user, label, options, func)
+   -- set up options
+   local options_display = ""
+   local options_number = 0
+   for i,v in ipairs(options) do
+      local checked = false
+      if v[2] then checked = true end
+      if checked then
+         checked = "x"
+      else
+         checked = " "
+      end
+      options_display = options_display..i..". ["..
+         checked.."] "..v[1].."\n"
+      options_number = options_number + 1
+   end
+   if options_number == 0 then
+      print("Error: No options given for dropdown")
+      return nil
+   end
+   options_display = options_display..
+      "List numbers to check (e.g., 1,2,5):"
+   -- begin displaying
+   print(user .. ": " .. label)
+   print(options_display)
+   -- read input and produce output
+   local answer = io.read()
+   local answer_table = {}
+   for match in (answer..","):gmatch("(.-)"..",") do
+      table.insert(answer_table, tonumber(match))
+   end
+   local result_table = modpol.util.copy_table(options)
+   for i,v in ipairs(answer_table) do
+      -- flip the boolean on selected options
+      result_table[v][2] = not result_table[v][2]
+   end
+   func(result_table)
+end
+
+
+-- Function: modpol.interactions.binary_poll_user
 -- Params: user (string), question (string), func (function)
 --   func input: user input (string: y/n)
 -- Output: Applies "func" to user input