Skip to content

Commit

Permalink
Update CAPI python
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoWQ222 committed Mar 2, 2024
1 parent 7f7bcb3 commit 3458499
Show file tree
Hide file tree
Showing 16 changed files with 9,593 additions and 8,835 deletions.
6,783 changes: 3,977 additions & 2,806 deletions CAPI/cpp/proto/Message2Clients.pb.cc
100755 → 100644

Large diffs are not rendered by default.

6,010 changes: 2,695 additions & 3,315 deletions CAPI/cpp/proto/Message2Clients.pb.h
100755 → 100644

Large diffs are not rendered by default.

2,709 changes: 1,558 additions & 1,151 deletions CAPI/cpp/proto/Message2Server.pb.cc
100755 → 100644

Large diffs are not rendered by default.

1,943 changes: 820 additions & 1,123 deletions CAPI/cpp/proto/Message2Server.pb.h
100755 → 100644

Large diffs are not rendered by default.

363 changes: 231 additions & 132 deletions CAPI/cpp/proto/MessageType.pb.cc
100755 → 100644

Large diffs are not rendered by default.

443 changes: 206 additions & 237 deletions CAPI/cpp/proto/MessageType.pb.h
100755 → 100644

Large diffs are not rendered by default.

Empty file modified CAPI/cpp/proto/Services.grpc.pb.cc
100755 → 100644
Empty file.
Empty file modified CAPI/cpp/proto/Services.grpc.pb.h
100755 → 100644
Empty file.
14 changes: 4 additions & 10 deletions CAPI/cpp/proto/Services.pb.cc
100755 → 100644

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 10 additions & 21 deletions CAPI/cpp/proto/Services.pb.h
100755 → 100644

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 21 additions & 10 deletions CAPI/python/PyAPI/Communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,29 @@ def GetMessage2Client(self) -> Message2Clients.MessageToClient:
self.__haveNewMessage = False
return self.__message2Client

def AddPlayer(self, playerID: int, teamID: int, shipType: THUAI7.ShipType, cellX: int, cellY: int) -> None:
def AddPlayer(self, playerID: int, teamID: int, cellX: int, cellY: int,shipType: THUAI7.ShipType) -> None:
def tMessage():
try:
playerMsg = THUAI72Proto.THUAI72ProtobufPlayerMsg(playerID, teamID, shipType, cellX, cellY)
for msg in self.__THUAI7Stub.AddPlayer(playerMsg):
with self.__cvMessage:
self.__haveNewMessage = True
self.__message2Client = msg
self.__cvMessage.notify()
with self.__mtxLimit:
self.__counter = 0
self.__counterMove = 0
if playerID==0:
playerMsg = THUAI72Proto.THUAI72ProtobufPlayerMsg(playerID, teamID, shipType, cellX, cellY)
for msg in self.__THUAI7Stub.AddPlayer(playerMsg):
with self.__cvMessage:
self.__haveNewMessage = True
self.__message2Client = msg
self.__cvMessage.notify()
with self.__mtxLimit:
self.__counter = 0
self.__counterMove = 0
elif playerID>=1 and playerID<=8:
playerMsg = THUAI72Proto.THUAI72ProtobufPlayerMsg(playerID, teamID,shipType,cellX,cellY)
for msg in self.__THUAI7Stub.AddPlayer(playerMsg):
with self.__cvMessage:
self.__haveNewMessage = True
self.__message2Client = msg
self.__cvMessage.notify()
with self.__mtxLimit:
self.__counter = 0
self.__counterMove = 0
except grpc.RpcError:
return

Expand Down
84 changes: 55 additions & 29 deletions CAPI/python/PyAPI/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ def BuildShip(self, shipType: THUAI7.ShipType, cellX: int, cellY: int) -> bool:
self.__logger.debug("Called BuildShip")
return self.__comm.BuildShip(cellX, cellY, shipType, self.__teamID)

def __TryConnection(self) -> bool:
self.__logger.info("Try to connect to the server.")
return self.__comm.TryConnection(self.__playerID, self.__teamID)

def __ProcessMessage(self) -> None:
def messageThread():
self.__logger.info("Message thread started")
Expand Down Expand Up @@ -275,9 +279,14 @@ def LoadBuffer(self, message: Message2Clients.MessageToClient) -> None:
self.__bufferState.guids.clear()
self.__logger.debug("Buffer cleared")

for obj in message.obj_message:
if obj.WhichOneof("message_of_obj") == "ship_message":
self.__bufferState.guids.append(obj.ship_message.guid)
if self.__playerID!=0:
for obj in message.obj_message:
if obj.WhichOneof("message_of_obj") == "ship_message":
self.__bufferState.guids.append(obj.ship_message.guid)
else:
for obj in message.obj_message:
if obj.WhichOneof("message_of_obj")=="team_message":
self.__bufferState.guids.append(obj.team_message.guid)

self.__bufferState.gameInfo = Proto2THUAI7.Protobuf2THUAI7GameInfo(message.all_message)

Expand All @@ -298,25 +307,32 @@ def LoadBuffer(self, message: Message2Clients.MessageToClient) -> None:
self.__counterBuffer += 1
self.__cvBuffer.notify()

def LoadBufferSelf(self, message: Message2Clients.MessageToClient) -> None:
for item in message.obj_message:
if item.WhichOneof("message_of_obj") == "ship_message":
if item.ship_message.player_id == self.__playerID:
self.__bufferState.self = Proto2THUAI7.Protobuf2THUAI7Ship(item.ship_message)
self.__bufferState.ships.append(self.__bufferState.self)
else:
self.__bufferState.ships.append(Proto2THUAI7.Protobuf2THUAI7Ship(item.ship_message))
self.__logger.debug("Load ship")
def __LoadBufferSelf(self, message: Message2Clients.MessageToClient) -> None:
if self.__playerID!=0:
for item in message.obj_message:
if item.WhichOneof("message_of_obj") == "ship_message":
if item.ship_message.player_id == self.__playerID:
self.__bufferState.self = Proto2THUAI7.Protobuf2THUAI7Ship(item.ship_message)
self.__bufferState.ships.append(self.__bufferState.self)
else:
self.__bufferState.ships.append(Proto2THUAI7.Protobuf2THUAI7Ship(item.ship_message))
self.__logger.debug("Load ship")
else:
for item in message.obj_message:
if item.WhichOneof("message_of_obj")=="team_message":
self.__bufferState.self = Proto2THUAI7.Protobuf2THUAI7Team(item.team_message)
self.__bufferState.teams.append(self.__bufferState.self)
self.__logger.debug("Load team")

def LoadBufferCase(self, item: Message2Clients.MessageOfObj) -> None:
def __LoadBufferCase(self, item: Message2Clients.MessageOfObj) -> None:
if item.WhichOneof("message_of_obj") == "ship_message":
if AssistFunction.HaveView(self.__bufferState.self.viewRange,
if item.ship_message.team_id!=self.__teamID:
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 All @@ -335,7 +351,7 @@ def LoadBufferCase(self, item: Message2Clients.MessageOfObj) -> None:
self.__logger.debug("Add Bullet!")

elif item.WhichOneof("message_of_obj") == "factory_message":
if item.factory_message.team_id == self.__teamID:
if AssistFunction.HaveView(self.__bufferState.self.viewRange,self.__bufferState.self.x,self.__bufferState.self.y,item.factory_message.x,item.factory_message.y,self.__bufferState.gameMap):
pos = (
AssistFunction.GridToCell(
item.factory_message.x), AssistFunction.GridToCell(
Expand All @@ -348,7 +364,14 @@ def LoadBufferCase(self, item: Message2Clients.MessageOfObj) -> None:
self.__logger.debug("Update Factory")

elif item.WhichOneof("message_of_obj") == "community_message":
if item.community_message.team_id == self.__teamID:
if AssistFunction.HaveView(
self.__bufferState.self.viewRange,
self.__bufferState.self.x,
self.__bufferState.self.y,
item.community_message.x,
item.community_message.y,
self.__bufferState.gameMap,
):
pos = (
AssistFunction.GridToCell(
item.community_message.x), AssistFunction.GridToCell(
Expand All @@ -361,7 +384,7 @@ def LoadBufferCase(self, item: Message2Clients.MessageOfObj) -> None:
self.__logger.debug("Update Community")

elif item.WhichOneof("message_of_obj") == "fort_message":
if item.fort_message.team_id == self.__teamID:
if AssistFunction.HaveView(self.__bufferState.self.viewRange,self.__bufferState.self.x,self.__bufferState.self.y,item.fort_message.x,item.fort_message.y,self.__bufferState.gameMap):
pos = (AssistFunction.GridToCell(item.fort_message.x), AssistFunction.GridToCell(item.fort_message.y))
if pos not in self.__bufferState.mapInfo.fortState:
self.__bufferState.mapInfo.fortState[pos] = item.fort_message.hp
Expand All @@ -379,17 +402,19 @@ def LoadBufferCase(self, item: Message2Clients.MessageOfObj) -> None:
self.__logger.debug("Update Wormhole")

elif item.WhichOneof("message_of_obj") == "home_message":
pos = (AssistFunction.GridToCell(item.home_message.x), AssistFunction.GridToCell(item.home_message.y))
self.__bufferState.mapInfo.homeState[pos] = item.home_message.hp
self.__logger.debug("Update Home")
if AssistFunction.HaveView(self.__bufferState.self.viewRange,self.__bufferState.self.x,self.__bufferState.self.y,item.home_message.x,item.home_message.y,self.__bufferState.gameMap):
pos = (AssistFunction.GridToCell(item.home_message.x), AssistFunction.GridToCell(item.home_message.y))
self.__bufferState.mapInfo.homeState[pos] = item.home_message.hp
self.__logger.debug("Update Home")

elif item.WhichOneof("message_of_obj") == "resource_message":
pos = (
AssistFunction.GridToCell(
item.resource_message.x), AssistFunction.GridToCell(
item.resource_message.y))
self.__bufferState.mapInfo.resourceState[pos] = item.resource_message.progress
self.__logger.debug("Update Resource")
if AssistFunction.HaveView(self.__bufferState.self.viewRange,self.__bufferState.self.x,self.__bufferState.self.y,item.resource_message.x,item.resource_message.y,self.__bufferState.gameMap):
pos = (
AssistFunction.GridToCell(
item.resource_message.x), AssistFunction.GridToCell(
item.resource_message.y))
self.__bufferState.mapInfo.resourceState[pos] = item.resource_message.progress
self.__logger.debug("Update Resource")

elif item.WhichOneof("message_of_obj") == "news_message":
if item.news_message.to_id == self.__playerID:
Expand Down Expand Up @@ -419,8 +444,9 @@ def LoadBufferCase(self, item: Message2Clients.MessageOfObj) -> None:
# self.__logger.debug("Add Bombed Bullet!")

elif item.WhichOneof("message_of_obj") == "team_message":
self.__bufferState.teams.append(Proto2THUAI7.Protobuf2THUAI7Team(item.team_message))
self.__logger.debug("Add Team!")
if item.team_message.team_id != self.__teamID:
self.__bufferState.teams.append(Proto2THUAI7.Protobuf2THUAI7Team(item.team_message))
self.__logger.debug("Add Team!")

else:
self.__logger.error("Unknown message!")
Expand Down
2 changes: 2 additions & 0 deletions CAPI/python/PyAPI/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,5 @@ def THUAI72ProtobufPlayerMsg(
y=y,
ship_type=THUAI72Proto.shipTypeDict[shipType]
)


10 changes: 10 additions & 0 deletions CAPI/python/generate_proto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

python3 -m pip install -r requirements.txt

mkdir -p proto

python3 -m grpc_tools.protoc -I../../dependency/proto/ --python_out=./proto --pyi_out=./proto MessageType.proto
python3 -m grpc_tools.protoc -I../../dependency/proto/ --python_out=./proto --pyi_out=./proto Message2Clients.proto
python3 -m grpc_tools.protoc -I../../dependency/proto/ --python_out=./proto --pyi_out=./proto Message2Server.proto
python3 -m grpc_tools.protoc -I../../dependency/proto/ --python_out=./proto --pyi_out=./proto --grpc_python_out=./proto Services.proto
3 changes: 3 additions & 0 deletions CAPI/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
grpcio==1.54.2
grpcio-tools==1.54.2
numpy
2 changes: 1 addition & 1 deletion dependency/proto/Message2Clients.proto
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ message MessageOfTeam
int64 player_id = 2;
int32 score = 3;
int32 money = 4;

int64 guid=5;
}

message MessageOfObj
Expand Down

0 comments on commit 3458499

Please sign in to comment.