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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. self.__mtxLimit = threading.Lock()
  24. self.__counter = 0
  25. self.__limit = 50
  26. def Move(self, time: int, angle: float, playerID: int) -> bool:
  27. try:
  28. with self.__mtxLimit:
  29. if self.__counter > self.__limit:
  30. return False
  31. self.__counter += 1
  32. moveResult = self.__THUAI6Stub.Move(
  33. THUAI62Proto.THUAI62ProtobufMove(time, angle, playerID)
  34. )
  35. except grpc.RpcError as e:
  36. return False
  37. else:
  38. return moveResult.act_success
  39. def PickProp(self, propType: THUAI6.PropType, playerID: int) -> bool:
  40. try:
  41. with self.__mtxLimit:
  42. if self.__counter > self.__limit:
  43. return False
  44. self.__counter += 1
  45. pickResult = self.__THUAI6Stub.PickProp(
  46. THUAI62Proto.THUAI62ProtobufProp(propType, playerID)
  47. )
  48. except grpc.RpcError as e:
  49. return False
  50. else:
  51. return pickResult.act_success
  52. def UseProp(self, propType: THUAI6.PropType, playerID: int) -> bool:
  53. try:
  54. with self.__mtxLimit:
  55. if self.__counter > self.__limit:
  56. return False
  57. self.__counter += 1
  58. useResult = self.__THUAI6Stub.UseProp(
  59. THUAI62Proto.THUAI62ProtobufProp(propType, playerID)
  60. )
  61. except grpc.RpcError as e:
  62. return False
  63. else:
  64. return useResult.act_success
  65. def ThrowProp(self, propType: THUAI6.PropType, playerID: int) -> bool:
  66. try:
  67. with self.__mtxLimit:
  68. if self.__counter > self.__limit:
  69. return False
  70. self.__counter += 1
  71. throwResult = self.__THUAI6Stub.ThrowProp(
  72. THUAI62Proto.THUAI62ProtobufProp(propType, playerID)
  73. )
  74. except grpc.RpcError as e:
  75. return False
  76. else:
  77. return throwResult.act_success
  78. def UseSkill(self, skillID: int, playerID: int) -> bool:
  79. try:
  80. with self.__mtxLimit:
  81. if self.__counter > self.__limit:
  82. return False
  83. self.__counter += 1
  84. useResult = self.__THUAI6Stub.UseSkill(
  85. THUAI62Proto.THUAI62ProtobufSkill(skillID, playerID)
  86. )
  87. except grpc.RpcError as e:
  88. return False
  89. else:
  90. return useResult.act_success
  91. def SendMessage(self, toID: int, message: Union[str, bytes], playerID: int) -> bool:
  92. try:
  93. with self.__mtxLimit:
  94. if self.__counter > self.__limit:
  95. return False
  96. self.__counter += 1
  97. sendResult = self.__THUAI6Stub.SendMessage(
  98. THUAI62Proto.THUAI62ProtobufSend(message, toID, playerID)
  99. )
  100. except grpc.RpcError as e:
  101. return False
  102. else:
  103. return sendResult.act_success
  104. def Graduate(self, playerID: int) -> bool:
  105. try:
  106. with self.__mtxLimit:
  107. if self.__counter > self.__limit:
  108. return False
  109. self.__counter += 1
  110. escapeResult = self.__THUAI6Stub.Graduate(
  111. THUAI62Proto.THUAI62ProtobufID(playerID)
  112. )
  113. except grpc.RpcError as e:
  114. return False
  115. else:
  116. return escapeResult.act_success
  117. def StartLearning(self, playerID: int) -> bool:
  118. try:
  119. with self.__mtxLimit:
  120. if self.__counter > self.__limit:
  121. return False
  122. self.__counter += 1
  123. learnResult = self.__THUAI6Stub.StartLearning(
  124. THUAI62Proto.THUAI62ProtobufID(playerID)
  125. )
  126. except grpc.RpcError as e:
  127. return False
  128. else:
  129. return learnResult.act_success
  130. def StartEncourageMate(self, playerID: int, mateID: int) -> bool:
  131. try:
  132. with self.__mtxLimit:
  133. if self.__counter > self.__limit:
  134. return False
  135. self.__counter += 1
  136. helpResult = self.__THUAI6Stub.StartTreatMate(
  137. THUAI62Proto.THUAI62ProtobufTreatAndRescue(playerID, mateID)
  138. )
  139. except grpc.RpcError as e:
  140. return False
  141. else:
  142. return helpResult.act_success
  143. def StartRouseMate(self, playerID: int, mateID: int) -> bool:
  144. try:
  145. with self.__mtxLimit:
  146. if self.__counter > self.__limit:
  147. return False
  148. self.__counter += 1
  149. helpResult = self.__THUAI6Stub.StartRescueMate(
  150. THUAI62Proto.THUAI62ProtobufTreatAndRescue(playerID, mateID)
  151. )
  152. except grpc.RpcError as e:
  153. return False
  154. else:
  155. return helpResult.act_success
  156. def Attack(self, angle: float, playerID: int) -> bool:
  157. try:
  158. with self.__mtxLimit:
  159. if self.__counter > self.__limit:
  160. return False
  161. self.__counter += 1
  162. attackResult = self.__THUAI6Stub.Attack(
  163. THUAI62Proto.THUAI62ProtobufAttack(angle, playerID)
  164. )
  165. except grpc.RpcError as e:
  166. return False
  167. else:
  168. return attackResult.act_success
  169. def OpenDoor(self, playerID: int) -> bool:
  170. try:
  171. with self.__mtxLimit:
  172. if self.__counter > self.__limit:
  173. return False
  174. self.__counter += 1
  175. openResult = self.__THUAI6Stub.OpenDoor(
  176. THUAI62Proto.THUAI62ProtobufID(playerID)
  177. )
  178. except grpc.RpcError as e:
  179. return False
  180. else:
  181. return openResult.act_success
  182. def CloseDoor(self, playerID: int) -> bool:
  183. try:
  184. with self.__mtxLimit:
  185. if self.__counter > self.__limit:
  186. return False
  187. self.__counter += 1
  188. closeResult = self.__THUAI6Stub.CloseDoor(
  189. THUAI62Proto.THUAI62ProtobufID(playerID)
  190. )
  191. except grpc.RpcError as e:
  192. return False
  193. else:
  194. return closeResult.act_success
  195. def SkipWindow(self, playerID: int) -> bool:
  196. try:
  197. with self.__mtxLimit:
  198. if self.__counter > self.__limit:
  199. return False
  200. self.__counter += 1
  201. skipResult = self.__THUAI6Stub.SkipWindow(
  202. THUAI62Proto.THUAI62ProtobufID(playerID)
  203. )
  204. except grpc.RpcError as e:
  205. return False
  206. else:
  207. return skipResult.act_success
  208. def StartOpenGate(self, playerID: int) -> bool:
  209. try:
  210. with self.__mtxLimit:
  211. if self.__counter > self.__limit:
  212. return False
  213. self.__counter += 1
  214. openResult = self.__THUAI6Stub.StartOpenGate(
  215. THUAI62Proto.THUAI62ProtobufID(playerID)
  216. )
  217. except grpc.RpcError as e:
  218. return False
  219. else:
  220. return openResult.act_success
  221. def StartOpenChest(self, playerID: int) -> bool:
  222. try:
  223. with self.__mtxLimit:
  224. if self.__counter > self.__limit:
  225. return False
  226. self.__counter += 1
  227. openResult = self.__THUAI6Stub.StartOpenChest(
  228. THUAI62Proto.THUAI62ProtobufID(playerID)
  229. )
  230. except grpc.RpcError as e:
  231. return False
  232. else:
  233. return openResult.act_success
  234. def EndAllAction(self, playerID: int) -> bool:
  235. try:
  236. with self.__mtxLimit:
  237. if self.__counter > self.__limit:
  238. return False
  239. self.__counter += 1
  240. endResult = self.__THUAI6Stub.EndAllAction(
  241. THUAI62Proto.THUAI62ProtobufID(playerID)
  242. )
  243. except grpc.RpcError as e:
  244. return False
  245. else:
  246. return endResult.act_success
  247. def TryConnection(self, playerID: int) -> bool:
  248. try:
  249. connectResult = self.__THUAI6Stub.TryConnection(
  250. THUAI62Proto.THUAI62ProtobufID(playerID)
  251. )
  252. except grpc.RpcError as e:
  253. return False
  254. else:
  255. return True
  256. def GetMessage2Client(self) -> Message2Clients.MessageToClient:
  257. with self.__cvMessage:
  258. self.__cvMessage.wait_for(lambda: self.__haveNewMessage)
  259. self.__haveNewMessage = False
  260. return self.__message2Client
  261. def AddPlayer(self, playerID: int, playerType: THUAI6.PlayerType) -> None:
  262. def tMessage():
  263. try:
  264. if playerType == THUAI6.PlayerType.StudentPlayer:
  265. studentType = Setting.studentType()[playerID]
  266. else:
  267. studentType = THUAI6.StudentType.NullStudentType
  268. playerMsg = THUAI62Proto.THUAI62ProtobufPlayer(
  269. playerID, playerType, studentType, Setting.trickerType()
  270. )
  271. for msg in self.__THUAI6Stub.AddPlayer(playerMsg):
  272. with self.__cvMessage:
  273. self.__haveNewMessage = True
  274. self.__message2Client = msg
  275. self.__cvMessage.notify()
  276. with self.__mtxLimit:
  277. self.__counter = 0
  278. except grpc.RpcError as e:
  279. return
  280. threading.Thread(target=tMessage).start()