2
0

process.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. register_callback(self.start_time + policy.duration)
  43. end
  44. if (duration and (consent_ratio or yes_threshold or no_threshold)) or (yes_threshold) or (consent_ratio) then
  45. -- well formed policy
  46. else
  47. -- invalid
  48. end
  49. }
  50. -- update vote count
  51. function update_consent(user, approve) {
  52. if approve then
  53. table.insert(yes_votes, user)
  54. else
  55. table.insert(no_votes, user)
  56. if not duration then
  57. eval_consent()
  58. end
  59. }
  60. -- evaluate state of vote
  61. function eval_consent() {
  62. consent_ratio = #yes_votes / (#yes_votes + #no_votes)
  63. quorum = (#yes_votes + #no_votes) / member_count
  64. if policy.duration then
  65. if policy.consent_ratio then
  66. if policy.quorum then
  67. if quorum < policy.quorum then
  68. fail()
  69. end
  70. end
  71. if consent_ratio >= policy.consent_ratio then
  72. pass()
  73. else
  74. fail()
  75. end
  76. elseif policy.yes_threshold then
  77. if #yes_votes >= policy.yes_threshold then
  78. pass()
  79. else
  80. fail()
  81. end
  82. elseif policy.no_threshold then
  83. if #no_votes <= policy.no_threshold then
  84. fail()
  85. else
  86. pass()
  87. end
  88. end
  89. elseif policy.yes_threshold then
  90. if policy.no_threshold then
  91. if #no_votes >= policy.no_threshold then
  92. fail()
  93. end
  94. if #yes_votes >= policy.yes_threshold then
  95. pass()
  96. end
  97. elseif policy.consent_ratio and policy.quorum then
  98. if quorum >= policy.quorum then
  99. if consent_ratio >= policy.consent_ratio then
  100. pass()
  101. else
  102. fail()
  103. end
  104. end
  105. end
  106. }
  107. policy_table_format = {
  108. "create_child_org": {
  109. defer_to = nil,
  110. -- duration
  111. duration = nil, -- evaluates end conditions when reached
  112. -- thesholds
  113. no_threshold = nil, -- fails if reached
  114. yes_threshold = nil, -- succeeds if reached
  115. --ratios
  116. consent_ratio = nil, -- % of voters
  117. quorum = nil, -- % of members that vote
  118. }
  119. "create_child_org": {
  120. consent_threshold = 0.51,
  121. max_duration = 89324, -- seconds until vote closes if threshold not reached, or nil for no limit
  122. defer = nil, -- org id to defer to, or nil
  123. }
  124. }