Browse Source

Got first token modules working, and a bunch of bugfixes throughout

Nathan Schneider 2 years ago
parent
commit
286d131839

+ 1 - 0
login.lua

@@ -1,5 +1,6 @@
 dofile("modpol_core/modpol.lua")
 
+modpol.instance.members = {}
 modpol.orgs.reset()
 
 print("Log in as which user?")

+ 3 - 0
modpol_core/api.lua

@@ -15,6 +15,7 @@ dofile (localdir .. "/interactions/interactions.lua")
 dofile (localdir .. "/modules/add_child_org_consent.lua")
 dofile (localdir .. "/modules/change_modules.lua")
 dofile (localdir .. "/modules/consent.lua")
+dofile (localdir .. "/modules/create_token.lua")
 dofile (localdir .. "/modules/defer_consent.lua")
 dofile (localdir .. "/modules/join_org_consent.lua")
 dofile (localdir .. "/modules/leave_org.lua")
@@ -25,3 +26,5 @@ dofile (localdir .. "/modules/remove_member_consent.lua")
 dofile (localdir .. "/modules/remove_org_consent.lua")
 dofile (localdir .. "/modules/remove_org.lua")
 dofile (localdir .. "/modules/rename_org_consent.lua")
+dofile (localdir .. "/modules/send_token.lua")
+dofile (localdir .. "/modules/tokenomics.lua")

+ 6 - 0
modpol_core/interactions/interactions.lua

@@ -269,3 +269,9 @@ end
 -- output: gets question from initiator, asks all org members, broadcasts answers
 
 
+-- TESTING
+
+--testing command
+function modpol.msg(text)
+   modpol.interactions.message("TEST MSG: ",text)
+end

+ 0 - 2
modpol_core/modules/change_modules.lua

@@ -148,12 +148,10 @@ function change_modules:propose_change(type, mod_text)
                "Consent reached:\nAdding \""
                ..mod_text.."\" to org "..self.org.name)
          elseif type == "remove" then
-            modpol.msg("Removing!")
             local i = 0
             for k,v in pairs(self.org.modules) do
                i = i + 1
                if v.name == mod_text then
-                  modpol.msg("got it!")
                   self.org.modules[k] = nil
                end                  
             end

+ 53 - 0
modpol_core/modules/create_token.lua

@@ -0,0 +1,53 @@
+--- create_token
+-- @module create_token
+-- depends on tokenomics
+
+local create_token = {
+    name = "Create a token (consent)",
+    slug = "create_token",
+    desc = "With org consent, creates an org token",
+    hide = false;
+}
+
+--- (Required) Data for module
+-- Variables that module uses during the course of a process
+-- Can be blank
+create_token.data = {
+}
+
+create_token.config = {
+   token_name = ""
+}
+
+--- (Required): initiate function
+-- @param result (optional) Callback if this module is embedded in other modules
+-- @function initiate
+function create_token:initiate(result)
+   modpol.interactions.text_query(
+      self.initiator,
+      "Token name (alpha-numeric, no spaces):",
+      function(input)
+         self.config.token_name = input
+         self.org:call_module(
+            "tokenomics",
+            self.initiator,
+            {
+               consent = true,
+               token_slug = self.config.token_name
+            },
+            function(input2)
+               modpol.interactions.org_dashboard(
+                  self.initiator, self.org.name)
+               if result then result() end
+               -- call this wherever process might end:
+               self.org:delete_process(self.id)
+            end
+         )
+         modpol.interactions.org_dashboard(
+            self.initiator, self.org.name)
+      end
+   )
+end
+
+--- (Required) Add to module table
+modpol.modules.create_token = create_token

+ 77 - 0
modpol_core/modules/send_token.lua

@@ -0,0 +1,77 @@
+--- send_token
+-- @module send_token
+-- depends on tokenomics
+
+local send_token = {
+    name = "Send tokens",
+    slug = "send_token",
+    desc = "Send tokens to another user",
+    hide = false;
+}
+
+--- (Required) Data for module
+-- Variables that module uses during the course of a process
+-- Can be blank
+send_token.data = {
+}
+
+send_token.config = {
+   token_name = ""
+}
+
+--- (Required): initiate function
+-- @param result (optional) Callback if this module is embedded in other modules
+-- @function initiate
+function send_token:initiate(result)
+   local token_list = {}
+   if self.org.tokens then
+      for k,v in pairs(self.org.tokens) do
+         table.insert(token_list, k)
+      end
+   end
+   if token_list == {} then
+      modpol.interactions.message(
+         self.initiator,
+         "No tokens in org")
+      modpol.interactions.org_dashboard(
+         self.initiator, self.org.name)
+      self.org:delete_process(self.id)
+      return
+   else
+      modpol.interactions.dropdown_query(
+         self.initiator,
+         "Which token do you want to send?",
+         token_list,
+         function(input_token)
+            modpol.interactions.dropdown_query(
+               self.initiator,
+               "Who do you want to send to?",
+               modpol.util.copy_table(self.org.members),
+               function(input_recipient)
+                  modpol.interactions.text_query(
+                     self.initiator,
+                     "How much do you want to give (a number)?",
+                     function(input_amount)
+                        modpol.modules.tokenomics.transfer(
+                           self.org.id,
+                           input_token,
+                           self.initiator,
+                           input_recipient,
+                           input_amount
+                        )
+                        modpol.interactions.org_dashboard(
+                           self.initiator, self.org.name)
+                        -- close process
+                        if result then result() end
+                        self.org:delete_process(self.id)
+                     end
+                  )
+               end
+            )
+         end
+      )
+   end
+end
+
+--- (Required) Add to module table
+modpol.modules.send_token = send_token

+ 13 - 5
modpol_core/modules/tokenomics.lua

@@ -1,5 +1,6 @@
 --- tokenomics
 -- @module tokenomics
+-- Depends on consent
 
 local tokenomics = {
     name = "Tokenomics",
@@ -50,10 +51,15 @@ function tokenomics:initiate(result)
             "consent",
             self.initiator,
             {
-               prompt = "Create token "..self.token_slug.."?",
+               prompt = "Create token "..
+                  self.config.token_slug.."?",
                votes_required = #self.org.members
             },
             function()
+               modpol.interactions.message_org(
+                  self.initiator, self.org.id,
+                  "Consent reached: creating token "..
+                  self.config.token_slug)
                self:create_token()
             end
          )
@@ -65,11 +71,13 @@ end
 
 function tokenomics:create_token()
    if not self.org.tokens then self.org.tokens = {} end
-   self.org.tokens[slug] = self.config.token_variables
+   self.org.tokens[self.config.token_slug] =
+      self.config.token_variables
+   self.org:record("Created token "..self.config.token_slug,
+                   self.slug)
    modpol.interactions.message_org(
       self.initiator, self.org.id,
-      "Token "..self.config.token_slug.." created")
-      -- TODO need to add to persistent storage?
+      "Created token "..self.config.token_slug)
    if self.data.result then self.data.result() end
    -- call this wherever process might end:
    self.org:delete_process(self.id)
@@ -135,7 +143,7 @@ end
 function tokenomics.transfer(org, token, sender, recipient, amount)
    local sender_balance = tokenomics.balance(org, token, sender)
    local recipient_balance = tokenomics.balance(org, token, recipient)
-   if not sender_balance or recipient balance then
+   if not sender_balance or recipient_balance then
       return nil, "Transfer failed, user not found"
    else
       sender_balance = sender_balance - amount

+ 8 - 4
modpol_core/orgs/process.lua

@@ -26,7 +26,7 @@ function modpol.orgs:call_module(module_slug, initiator, config, result)
     local module = modpol.modules[module_slug]
 
     -- sets default values for undeclared config variables
-    if #module.config > 0 then
+    if modpol.util.num_pairs(module.config) > 0 and config then
         for k, v in pairs(module.config) do
             if config[k] == nil then
                 config[k] = v
@@ -34,6 +34,7 @@ function modpol.orgs:call_module(module_slug, initiator, config, result)
         end
     end
 
+
     -- setting default params
     local new_process = {
         metatable = {__index = module},
@@ -101,11 +102,14 @@ function modpol.orgs:interact(process_id, user)
    if self.pending[user] then
       local callback = self.pending[user][process_id]
       if callback then
+         -- get data in case callback ends process
+         local slug = self.processes[process_id].slug
+         -- run callback
          process[callback](process, user)
          -- record org data
-         local msg = "Updating "..self.processes[process_id].slug..
-      " process id "..process_id.." in org "..self.name
-         self:record(msg, self.processes[process_id].slug)
+         local msg = "Updating "..slug..
+            " process id "..process_id.." in org "..self.name
+         self:record(msg, slug)
       end
    end
 end

+ 9 - 2
modpol_core/util/misc.lua

@@ -1,11 +1,18 @@
 modpol.util = {}
 
 --- @function modpol.copy_table
+-- @param t table
 -- Returns a copy of the table inputted
 function modpol.util.copy_table(t)
    local t2 = {}
-   for k,v in pairs(t) do
-      t2[k] = v
+   if ipairs(t) then
+      for i,v in ipairs(t) do
+         t2[i] = v
+      end
+   elseif pairs(t) then
+      for k,v in pairs(t) do
+         t2[k] = v
+      end
    end
    return t2
 end

+ 0 - 5
modpol_minetest/api.lua

@@ -11,11 +11,6 @@ local localdir = minetest.get_modpath("modpol") .. "/modpol_minetest"
 dofile (localdir .. "/overrides/interactions.lua")
 dofile (localdir .. "/overrides/users.lua")
 
---testing command for "singleplayer"
-function modpol.msg(text)
-   modpol.interactions.message("singleplayer",text)
-end
-
 -- ===================================================================
 -- Minetest Chatcommands
 -- ===================================================================

+ 1 - 0
modpol_minetest/chatcommands.lua

@@ -34,6 +34,7 @@ regchat(
    "mptest", {
       privs = {privs=true},
       func = function(user)
+         modpol.instance.members = modpol.list_users()
          modpol.orgs.reset()
          modpol.interactions.dashboard(user)
          return true, "Reset orgs"

+ 1 - 1
modpol_minetest/modules/priv_to_org.lua

@@ -20,7 +20,7 @@ priv_to_org.config = {
 function priv_to_org:initiate(result) 
    local player_privs = minetest.get_player_privs(self.initiator)
    -- construct table for display
-   local player_privs_table = {"View..."}
+   local player_privs_table = {}
    for k,v in pairs(player_privs) do
       if player_privs[k] then
          table.insert(player_privs_table,k)

+ 10 - 0
modpol_minetest/overrides/interactions.lua

@@ -218,6 +218,9 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
                   function(input)
                      if input == "Yes" then
                         org:call_module(module.slug, pname)
+                     elseif input == "No" then
+                        modpol.interactions.org_dashboard(
+                           pname, org.id)
                      end
                end)
             end
@@ -389,3 +392,10 @@ minetest.register_on_player_receive_fields(function (player, formname, fields)
          minetest.close_formspec(pname, formname)
       end
 end)
+
+-- TESTING
+
+--testing command for "singleplayer"
+function modpol.msg(text)
+   modpol.interactions.message("singleplayer",text)
+end