Skip to content

Commit

Permalink
Merge branch 'eesast:dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
hhzzff authored Mar 2, 2024
2 parents 6b6eee1 + caca192 commit 425aef5
Show file tree
Hide file tree
Showing 47 changed files with 16,370 additions and 11,315 deletions.
66 changes: 33 additions & 33 deletions CAPI/cpp/API/include/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ILogic
virtual bool Move(int64_t time, double angle) = 0;
virtual bool Recover() = 0;
virtual bool Produce() = 0;
virtual bool ReBuild(THUAI7::ConstructionType constructionType) = 0;
virtual bool Rebuild(THUAI7::ConstructionType constructionType) = 0;
virtual bool Construct(THUAI7::ConstructionType constructionType) = 0;
virtual bool Attack(double angle) = 0;
[[nodiscard]] virtual bool HaveView(int32_t gridX, int32_t gridY, int32_t selfX, int32_t selfY, int32_t viewRange) const = 0;
Expand Down Expand Up @@ -139,7 +139,7 @@ class IShipAPI : public IAPI
virtual std::future<bool> Attack(double angleInRadian) = 0;
virtual std::future<bool> Recover() = 0;
virtual std::future<bool> Produce() = 0;
virtual std::future<bool> ReBuild(THUAI7::ConstructionType constructionType) = 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;
virtual bool HaveView(int32_t gridX, int32_t gridY) const = 0;
Expand All @@ -165,6 +165,7 @@ class IGameTimer

class ShipAPI : public IShipAPI, public IGameTimer
{
public:
ShipAPI(ILogic& logic) :
logic(logic)
{
Expand Down Expand Up @@ -194,7 +195,7 @@ class ShipAPI : public IShipAPI, public IGameTimer
std::future<bool> Attack(double angleInRadian) override;
std::future<bool> Recover() override;
std::future<bool> Produce() override;
std::future<bool> ReBuild(THUAI7::ConstructionType constructionType) override;
std::future<bool> Rebuild(THUAI7::ConstructionType constructionType) override;
std::future<bool> Construct(THUAI7::ConstructionType constructionType) override;

[[nodiscard]] std::vector<std::shared_ptr<const THUAI7::Ship>> GetShips() const override;
Expand All @@ -212,14 +213,16 @@ class ShipAPI : public IShipAPI, public IGameTimer
[[nodiscard]] bool HaveView(int32_t gridX, int32_t gridY) const override;
[[nodiscard]] int32_t GetMoney() const override;
[[nodiscard]] int32_t GetScore() const override;

void Print(std::string str) const override
void Print(std::string str) const
{
}
void PrintShip() const
{
}
void PrintShip() const override
void PrintTeam() const
{
}
void PrintSelfInfo() const override
void PrintSelfInfo() const
{
}

Expand All @@ -229,6 +232,7 @@ class ShipAPI : public IShipAPI, public IGameTimer

class TeamAPI : public ITeamAPI, public IGameTimer
{
public:
TeamAPI(ILogic& logic) :
logic(logic)
{
Expand Down Expand Up @@ -268,14 +272,16 @@ class TeamAPI : public ITeamAPI, public IGameTimer
std::future<bool> InstallModule(int64_t playerID, THUAI7::ModuleType moduleType) override;
std::future<bool> Recycle(int64_t playerID) override;
std::future<bool> BuildShip(THUAI7::ShipType shipType, int32_t cellX, int32_t cellY) override;

void Print(std::string str) const override
void Print(std::string str) const
{
}
void PrintTeam() const override
void PrintShip() const
{
}
void PrintSelfInfo() const override
void PrintTeam() const
{
}
void PrintSelfInfo() const
{
}

Expand All @@ -285,13 +291,10 @@ class TeamAPI : public ITeamAPI, public IGameTimer

class ShipDebugAPI : public IShipAPI, public IGameTimer
{
public:
ShipDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int64_t shipID);
void StartTimer() override
{
}
void EndTimer() override
{
}
void StartTimer() override;
void EndTimer() override;
void Play(IAI& ai) override;
std::future<bool> SendTextMessage(int64_t, std::string) override;
std::future<bool> SendBinaryMessage(int64_t, std::string) override;
Expand All @@ -309,7 +312,7 @@ class ShipDebugAPI : public IShipAPI, public IGameTimer
std::future<bool> Attack(double angleInRadian) override;
std::future<bool> Recover() override;
std::future<bool> Produce() override;
std::future<bool> ReBuild(THUAI7::ConstructionType constructionType) override;
std::future<bool> Rebuild(THUAI7::ConstructionType constructionType) override;
std::future<bool> Construct(THUAI7::ConstructionType constructionType) override;

[[nodiscard]] std::vector<std::shared_ptr<const THUAI7::Ship>> GetShips() const override;
Expand All @@ -328,13 +331,10 @@ class ShipDebugAPI : public IShipAPI, public IGameTimer
[[nodiscard]] int32_t GetMoney() const override;
[[nodiscard]] int32_t GetScore() const override;

void Print(std::string str) const override
{
}
void PrintShip() const override
{
}
void PrintSelfInfo() const override
void Print(std::string str) const override;
void PrintShip() const override;
void PrintSelfInfo() const override;
void PrintTeam() const
{
}

Expand All @@ -346,13 +346,10 @@ class ShipDebugAPI : public IShipAPI, public IGameTimer

class TeamDebugAPI : public ITeamAPI, public IGameTimer
{
public:
TeamDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int64_t TeamID);
void StartTimer() override
{
}
void EndTimer() override
{
}
void StartTimer() override;
void EndTimer() override;
void Play(IAI& ai) override;

std::future<bool> SendTextMessage(int64_t, std::string) override;
Expand Down Expand Up @@ -382,10 +379,13 @@ class TeamDebugAPI : public ITeamAPI, public IGameTimer
std::future<bool> InstallModule(int64_t playerID, THUAI7::ModuleType moduleType) override;
std::future<bool> Recycle(int64_t playerID) override;
std::future<bool> BuildShip(THUAI7::ShipType shipType, int32_t cellX, int32_t cellY) override;
void Print(std::string str) const override
void Print(std::string str) const override;
void PrintSelfInfo() const override;
// TODO
void PrintTeam() const
{
}
void PrintSelfInfo() const override
void PrintShip() const
{
}

Expand Down
2 changes: 1 addition & 1 deletion CAPI/cpp/API/include/Communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Communication
bool EndAllAction(int64_t playerID, int64_t teamID);
// Ship
bool Move(int64_t playerID, int64_t teamID, int64_t time, double angle);
bool Recover(int64_t playerID, int64_t teamID);
bool Recover(int64_t playerID, int64_t recover, int64_t teamID);
bool Produce(int64_t playerID, int64_t teamID);
bool Rebuild(int64_t playerID, int64_t teamID, THUAI7::ConstructionType constructionType);
bool Construct(int64_t playerID, int64_t teamID, THUAI7::ConstructionType constructionType);
Expand Down
2 changes: 1 addition & 1 deletion CAPI/cpp/API/include/logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Logic : public ILogic
bool Move(int64_t time, double angle);
bool Recover();
bool Produce();
bool ReBuild(THUAI7::ConstructionType constructionType);
bool Rebuild(THUAI7::ConstructionType constructionType);
bool Construct(THUAI7::ConstructionType constructionType);
bool Attack(double angle);
[[nodiscard]] bool HaveView(int32_t gridX, int32_t gridY, int32_t selfX, int32_t selfY, int32_t viewRange) const;
Expand Down
6 changes: 0 additions & 6 deletions CAPI/cpp/API/include/structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ namespace THUAI7
Blue = 2,
};

inline std::map<PlayerTeam, int32_t> playerTeamDict{
{PlayerTeam::NullTeam, 0},
{PlayerTeam::Red, 1},
{PlayerTeam::Blue, 2},
};

enum class PlayerType : unsigned char
{
NullPlayerType = 0,
Expand Down
10 changes: 10 additions & 0 deletions CAPI/cpp/API/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ namespace Proto2THUAI7
gameInfo->gameTime = allMsg.game_time();
gameInfo->redScore = allMsg.red_team_score();
gameInfo->blueScore = allMsg.blue_team_score();
return gameInfo;
}
} // namespace Proto2THUAI7
// 辅助函数,用于将proto信息转换为THUAI7信息
Expand Down Expand Up @@ -466,6 +467,15 @@ namespace THUAI72Proto
return IDMsg;
}

inline protobuf::RecoverMsg THUAI72ProtobufRecoverMsg(int64_t playerID, int64_t recover, int64_t teamID)
{
protobuf::RecoverMsg RecoverMsg;
RecoverMsg.set_player_id(playerID);
RecoverMsg.set_recover(recover);
RecoverMsg.set_team_id(teamID);
return RecoverMsg;
}

inline protobuf::ConstructMsg THUAI72ProtobufConstructMsg(int64_t playerID, int64_t teamID, THUAI7::ConstructionType constructionType)
{
protobuf::ConstructMsg constructMsg;
Expand Down
4 changes: 2 additions & 2 deletions CAPI/cpp/API/src/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ std::future<bool> ShipAPI::Produce()
{ return logic.Produce(); });
}

std::future<bool> ShipAPI::ReBuild(THUAI7::ConstructionType constructionType)
std::future<bool> ShipAPI::Rebuild(THUAI7::ConstructionType constructionType)
{
return std::async(std::launch::async, [=]()
{ return logic.ReBuild(constructionType); });
{ return logic.Rebuild(constructionType); });
}

std::future<bool> ShipAPI::Construct(THUAI7::ConstructionType constructionType)
Expand Down
16 changes: 14 additions & 2 deletions CAPI/cpp/API/src/Communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool Communication::EndAllAction(int64_t playerID, int64_t teamID)
return false;
}

bool Communication::Recover(int64_t playerID, int64_t teamID)
bool Communication::Recover(int64_t playerID, int64_t recover, int64_t teamID)
{
{
std::lock_guard<std::mutex> lock(mtxLimit);
Expand All @@ -85,7 +85,7 @@ bool Communication::Recover(int64_t playerID, int64_t teamID)
}
protobuf::BoolRes recoverResult;
ClientContext context;
auto request = THUAI72Proto::THUAI72ProtobufIDMsg(playerID, teamID);
auto request = THUAI72Proto::THUAI72ProtobufRecoverMsg(playerID, recover, teamID);
auto status = THUAI7Stub->Recover(&context, request, &recoverResult);
if (status.ok())
return recoverResult.act_success();
Expand Down Expand Up @@ -199,6 +199,18 @@ bool Communication::BuildShip(int64_t teamID, THUAI7::ShipType shipType, int32_t
return false;
}

bool Communication::Recycle(int64_t playerID, int64_t teamID)
{
protobuf::BoolRes reply;
ClientContext context;
auto request = THUAI72Proto::THUAI72ProtobufIDMsg(playerID, teamID);
auto status = THUAI7Stub->Recycle(&context, request, &reply);
if (status.ok())
return true;
else
return false;
}

bool Communication::TryConnection(int64_t playerID, int64_t teamID)
{
protobuf::BoolRes reply;
Expand Down
34 changes: 32 additions & 2 deletions CAPI/cpp/API/src/DebugAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ std::future<bool> ShipDebugAPI::Produce()
return std::async(std::launch::async, [=]()
{ return logic.Produce(); });
}
std::future<bool> ShipDebugAPI::ReBuild(THUAI7::ConstructionType constructionType)
std::future<bool> ShipDebugAPI::Rebuild(THUAI7::ConstructionType constructionType)
{
return std::async(std::launch::async, [=]()
{ return logic.ReBuild(constructionType); });
{ return logic.Rebuild(constructionType); });
}

std::future<bool> ShipDebugAPI::Construct(THUAI7::ConstructionType constructionType)
Expand Down Expand Up @@ -261,6 +261,12 @@ void ShipDebugAPI::PrintSelfInfo() const
logger->info("*********************\n");
}

std::future<bool> ShipDebugAPI::EndAllAction()
{
return std::async(std::launch::async, [this]()
{ return logic.EndAllAction(); });
}

TeamDebugAPI::TeamDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int64_t playerID) :
logic(logic)
{
Expand Down Expand Up @@ -423,6 +429,19 @@ std::future<bool> TeamDebugAPI::InstallModule(int64_t playerID, THUAI7::ModuleTy
return std::async(std::launch::async, [=]()
{ return logic.InstallModule(playerID, moduleType); });
}

std::future<bool> TeamDebugAPI::Recycle(int64_t playerID)
{
return std::async(std::launch::async, [=]()
{ return logic.Recycle(playerID); });
}

std::future<bool> TeamDebugAPI::BuildShip(THUAI7::ShipType shipType, int32_t x, int32_t y)
{
return std::async(std::launch::async, [=]()
{ return logic.BuildShip(shipType, x, y); });
}

void TeamDebugAPI::PrintSelfInfo() const
{
auto Team = logic.TeamGetSelfInfo();
Expand All @@ -440,3 +459,14 @@ void TeamDebugAPI::Play(IAI& ai)
{
ai.play(*this);
}

void TeamDebugAPI::Print(std::string str) const
{
logger->info(str);
}

std::future<bool> TeamDebugAPI::EndAllAction()
{
return std::async(std::launch::async, [this]()
{ return logic.EndAllAction(); });
}
24 changes: 19 additions & 5 deletions CAPI/cpp/API/src/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,21 @@ bool Logic::Attack(double angle)
bool Logic::Recover()
{
logger->debug("Called Recover");
return pComm->Recover(playerID, teamID);
// TODO recover
int64_t recover = 1;
return pComm->Recover(playerID, recover, teamID);
}

bool Logic::Construct(THUAI7::ConstructionType constructiontype)
{
logger->debug("Called Construct");
return pComm->Construct(playerID, teamID, constructiontype);
}

bool Logic::BuildShip(THUAI7::ShipType shiptype, int32_t x, int32_t y)
{
logger->debug("Called BuildShip");
return pComm->BuildShip(teamID, shiptype, x, y);
}

// 等待完成
Expand Down Expand Up @@ -300,14 +314,14 @@ void Logic::ProcessMessage()
{
auto map = std::vector<std::vector<THUAI7::PlaceType>>();
auto mapResult = item.map_message();
for (int32_t i = 0; i < item.map_message().row_size(); i++)
for (int32_t i = 0; i < item.map_message().rows_size(); i++)
{
std::vector<THUAI7::PlaceType> row;
for (int32_t j = 0; j < mapResult.row(i).col_size(); j++)
for (int32_t j = 0; j < mapResult.rows(i).cols_size(); j++)
{
if (Proto2THUAI7::placeTypeDict.count(mapResult.row(i).col(j)) == 0)
if (Proto2THUAI7::placeTypeDict.count(mapResult.rows(i).cols(j)) == 0)
logger->error("Unknown place type!");
row.push_back(Proto2THUAI7::placeTypeDict[mapResult.row(i).col(j)]);
row.push_back(Proto2THUAI7::placeTypeDict[mapResult.rows(i).cols(j)]);
}
map.push_back(std::move(row));
}
Expand Down
4 changes: 2 additions & 2 deletions CAPI/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 临时CMakeLists,仅供本地调试用
cmake_minimum_required(VERSION 3.5)

project(THUAI6_CAPI VERSION 1.0)
project(THUAI7_CAPI VERSION 1.0)

set(CMAKE_CXX_STANDARD 17)

Expand All @@ -25,4 +25,4 @@ protobuf::libprotobuf
gRPC::grpc
gRPC::grpc++_reflection
gRPC::grpc++
)
)
Loading

0 comments on commit 425aef5

Please sign in to comment.