host.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import json
  5. from time import sleep
  6. from datetime import datetime, time
  7. from cached_property import cached_property
  8. from tasks.GlobalGame.config import TeamFlow, Transport
  9. from module.team_flow.mqtt import Mqtt
  10. from module.config.config import Config
  11. from module.team_flow.player import Player
  12. from module.team_flow.task import Task
  13. from module.logger import logger
  14. def on_message(client, userdata, msg):
  15. try:
  16. data = json.loads(msg.payload)
  17. except json.JSONDecodeError:
  18. logger.error(f'Get [{msg.topic}]: {msg.payload}')
  19. return
  20. logger.info(f'Get {msg.topic}: {data}')
  21. for username, data in data.items():
  22. # 反正只有一项
  23. if username == userdata.username:
  24. continue
  25. userdata.match_topic[msg.topic](username, data)
  26. class Host(Mqtt, Player):
  27. def __init__(self, config: Config):
  28. Player.__init__(self, config.global_game.team_flow.username)
  29. Mqtt.__init__(self, config.global_game.team_flow)
  30. self.config = config
  31. self.players: list[Player] = []
  32. self.set_on_message(on_message)
  33. @cached_property
  34. def match_topic(self):
  35. return {
  36. 'FirstNotice': self.on_first_notice,
  37. 'LastWill': self.on_last_will,
  38. 'TaskStart': self.on_task_start,
  39. 'Strategy': self.on_strategy
  40. }
  41. def on_first_notice(self, player: str, data: dict):
  42. # 其他人上线,就广播自己的策略出去
  43. if 'type' in data:
  44. logger.info(f'Player {player} is online')
  45. self.q_publish.put(['Strategy', self.publish_data()])
  46. else:
  47. # 如果是其他人像
  48. pass
  49. def on_last_will(self, player: str, data: dict):
  50. if player not in self.players:
  51. logger.warning(f'Player {player} is not in players')
  52. return
  53. self.players.remove(player)
  54. logger.info(f'Player {player} is offline')
  55. self._config_to_player()
  56. self._update_strategy()
  57. self._player_to_config()
  58. self.q_publish.put(['Strategy', self.publish_data()])
  59. def on_task_start(self, player: str, data: dict):
  60. pass
  61. def on_strategy(self, player: str, data: dict):
  62. pass
  63. def _config_to_player(self):
  64. """
  65. 作为主角, 更新自身的多人任务的缓存
  66. 也就是从 config -> multi_tasks
  67. :return:
  68. """
  69. tasks = {'orochi': self.config.model.orochi,
  70. 'fallen_sun': self.config.model.fallen_sun,
  71. 'eternity_sea': self.config.model.eternity_sea,
  72. 'evo_zone': self.config.model.evo_zone,
  73. 'exploration': self.config.model.exploration
  74. }
  75. for key, value in tasks.items():
  76. task_name: str = key
  77. limit_time: time = None
  78. limit_count: int = None
  79. role: str = None
  80. if key == 'orochi':
  81. limit_time = value.orochi_config.limit_time
  82. limit_count = value.orochi_config.limit_count
  83. role = str(value.orochi_config.user_status)
  84. elif key == 'fallen_sun':
  85. limit_time = value.fallen_sun_config.limit_time
  86. limit_count = value.fallen_sun_config.limit_count
  87. role = str(value.fallen_sun_config.user_status)
  88. elif key == 'eternity_sea':
  89. limit_time = value.eternity_sea_config.limit_time
  90. limit_count = value.eternity_sea_config.limit_count
  91. role = str(value.eternity_sea_config.user_status)
  92. elif key == 'evo_zone':
  93. limit_time = value.evo_zone_config.limit_time
  94. limit_count = value.evo_zone_config.limit_count
  95. role = str(value.evo_zone_config.user_status)
  96. elif key == 'exploration':
  97. # TODO 等待探索完成
  98. limit_time = time(minute=30)
  99. limit_count = 50
  100. role = 'leader'
  101. continue
  102. else:
  103. continue
  104. if not value.scheduler.enable:
  105. continue
  106. if task_name not in self.multi_tasks:
  107. # 第一次
  108. logger.info(f'First time to add [{task_name}] to multi_tasks')
  109. self.multi_tasks[task_name] = Task(
  110. next_run=value.scheduler.next_run,
  111. target_run=value.scheduler.next_run,
  112. limit_time=limit_time,
  113. team_task=True,
  114. role=role,
  115. limit_count=limit_count,
  116. )
  117. else:
  118. # 更新
  119. logger.info(f'Update {task_name} to multi_tasks')
  120. self.multi_tasks[task_name].update_info(next_run=value.scheduler.next_run,
  121. limit_time=limit_time,
  122. role=role,
  123. limit_count=limit_count)
  124. def _player_to_config(self):
  125. for key, value in self.multi_tasks.items():
  126. if key == 'orochi':
  127. self.config.task_delay(task='Orochi', target=value.next_run)
  128. elif key == 'fallen_sun':
  129. self.config.task_delay(task='FallenSun', target=value.next_run)
  130. elif key == 'eternity_sea':
  131. self.config.task_delay(task='EternitySea', target=value.next_run)
  132. elif key == 'evo_zone':
  133. self.config.task_delay(task='EvoZone', target=value.next_run)
  134. elif key == 'exploration':
  135. # TODO 等待探索完成
  136. pass
  137. else:
  138. continue
  139. def _update_strategy(self):
  140. """
  141. 更新策略:
  142. :return:
  143. """
  144. pass
  145. if __name__ == '__main__':
  146. host = Host(Config('oas1'))
  147. sleep(20)
  148. # 更新config -> multi_tasks -> publish_data
  149. host._config_to_player()
  150. host.q_publish.put(['Strategy' ,host.publish_data()])
  151. host.q_publish.put(['Strategy' ,host.publish_data()])
  152. host.q_publish.put(['Strategy' ,host.publish_data()])
  153. sleep(46)