process.lua 1.9 KB

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