Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ✨ Add RepairHome and RepairWormhole #366

Merged
merged 13 commits into from
May 16, 2024
Merged
8 changes: 8 additions & 0 deletions CAPI/cpp/API/include/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class ILogic
virtual bool Move(int64_t time, double angle) = 0;
virtual bool Recover(int64_t recover) = 0;
virtual bool Produce() = 0;
virtual bool RepairWormhole() = 0;
virtual bool RepairHome() = 0;
virtual bool Rebuild(THUAI7::ConstructionType constructionType) = 0;
virtual bool Construct(THUAI7::ConstructionType constructionType) = 0;
virtual bool Attack(double angle) = 0;
Expand Down Expand Up @@ -139,6 +141,8 @@ class IShipAPI : public IAPI
virtual std::future<bool> Attack(double angleInRadian) = 0;
virtual std::future<bool> Recover(int64_t recover) = 0;
virtual std::future<bool> Produce() = 0;
virtual std::future<bool> RepairWormhole() = 0;
virtual std::future<bool> RepairHome() = 0;
virtual std::future<bool> Rebuild(THUAI7::ConstructionType constructionType) = 0;
virtual std::future<bool> Construct(THUAI7::ConstructionType constructionType) = 0;
virtual std::shared_ptr<const THUAI7::Ship> GetSelfInfo() const = 0;
Expand Down Expand Up @@ -195,6 +199,8 @@ class ShipAPI : public IShipAPI, public IGameTimer
std::future<bool> Attack(double angleInRadian) override;
std::future<bool> Recover(int64_t recover) override;
std::future<bool> Produce() override;
std::future<bool> RepairWormhole() override;
std::future<bool> RepairHome() override;
std::future<bool> Rebuild(THUAI7::ConstructionType constructionType) override;
std::future<bool> Construct(THUAI7::ConstructionType constructionType) override;

Expand Down Expand Up @@ -312,6 +318,8 @@ class ShipDebugAPI : public IShipAPI, public IGameTimer
std::future<bool> Attack(double angleInRadian) override;
std::future<bool> Recover(int64_t recover) override;
std::future<bool> Produce() override;
std::future<bool> RepairWormhole() override;
std::future<bool> RepairHome() override;
std::future<bool> Rebuild(THUAI7::ConstructionType constructionType) override;
std::future<bool> Construct(THUAI7::ConstructionType constructionType) override;

Expand Down
2 changes: 2 additions & 0 deletions CAPI/cpp/API/include/Communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Communication
bool Move(int32_t playerID, int32_t teamID, int64_t time, double angle);
bool Recover(int32_t playerID, int64_t recover, int32_t teamID);
bool Produce(int32_t playerID, int32_t teamID);
bool RepairWormhole(int32_t playerID, int32_t teamID);
bool RepairHome(int32_t playerID, int32_t teamID);
bool Rebuild(int32_t playerID, int32_t teamID, THUAI7::ConstructionType constructionType);
bool Construct(int32_t playerID, int32_t teamID, THUAI7::ConstructionType constructionType);
bool Attack(int32_t playerID, int32_t teamID, double angle);
Expand Down
2 changes: 2 additions & 0 deletions CAPI/cpp/API/include/logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class Logic : public ILogic
bool Move(int64_t time, double angle);
bool Recover(int64_t recover);
bool Produce();
bool RepairWormhole();
bool RepairHome();
bool Rebuild(THUAI7::ConstructionType constructionType);
bool Construct(THUAI7::ConstructionType constructionType);
bool Attack(double angle);
Expand Down
12 changes: 12 additions & 0 deletions CAPI/cpp/API/src/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,18 @@ std::future<bool> ShipAPI::Produce()
{ return logic.Produce(); });
}

std::future<bool> ShipAPI::RepairWormhole()
{
return std::async(std::launch::async, [=]()
{ return logic.RepairWormhole(); });
}

std::future<bool> ShipAPI::RepairHome()
{
return std::async(std::launch::async, [=]()
{ return logic.RepairHome(); });
}

std::future<bool> ShipAPI::Rebuild(THUAI7::ConstructionType constructionType)
{
return std::async(std::launch::async, [=]()
Expand Down
38 changes: 38 additions & 0 deletions CAPI/cpp/API/src/Communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,44 @@ bool Communication::Produce(int32_t playerID, int32_t teamID)
return false;
}

bool Communication::RepairWormhole(int32_t playerID, int32_t teamID)
{
{
std::lock_guard<std::mutex> lock(mtxLimit);
if (counter >= limit || counterMove >= moveLimit)
return false;
counter++;
counterMove++;
}
protobuf::BoolRes repairWormholeResult;
ClientContext context;
auto request = THUAI72Proto::THUAI72ProtobufIDMsg(playerID, teamID);
auto status = THUAI7Stub->RepairWormhole(&context, request, &repairWormholeResult);
if (status.ok())
return repairWormholeResult.act_success();
else
return false;
}

bool Communication::RepairHome(int32_t playerID, int32_t teamID)
{
{
std::lock_guard<std::mutex> lock(mtxLimit);
if (counter >= limit || counterMove >= moveLimit)
return false;
counter++;
counterMove++;
}
protobuf::BoolRes repairHomeResult;
ClientContext context;
auto request = THUAI72Proto::THUAI72ProtobufIDMsg(playerID, teamID);
auto status = THUAI7Stub->RepairHome(&context, request, &repairHomeResult);
if (status.ok())
return repairHomeResult.act_success();
else
return false;
}

bool Communication::Rebuild(int32_t playerID, int32_t teamID, THUAI7::ConstructionType constructionType)
{
{
Expand Down
21 changes: 21 additions & 0 deletions CAPI/cpp/API/src/DebugAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,27 @@ std::future<bool> ShipDebugAPI::Produce()
logger->warn("Produce: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}

std::future<bool> ShipDebugAPI::RepairWormhole()
{
logger->info("RepairWormhole: called at {}ms", Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result= logic.RepairWormhole();
if(!result)
logger->warn("RepairWormhole: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}

std::future<bool> ShipDebugAPI::RepairHome()
{
logger->info("RepairHome: called at {}ms", Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result= logic.RepairHome();
if(!result)
logger->warn("RepairHome: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}

std::future<bool> ShipDebugAPI::Rebuild(THUAI7::ConstructionType constructionType)
{
logger->info("Rebuild: rebuilding {}, called at {}ms", THUAI7::constructionDict[constructionType], Time::TimeSinceStart(startPoint));
Expand Down
12 changes: 12 additions & 0 deletions CAPI/cpp/API/src/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ bool Logic::Produce()
return pComm->Produce(playerID, teamID);
}

bool Logic::RepairWormhole()
{
logger->debug("Called RepairWormhole");
return pComm->RepairWormhole(playerID, teamID);
}

bool Logic::RepairHome()
{
logger->debug("Called RepairHome");
return pComm->RepairHome(playerID, teamID);
}

bool Logic::Rebuild(THUAI7::ConstructionType constructionType)
{
logger->debug("Called Rebuild");
Expand Down
6 changes: 6 additions & 0 deletions CAPI/python/PyAPI/API.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def Recover(self, recover: int) -> Future[bool]:
def Produce(self) -> Future[bool]:
return self.__pool.submit(self.__logic.Produce)

def RepairWormhole(self) -> Future[bool]:
return self.__pool.submit(self.__logic.RepairWormhole)

def RepairHome(self) -> Future[bool]:
return self.__pool.submit(self.__logic.RepairHome)

def Rebuild(self, constructionType: THUAI7.ConstructionType) -> Future[bool]:
return self.__pool.submit(self.__logic.Rebuild, constructionType)

Expand Down
28 changes: 28 additions & 0 deletions CAPI/python/PyAPI/Communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,34 @@ def Produce(self, playerID: int, teamID: int) -> bool:
else:
return produceResult.act_success

def RepairWormhole(self, playerID: int, teamID: int) -> bool:
try:
with self.__mtxLimit:
if self.__counter >= self.__limit:
return False
self.__counter += 1
produceResult: Message2Clients.BoolRes = self.__THUAI7Stub.RepairWormhole(
THUAI72Proto.THUAI72ProtobufIDMsg(playerID, teamID)
)
except grpc.RpcError:
return False
else:
return produceResult.act_success

def RepairHome(self, playerID: int, teamID: int) -> bool:
try:
with self.__mtxLimit:
if self.__counter >= self.__limit:
return False
self.__counter += 1
produceResult: Message2Clients.BoolRes = self.__THUAI7Stub.RepairHome(
THUAI72Proto.THUAI72ProtobufIDMsg(playerID, teamID)
)
except grpc.RpcError:
return False
else:
return produceResult.act_success

def Rebuild(
self, constructionType: THUAI7.ConstructionType, playerID: int, teamID: int
) -> bool:
Expand Down
38 changes: 36 additions & 2 deletions CAPI/python/PyAPI/DebugAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@

class ShipDebugAPI(IShipAPI, IGameTimer):
def __init__(
self, logic: ILogic, file: bool, screen: bool, warnOnly: bool, playerID: int, teamID: int
self,
logic: ILogic,
file: bool,
screen: bool,
warnOnly: bool,
playerID: int,
teamID: int,
) -> None:
self.__logic = logic
self.__pool = ThreadPoolExecutor(20)
Expand Down Expand Up @@ -106,6 +112,28 @@ def logProduce() -> bool:

return self.__pool.submit(logProduce)

def RepairWormhole(self) -> Future[bool]:
self.__logger.info(f"RepairWormhole: called at {self.__GetTime()}ms")

def logRepairWormhole() -> bool:
result = self.__logic.RepairWormhole()
if not result:
self.__logger.warning(f"RepairWormhole failed at {self.__GetTime()}ms")
return result

return self.__pool.submit(logRepairWormhole)

def RepairHome(self) -> Future[bool]:
self.__logger.info(f"RepairHome: called at {self.__GetTime()}ms")

def logRepairHome() -> bool:
result = self.__logic.RepairHome()
if not result:
self.__logger.warning(f"RepairHome failed at {self.__GetTime()}ms")
return result

return self.__pool.submit(logRepairHome)

def Rebuild(self, constructionType: THUAI7.ConstructionType) -> Future[bool]:
self.__logger.info(
f"Rebuild: called at {self.__GetTime()}ms construction type {constructionType}"
Expand Down Expand Up @@ -300,7 +328,13 @@ def Play(self, ai: IAI) -> None:

class TeamDebugAPI(ITeamAPI, IGameTimer):
def __init__(
self, logic: ILogic, file: bool, screen: bool, warnOnly: bool, playerID: int, teamID: int
self,
logic: ILogic,
file: bool,
screen: bool,
warnOnly: bool,
playerID: int,
teamID: int,
) -> None:
self.__logic = logic
self.__pool = ThreadPoolExecutor(20)
Expand Down
30 changes: 29 additions & 1 deletion CAPI/python/PyAPI/Interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ def Rebuild(self, constructionType: THUAI7.ConstructionType) -> bool:
def Construct(self, constructionType: THUAI7.ConstructionType) -> bool:
pass

@abstractmethod
def RepairHome(self) -> bool:
pass

@abstractmethod
def RepairWormhole(self) -> bool:
pass

@abstractmethod
def Attack(self, angle: float) -> bool:
pass
Expand Down Expand Up @@ -416,6 +424,24 @@ def Rebuild(self, constructionType: THUAI7.ConstructionType) -> Future[bool]:
"""
pass

@abstractmethod
def RepairWormhole(self) -> Future[bool]:
"""发出修复虫洞指令
- 需要接近待建造的 `Wormhole`

:return: 是否成功进入修复虫洞状态,通过`.result()`方法等待获取`bool`
"""
pass

@abstractmethod
def RepairHome(self) -> Future[bool]:
"""发出修复大本营指令
- 需要接近待建造的 `Home`

:return: 是否成功进入修复大本营状态,通过`.result()`方法等待获取`bool`
"""
pass

@abstractmethod
def Construct(self, constructionType: THUAI7.ConstructionType) -> Future[bool]:
"""发出建造指令
Expand Down Expand Up @@ -455,7 +481,9 @@ def GetSelfInfo(self) -> THUAI7.Team:
pass

@abstractmethod
def InstallModule(self, playerID: int, moduleType: THUAI7.ModuleType) -> Future[bool]:
def InstallModule(
self, playerID: int, moduleType: THUAI7.ModuleType
) -> Future[bool]:
"""安装模块

:param playerID: 待安装模块的舰船编号
Expand Down
8 changes: 8 additions & 0 deletions CAPI/python/PyAPI/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ def Produce(self) -> bool:
self.__logger.debug("Called Produce")
return self.__comm.Produce(self.__playerID, self.__teamID)

def RepairWormhole(self) -> bool:
self.__logger.debug("Called RepairWormhole")
return self.__comm.RepairWormhole(self.__playerID, self.__teamID)

def RepairHome(self) -> bool:
self.__logger.debug("Called RepairHome")
return self.__comm.RepairHome(self.__playerID, self.__teamID)

def Rebuild(self, constructionType: THUAI7.ConstructionType) -> bool:
self.__logger.debug("Called Rebuild")
return self.__comm.Rebuild(constructionType, self.__playerID, self.__teamID)
Expand Down
3 changes: 3 additions & 0 deletions dependency/proto/Services.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ service AvailableService
rpc Construct(ConstructMsg) returns (BoolRes); // 修建建筑
rpc Attack(AttackMsg) returns (BoolRes); // 攻击
rpc Send(SendMsg) returns (BoolRes); // 传递信息
rpc RepairHome(IDMsg) returns (BoolRes); // 修理大本营
rpc RepairWormhole(IDMsg) returns (BoolRes); // 修理虫洞

// 大本营
rpc InstallModule(InstallMsg) returns (BoolRes);
rpc Recycle(IDMsg) returns (BoolRes);
Expand Down
32 changes: 32 additions & 0 deletions logic/Server/RpcServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,38 @@
return Task.FromResult(boolRes);
}

public override Task<BoolRes> RepairHome(IDMsg request, ServerCallContext context)
{
GameServerLogging.logger.ConsoleLogDebug(
$"TRY RepairHome: Player {request.PlayerId} from Team {request.TeamId}");
BoolRes boolRes = new();
if (request.PlayerId >= spectatorMinPlayerID)
{
boolRes.ActSuccess = false;
return Task.FromResult(boolRes);
}
// var gameID = communicationToGameID[request.TeamId][request.PlayerId];
boolRes.ActSuccess = game.RepairHome(request.TeamId, request.PlayerId);

Check failure on line 312 in logic/Server/RpcServices.cs

View workflow job for this annotation

GitHub Actions / deploy-to-tencent-cos

'Game' does not contain a definition for 'RepairHome' and no accessible extension method 'RepairHome' accepting a first argument of type 'Game' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 312 in logic/Server/RpcServices.cs

View workflow job for this annotation

GitHub Actions / deploy-to-tencent-cos

'Game' does not contain a definition for 'RepairHome' and no accessible extension method 'RepairHome' accepting a first argument of type 'Game' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 312 in logic/Server/RpcServices.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

'Game' does not contain a definition for 'RepairHome' and no accessible extension method 'RepairHome' accepting a first argument of type 'Game' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 312 in logic/Server/RpcServices.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

'Game' does not contain a definition for 'RepairHome' and no accessible extension method 'RepairHome' accepting a first argument of type 'Game' could be found (are you missing a using directive or an assembly reference?)
GameServerLogging.logger.ConsoleLogDebug("END RepairHome");
return Task.FromResult(boolRes);
}

public override Task<BoolRes> RepairWormhole(IDMsg request, ServerCallContext context)
{
GameServerLogging.logger.ConsoleLogDebug(
$"TRY RepairWormhole: Player {request.PlayerId} from Team {request.TeamId}");
BoolRes boolRes = new();
if (request.PlayerId >= spectatorMinPlayerID)
{
boolRes.ActSuccess = false;
return Task.FromResult(boolRes);
}
// var gameID = communicationToGameID[request.TeamId][request.PlayerId];
boolRes.ActSuccess = game.RepairWormhole(request.TeamId, request.PlayerId);

Check failure on line 328 in logic/Server/RpcServices.cs

View workflow job for this annotation

GitHub Actions / deploy-to-tencent-cos

'Game' does not contain a definition for 'RepairWormhole' and no accessible extension method 'RepairWormhole' accepting a first argument of type 'Game' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 328 in logic/Server/RpcServices.cs

View workflow job for this annotation

GitHub Actions / deploy-to-tencent-cos

'Game' does not contain a definition for 'RepairWormhole' and no accessible extension method 'RepairWormhole' accepting a first argument of type 'Game' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 328 in logic/Server/RpcServices.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

'Game' does not contain a definition for 'RepairWormhole' and no accessible extension method 'RepairWormhole' accepting a first argument of type 'Game' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 328 in logic/Server/RpcServices.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

'Game' does not contain a definition for 'RepairWormhole' and no accessible extension method 'RepairWormhole' accepting a first argument of type 'Game' could be found (are you missing a using directive or an assembly reference?)
GameServerLogging.logger.ConsoleLogDebug("END RepairWormhole");
return Task.FromResult(boolRes);
}

public override Task<BoolRes> Rebuild(ConstructMsg request, ServerCallContext context)
{
GameServerLogging.logger.ConsoleLogDebug(
Expand Down
Loading