host.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. else:
  46. # 如果是其他人像
  47. pass
  48. def on_last_will(self, player: str, data: dict):
  49. pass
  50. def on_task_start(self, player: str, data: dict):
  51. pass
  52. def on_strategy(self, player: str, data: dict):
  53. pass
  54. def update_multi_tasks(self):
  55. """
  56. 作为主角, 更新自身的多人任务的缓存
  57. 也就是从 config -> multi_tasks
  58. :return:
  59. """
  60. tasks = {'orochi': self.config.model.orochi,
  61. 'fallen_sun': self.config.model.fallen_sun,
  62. 'eternity_sea': self.config.model.eternity_sea,
  63. 'evo_zone': self.config.model.evo_zone,
  64. 'exploration': self.config.model.exploration
  65. }
  66. for key, value in tasks.items():
  67. task_name: str = key
  68. limit_time: time = None
  69. limit_count: int = None
  70. role: str = None
  71. if key == 'orochi':
  72. limit_time = value.orochi_config.limit_time
  73. limit_count = value.orochi_config.limit_count
  74. role = str(value.orochi_config.user_status)
  75. elif key == 'fallen_sun':
  76. limit_time = value.fallen_sun_config.limit_time
  77. limit_count = value.fallen_sun_config.limit_count
  78. role = str(value.fallen_sun_config.user_status)
  79. elif key == 'eternity_sea':
  80. limit_time = value.eternity_sea_config.limit_time
  81. limit_count = value.eternity_sea_config.limit_count
  82. role = str(value.eternity_sea_config.user_status)
  83. elif key == 'evo_zone':
  84. limit_time = value.evo_zone_config.limit_time
  85. limit_count = value.evo_zone_config.limit_count
  86. role = str(value.evo_zone_config.user_status)
  87. elif key == 'exploration':
  88. # TODO 等待探索完成
  89. limit_time = time(minute=30)
  90. limit_count = 50
  91. role = 'leader'
  92. continue
  93. else:
  94. continue
  95. if not value.scheduler.enable:
  96. continue
  97. if task_name not in self.multi_tasks:
  98. # 第一次
  99. logger.info(f'First time to add [{task_name}] to multi_tasks')
  100. self.multi_tasks[task_name] = Task(
  101. next_run=value.scheduler.next_run,
  102. target_run=value.scheduler.next_run,
  103. limit_time=limit_time,
  104. team_task=True,
  105. role=role,
  106. limit_count=limit_count,
  107. )
  108. else:
  109. # 更新
  110. logger.info(f'Update {task_name} to multi_tasks')
  111. self.multi_tasks[task_name].update_info(next_run=value.scheduler.next_run,
  112. limit_time=limit_time,
  113. role=role,
  114. limit_count=limit_count)
  115. def update_strategy(self):
  116. """
  117. 更新策略
  118. :return:
  119. """
  120. pass
  121. if __name__ == '__main__':
  122. host = Host(Config('oas1'))
  123. sleep(20)
  124. # 更新config -> multi_tasks -> publish_data
  125. host.update_multi_tasks()
  126. host.q_publish.put(['Strategy' ,host.publish_data()])
  127. host.q_publish.put(['Strategy' ,host.publish_data()])
  128. host.q_publish.put(['Strategy' ,host.publish_data()])
  129. sleep(46)