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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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) -> bool:
  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 ThrowProp(self, propType: THUAI6.PropType, playerID: int) -> bool:
  51. try:
  52. throwResult = self.__THUAI6Stub.ThrowProp(
  53. THUAI62Proto.THUAI62ProtobufProp(propType, playerID))
  54. except grpc.RpcError as e:
  55. return False
  56. else:
  57. return throwResult.act_success
  58. def UseSkill(self, skillID: int, playerID: int) -> bool:
  59. try:
  60. useResult = self.__THUAI6Stub.UseSkill(
  61. THUAI62Proto.THUAI62ProtobufSkill(skillID, playerID))
  62. except grpc.RpcError as e:
  63. return False
  64. else:
  65. return useResult.act_success
  66. def SendMessage(self, toID: int, message: str, playerID: int) -> bool:
  67. try:
  68. sendResult = self.__THUAI6Stub.SendMessage(
  69. THUAI62Proto.THUAI62ProtobufSend(message, toID, playerID))
  70. except grpc.RpcError as e:
  71. return False
  72. else:
  73. return sendResult.act_success
  74. def Graduate(self, playerID: int) -> bool:
  75. try:
  76. escapeResult = self.__THUAI6Stub.Graduate(
  77. THUAI62Proto.THUAI62ProtobufID(playerID))
  78. except grpc.RpcError as e:
  79. return False
  80. else:
  81. return escapeResult.act_success
  82. def StartLearning(self, playerID: int) -> bool:
  83. try:
  84. learnResult = self.__THUAI6Stub.StartLearning(
  85. THUAI62Proto.THUAI62ProtobufID(playerID))
  86. except grpc.RpcError as e:
  87. return False
  88. else:
  89. return learnResult.act_success
  90. def StartEncourageMate(self, playerID: int, mateID: int) -> bool:
  91. try:
  92. helpResult = self.__THUAI6Stub.StartTreatMate(
  93. THUAI62Proto.THUAI62ProtobufTreatAndRescue(playerID, mateID))
  94. except grpc.RpcError as e:
  95. return False
  96. else:
  97. return helpResult.act_success
  98. def StartRouseMate(self, playerID: int, mateID: int) -> bool:
  99. try:
  100. helpResult = self.__THUAI6Stub.StartRescueMate(
  101. THUAI62Proto.THUAI62ProtobufTreatAndRescue(playerID, mateID))
  102. except grpc.RpcError as e:
  103. return False
  104. else:
  105. return helpResult.act_success
  106. def Attack(self, angle: float, playerID: int) -> bool:
  107. try:
  108. attackResult = self.__THUAI6Stub.Attack(
  109. THUAI62Proto.THUAI62ProtobufAttack(angle, playerID))
  110. except grpc.RpcError as e:
  111. return False
  112. else:
  113. return attackResult.act_success
  114. def OpenDoor(self, playerID: int) -> bool:
  115. try:
  116. openResult = self.__THUAI6Stub.OpenDoor(
  117. THUAI62Proto.THUAI62ProtobufID(playerID))
  118. except grpc.RpcError as e:
  119. return False
  120. else:
  121. return openResult.act_success
  122. def CloseDoor(self, playerID: int) -> bool:
  123. try:
  124. closeResult = self.__THUAI6Stub.CloseDoor(
  125. THUAI62Proto.THUAI62ProtobufID(playerID))
  126. except grpc.RpcError as e:
  127. return False
  128. else:
  129. return closeResult.act_success
  130. def SkipWindow(self, playerID: int) -> bool:
  131. try:
  132. skipResult = self.__THUAI6Stub.SkipWindow(
  133. THUAI62Proto.THUAI62ProtobufID(playerID))
  134. except grpc.RpcError as e:
  135. return False
  136. else:
  137. return skipResult.act_success
  138. def StartOpenGate(self, playerID: int) -> bool:
  139. try:
  140. openResult = self.__THUAI6Stub.StartOpenGate(
  141. THUAI62Proto.THUAI62ProtobufID(playerID))
  142. except grpc.RpcError as e:
  143. return False
  144. else:
  145. return openResult.act_success
  146. def StartOpenChest(self, playerID: int) -> bool:
  147. try:
  148. openResult = self.__THUAI6Stub.StartOpenChest(
  149. THUAI62Proto.THUAI62ProtobufID(playerID))
  150. except grpc.RpcError as e:
  151. return False
  152. else:
  153. return openResult.act_success
  154. def EndAllAction(self, playerID: int) -> bool:
  155. try:
  156. endResult = self.__THUAI6Stub.EndAllAction(
  157. THUAI62Proto.THUAI62ProtobufID(playerID))
  158. except grpc.RpcError as e:
  159. return False
  160. else:
  161. return endResult.act_success
  162. def TryConnection(self, playerID: int) -> bool:
  163. try:
  164. connectResult = self.__THUAI6Stub.TryConnection(
  165. THUAI62Proto.THUAI62ProtobufID(playerID))
  166. except grpc.RpcError as e:
  167. return False
  168. else:
  169. return True
  170. def GetMessage2Client(self) -> Message2Clients.MessageToClient:
  171. with self.__cvMessage:
  172. self.__cvMessage.wait_for(lambda: self.__haveNewMessage)
  173. self.__haveNewMessage = False
  174. return self.__message2Client
  175. def AddPlayer(self, playerID: int, playerType: THUAI6.PlayerType) -> None:
  176. def tMessage():
  177. try:
  178. if playerType == THUAI6.PlayerType.StudentPlayer:
  179. studentType = Setting.studentType()[playerID]
  180. else:
  181. studentType = THUAI6.StudentType.NullStudentType
  182. playerMsg = THUAI62Proto.THUAI62ProtobufPlayer(
  183. playerID, playerType, studentType, Setting.trickerType())
  184. for msg in self.__THUAI6Stub.AddPlayer(playerMsg):
  185. with self.__cvMessage:
  186. self.__haveNewMessage = True
  187. self.__message2Client = msg
  188. self.__cvMessage.notify()
  189. except grpc.RpcError as e:
  190. return
  191. threading.Thread(target=tMessage).start()