Research portable Memory game | Исследовать портируемую игру Память
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

54 行
1.3KB

  1. import copy
  2. class ctx_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.recentField = c.recentField
  16. self.context.setField(c.recentField, c.field(c.recentField))
  17. self.reportContext()
  18. def processQueue(self):
  19. # Decline recursion.
  20. if self.isProcessingQueue:
  21. return
  22. self.isProcessingQueue = True
  23. while len(self.queue) > 0:
  24. self.executeFunctions()
  25. self.isProcessingQueue = False
  26. def registerCallback(self, cb):
  27. self.callbacks.append(cb)
  28. def registerFieldCallback(self, fieldName, cb):
  29. self.callbacks.append(lambda c: cb(c) if c.recentField == fieldName else None)
  30. def registerFunction(self, f):
  31. self.functions.append(f)
  32. def registerFunctions(self, funcs):
  33. for f in funcs:
  34. self.functions.append(f)
  35. def reportContext(self):
  36. for cb in self.callbacks:
  37. cb(self.context)
  38. def set(self, fieldName, value):
  39. c = copy.deepcopy(self.context)
  40. c.setField(fieldName, value)
  41. c.recentField = fieldName
  42. self.queue.append(c)
  43. self.processQueue()