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.

Communication.py 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. from queue import Queue
  2. import grpc
  3. import threading
  4. import proto.Message2Clients_pb2 as Message2Clients
  5. import proto.Message2Server_pb2 as Message2Server
  6. import proto.MessageType_pb2 as MessageType
  7. import proto.Services_pb2_grpc as Services
  8. from logic import Logic
  9. from Interface import IErrorHandler
  10. from utils import THUAI62Proto
  11. import structures as THUAI6
  12. # 使用gRPC的异步来减少通信对于选手而言损失的时间,而gRPC的return值有result()方法,故若连接错误时也应当返回一个具有result()方法的对象,使用此处的ErrorHandler类来实现
  13. class BoolErrorHandler(IErrorHandler):
  14. @staticmethod
  15. def result():
  16. return False
  17. class Communication:
  18. __THUAI6Stub: Services.AvailableServiceStub
  19. __haveNewMessage: bool
  20. __message2Client: Message2Clients.MessageToClient
  21. __messageQueue: Queue # Python的Queue是线程安全的,故无须自己实现Queue
  22. def __init__(self, sIP: str, sPort: str):
  23. aim = sIP + ':' + sPort
  24. channel = grpc.insecure_channel(aim)
  25. self.__THUAI6Stub = Services.AvailableServiceStub(channel)
  26. self.__haveNewMessage = False
  27. self.__messageQueue = Queue()
  28. def Move(self, time: int, angle: float, playerID: int) -> bool:
  29. try:
  30. moveResult = self.__THUAI6Stub.Move(
  31. THUAI62Proto.THUAI62ProtobufMove(time, angle, playerID))
  32. except grpc.RpcError as e:
  33. return False
  34. else:
  35. return moveResult.act_success
  36. def PickProp(self, propType: THUAI6.PropType, playerID: int) -> bool:
  37. try:
  38. pickResult = self.__THUAI6Stub.PickProp(
  39. THUAI62Proto.THUAI62ProtobufPick(propType, playerID))
  40. except grpc.RpcError as e:
  41. return False
  42. else:
  43. return pickResult.act_success
  44. def UseProp(self, playerID: int):
  45. try:
  46. useResult = self.__THUAI6Stub.UseProp(
  47. THUAI62Proto.THUAI62ProtobufID(playerID))
  48. except grpc.RpcError as e:
  49. return False
  50. else:
  51. return useResult.act_success
  52. def UseSkill(self, playerID: int) -> bool:
  53. try:
  54. useResult = self.__THUAI6Stub.UseSkill(
  55. THUAI62Proto.THUAI62ProtobufID(playerID))
  56. except grpc.RpcError as e:
  57. return False
  58. else:
  59. return useResult.act_success
  60. def SendMessage(self, toID: int, message: str, playerID: int) -> bool:
  61. try:
  62. sendResult = self.__THUAI6Stub.SendMessage(
  63. THUAI62Proto.THUAI62ProtobufSend(message, toID, playerID))
  64. except grpc.RpcError as e:
  65. return False
  66. else:
  67. return sendResult.act_success
  68. def HaveMessage(self) -> bool:
  69. return not self.__messageQueue.empty()
  70. def GetMessage(self) -> tuple[int, str]:
  71. try:
  72. message = self.__messageQueue.get_nowait()
  73. except Exception as e:
  74. return -1, ''
  75. else:
  76. return message
  77. def ReadMessage(self, playerID: int) -> None:
  78. def tRead():
  79. try:
  80. for msg in self.__THUAI6Stub.GetMessage(
  81. THUAI62Proto.THUAI62ProtobufID(playerID)):
  82. self.__messageQueue.put(
  83. (msg.from_player_id, msg.message_received))
  84. except grpc.RpcError as e:
  85. return
  86. threading.Thread(target=tRead).start()
  87. def Escape(self, playerID: int) -> bool:
  88. try:
  89. escapeResult = self.__THUAI6Stub.Escape(
  90. THUAI62Proto.THUAI62ProtobufID(playerID))
  91. except grpc.RpcError as e:
  92. return False
  93. else:
  94. return escapeResult.act_success
  95. def StartFixMachine(self, playerID: int) -> bool:
  96. try:
  97. fixResult = self.__THUAI6Stub.StartFixMachine(
  98. THUAI62Proto.THUAI62ProtobufID(playerID))
  99. except grpc.RpcError as e:
  100. return False
  101. else:
  102. return fixResult.act_success
  103. def EndFixMachine(self, playerID: int) -> bool:
  104. try:
  105. fixResult = self.__THUAI6Stub.EndFixMachine(
  106. THUAI62Proto.THUAI62ProtobufID(playerID))
  107. except grpc.RpcError as e:
  108. return False
  109. else:
  110. return fixResult.act_success
  111. def StartSaveHuman(self, playerID: int) -> bool:
  112. try:
  113. saveResult = self.__THUAI6Stub.StartSaveHuman(
  114. THUAI62Proto.THUAI62ProtobufID(playerID))
  115. except grpc.RpcError as e:
  116. return False
  117. else:
  118. return saveResult.act_success
  119. def EndSaveHuman(self, playerID: int) -> bool:
  120. try:
  121. saveResult = self.__THUAI6Stub.EndSaveHuman(
  122. THUAI62Proto.THUAI62ProtobufID(playerID))
  123. except grpc.RpcError as e:
  124. return False
  125. else:
  126. return saveResult.act_success
  127. def Attack(self, angle: float, playerID: int) -> bool:
  128. try:
  129. attackResult = self.__THUAI6Stub.Attack(
  130. THUAI62Proto.THUAI62ProtobufAttack(angle, playerID))
  131. except grpc.RpcError as e:
  132. return False
  133. else:
  134. return attackResult.act_success
  135. def CarryHuman(self, playerID: int) -> bool:
  136. try:
  137. carryResult = self.__THUAI6Stub.CarryHuman(
  138. THUAI62Proto.THUAI62ProtobufID(playerID))
  139. except grpc.RpcError as e:
  140. return False
  141. else:
  142. return carryResult.act_success
  143. def ReleaseHuman(self, playerID: int) -> bool:
  144. try:
  145. releaseResult = self.__THUAI6Stub.ReleaseHuman(
  146. THUAI62Proto.THUAI62ProtobufID(playerID))
  147. except grpc.RpcError as e:
  148. return False
  149. else:
  150. return releaseResult.act_success
  151. def HangHuman(self, playerID: int) -> bool:
  152. try:
  153. hangResult = self.__THUAI6Stub.HangHuman(
  154. THUAI62Proto.THUAI62ProtobufID(playerID))
  155. except grpc.RpcError as e:
  156. return False
  157. else:
  158. return hangResult.act_success
  159. def TryConnection(self, playerID: int) -> bool:
  160. try:
  161. connectResult = self.__THUAI6Stub.TryConnection(
  162. THUAI62Proto.THUAI62ProtobufID(playerID))
  163. except grpc.RpcError as e:
  164. return False
  165. else:
  166. return True
  167. def HaveMessage2Client(self) -> bool:
  168. return self.__haveNewMessage
  169. def GetMessage2Client(self) -> Message2Clients.MessageToClient:
  170. self.__haveNewMessage = False
  171. return self.__message2Client
  172. def AddPlayer(self, playerID: int, playerType: THUAI6.PlayerType, humanType: THUAI6.HumanType, butcherType: THUAI6.ButcherType) -> None:
  173. def tMessage():
  174. try:
  175. playerMsg = THUAI62Proto.THUAI62ProtobufPlayer(
  176. playerID, playerType, humanType, butcherType)
  177. for msg in self.__THUAI6Stub.AddPlayer(playerMsg):
  178. self.__haveNewMessage = True
  179. self.__message2Client = msg
  180. except grpc.RpcError as e:
  181. return
  182. threading.Thread(target=tMessage).start()