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

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