process.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "' .. 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. -- setting default params
  23. local new_process = {
  24. metatable = {__index = module},
  25. initiator = initiator,
  26. org = self,
  27. id = index
  28. }
  29. -- copying default fields from setup
  30. for k, v in pairs(module.setup) do
  31. new_process[k] = v
  32. end
  33. setmetatable(new_process, new_process.metatable)
  34. self.processes[index] = new_process
  35. -- sets default values for undeclared config variables
  36. for k, v in pairs(module.config) do
  37. if config[k] == nil then
  38. config[k] = v
  39. end
  40. end
  41. self.processes[index]:initiate(config, result))
  42. return index
  43. end
  44. function modpol.orgs:delete_process(id)
  45. self.processes[id] = 'deleted'
  46. end
  47. function modpol.orgs:add_pending_action(process_id, user, callback)
  48. self.pending[user] = self.pending[user] or {}
  49. self.pending[user][process_id] = callback
  50. end
  51. function modpol.orgs:remove_pending_action(process_id, user)
  52. if self.pending[user] then
  53. self.pending[user][process_id] = nil
  54. end
  55. end
  56. function modpol.orgs:wipe_pending_actions(process_id)
  57. for user in pairs(self.pending) do
  58. self.pending[user][process_id] = nil
  59. end
  60. end
  61. function modpol.orgs:has_pending_actions()
  62. -- next() will return the next pair in a table
  63. -- if next() returns nil, the table is empty
  64. if not self.pending[user] then
  65. return false
  66. else
  67. if not next(self.pending[user]) then
  68. return false
  69. else
  70. return true
  71. end
  72. end
  73. end
  74. function modpol.orgs:interact(process_id, user)
  75. local process = self.processes[process_id]
  76. if self.pending[user] then
  77. local callback = self.pending[user][process_id]
  78. if callback then
  79. process[callback](process, user)
  80. end
  81. end
  82. end