process.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function modpol.orgs:call_module(module_name, initiator)
  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. local new_process = module.create(initiator, self, index)
  23. self.processes[index] = new_process
  24. self.processes[index]:initiate()
  25. return index
  26. end
  27. function modpol.orgs:delete_process(id)
  28. self.processes[id] = 'deleted'
  29. end
  30. function modpol.orgs:add_pending_action(process_id, user, callback)
  31. self.pending[user] = self.pending[user] or {}
  32. self.pending[user][process_id] = callback
  33. end
  34. function modpol.orgs:remove_pending_action(process_id, user)
  35. if self.pending[user] then
  36. self.pending[user][process_id] = nil
  37. end
  38. end
  39. function modpol.orgs:wipe_pending_actions(process_id)
  40. for user in pairs(self.pending) do
  41. self.pending[user][process_id] = nil
  42. end
  43. end
  44. function modpol.orgs:has_pending_actions()
  45. -- next() will return the next pair in a table
  46. -- if next() returns nil, the table is empty
  47. if not self.pending[user] then
  48. return false
  49. else
  50. if not next(self.pending[user]) then
  51. return false
  52. else
  53. return true
  54. end
  55. end
  56. end
  57. function modpol.orgs:interact(process_id, user)
  58. local process = self.processes[process_id]
  59. if self.pending[user] then
  60. local callback = self.pending[user][process_id]
  61. if callback then
  62. process[callback](process, user)
  63. end
  64. end
  65. end