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

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