Skip to content

Commit 06f9ece

Browse files
authored
Merge branch 'eesast:dev' into dev
2 parents d9c3fe5 + a1a5e01 commit 06f9ece

16 files changed

Lines changed: 1272 additions & 1222 deletions

File tree

CAPI/cpp/API/include/API.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class ILogic
7575
virtual bool BuildCharacter(THUAI9::CharacterType CharacterType, int32_t playerID) = 0;
7676
virtual bool ProduceGoods(THUAI9::GoodsType goodsType, int32_t maxProduceNum) = 0;
7777
virtual bool UplevelTech(THUAI9::TechType techType) = 0;
78+
virtual std::string AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey) = 0;
7879
};
7980

8081
class IAPI
@@ -93,6 +94,7 @@ class IAPI
9394
// 等待下一帧
9495
virtual bool Wait() = 0;
9596
virtual std::future<bool> EndAllAction() = 0;
97+
virtual std::future<std::string> AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey = "") = 0;
9698
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI9::Character>> GetCharacters() const = 0;
9799
[[nodiscard]] virtual std::vector<std::shared_ptr<const THUAI9::Character>> GetEnemyCharacters() const = 0;
98100
[[nodiscard]] virtual std::vector<std::vector<THUAI9::PlaceType>> GetFullMap() const = 0;
@@ -190,6 +192,7 @@ class CharacterAPI : public ICharacterAPI, public IGameTimer
190192
[[nodiscard]] int32_t GetFrameCount() const override;
191193
bool Wait() override;
192194
std::future<bool> EndAllAction() override;
195+
std::future<std::string> AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey = "") override;
193196

194197
std::future<bool> Move(int64_t moveTimeInMilliseconds, double angle) override;
195198
std::future<bool> MoveRight(int64_t timeInMilliseconds) override;
@@ -258,6 +261,7 @@ class TeamAPI : public ITeamAPI, public IGameTimer
258261
[[nodiscard]] int32_t GetFrameCount() const override;
259262
bool Wait() override;
260263
std::future<bool> EndAllAction() override;
264+
std::future<std::string> AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey = "") override;
261265

262266
[[nodiscard]] std::vector<std::shared_ptr<const THUAI9::Character>> GetCharacters() const override;
263267
[[nodiscard]] std::vector<std::shared_ptr<const THUAI9::Character>> GetEnemyCharacters() const override;
@@ -307,6 +311,7 @@ class CharacterDebugAPI : public ICharacterAPI, public IGameTimer
307311
bool Wait() override;
308312
[[nodiscard]] int32_t GetFrameCount() const override;
309313
std::future<bool> EndAllAction() override;
314+
std::future<std::string> AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey = "") override;
310315

311316
std::future<bool> Move(int64_t moveTimeInMilliseconds, double angle) override;
312317
std::future<bool> MoveRight(int64_t timeInMilliseconds) override;
@@ -366,6 +371,7 @@ class TeamDebugAPI : public ITeamAPI, public IGameTimer
366371
[[nodiscard]] int32_t GetFrameCount() const override;
367372
bool Wait() override;
368373
std::future<bool> EndAllAction() override;
374+
std::future<std::string> AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey = "") override;
369375

370376
[[nodiscard]] std::vector<std::shared_ptr<const THUAI9::Character>> GetCharacters() const override;
371377
[[nodiscard]] std::vector<std::shared_ptr<const THUAI9::Character>> GetEnemyCharacters() const override;

CAPI/cpp/API/include/Communication.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class Communication
5050
bool ProduceGoods(int64_t teamID, THUAI9::GoodsType goodsType, int32_t maxProduceNum);
5151
bool UplevelTech(int64_t teamID, THUAI9::TechType techType);
5252

53+
std::string AskAI(int64_t teamID, int64_t currentGameTime, const std::string& prompt, const std::string& apiKey);
54+
5355
private:
5456
std::unique_ptr<protobuf::AvailableService::Stub> THUAI9Stub;
5557
bool haveNewMessage = false;

CAPI/cpp/API/include/logic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class Logic : public ILogic
136136
bool BuildCharacter(THUAI9::CharacterType CharacterType, int32_t playerID);
137137
bool ProduceGoods(THUAI9::GoodsType goodsType, int32_t maxProduceNum);
138138
bool UplevelTech(THUAI9::TechType techType);
139+
std::string AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey);
139140

140141
bool TryConnection();
141142
void ProcessMessage();

CAPI/cpp/API/src/AI.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ void AI::play(ICharacterAPI& api)
1818
void AI::play(ITeamAPI& api)
1919
{
2020
(void)api;
21+
// auto answer = api.AskAI(/*currentGameTime*/, "your prompt", "<your uuid you can copy from eesast>").get();
2122
// TODO: implement team strategy.
2223
}

CAPI/cpp/API/src/API.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,24 @@ std::future<bool> CharacterAPI::EndAllAction()
8181
{ return logic.EndAllAction(); });
8282
}
8383

84+
std::future<std::string> CharacterAPI::AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey)
85+
{
86+
return std::async(std::launch::async, [this, currentGameTime, prompt = std::move(prompt), apiKey = std::move(apiKey)]() mutable
87+
{ return logic.AskAI(currentGameTime, std::move(prompt), std::move(apiKey)); });
88+
}
89+
8490
std::future<bool> TeamAPI::EndAllAction()
8591
{
8692
return std::async(std::launch::async, [this]()
8793
{ return logic.EndAllAction(); });
8894
}
8995

96+
std::future<std::string> TeamAPI::AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey)
97+
{
98+
return std::async(std::launch::async, [this, currentGameTime, prompt = std::move(prompt), apiKey = std::move(apiKey)]() mutable
99+
{ return logic.AskAI(currentGameTime, std::move(prompt), std::move(apiKey)); });
100+
}
101+
90102
std::vector<std::shared_ptr<const THUAI9::Character>> CharacterAPI::GetCharacters() const
91103
{
92104
return logic.GetCharacters();

CAPI/cpp/API/src/Communication.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,23 @@ bool Communication::UplevelTech(int64_t teamID, THUAI9::TechType techType)
187187
return status.ok() && result.act_success();
188188
}
189189

190+
std::string Communication::AskAI(int64_t teamID, int64_t currentGameTime, const std::string& prompt, const std::string& apiKey)
191+
{
192+
if (!ConsumeQuota(mtxLimit, counter, limit))
193+
return {};
194+
195+
protobuf::StrategicAIResponse reply;
196+
ClientContext context;
197+
protobuf::StrategicAIRequest request;
198+
request.set_team_id(teamID);
199+
request.set_current_game_time(currentGameTime);
200+
request.set_prompt(apiKey + "||" + prompt);
201+
auto status = THUAI9Stub->AskAI(&context, request, &reply);
202+
if (status.ok() && reply.act_success())
203+
return reply.answer();
204+
return {};
205+
}
206+
190207
bool Communication::TryConnection(int32_t playerID, int32_t teamID)
191208
{
192209
protobuf::BoolRes reply;

CAPI/cpp/API/src/DebugAPI.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ std::future<bool> CharacterDebugAPI::EndAllAction()
9999
{ return logic.EndAllAction(); });
100100
}
101101

102+
std::future<std::string> CharacterDebugAPI::AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey)
103+
{
104+
return std::async(std::launch::async, [this, currentGameTime, prompt = std::move(prompt), apiKey = std::move(apiKey)]() mutable
105+
{ return logic.AskAI(currentGameTime, std::move(prompt), std::move(apiKey)); });
106+
}
107+
102108
std::future<bool> CharacterDebugAPI::Move(int64_t moveTimeInMilliseconds, double angle)
103109
{
104110
logger->info("Move {} ms", moveTimeInMilliseconds);
@@ -352,6 +358,12 @@ std::future<bool> TeamDebugAPI::EndAllAction()
352358
{ return logic.EndAllAction(); });
353359
}
354360

361+
std::future<std::string> TeamDebugAPI::AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey)
362+
{
363+
return std::async(std::launch::async, [this, currentGameTime, prompt = std::move(prompt), apiKey = std::move(apiKey)]() mutable
364+
{ return logic.AskAI(currentGameTime, std::move(prompt), std::move(apiKey)); });
365+
}
366+
355367
std::vector<std::shared_ptr<const THUAI9::Character>> TeamDebugAPI::GetCharacters() const
356368
{
357369
return logic.GetCharacters();

CAPI/cpp/API/src/logic.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ bool Logic::UplevelTech(THUAI9::TechType techType)
280280
return pComm->UplevelTech(teamID, techType);
281281
}
282282

283+
std::string Logic::AskAI(int64_t currentGameTime, std::string prompt, std::string apiKey)
284+
{
285+
return pComm->AskAI(teamID, currentGameTime, prompt, apiKey);
286+
}
287+
283288
bool Logic::TryConnection()
284289
{
285290
return pComm->TryConnection(playerID, teamID);

0 commit comments

Comments
 (0)