process.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. --- Process functions for orgs
  2. function modpol.orgs:call_module(module_slug, initiator, config, result)
  3. if not modpol.modules[module_slug] then
  4. modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_slug .. '" not found')
  5. return
  6. end
  7. local empty_index = nil
  8. -- linear search for empty process slots (lazy deletion)
  9. for k, v in ipairs(self.processes) do
  10. if v == 'deleted' then
  11. empty_index = k
  12. break
  13. end
  14. end
  15. local index
  16. -- attempts to fill empty spots in list, otherwise appends to end
  17. if empty_index then
  18. index = empty_index
  19. else
  20. index = #self.processes + 1
  21. end
  22. local module = modpol.modules[module_slug]
  23. -- sets default values for undeclared config variables
  24. if #module.config > 0 then
  25. for k, v in pairs(module.config) do
  26. if config[k] == nil then
  27. config[k] = v
  28. end
  29. end
  30. end
  31. -- setting default params
  32. local new_process = {
  33. metatable = {__index = module},
  34. initiator = initiator,
  35. org = self,
  36. id = index,
  37. config = config,
  38. data = module.data,
  39. slug = module_slug
  40. }
  41. setmetatable(new_process, new_process.metatable)
  42. self.processes[index] = new_process
  43. self.processes[index]:initiate(result)
  44. return index
  45. end
  46. function modpol.orgs:delete_process(id)
  47. self.processes[id] = 'deleted'
  48. end
  49. function modpol.orgs:add_pending_action(process_id, user, callback)
  50. self.pending[user] = self.pending[user] or {}
  51. self.pending[user][process_id] = callback
  52. end
  53. function modpol.orgs:remove_pending_action(process_id, user)
  54. if self.pending[user] then
  55. self.pending[user][process_id] = nil
  56. end
  57. end
  58. function modpol.orgs:wipe_pending_actions(process_id)
  59. for user in pairs(self.pending) do
  60. self.pending[user][process_id] = nil
  61. end
  62. end
  63. function modpol.orgs:has_pending_actions(user)
  64. -- next() will return the next pair in a table
  65. -- if next() returns nil, the table is empty
  66. if not self.pending[user] then
  67. return false
  68. else
  69. if not next(self.pending[user]) then
  70. return false
  71. else
  72. return true
  73. end
  74. end
  75. end
  76. function modpol.orgs:interact(process_id, user)
  77. local process = self.processes[process_id]
  78. modpol.interactions.message(user,"hi!")
  79. if self.pending[user] then
  80. modpol.interactions.message(user,"id: "..process_id)
  81. local callback = self.pending[user][process_id]
  82. if callback then
  83. modpol.interactions.message(user,"la!")
  84. process[callback](process, user)
  85. end
  86. end
  87. end