You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 9.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import structures as THUAI6
  2. from typing import Final, List
  3. import proto.Message2Clients_pb2 as Message2Clients
  4. import proto.Message2Server_pb2 as Message2Server
  5. import proto.MessageType_pb2 as MessageType
  6. numOfGridPerCell: Final[int] = 1000
  7. # 起到NameSpace的作用
  8. class NoInstance:
  9. def __call__(self, *args, **kwargs):
  10. raise TypeError("This class cannot be instantiated.")
  11. class AssistFunction(NoInstance):
  12. # 辅助函数
  13. @staticmethod
  14. def CellToGrid(cell: int) -> int:
  15. return cell*numOfGridPerCell+numOfGridPerCell//2
  16. @staticmethod
  17. def GridToCell(grid: int) -> int:
  18. return grid//numOfGridPerCell
  19. class Proto2THUAI6(NoInstance):
  20. placeTypeDict: Final[dict] = {
  21. MessageType.NULL_PLACE_TYPE: THUAI6.PlaceType.NullPlaceType,
  22. MessageType.WALL: THUAI6.PlaceType.Wall,
  23. MessageType.LAND: THUAI6.PlaceType.Land,
  24. MessageType.GRASS: THUAI6.PlaceType.Grass,
  25. MessageType.MACHINE: THUAI6.PlaceType.Machine,
  26. MessageType.GATE: THUAI6.PlaceType.Gate,
  27. MessageType.HIDDEN_GATE: THUAI6.PlaceType.HiddenGate}
  28. shapeTypeDict: Final[dict] = {
  29. MessageType.NULL_SHAPE_TYPE: THUAI6.ShapeType.NullShapeType,
  30. MessageType.SQUARE: THUAI6.ShapeType.Square,
  31. MessageType.CIRCLE: THUAI6.ShapeType.Circle}
  32. propTypeDict: Final[dict] = {
  33. MessageType.NULL_PROP_TYPE: THUAI6.PropType.NullPropType,
  34. MessageType.PTYPE1: THUAI6.PropType.PropType1, }
  35. playerTypeDict: Final[dict] = {
  36. MessageType.NULL_PLAYER_TYPE: THUAI6.PlayerType.NullPlayerType,
  37. MessageType.HUMAN_PLAYER: THUAI6.PlayerType.HumanPlayer,
  38. MessageType.BUTCHER_PLAYER: THUAI6.PlayerType.ButcherPlayer}
  39. humanTypeDict: Final[dict] = {
  40. MessageType.NULL_HUMAN_TYPE: THUAI6.HumanType.NullHumanType,
  41. MessageType.HUMANTYPE1: THUAI6.HumanType.HumanType1, }
  42. butcherTypeDict: Final[dict] = {
  43. MessageType.NULL_BUTCHER_TYPE: THUAI6.ButcherType.NullButcherType,
  44. MessageType.BUTCHERTYPE1: THUAI6.ButcherType.ButcherType1, }
  45. humanBuffTypeDict: Final[dict] = {
  46. MessageType.NULL_HBUFF_TYPE: THUAI6.HumanBuffType.NullHumanBuffType,
  47. MessageType.HBUFFTYPE1: THUAI6.HumanBuffType.HumanBuffType1, }
  48. butcherBuffTypeDict: Final[dict] = {
  49. MessageType.NULL_BBUFF_TYPE: THUAI6.ButcherBuffType.NullButcherBuffType,
  50. MessageType.BBUFFTYPE1: THUAI6.ButcherBuffType.ButcherBuffType1, }
  51. humanStateDict: Final[dict] = {
  52. MessageType.NULL_STATUS: THUAI6.HumanState.NullHumanState,
  53. MessageType.IDLE: THUAI6.HumanState.Idle,
  54. MessageType.FIXING: THUAI6.HumanState.Fixing,
  55. MessageType.DYING: THUAI6.HumanState.Dying,
  56. MessageType.ON_CHAIR: THUAI6.HumanState.OnChair,
  57. MessageType.DEAD: THUAI6.HumanState.Dead}
  58. gameStateDict: Final[dict] = {
  59. MessageType.NULL_GAME_STATE: THUAI6.GameState.NullGameState,
  60. MessageType.GAME_START: THUAI6.GameState.GameStart,
  61. MessageType.GAME_RUNNING: THUAI6.GameState.GameRunning,
  62. MessageType.GAME_END: THUAI6.GameState.GameEnd}
  63. # 用于将Proto的对象转为THUAI6的对象
  64. @staticmethod
  65. def Protobuf2THUAI6Butcher(butcherMsg: Message2Clients.MessageOfButcher) -> THUAI6.Butcher:
  66. butcher = THUAI6.Butcher()
  67. butcher.x = butcherMsg.x
  68. butcher.y = butcherMsg.y
  69. butcher.speed = butcherMsg.speed
  70. butcher.damage = butcherMsg.damage
  71. butcher.timeUntilSkillAvailable = butcherMsg.time_until_skill_available
  72. butcher.place = Proto2THUAI6.placeTypeDict[butcherMsg.place]
  73. butcher.prop = Proto2THUAI6.propTypeDict[butcherMsg.prop]
  74. butcher.butcherType = Proto2THUAI6.butcherTypeDict[butcherMsg.butcher_type]
  75. butcher.guid = butcherMsg.guid
  76. butcher.movable = butcherMsg.movable
  77. butcher.playerID = butcherMsg.player_id
  78. butcher.viewRange = butcherMsg.view_range
  79. butcher.radius = butcherMsg.radius
  80. butcher.buff.clear()
  81. for buff in butcherMsg.buff:
  82. butcher.buff.append(Proto2THUAI6.butcherBuffTypeDict[buff])
  83. return butcher
  84. @staticmethod
  85. def Protobuf2THUAI6Human(humanMsg: Message2Clients.MessageOfHuman) -> THUAI6.Human:
  86. human = THUAI6.Human()
  87. human.x = humanMsg.x
  88. human.y = humanMsg.y
  89. human.speed = humanMsg.speed
  90. human.life = humanMsg.life
  91. human.hangedTime = humanMsg.hanged_time
  92. human.timeUntilSkillAvailable = humanMsg.time_until_skill_available
  93. human.place = Proto2THUAI6.placeTypeDict[humanMsg.place]
  94. human.prop = Proto2THUAI6.propTypeDict[humanMsg.prop]
  95. human.humanType = Proto2THUAI6.humanTypeDict[humanMsg.human_type]
  96. human.guid = humanMsg.guid
  97. human.state = Proto2THUAI6.humanStateDict[humanMsg.state]
  98. human.playerID = humanMsg.player_id
  99. human.viewRange = humanMsg.view_range
  100. human.radius = humanMsg.radius
  101. human.buff.clear()
  102. for buff in humanMsg.buff:
  103. human.buff.append(Proto2THUAI6.humanBuffTypeDict[buff])
  104. return human
  105. @staticmethod
  106. def Protobuf2THUAI6Prop(propMsg: Message2Clients.MessageOfProp) -> THUAI6.Prop:
  107. prop = THUAI6.Prop()
  108. prop.x = propMsg.x
  109. prop.y = propMsg.y
  110. prop.type = Proto2THUAI6.propTypeDict[propMsg.type]
  111. prop.guid = propMsg.guid
  112. prop.size = propMsg.size
  113. prop.facingDirection = propMsg.facing_direction
  114. prop.isMoving = propMsg.is_moving
  115. return prop
  116. @staticmethod
  117. def Protobuf2THUAI6Map(mapMsg: Message2Clients.MessageOfMap) -> List[List[THUAI6.PlaceType]]:
  118. map = []
  119. for row in mapMsg.row:
  120. newRow = []
  121. for place in row.col:
  122. newRow.append(Proto2THUAI6.placeTypeDict[place])
  123. map.append(newRow)
  124. return map
  125. class THUAI62Proto(NoInstance):
  126. placeTypeDict: Final[dict] = {
  127. THUAI6.PlaceType.NullPlaceType: MessageType.NULL_PLACE_TYPE,
  128. THUAI6.PlaceType.Wall: MessageType.WALL,
  129. THUAI6.PlaceType.Land: MessageType.LAND,
  130. THUAI6.PlaceType.Grass: MessageType.GRASS,
  131. THUAI6.PlaceType.Machine: MessageType.MACHINE,
  132. THUAI6.PlaceType.Gate: MessageType.GATE,
  133. THUAI6.PlaceType.HiddenGate: MessageType.HIDDEN_GATE}
  134. shapeTypeDict: Final[dict] = {
  135. THUAI6.ShapeType.NullShapeType: MessageType.NULL_SHAPE_TYPE,
  136. THUAI6.ShapeType.Square: MessageType.SQUARE,
  137. THUAI6.ShapeType.Circle: MessageType.CIRCLE}
  138. propTypeDict: Final[dict] = {
  139. THUAI6.PropType.NullPropType: MessageType.NULL_PROP_TYPE,
  140. THUAI6.PropType.PropType1: MessageType.PTYPE1, }
  141. playerTypeDict: Final[dict] = {
  142. THUAI6.PlayerType.NullPlayerType: MessageType.NULL_PLAYER_TYPE,
  143. THUAI6.PlayerType.HumanPlayer: MessageType.HUMAN_PLAYER,
  144. THUAI6.PlayerType.ButcherPlayer: MessageType.BUTCHER_PLAYER}
  145. humanTypeDict: Final[dict] = {
  146. THUAI6.HumanType.NullHumanType: MessageType.NULL_HUMAN_TYPE,
  147. THUAI6.HumanType.HumanType1: MessageType.HUMANTYPE1, }
  148. butcherTypeDict: Final[dict] = {
  149. THUAI6.ButcherType.NullButcherType: MessageType.NULL_BUTCHER_TYPE,
  150. THUAI6.ButcherType.ButcherType1: MessageType.BUTCHERTYPE1, }
  151. humanBuffTypeDict: Final[dict] = {
  152. THUAI6.HumanBuffType.NullHumanBuffType: MessageType.NULL_HBUFF_TYPE,
  153. THUAI6.HumanBuffType.HumanBuffType1: MessageType.HBUFFTYPE1, }
  154. butcherBuffTypeDict: Final[dict] = {
  155. THUAI6.ButcherBuffType.NullButcherBuffType: MessageType.NULL_BBUFF_TYPE,
  156. THUAI6.ButcherBuffType.ButcherBuffType1: MessageType.BBUFFTYPE1, }
  157. humanStateDict: Final[dict] = {
  158. THUAI6.HumanState.NullHumanState: MessageType.NULL_STATUS,
  159. THUAI6.HumanState.Idle: MessageType.IDLE,
  160. THUAI6.HumanState.Fixing: MessageType.FIXING,
  161. THUAI6.HumanState.Dying: MessageType.DYING,
  162. THUAI6.HumanState.OnChair: MessageType.ON_CHAIR,
  163. THUAI6.HumanState.Dead: MessageType.DEAD}
  164. gameStateDict: Final[dict] = {
  165. THUAI6.GameState.NullGameState: MessageType.NULL_GAME_STATE,
  166. THUAI6.GameState.GameStart: MessageType.GAME_START,
  167. THUAI6.GameState.GameRunning: MessageType.GAME_RUNNING,
  168. THUAI6.GameState.GameEnd: MessageType.GAME_END}
  169. # 用于将THUAI6的对象转为Proto的对象
  170. @staticmethod
  171. def THUAI62ProtobufPlayer(playerID: int, playerType: THUAI6.PlayerType, humanType: THUAI6.HumanType, butcherType: THUAI6.ButcherType) -> Message2Server.PlayerMsg:
  172. return Message2Server.PlayerMsg(player_id=playerID, player_type=THUAI62Proto.playerTypeDict[playerType], human_type=THUAI62Proto.humanTypeDict[humanType], butcher_type=THUAI62Proto.butcherTypeDict[butcherType])
  173. @staticmethod
  174. def THUAI62ProtobufID(playerID: int) -> Message2Server.IDMsg:
  175. return Message2Server.IDMsg(player_id=playerID)
  176. @staticmethod
  177. def THUAI62ProtobufMove(time: int, angle: float, id: int) -> Message2Server.MoveMsg:
  178. return Message2Server.MoveMsg(player_id=id, angle=angle, time_in_milliseconds=time)
  179. @staticmethod
  180. def THUAI62ProtobufPick(prop: THUAI6.PropType, id: int) -> Message2Server.PickMsg:
  181. return Message2Server.PickMsg(player_id=id, prop_type=THUAI62Proto.propTypeDict[prop])
  182. @staticmethod
  183. def THUAI62ProtobufSend(msg: str, toID: int, id: int) -> Message2Server.SendMsg:
  184. return Message2Server.SendMsg(player_id=id, to_player_id=toID, message=msg)
  185. @staticmethod
  186. def THUAI62ProtobufAttack(angle: float, id: int) -> Message2Server.AttackMsg:
  187. return Message2Server.AttackMsg(player_id=id, angle=angle)