Skip to content

Commit

Permalink
style(PyAPI): 🎨 improve format
Browse files Browse the repository at this point in the history
fix some type hint & wrong attrs
  • Loading branch information
asdawej committed Feb 29, 2024
1 parent 8ac5f81 commit 7f7bcb3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 86 deletions.
62 changes: 30 additions & 32 deletions CAPI/python/PyAPI/Communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def Move(self, time: int, angle: float, playerID: int) -> bool:
return False
self.__counter += 1
self.__counterMove += 1
moveResult = self.__THUAI7Stub.Move(
moveResult: Message2Clients.MoveRes = self.__THUAI7Stub.Move(
THUAI72Proto.THUAI72ProtobufMoveMsg(time, angle, playerID)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return moveResult.act_success
Expand All @@ -54,12 +54,11 @@ def SendMessage(self, toID: int, message: Union[str, bytes], playerID: int, team
if self.__counter >= self.__limit:
return False
self.__counter += 1
sendResult = self.__THUAI7Stub.SendMessage(
sendResult: Message2Clients.BoolRes = self.__THUAI7Stub.Send(
THUAI72Proto.THUAI72ProtobufSendMsg(
playerID, toID, teamID, message, True if isinstance(
message, bytes) else False)
playerID, toID, teamID, message, True if isinstance(message, bytes) else False)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return sendResult.act_success
Expand All @@ -70,10 +69,10 @@ def Attack(self, angle: float, playerID: int, teamID: int) -> bool:
if self.__counter >= self.__limit:
return False
self.__counter += 1
attackResult = self.__THUAI7Stub.Attack(
attackResult: Message2Clients.BoolRes = self.__THUAI7Stub.Attack(
THUAI72Proto.THUAI72ProtobufAttackMsg(playerID, teamID, angle)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return attackResult.act_success
Expand All @@ -84,10 +83,10 @@ def Recover(self, playerID: int, teamID: int, recover: int) -> bool:
if self.__counter >= self.__limit:
return False
self.__counter += 1
recoverResult = self.__THUAI7Stub.Recover(
recoverResult: Message2Clients.BoolRes = self.__THUAI7Stub.Recover(
THUAI72Proto.THUAI72ProtobufRecoverMsg(playerID, recover, teamID)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return recoverResult.act_success
Expand All @@ -98,10 +97,10 @@ def Produce(self, playerID: int, teamID: int) -> bool:
if self.__counter >= self.__limit:
return False
self.__counter += 1
produceResult = self.__THUAI7Stub.Produce(
produceResult: Message2Clients.BoolRes = self.__THUAI7Stub.Produce(
THUAI72Proto.THUAI72ProtobufIDMsg(playerID, teamID)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return produceResult.act_success
Expand All @@ -112,10 +111,10 @@ def Rebuild(self, constructionType: THUAI7.ConstructionType, playerID: int, team
if self.__counter >= self.__limit:
return False
self.__counter += 1
rebuildResult = self.__THUAI7Stub.Rebuild(
rebuildResult: Message2Clients.BoolRes = self.__THUAI7Stub.Rebuild(
THUAI72Proto.THUAI72ProtobufConstructMsg(playerID, teamID, constructionType)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return rebuildResult.act_success
Expand All @@ -126,10 +125,10 @@ def Construct(self, constructionType: THUAI7.ConstructionType, playerID: int, te
if self.__counter >= self.__limit:
return False
self.__counter += 1
constructResult = self.__THUAI7Stub.Construct(
constructResult: Message2Clients.BoolRes = self.__THUAI7Stub.Construct(
THUAI72Proto.THUAI72ProtobufConstructMsg(playerID, teamID, constructionType)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return constructResult.act_success
Expand All @@ -144,10 +143,10 @@ def EndAllAction(self, playerID: int, teamID: int) -> bool:
return False
self.__counter += 1
self.__counterMove += 1
endResult = self.__THUAI7Stub.EndAllAction(
endResult: Message2Clients.BoolRes = self.__THUAI7Stub.EndAllAction(
THUAI72Proto.THUAI72ProtobufIDMsg(playerID, teamID)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return endResult.act_success
Expand All @@ -158,12 +157,11 @@ def SendMessage(self, toID: int, message: Union[str, bytes], playerID: int, team
if self.__counter >= self.__limit:
return False
self.__counter += 1
sendResult = self.__THUAI7Stub.SendMessage(
sendResult: Message2Clients.BoolRes = self.__THUAI7Stub.Send(
THUAI72Proto.THUAI72ProtobufSendMsg(
playerID, toID, teamID, message, True if isinstance(
message, bytes) else False)
playerID, toID, teamID, message, True if isinstance(message, bytes) else False)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return sendResult.act_success
Expand All @@ -174,10 +172,10 @@ def InstallModule(self, moduleType: THUAI7.ModuleType, playerID: int, teamID: in
if self.__counter >= self.__limit:
return False
self.__counter += 1
installResult = self.__THUAI7Stub.InstallModule(
installResult: Message2Clients.BoolRes = self.__THUAI7Stub.InstallModule(
THUAI72Proto.THUAI72ProtobufInstallMsg(playerID, teamID, moduleType)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return installResult.act_success
Expand All @@ -188,10 +186,10 @@ def Recycle(self, playerID: int, teamID: int) -> bool:
if self.__counter >= self.__limit:
return False
self.__counter += 1
recycleResult = self.__THUAI7Stub.Recycle(
recycleResult: Message2Clients.BoolRes = self.__THUAI7Stub.Recycle(
THUAI72Proto.THUAI72ProtobufIDMsg(playerID, teamID)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return recycleResult.act_success
Expand All @@ -202,23 +200,23 @@ def BuildShip(self, cellX: int, cellY: int, shipType: THUAI7.ShipType, teamID: i
if self.__counter >= self.__limit:
return False
self.__counter += 1
buildResult = self.__THUAI7Stub.BuildShip(
buildResult: Message2Clients.BoolRes = self.__THUAI7Stub.BuildShip(
THUAI72Proto.THUAI72ProtobufBuildShipMsg(teamID, shipType, cellX, cellY)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return buildResult.act_success

def TryConnection(self, playerID: int, teamID: int) -> bool:
try:
tryResult = self.__THUAI7Stub.TryConnection(
tryResult: Message2Clients.BoolRes = self.__THUAI7Stub.TryConnection(
THUAI72Proto.THUAI72ProtobufIDMsg(playerID, teamID)
)
except grpc.RpcError as e:
except grpc.RpcError:
return False
else:
return True
return tryResult.act_success

def GetMessage2Client(self) -> Message2Clients.MessageToClient:
with self.__cvMessage:
Expand All @@ -238,7 +236,7 @@ def tMessage():
with self.__mtxLimit:
self.__counter = 0
self.__counterMove = 0
except grpc.RpcError as e:
except grpc.RpcError:
return

threading.Thread(target=tMessage).start()
28 changes: 17 additions & 11 deletions CAPI/python/PyAPI/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,16 @@ def GetResourceState(self, cellX: int, cellY: int) -> int:
def GetHomeHp(self) -> int:
with self.__mtxState:
self.__logger.debug("Called GetHomeHp")
return copy.deepcopy(self.__currentState.gameInfo.blueHomeHp if self.__teamID ==
1 else self.__currentState.gameInfo.redHomeHp)
return copy.deepcopy(self.__currentState.gameInfo.blueHomeHp
if self.__teamID == 1
else self.__currentState.gameInfo.redHomeHp)

def GetMoney(self) -> int:
with self.__mtxState:
self.__logger.debug("Called GetMoney")
return copy.deepcopy(self.__currentState.gameInfo.blueMoney if self.__teamID ==
1 else self.__currentState.gameInfo.redMoney)
return copy.deepcopy(self.__currentState.gameInfo.blueMoney
if self.__teamID == 1
else self.__currentState.gameInfo.redMoney)

def Attack(self, angle: float) -> int:
self.__logger.debug("Called Attack")
Expand Down Expand Up @@ -231,11 +233,11 @@ def messageThread():
for obj in clientMsg.obj_message:
if obj.WhichOneof("message_of_obj") == "map_message":
gameMap: List[List[THUAI7.PlaceType]] = []
for row in obj.map_message.row:
col: List[THUAI7.PlaceType] = []
for place in row.col:
col.append(Proto2THUAI7.placeTypeDict[place])
gameMap.append(col)
for row in obj.map_message.rows:
cols: List[THUAI7.PlaceType] = []
for place in row.cols:
cols.append(Proto2THUAI7.placeTypeDict[place])
gameMap.append(cols)
self.__currentState.gameMap = gameMap
self.__bufferState.gameMap = gameMap
self.__logger.info("Game map loaded!")
Expand Down Expand Up @@ -308,8 +310,12 @@ def LoadBufferSelf(self, message: Message2Clients.MessageToClient) -> None:

def LoadBufferCase(self, item: Message2Clients.MessageOfObj) -> None:
if item.WhichOneof("message_of_obj") == "ship_message":
if AssistFunction.HaveView(self.__bufferState.self.viewRange, self.__bufferState.self.x,
self.__bufferState.self.y, item.ship_message.x, item.ship_message.y, self.__bufferState.gameMap):
if AssistFunction.HaveView(self.__bufferState.self.viewRange,
self.__bufferState.self.x,
self.__bufferState.self.y,
item.ship_message.x,
item.ship_message.y,
self.__bufferState.gameMap):
if item.ship_message.team_id != self.__teamID:
self.__bufferState.enemyShips.append(Proto2THUAI7.Protobuf2THUAI7Ship(item.ship_message))
self.__logger.debug("Load enemy ship")
Expand Down
Loading

0 comments on commit 7f7bcb3

Please sign in to comment.