Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1017B

  1. import copy
  2. class Controller:
  3. def __init__(self, c):
  4. self.callbacks = []
  5. self.context = c
  6. self.functions = []
  7. self.isProcessingQueue = False
  8. self.queue = []
  9. def executeFunctions(self):
  10. c = self.queue.pop(0)
  11. for f in self.functions:
  12. ctx = f(c)
  13. if ctx.recentField != "none":
  14. self.queue.append(ctx)
  15. self.context = c
  16. self.reportContext()
  17. def processQueue(self):
  18. # Decline recursion.
  19. if self.isProcessingQueue:
  20. return
  21. self.isProcessingQueue = True
  22. while len(self.queue) > 0:
  23. self.executeFunctions()
  24. self.isProcessingQueue = False
  25. def registerCallback(self, cb):
  26. self.callbacks.append(cb)
  27. def registerFunction(self, f):
  28. self.functions.append(f)
  29. def reportContext(self):
  30. for cb in self.callbacks:
  31. cb(self.context)
  32. def set(self, fieldName, value):
  33. c = copy.deepcopy(self.context)
  34. setattr(c, fieldName, value)
  35. c.recentField = fieldName
  36. self.queue.append(c)
  37. self.processQueue()