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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import PyAPI.structures as THUAI6
  2. from PyAPI.AI import Setting
  3. from PyAPI.utils import THUAI62Proto
  4. from PyAPI.Interface import IErrorHandler
  5. import proto.Services_pb2_grpc as Services
  6. import proto.Message2Clients_pb2 as Message2Clients
  7. import threading
  8. import grpc
  9. # 使用gRPC的异步来减少通信对于选手而言损失的时间,而gRPC的return值有result()方法,故若连接错误时也应当返回一个具有result()方法的对象,使用此处的ErrorHandler类来实现
  10. class BoolErrorHandler(IErrorHandler):
  11. @staticmethod
  12. def result():
  13. return False
  14. class Communication:
  15. __THUAI6Stub: Services.AvailableServiceStub
  16. __haveNewMessage: bool
  17. __message2Client: Message2Clients.MessageToClient
  18. __mtxMessage: threading.Lock
  19. __cvMessage: threading.Condition
  20. def __init__(self, sIP: str, sPort: str):
  21. aim = sIP + ':' + sPort
  22. channel = grpc.insecure_channel(aim)
  23. self.__THUAI6Stub = Services.AvailableServiceStub(channel)
  24. self.__haveNewMessage = False
  25. self.__cvMessage = threading.Condition()
  26. def Move(self, time: int, angle: float, playerID: int) -> bool:
  27. try:
  28. moveResult = self.__THUAI6Stub.Move(
  29. THUAI62Proto.THUAI62ProtobufMove(time, angle, playerID))
  30. except grpc.RpcError as e:
  31. return False
  32. else:
  33. return moveResult.act_success
  34. def PickProp(self, propType: THUAI6.PropType, playerID: int) -> bool:
  35. try:
  36. pickResult = self.__THUAI6Stub.PickProp(
  37. THUAI62Proto.THUAI62ProtobufProp(propType, playerID))
  38. except grpc.RpcError as e:
  39. return False
  40. else:
  41. return pickResult.act_success
  42. def UseProp(self, propType: THUAI6.PropType, playerID: int):
  43. try:
  44. useResult = self.__THUAI6Stub.UseProp(
  45. THUAI62Proto.THUAI62ProtobufProp(propType, playerID))
  46. except grpc.RpcError as e:
  47. return False
  48. else:
  49. return useResult.act_success
  50. def UseSkill(self, skillID: int, playerID: int) -> bool:
  51. try:
  52. useResult = self.__THUAI6Stub.UseSkill(
  53. THUAI62Proto.THUAI62ProtobufSkill(skillID, playerID))
  54. except grpc.RpcError as e:
  55. return False
  56. else:
  57. return useResult.act_success
  58. def SendMessage(self, toID: int, message: str, playerID: int) -> bool:
  59. try:
  60. sendResult = self.__THUAI6Stub.SendMessage(
  61. THUAI62Proto.THUAI62ProtobufSend(message, toID, playerID))
  62. except grpc.RpcError as e:
  63. return False
  64. else:
  65. return sendResult.act_success
  66. def Graduate(self, playerID: int) -> bool:
  67. try:
  68. escapeResult = self.__THUAI6Stub.Graduate(
  69. THUAI62Proto.THUAI62ProtobufID(playerID))
  70. except grpc.RpcError as e:
  71. return False
  72. else:
  73. return escapeResult.act_success
  74. def StartLearning(self, playerID: int) -> bool:
  75. try:
  76. learnResult = self.__THUAI6Stub.StartLearning(
  77. THUAI62Proto.THUAI62ProtobufID(playerID))
  78. except grpc.RpcError as e:
  79. return False
  80. else:
  81. return learnResult.act_success
  82. def StartTreatMate(self, playerID: int, mateID: int) -> bool:
  83. try:
  84. helpResult = self.__THUAI6Stub.StartTreatMate(
  85. THUAI62Proto.THUAI62ProtobufTreatAndRescue(playerID, mateID))
  86. except grpc.RpcError as e:
  87. return False
  88. else:
  89. return helpResult.act_success
  90. def StartRescueMate(self, playerID: int, mateID: int) -> bool:
  91. try:
  92. helpResult = self.__THUAI6Stub.StartRescueMate(
  93. THUAI62Proto.THUAI62ProtobufTreatAndRescue(playerID, mateID))
  94. except grpc.RpcError as e:
  95. return False
  96. else:
  97. return helpResult.act_success
  98. def Attack(self, angle: float, playerID: int) -> bool:
  99. try:
  100. attackResult = self.__THUAI6Stub.Attack(
  101. THUAI62Proto.THUAI62ProtobufAttack(angle, playerID))
  102. except grpc.RpcError as e:
  103. return False
  104. else:
  105. return attackResult.act_success
  106. def OpenDoor(self, playerID: int) -> bool:
  107. try:
  108. openResult = self.__THUAI6Stub.OpenDoor(
  109. THUAI62Proto.THUAI62ProtobufID(playerID))
  110. except grpc.RpcError as e:
  111. return False
  112. else:
  113. return openResult.act_success
  114. def CloseDoor(self, playerID: int) -> bool:
  115. try:
  116. closeResult = self.__THUAI6Stub.CloseDoor(
  117. THUAI62Proto.THUAI62ProtobufID(playerID))
  118. except grpc.RpcError as e:
  119. return False
  120. else:
  121. return closeResult.act_success
  122. def SkipWindow(self, playerID: int) -> bool:
  123. try:
  124. skipResult = self.__THUAI6Stub.SkipWindow(
  125. THUAI62Proto.THUAI62ProtobufID(playerID))
  126. except grpc.RpcError as e:
  127. return False
  128. else:
  129. return skipResult.act_success
  130. def StartOpenGate(self, playerID: int) -> bool:
  131. try:
  132. openResult = self.__THUAI6Stub.StartOpenGate(
  133. THUAI62Proto.THUAI62ProtobufID(playerID))
  134. except grpc.RpcError as e:
  135. return False
  136. else:
  137. return openResult.act_success
  138. def StartOpenChest(self, playerID: int) -> bool:
  139. try:
  140. openResult = self.__THUAI6Stub.StartOpenChest(
  141. THUAI62Proto.THUAI62ProtobufID(playerID))
  142. except grpc.RpcError as e:
  143. return False
  144. else:
  145. return openResult.act_success
  146. def EndAllAction(self, playerID: int) -> bool:
  147. try:
  148. endResult = self.__THUAI6Stub.EndAllAction(
  149. THUAI62Proto.THUAI62ProtobufID(playerID))
  150. except grpc.RpcError as e:
  151. return False
  152. else:
  153. return endResult.act_success
  154. def TryConnection(self, playerID: int) -> bool:
  155. try:
  156. connectResult = self.__THUAI6Stub.TryConnection(
  157. THUAI62Proto.THUAI62ProtobufID(playerID))
  158. except grpc.RpcError as e:
  159. return False
  160. else:
  161. return True
  162. def GetMessage2Client(self) -> Message2Clients.MessageToClient:
  163. with self.__cvMessage:
  164. self.__cvMessage.wait_for(lambda: self.__haveNewMessage)
  165. self.__haveNewMessage = False
  166. return self.__message2Client
  167. def AddPlayer(self, playerID: int) -> None:
  168. def tMessage():
  169. try:
  170. playerMsg = THUAI62Proto.THUAI62ProtobufPlayer(
  171. playerID, Setting.playerType(), Setting.studentType(), Setting.trickerType())
  172. for msg in self.__THUAI6Stub.AddPlayer(playerMsg):
  173. with self.__cvMessage:
  174. self.__haveNewMessage = True
  175. self.__message2Client = msg
  176. self.__cvMessage.notify()
  177. except grpc.RpcError as e:
  178. return
  179. threading.Thread(target=tMessage).start()