process.lua 2.5 KB

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