process_old.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. old_request_format = {
  2. user=user, -- requesting user
  3. type="add_member", -- action
  4. params={user} -- action params
  5. }
  6. old_process_format = {
  7. type = "consent", -- delete
  8. id = nil,
  9. org_id = nil,
  10. request_id = nil, -- delete
  11. -- consent config
  12. majority_to_pass = 0.51, -- voting threshold
  13. votes_needed = nil,
  14. -- consent data
  15. total_votes = 0,
  16. votes_yes = {},
  17. votes_no = {}
  18. }
  19. new_process_format = {
  20. initiator = "user",
  21. status = "request",
  22. org_id = 12314,
  23. module = "create_child_org", -- policy table lookup
  24. process_id = 8347,
  25. timestamp = 1632850133, -- look into supporting other formats, overrides (turn based, etc.)
  26. data = {
  27. child_org_name = "oligarchy"
  28. },
  29. consent = {
  30. -- voter eligibilty frozen by action table invites
  31. start_time = 384179234,
  32. member_count = 14,
  33. votes_yes = {},
  34. votes_no = {}
  35. }
  36. }
  37. -- initialize values
  38. function init_consent(policy) {
  39. self.start_time = os.time()
  40. self.member_count = modpol.orgs.get_org(self.org_id):get_member_count()
  41. if policy.duration then
  42. -- mintest.after(time, func, ...args)
  43. -- should override modpol callback function
  44. register_callback(self.start_time + policy.duration)
  45. end
  46. if (duration and (consent_ratio or yes_threshold or no_threshold)) or (yes_threshold) or (consent_ratio) then
  47. -- well formed policy
  48. else
  49. -- invalid
  50. end
  51. }
  52. -- update vote count
  53. function update_consent(user, approve) {
  54. if approve then
  55. table.insert(yes_votes, user)
  56. else
  57. table.insert(no_votes, user)
  58. if not duration then
  59. eval_consent()
  60. end
  61. }
  62. -- evaluate state of vote
  63. function eval_consent() {
  64. consent_ratio = #yes_votes / (#yes_votes + #no_votes)
  65. quorum = (#yes_votes + #no_votes) / member_count
  66. if policy.duration then
  67. if policy.consent_ratio then
  68. if policy.quorum then
  69. if quorum < policy.quorum then
  70. fail()
  71. end
  72. end
  73. if consent_ratio >= policy.consent_ratio then
  74. pass()
  75. else
  76. fail()
  77. end
  78. elseif policy.yes_threshold then
  79. if #yes_votes >= policy.yes_threshold then
  80. pass()
  81. else
  82. fail()
  83. end
  84. elseif policy.no_threshold then
  85. if #no_votes <= policy.no_threshold then
  86. fail()
  87. else
  88. pass()
  89. end
  90. end
  91. elseif policy.yes_threshold then
  92. if policy.no_threshold then
  93. if #no_votes >= policy.no_threshold then
  94. fail()
  95. end
  96. if #yes_votes >= policy.yes_threshold then
  97. pass()
  98. end
  99. elseif policy.consent_ratio and policy.quorum then
  100. if quorum >= policy.quorum then
  101. if consent_ratio >= policy.consent_ratio then
  102. pass()
  103. else
  104. fail()
  105. end
  106. end
  107. end
  108. }
  109. policy_table_format = {
  110. "create_child_org": {
  111. defer_to = nil,
  112. -- duration
  113. duration = nil, -- evaluates end conditions when reached
  114. -- thesholds
  115. no_threshold = nil, -- fails if reached
  116. yes_threshold = nil, -- succeeds if reached
  117. --ratios
  118. consent_ratio = nil, -- % of voters
  119. quorum = nil, -- % of members that vote
  120. }
  121. "create_child_org": {
  122. consent_threshold = 0.51,
  123. max_duration = 89324, -- seconds until vote closes if threshold not reached, or nil for no limit
  124. defer = nil, -- org id to defer to, or nil
  125. }
  126. }