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.

51 lines
1.1KB

  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 registerFieldCallback(self, fieldName, cb):
  28. def execCB(c):
  29. if c.recentField == fieldName:
  30. cb(c)
  31. self.callbacks.append(execCB)
  32. def registerFunction(self, f):
  33. self.functions.append(f)
  34. def reportContext(self):
  35. for cb in self.callbacks:
  36. cb(self.context)
  37. def set(self, fieldName, value):
  38. c = copy.deepcopy(self.context)
  39. setattr(c, fieldName, value)
  40. c.recentField = fieldName
  41. self.queue.append(c)
  42. self.processQueue()