Browse Source

feat(CAPI): update with proto and some bug fix

tags/0.1.0
DragonAura 2 years ago
parent
commit
d5dfdef71d
17 changed files with 212 additions and 144 deletions
  1. +7
    -3
      CAPI/cpp/API/include/structures.h
  2. +27
    -24
      CAPI/cpp/API/include/utils.hpp
  3. +7
    -8
      CAPI/cpp/API/src/AI.cpp
  4. +0
    -1
      CAPI/cpp/API/src/Communication.cpp
  5. +5
    -0
      CAPI/cpp/API/src/logic.cpp
  6. +7
    -7
      CAPI/cpp/API/src/main.cpp
  7. +39
    -37
      CAPI/cpp/proto/MessageType.pb.cc
  8. +21
    -20
      CAPI/cpp/proto/MessageType.pb.h
  9. +41
    -10
      CAPI/python/PyAPI/AI.py
  10. +2
    -2
      CAPI/python/PyAPI/DebugAPI.py
  11. +4
    -1
      CAPI/python/PyAPI/logic.py
  12. +7
    -6
      CAPI/python/PyAPI/main.py
  13. +3
    -1
      CAPI/python/PyAPI/structures.py
  14. +25
    -21
      CAPI/python/PyAPI/utils.py
  15. +12
    -0
      CAPI/python/generate_proto.sh
  16. +2
    -2
      CAPI/python/requirements.txt
  17. +3
    -1
      CAPI/python/run.sh

+ 7
- 3
CAPI/cpp/API/include/structures.h View File

@@ -52,7 +52,7 @@ namespace THUAI6
Key5 = 2,
Key6 = 3,
AddSpeed = 4,
AddLifeOrAp = 5,
AddLifeOrClairaudience = 5,
AddHpOrAp = 6,
ShieldOrSpear = 7,
RecoveryFromDizziness = 8,
@@ -83,7 +83,8 @@ namespace THUAI6
Athlete = 1,
Teacher = 2,
StraightAStudent = 3,
StudentType4 = 4,
Robot = 4,
TechOtaku = 5,
};

// 捣蛋鬼类型
@@ -282,12 +283,15 @@ namespace THUAI6
{StudentType::Athlete, "Athlete"},
{StudentType::Teacher, "Teacher"},
{StudentType::StraightAStudent, "StraightAStudent"},
{StudentType::Robot, "Robot"},
{StudentType::TechOtaku, "TechOtaku"},
};

inline std::map<TrickerType, std::string> trickerTypeDict{
{TrickerType::NullTrickerType, "NullTrickerType"},
{TrickerType::Assassin, "Assassin"},
{TrickerType::Klee, "Klee"},
{TrickerType::ANoisyPerson, "ANoisyPerson"},
};

inline std::map<PlayerState, std::string> playerStateDict{
@@ -339,7 +343,7 @@ namespace THUAI6
{PropType::Key5, "Key5"},
{PropType::Key6, "Key6"},
{PropType::AddSpeed, "AddSpeed"},
{PropType::AddLifeOrAp, "AddLifeOrAp"},
{PropType::AddLifeOrClairaudience, "AddLifeOrClairaudience"},
{PropType::AddHpOrAp, "AddHpOrAp"},
{PropType::ShieldOrSpear, "ShieldOrSpear"},
{PropType::RecoveryFromDizziness, "RecoveryFromDizziness"},


+ 27
- 24
CAPI/cpp/API/include/utils.hpp View File

@@ -4,6 +4,7 @@
#define UTILS_HPP

#include <cstdint>
#include <cmath>
#include "Message2Clients.pb.h"
#include "Message2Server.pb.h"
#include "MessageType.pb.h"
@@ -93,7 +94,7 @@ namespace Proto2THUAI6
{protobuf::PropType::KEY6, THUAI6::PropType::Key6},
{protobuf::PropType::ADD_SPEED, THUAI6::PropType::AddSpeed},
{protobuf::PropType::ADD_HP_OR_AP, THUAI6::PropType::AddHpOrAp},
{protobuf::PropType::ADD_LIFE_OR_AP, THUAI6::PropType::AddLifeOrAp},
{protobuf::PropType::ADD_LIFE_OR_CLAIRAUDIENCE, THUAI6::PropType::AddLifeOrClairaudience},
{protobuf::PropType::SHIELD_OR_SPEAR, THUAI6::PropType::ShieldOrSpear},
{protobuf::PropType::RECOVERY_FROM_DIZZINESS, THUAI6::PropType::RecoveryFromDizziness},
};
@@ -107,34 +108,35 @@ namespace Proto2THUAI6
inline std::map<protobuf::StudentType, THUAI6::StudentType> studentTypeDict{
{protobuf::StudentType::NULL_STUDENT_TYPE, THUAI6::StudentType::NullStudentType},
{protobuf::StudentType::ATHLETE, THUAI6::StudentType::Athlete},
{protobuf::StudentType::STUDENTTYPE2, THUAI6::StudentType::Teacher},
{protobuf::StudentType::STUDENTTYPE3, THUAI6::StudentType::StraightAStudent},
{protobuf::StudentType::STUDENTTYPE4, THUAI6::StudentType::StudentType4},
{protobuf::StudentType::TEACHER, THUAI6::StudentType::Teacher},
{protobuf::StudentType::STRAIGHT_A_STUDENT, THUAI6::StudentType::StraightAStudent},
{protobuf::StudentType::ROBOT, THUAI6::StudentType::Robot},
{protobuf::StudentType::TECH_OTAKU, THUAI6::StudentType::TechOtaku},
};

inline std::map<protobuf::TrickerType, THUAI6::TrickerType> trickerTypeDict{
{protobuf::TrickerType::NULL_TRICKER_TYPE, THUAI6::TrickerType::NullTrickerType},
{protobuf::TrickerType::ASSASSIN, THUAI6::TrickerType::Assassin},
{protobuf::TrickerType::TRICKERTYPE2, THUAI6::TrickerType::Klee},
{protobuf::TrickerType::TRICKERTYPE3, THUAI6::TrickerType::ANoisyPerson},
{protobuf::TrickerType::KLEE, THUAI6::TrickerType::Klee},
{protobuf::TrickerType::A_NOISY_PERSON, THUAI6::TrickerType::ANoisyPerson},
{protobuf::TrickerType::TRICKERTYPE4, THUAI6::TrickerType::TrickerType4},
};

inline std::map<protobuf::StudentBuffType, THUAI6::StudentBuffType> studentBuffTypeDict{
{protobuf::StudentBuffType::NULL_SBUFF_TYPE, THUAI6::StudentBuffType::NullStudentBuffType},
{protobuf::StudentBuffType::SBUFFTYPE1, THUAI6::StudentBuffType::AddSpeed},
{protobuf::StudentBuffType::SBUFFTYPE2, THUAI6::StudentBuffType::AddLife},
{protobuf::StudentBuffType::SBUFFTYPE3, THUAI6::StudentBuffType::Shield},
{protobuf::StudentBuffType::SBUFFTYPE4, THUAI6::StudentBuffType::Invisible},
{protobuf::StudentBuffType::STUDENT_ADD_SPEED, THUAI6::StudentBuffType::AddSpeed},
{protobuf::StudentBuffType::ADD_LIFE, THUAI6::StudentBuffType::AddLife},
{protobuf::StudentBuffType::SHIELD, THUAI6::StudentBuffType::Shield},
{protobuf::StudentBuffType::STUDENT_INVISIBLE, THUAI6::StudentBuffType::Invisible},
};

inline std::map<protobuf::TrickerBuffType, THUAI6::TrickerBuffType> trickerBuffTypeDict{
{protobuf::TrickerBuffType::NULL_TBUFF_TYPE, THUAI6::TrickerBuffType::NullTrickerBuffType},
{protobuf::TrickerBuffType::TBUFFTYPE1, THUAI6::TrickerBuffType::AddSpeed},
{protobuf::TrickerBuffType::TBUFFTYPE2, THUAI6::TrickerBuffType::Spear},
{protobuf::TrickerBuffType::TBUFFTYPE3, THUAI6::TrickerBuffType::AddAp},
{protobuf::TrickerBuffType::TBUFFTYPE4, THUAI6::TrickerBuffType::Clairaudience},
{protobuf::TrickerBuffType::INVISIBLE, THUAI6::TrickerBuffType::Invisible},
{protobuf::TrickerBuffType::TRICKER_ADD_SPEED, THUAI6::TrickerBuffType::AddSpeed},
{protobuf::TrickerBuffType::SPEAR, THUAI6::TrickerBuffType::Spear},
{protobuf::TrickerBuffType::ADD_AP, THUAI6::TrickerBuffType::AddAp},
{protobuf::TrickerBuffType::CLAIRAUDIENCE, THUAI6::TrickerBuffType::Clairaudience},
{protobuf::TrickerBuffType::TRICKER_INVISIBLE, THUAI6::TrickerBuffType::Invisible},
};

inline std::map<protobuf::PlayerState, THUAI6::PlayerState> playerStateDict{
@@ -170,8 +172,8 @@ namespace Proto2THUAI6
{protobuf::BulletType::NULL_BULLET_TYPE, THUAI6::BulletType::NullBulletType},
{protobuf::BulletType::FLYING_KNIFE, THUAI6::BulletType::FlyingKnife},
{protobuf::BulletType::COMMON_ATTACK_OF_TRICKER, THUAI6::BulletType::CommonAttackOfTricker},
{protobuf::BulletType::FAST_BULLET, THUAI6::BulletType::BombBomb},
{protobuf::BulletType::ORDINARY_BULLET, THUAI6::BulletType::JumpyDumpty},
{protobuf::BulletType::BOMB_BOMB, THUAI6::BulletType::BombBomb},
{protobuf::BulletType::JUMPY_DUMPTY, THUAI6::BulletType::JumpyDumpty},
{protobuf::BulletType::ATOM_BOMB, THUAI6::BulletType::AtomBomb},
};

@@ -350,7 +352,7 @@ namespace THUAI62Proto
{THUAI6::PropType::Key5, protobuf::PropType::KEY5},
{THUAI6::PropType::Key6, protobuf::PropType::KEY6},
{THUAI6::PropType::AddHpOrAp, protobuf::PropType::ADD_HP_OR_AP},
{THUAI6::PropType::AddLifeOrAp, protobuf::PropType::ADD_LIFE_OR_AP},
{THUAI6::PropType::AddLifeOrClairaudience, protobuf::PropType::ADD_LIFE_OR_CLAIRAUDIENCE},
{THUAI6::PropType::AddSpeed, protobuf::PropType::ADD_SPEED},
{THUAI6::PropType::ShieldOrSpear, protobuf::PropType::SHIELD_OR_SPEAR},
};
@@ -364,14 +366,15 @@ namespace THUAI62Proto
inline std::map<THUAI6::StudentType, protobuf::StudentType> studentTypeDict{
{THUAI6::StudentType::NullStudentType, protobuf::StudentType::NULL_STUDENT_TYPE},
{THUAI6::StudentType::Athlete, protobuf::StudentType::ATHLETE},
{THUAI6::StudentType::Teacher, protobuf::StudentType::STUDENTTYPE2},
{THUAI6::StudentType::StraightAStudent, protobuf::StudentType::STUDENTTYPE3},
{THUAI6::StudentType::StudentType4, protobuf::StudentType::STUDENTTYPE4},
{THUAI6::StudentType::Teacher, protobuf::StudentType::TEACHER},
{THUAI6::StudentType::StraightAStudent, protobuf::StudentType::STRAIGHT_A_STUDENT},
{THUAI6::StudentType::Robot, protobuf::StudentType::ROBOT},
{THUAI6::StudentType::TechOtaku, protobuf::StudentType::TECH_OTAKU},
};

// inline std::map<THUAI6::StudentBuffType, protobuf::StudentBuffType> studentBuffTypeDict{
// {THUAI6::StudentBuffType::NullStudentBuffType, protobuf::StudentBuffType::NULL_SBUFF_TYPE},
// {THUAI6::StudentBuffType::StudentBuffType1, protobuf::StudentBuffType::SBUFFTYPE1},
// {THUAI6::StudentBuffType::StudentBuffType1, protobuf::StudentBuffType::ADD_SPEED},
// {THUAI6::StudentBuffType::StudentBuffType2, protobuf::StudentBuffType::SBUFFTYPE2},
// {THUAI6::StudentBuffType::StudentBuffType3, protobuf::StudentBuffType::SBUFFTYPE3},
// {THUAI6::StudentBuffType::StudentBuffType4, protobuf::StudentBuffType::SBUFFTYPE4},
@@ -380,8 +383,8 @@ namespace THUAI62Proto
inline std::map<THUAI6::TrickerType, protobuf::TrickerType> trickerTypeDict{
{THUAI6::TrickerType::NullTrickerType, protobuf::TrickerType::NULL_TRICKER_TYPE},
{THUAI6::TrickerType::Assassin, protobuf::TrickerType::ASSASSIN},
{THUAI6::TrickerType::Klee, protobuf::TrickerType::TRICKERTYPE2},
{THUAI6::TrickerType::ANoisyPerson, protobuf::TrickerType::TRICKERTYPE3},
{THUAI6::TrickerType::Klee, protobuf::TrickerType::KLEE},
{THUAI6::TrickerType::ANoisyPerson, protobuf::TrickerType::A_NOISY_PERSON},
{THUAI6::TrickerType::TrickerType4, protobuf::TrickerType::TRICKERTYPE4},
};



+ 7
- 8
CAPI/cpp/API/src/AI.cpp View File

@@ -6,7 +6,7 @@
extern const bool asynchronous = false;

// 选手必须定义该变量来选择自己的阵营
extern const THUAI6::PlayerType playerType = THUAI6::PlayerType::StudentPlayer;
extern const THUAI6::PlayerType playerType = THUAI6::PlayerType::TrickerPlayer;

// 选手需要将两个都定义,本份代码中不选择的阵营任意定义即可
extern const THUAI6::TrickerType trickerType = THUAI6::TrickerType::Assassin;
@@ -17,15 +17,14 @@ extern const THUAI6::StudentType studentType = THUAI6::StudentType::Athlete;

void AI::play(IStudentAPI& api)
{
api.SendMessage(1, "Hello, I'm player 0 using C++ interface!");
if (api.HaveMessage())
{
auto msg = api.GetMessage();
api.Print("Player " + std::to_string(msg.first) + " says: " + msg.second);
}
api.PrintTricker();
}

void AI::play(ITrickerAPI& api)
{
api.Move(100, 1);
if (api.HaveMessage())
{
auto msg = api.GetMessage();
api.Print("Message from " + std::to_string(msg.first) + ": " + msg.second);
}
}

+ 0
- 1
CAPI/cpp/API/src/Communication.cpp View File

@@ -215,7 +215,6 @@ bool Communication::TryConnection(int64_t playerID)
auto status = THUAI6Stub->TryConnection(&context, request, &reply);
if (status.ok())
{
std::cout << "Connection success!" << std::endl;
return true;
}
else


+ 5
- 0
CAPI/cpp/API/src/logic.cpp View File

@@ -75,6 +75,11 @@ std::vector<std::vector<THUAI6::PlaceType>> Logic::GetFullMap() const
THUAI6::PlaceType Logic::GetPlaceType(int32_t cellX, int32_t cellY) const
{
std::unique_lock<std::mutex> lock(mtxState);
if (cellX < 0 || cellX >= currentState->gameMap.size() || cellY < 0 || cellY >= currentState->gameMap[0].size())
{
logger->warn("Invalid position!");
return THUAI6::PlaceType::NullPlaceType;
}
logger->debug("Called GetPlaceType");
return currentState->gameMap[cellX][cellY];
}


+ 7
- 7
CAPI/cpp/API/src/main.cpp View File

@@ -18,13 +18,13 @@ int THUAI6Main(int argc, char** argv, CreateAIFunc AIBuilder)
extern const THUAI6::PlayerType playerType;
extern const THUAI6::TrickerType trickerType;
extern const THUAI6::StudentType studentType;
{
file = true;
print = true;
Logic logic(playerType, pID, trickerType, studentType);
logic.Main(AIBuilder, sIP, sPort, file, print, warnOnly);
return 0;
}
// {
// file = true;
// print = true;
// Logic logic(playerType, pID, trickerType, studentType);
// logic.Main(AIBuilder, sIP, sPort, file, print, warnOnly);
// return 0;
// }

// 使用cmdline的正式版本
try


+ 39
- 37
CAPI/cpp/proto/MessageType.pb.cc View File

@@ -30,48 +30,49 @@ static constexpr ::_pbi::MigrationSchema* schemas = nullptr;
static constexpr ::_pb::Message* const* file_default_instances = nullptr;

const char descriptor_table_protodef_MessageType_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) =
"\n\021MessageType.proto\022\010protobuf*\207\001\n\nBullet"
"\n\021MessageType.proto\022\010protobuf*\202\001\n\nBullet"
"Type\022\024\n\020NULL_BULLET_TYPE\020\000\022\020\n\014FLYING_KNI"
"FE\020\001\022\034\n\030COMMON_ATTACK_OF_TRICKER\020\002\022\017\n\013FA"
"ST_BULLET\020\003\022\023\n\017ORDINARY_BULLET\020\004\022\r\n\tATOM"
"_BOMB\020\005*\241\001\n\tPlaceType\022\023\n\017NULL_PLACE_TYPE"
"\020\000\022\010\n\004LAND\020\001\022\010\n\004WALL\020\002\022\t\n\005GRASS\020\003\022\r\n\tCLA"
"SSROOM\020\004\022\010\n\004GATE\020\005\022\017\n\013HIDDEN_GATE\020\006\022\n\n\006W"
"INDOW\020\007\022\t\n\005DOOR3\020\010\022\t\n\005DOOR5\020\t\022\t\n\005DOOR6\020\n"
"\022\t\n\005CHEST\020\013*8\n\tShapeType\022\023\n\017NULL_SHAPE_T"
"YPE\020\000\022\n\n\006CIRCLE\020\001\022\n\n\006SQUARE\020\002*\243\001\n\010PropTy"
"pe\022\022\n\016NULL_PROP_TYPE\020\000\022\r\n\tADD_SPEED\020\001\022\022\n"
"\016ADD_LIFE_OR_AP\020\002\022\020\n\014ADD_HP_OR_AP\020\003\022\023\n\017S"
"HIELD_OR_SPEAR\020\004\022\010\n\004KEY3\020\005\022\010\n\004KEY5\020\006\022\010\n\004"
"KEY6\020\007\022\033\n\027RECOVERY_FROM_DIZZINESS\020\010*f\n\017S"
"tudentBuffType\022\023\n\017NULL_SBUFF_TYPE\020\000\022\016\n\nS"
"BUFFTYPE1\020\001\022\016\n\nSBUFFTYPE2\020\002\022\016\n\nSBUFFTYPE"
"3\020\003\022\016\n\nSBUFFTYPE4\020\004*\251\002\n\013PlayerState\022\017\n\013N"
"ULL_STATUS\020\000\022\010\n\004IDLE\020\001\022\014\n\010LEARNING\020\002\022\014\n\010"
"ADDICTED\020\003\022\010\n\004QUIT\020\004\022\r\n\tGRADUATED\020\005\022\013\n\007T"
"REATED\020\006\022\013\n\007RESCUED\020\007\022\013\n\007STUNNED\020\010\022\014\n\010TR"
"EATING\020\t\022\014\n\010RESCUING\020\n\022\014\n\010SWINGING\020\013\022\r\n\t"
"ATTACKING\020\014\022\013\n\007LOCKING\020\r\022\r\n\tRUMMAGING\020\016\022"
"\014\n\010CLIMBING\020\017\022\023\n\017OPENING_A_CHEST\020\020\022\027\n\023US"
"ING_SPECIAL_SKILL\020\021\022\022\n\016OPENING_A_GATE\020\022*"
"u\n\017TrickerBuffType\022\023\n\017NULL_TBUFF_TYPE\020\000\022"
"\016\n\nTBUFFTYPE1\020\001\022\016\n\nTBUFFTYPE2\020\002\022\016\n\nTBUFF"
"TYPE3\020\003\022\016\n\nTBUFFTYPE4\020\004\022\r\n\tINVISIBLE\020\005*J"
"\n\nPlayerType\022\024\n\020NULL_PLAYER_TYPE\020\000\022\022\n\016ST"
"UDENT_PLAYER\020\001\022\022\n\016TRICKER_PLAYER\020\002*g\n\013St"
"udentType\022\025\n\021NULL_STUDENT_TYPE\020\000\022\013\n\007ATHL"
"ETE\020\001\022\020\n\014STUDENTTYPE2\020\002\022\020\n\014STUDENTTYPE3\020"
"\003\022\020\n\014STUDENTTYPE4\020\004*h\n\013TrickerType\022\025\n\021NU"
"LL_TRICKER_TYPE\020\000\022\014\n\010ASSASSIN\020\001\022\020\n\014TRICK"
"ERTYPE2\020\002\022\020\n\014TRICKERTYPE3\020\003\022\020\n\014TRICKERTY"
"PE4\020\004*P\n\tGameState\022\023\n\017NULL_GAME_STATE\020\000\022"
"\016\n\nGAME_START\020\001\022\020\n\014GAME_RUNNING\020\002\022\014\n\010GAM"
"E_END\020\003b\006proto3";
"FE\020\001\022\034\n\030COMMON_ATTACK_OF_TRICKER\020\002\022\r\n\tBO"
"MB_BOMB\020\003\022\020\n\014JUMPY_DUMPTY\020\004\022\r\n\tATOM_BOMB"
"\020\005*\241\001\n\tPlaceType\022\023\n\017NULL_PLACE_TYPE\020\000\022\010\n"
"\004LAND\020\001\022\010\n\004WALL\020\002\022\t\n\005GRASS\020\003\022\r\n\tCLASSROO"
"M\020\004\022\010\n\004GATE\020\005\022\017\n\013HIDDEN_GATE\020\006\022\n\n\006WINDOW"
"\020\007\022\t\n\005DOOR3\020\010\022\t\n\005DOOR5\020\t\022\t\n\005DOOR6\020\n\022\t\n\005C"
"HEST\020\013*8\n\tShapeType\022\023\n\017NULL_SHAPE_TYPE\020\000"
"\022\n\n\006CIRCLE\020\001\022\n\n\006SQUARE\020\002*\256\001\n\010PropType\022\022\n"
"\016NULL_PROP_TYPE\020\000\022\r\n\tADD_SPEED\020\001\022\035\n\031ADD_"
"LIFE_OR_CLAIRAUDIENCE\020\002\022\020\n\014ADD_HP_OR_AP\020"
"\003\022\023\n\017SHIELD_OR_SPEAR\020\004\022\010\n\004KEY3\020\005\022\010\n\004KEY5"
"\020\006\022\010\n\004KEY6\020\007\022\033\n\027RECOVERY_FROM_DIZZINESS\020"
"\010*n\n\017StudentBuffType\022\023\n\017NULL_SBUFF_TYPE\020"
"\000\022\025\n\021STUDENT_ADD_SPEED\020\001\022\014\n\010ADD_LIFE\020\002\022\n"
"\n\006SHIELD\020\003\022\025\n\021STUDENT_INVISIBLE\020\004*\251\002\n\013Pl"
"ayerState\022\017\n\013NULL_STATUS\020\000\022\010\n\004IDLE\020\001\022\014\n\010"
"LEARNING\020\002\022\014\n\010ADDICTED\020\003\022\010\n\004QUIT\020\004\022\r\n\tGR"
"ADUATED\020\005\022\013\n\007TREATED\020\006\022\013\n\007RESCUED\020\007\022\013\n\007S"
"TUNNED\020\010\022\014\n\010TREATING\020\t\022\014\n\010RESCUING\020\n\022\014\n\010"
"SWINGING\020\013\022\r\n\tATTACKING\020\014\022\013\n\007LOCKING\020\r\022\r"
"\n\tRUMMAGING\020\016\022\014\n\010CLIMBING\020\017\022\023\n\017OPENING_A"
"_CHEST\020\020\022\027\n\023USING_SPECIAL_SKILL\020\021\022\022\n\016OPE"
"NING_A_GATE\020\022*~\n\017TrickerBuffType\022\023\n\017NULL"
"_TBUFF_TYPE\020\000\022\025\n\021TRICKER_ADD_SPEED\020\001\022\t\n\005"
"SPEAR\020\002\022\n\n\006ADD_AP\020\003\022\021\n\rCLAIRAUDIENCE\020\004\022\025"
"\n\021TRICKER_INVISIBLE\020\005*J\n\nPlayerType\022\024\n\020N"
"ULL_PLAYER_TYPE\020\000\022\022\n\016STUDENT_PLAYER\020\001\022\022\n"
"\016TRICKER_PLAYER\020\002*q\n\013StudentType\022\025\n\021NULL"
"_STUDENT_TYPE\020\000\022\013\n\007ATHLETE\020\001\022\013\n\007TEACHER\020"
"\002\022\026\n\022STRAIGHT_A_STUDENT\020\003\022\t\n\005ROBOT\020\004\022\016\n\n"
"TECH_OTAKU\020\005*b\n\013TrickerType\022\025\n\021NULL_TRIC"
"KER_TYPE\020\000\022\014\n\010ASSASSIN\020\001\022\010\n\004KLEE\020\002\022\022\n\016A_"
"NOISY_PERSON\020\003\022\020\n\014TRICKERTYPE4\020\004*P\n\tGame"
"State\022\023\n\017NULL_GAME_STATE\020\000\022\016\n\nGAME_START"
"\020\001\022\020\n\014GAME_RUNNING\020\002\022\014\n\010GAME_END\020\003b\006prot"
"o3";
static ::_pbi::once_flag descriptor_table_MessageType_2eproto_once;
const ::_pbi::DescriptorTable descriptor_table_MessageType_2eproto = {
false,
false,
1455,
1482,
descriptor_table_protodef_MessageType_2eproto,
"MessageType.proto",
&descriptor_table_MessageType_2eproto_once,
@@ -291,6 +292,7 @@ namespace protobuf
case 2:
case 3:
case 4:
case 5:
return true;
default:
return false;


+ 21
- 20
CAPI/cpp/proto/MessageType.pb.h View File

@@ -55,8 +55,8 @@ namespace protobuf
NULL_BULLET_TYPE = 0,
FLYING_KNIFE = 1,
COMMON_ATTACK_OF_TRICKER = 2,
FAST_BULLET = 3,
ORDINARY_BULLET = 4,
BOMB_BOMB = 3,
JUMPY_DUMPTY = 4,
ATOM_BOMB = 5,
BulletType_INT_MIN_SENTINEL_DO_NOT_USE_ = std::numeric_limits<int32_t>::min(),
BulletType_INT_MAX_SENTINEL_DO_NOT_USE_ = std::numeric_limits<int32_t>::max()
@@ -156,7 +156,7 @@ namespace protobuf
{
NULL_PROP_TYPE = 0,
ADD_SPEED = 1,
ADD_LIFE_OR_AP = 2,
ADD_LIFE_OR_CLAIRAUDIENCE = 2,
ADD_HP_OR_AP = 3,
SHIELD_OR_SPEAR = 4,
KEY3 = 5,
@@ -191,16 +191,16 @@ namespace protobuf
enum StudentBuffType : int
{
NULL_SBUFF_TYPE = 0,
SBUFFTYPE1 = 1,
SBUFFTYPE2 = 2,
SBUFFTYPE3 = 3,
SBUFFTYPE4 = 4,
STUDENT_ADD_SPEED = 1,
ADD_LIFE = 2,
SHIELD = 3,
STUDENT_INVISIBLE = 4,
StudentBuffType_INT_MIN_SENTINEL_DO_NOT_USE_ = std::numeric_limits<int32_t>::min(),
StudentBuffType_INT_MAX_SENTINEL_DO_NOT_USE_ = std::numeric_limits<int32_t>::max()
};
bool StudentBuffType_IsValid(int value);
constexpr StudentBuffType StudentBuffType_MIN = NULL_SBUFF_TYPE;
constexpr StudentBuffType StudentBuffType_MAX = SBUFFTYPE4;
constexpr StudentBuffType StudentBuffType_MAX = STUDENT_INVISIBLE;
constexpr int StudentBuffType_ARRAYSIZE = StudentBuffType_MAX + 1;

const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* StudentBuffType_descriptor();
@@ -269,17 +269,17 @@ namespace protobuf
enum TrickerBuffType : int
{
NULL_TBUFF_TYPE = 0,
TBUFFTYPE1 = 1,
TBUFFTYPE2 = 2,
TBUFFTYPE3 = 3,
TBUFFTYPE4 = 4,
INVISIBLE = 5,
TRICKER_ADD_SPEED = 1,
SPEAR = 2,
ADD_AP = 3,
CLAIRAUDIENCE = 4,
TRICKER_INVISIBLE = 5,
TrickerBuffType_INT_MIN_SENTINEL_DO_NOT_USE_ = std::numeric_limits<int32_t>::min(),
TrickerBuffType_INT_MAX_SENTINEL_DO_NOT_USE_ = std::numeric_limits<int32_t>::max()
};
bool TrickerBuffType_IsValid(int value);
constexpr TrickerBuffType TrickerBuffType_MIN = NULL_TBUFF_TYPE;
constexpr TrickerBuffType TrickerBuffType_MAX = INVISIBLE;
constexpr TrickerBuffType TrickerBuffType_MAX = TRICKER_INVISIBLE;
constexpr int TrickerBuffType_ARRAYSIZE = TrickerBuffType_MAX + 1;

const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* TrickerBuffType_descriptor();
@@ -333,15 +333,16 @@ namespace protobuf
{
NULL_STUDENT_TYPE = 0,
ATHLETE = 1,
STUDENTTYPE2 = 2,
STUDENTTYPE3 = 3,
STUDENTTYPE4 = 4,
TEACHER = 2,
STRAIGHT_A_STUDENT = 3,
ROBOT = 4,
TECH_OTAKU = 5,
StudentType_INT_MIN_SENTINEL_DO_NOT_USE_ = std::numeric_limits<int32_t>::min(),
StudentType_INT_MAX_SENTINEL_DO_NOT_USE_ = std::numeric_limits<int32_t>::max()
};
bool StudentType_IsValid(int value);
constexpr StudentType StudentType_MIN = NULL_STUDENT_TYPE;
constexpr StudentType StudentType_MAX = STUDENTTYPE4;
constexpr StudentType StudentType_MAX = TECH_OTAKU;
constexpr int StudentType_ARRAYSIZE = StudentType_MAX + 1;

const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* StudentType_descriptor();
@@ -365,8 +366,8 @@ namespace protobuf
{
NULL_TRICKER_TYPE = 0,
ASSASSIN = 1,
TRICKERTYPE2 = 2,
TRICKERTYPE3 = 3,
KLEE = 2,
A_NOISY_PERSON = 3,
TRICKERTYPE4 = 4,
TrickerType_INT_MIN_SENTINEL_DO_NOT_USE_ = std::numeric_limits<int32_t>::min(),
TrickerType_INT_MAX_SENTINEL_DO_NOT_USE_ = std::numeric_limits<int32_t>::max()


+ 41
- 10
CAPI/python/PyAPI/AI.py View File

@@ -2,6 +2,8 @@ import PyAPI.structures as THUAI6
from PyAPI.Interface import IStudentAPI, ITrickerAPI, IAI
from typing import Union, Final, cast

import time


class Setting:
# 为假则play()期间确保游戏状态不更新,为真则只保证游戏状态在调用相关方法时不更新
@@ -39,20 +41,49 @@ class AssistFunction:
return grid // numOfGridPerCell


arrive: bool = False


class AI(IAI):
# 选手在这里实现自己的逻辑,要求和上面选择的阵营保持一致
def StudentPlay(self, api: IStudentAPI) -> None:
studentSelfInfo = api.GetSelfInfo()

if studentSelfInfo.playerID == 0:
api.SendMessage(1, "Hello, I'm player 0 using python interface!")
if studentSelfInfo.playerID == 1:
api.SendMessage(0, "Hello, I'm player 1 using python interface!")
api.Attack(float('nan'))
time.sleep(0.5)
api.PrintSelfInfo()
# api.SendMessage(4, "Hello World!")
# api.PrintSelfInfo()
# global arrive
# if not arrive:
# if api.GetSelfInfo().x < 25500:
# api.MoveDown(50)
# return
# if api.GetSelfInfo().y > 10500:
# api.MoveLeft(50)
# return
# arrive = True
# else:
# api.SkipWindow()
# # time.sleep(1)

# api.PrintSelfInfo()

# if api.GetSelfInfo().y < 18500:
# api.MoveRight(50)
# return
# api.StartLearning()

# if api.GetSelfInfo().y > 7000:
# api.MoveLeft(50)
# return
# if api.GetSelfInfo().x > 20500:
# api.MoveUp(50)
# return
# if api.GetSelfInfo().y > 4500:
# api.MoveLeft(50)
# return

api.PrintTricker()

if api.HaveMessage():
message = api.GetMessage()
api.Print(
f"Recieved Message from player {message[0]}: {message[1]}")
return

def TrickerPlay(self, api: ITrickerAPI) -> None:


+ 2
- 2
CAPI/python/PyAPI/DebugAPI.py View File

@@ -18,7 +18,7 @@ class StudentDebugAPI(IStudentAPI, IGameTimer):
self.__logger = logging.getLogger("api " + str(playerID))
self.__logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
"[%(name)s] [%(asctime)s] [%(levelname)s] %(message)s", "%H:%M:%S.%e")
"[%(name)s] [%(asctime)s.%(msecs)03d] [%(levelname)s] %(message)s", '%H:%M:%S')
# 确保文件存在
if not os.path.exists(os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/logs"):
os.makedirs(os.path.dirname(os.path.dirname(
@@ -452,7 +452,7 @@ class TrickerDebugAPI(ITrickerAPI, IGameTimer):
self.__logger = logging.getLogger("api " + str(playerID))
self.__logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
"[%(name)s] [%(asctime)s] [%(levelname)s] %(message)s", "%H:%M:%S.%e")
"[%(name)s] [%(asctime)s.%(msecs)03d] [%(levelname)s] %(message)s", '%H:%M:%S')
# 确保文件存在
if not os.path.exists(os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/logs"):
os.makedirs(os.path.dirname(os.path.dirname(


+ 4
- 1
CAPI/python/PyAPI/logic.py View File

@@ -96,6 +96,9 @@ class Logic(ILogic):

def GetPlaceType(self, x: int, y: int) -> THUAI6.PlaceType:
with self.__mtxState:
if x < 0 or x >= len(self.__currentState.gameMap) or y < 0 or y >= len(self.__currentState.gameMap[0]):
self.__logger.warning("Invalid position")
return THUAI6.PlaceType.NullPlaceType
self.__logger.debug("Called GetPlaceType")
return self.__currentState.gameMap[x][y]

@@ -544,7 +547,7 @@ class Logic(ILogic):
# 建立日志组件
self.__logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
"[%(name)s] [%(asctime)s] [%(levelname)s] %(message)s", "%H:%M:%S.%e")
"[%(name)s] [%(asctime)s.%(msecs)03d] [%(levelname)s] %(message)s", '%H:%M:%S')
# 确保文件存在
if not os.path.exists(os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/logs"):
os.makedirs(os.path.dirname(os.path.dirname(


+ 7
- 6
CAPI/python/PyAPI/main.py View File

@@ -26,12 +26,12 @@ def THUAI6Main(argv: List[str], AIBuilder: Callable) -> None:
help="Server`s Port 8888 in default", dest="sPort", default="8888")
parser.add_argument("-p", type=int, required=True,
help="Player`s ID", dest="pID", choices=[0, 1, 2, 3, 4])
parser.add_argument("-d", type=bool, required=False,
help="Set this flag to save the debug log to ./logs folder", dest="file", default=False, const=True, nargs='?')
parser.add_argument("-o", type=bool, required=False,
help="Set this flag to print the debug log to the screen", dest="screen", default=False, const=True, nargs='?')
parser.add_argument("-w", type=bool, required=False,
help="Set this flag to only print warning on the screen", dest="warnOnly", default=False, const=True, nargs='?')
parser.add_argument("-d", action='store_true',
help="Set this flag to save the debug log to ./logs folder", dest="file")
parser.add_argument("-o", action='store_true',
help="Set this flag to print the debug log to the screen", dest="screen")
parser.add_argument("-w", action='store_true',
help="Set this flag to only print warning on the screen", dest="warnOnly")
args = parser.parse_args()
pID = args.pID
sIP = args.sIP
@@ -39,6 +39,7 @@ def THUAI6Main(argv: List[str], AIBuilder: Callable) -> None:
file = args.file
screen = args.screen
warnOnly = args.warnOnly
print(warnOnly)
logic = Logic(pID)
logic.Main(AIBuilder, sIP, sPort, file, screen, warnOnly)



+ 3
- 1
CAPI/python/PyAPI/structures.py View File

@@ -42,7 +42,7 @@ class PropType(Enum):
Key5 = 2
Key6 = 3
AddSpeed = 4
AddLifeOrAp = 5
AddLifeOrClairaudience = 5
AddHpOrAp = 6
ShieldOrSpear = 7
RecoveryFromDizziness = 8
@@ -62,6 +62,8 @@ class StudentType(Enum):
Athlete = 1
Teacher = 2
StraightAStudent = 3
Robot = 4
TechOtaku = 5


class TrickerType(Enum):


+ 25
- 21
CAPI/python/PyAPI/utils.py View File

@@ -90,7 +90,7 @@ class Proto2THUAI6(NoInstance):
MessageType.KEY6: THUAI6.PropType.Key6,
MessageType.ADD_SPEED: THUAI6.PropType.AddSpeed,
MessageType.ADD_HP_OR_AP: THUAI6.PropType.AddHpOrAp,
MessageType.ADD_LIFE_OR_AP: THUAI6.PropType.AddLifeOrAp,
MessageType.ADD_LIFE_OR_CLAIRAUDIENCE: THUAI6.PropType.AddLifeOrClairaudience,
MessageType.SHIELD_OR_SPEAR: THUAI6.PropType.ShieldOrSpear,
MessageType.RECOVERY_FROM_DIZZINESS: THUAI6.PropType.RecoveryFromDizziness, }

@@ -102,29 +102,31 @@ class Proto2THUAI6(NoInstance):
studentTypeDict: Final[dict] = {
MessageType.NULL_STUDENT_TYPE: THUAI6.StudentType.NullStudentType,
MessageType.ATHLETE: THUAI6.StudentType.Athlete,
MessageType.STUDENTTYPE2: THUAI6.StudentType.Teacher,
MessageType.STUDENTTYPE3: THUAI6.StudentType.StraightAStudent, }
MessageType.TEACHER: THUAI6.StudentType.Teacher,
MessageType.STRAIGHT_A_STUDENT: THUAI6.StudentType.StraightAStudent,
MessageType.ROBOT: THUAI6.StudentType.Robot,
MessageType.TECH_OTAKU: THUAI6.StudentType.TechOtaku, }

trickerTypeDict: Final[dict] = {
MessageType.NULL_TRICKER_TYPE: THUAI6.TrickerType.NullTrickerType,
MessageType.ASSASSIN: THUAI6.TrickerType.Assassin,
MessageType.TRICKERTYPE2: THUAI6.TrickerType.Klee,
MessageType.TRICKERTYPE3: THUAI6.TrickerType.ANoisyPerson, }
MessageType.KLEE: THUAI6.TrickerType.Klee,
MessageType.A_NOISY_PERSON: THUAI6.TrickerType.ANoisyPerson, }

studentBuffTypeDict: Final[dict] = {
MessageType.NULL_SBUFF_TYPE: THUAI6.StudentBuffType.NullStudentBuffType,
MessageType.SBUFFTYPE1: THUAI6.StudentBuffType.AddSpeed,
MessageType.SBUFFTYPE2: THUAI6.StudentBuffType.AddLife,
MessageType.SBUFFTYPE3: THUAI6.StudentBuffType.Shield,
MessageType.SBUFFTYPE4: THUAI6.StudentBuffType.Invisible, }
MessageType.STUDENT_ADD_SPEED: THUAI6.StudentBuffType.AddSpeed,
MessageType.ADD_LIFE: THUAI6.StudentBuffType.AddLife,
MessageType.SHIELD: THUAI6.StudentBuffType.Shield,
MessageType.STUDENT_INVISIBLE: THUAI6.StudentBuffType.Invisible, }

trickerBuffTypeDict: Final[dict] = {
MessageType.NULL_TBUFF_TYPE: THUAI6.TrickerBuffType.NullTrickerBuffType,
MessageType.TBUFFTYPE1: THUAI6.TrickerBuffType.AddSpeed,
MessageType.TBUFFTYPE2: THUAI6.TrickerBuffType.Spear,
MessageType.TBUFFTYPE3: THUAI6.TrickerBuffType.AddAp,
MessageType.TBUFFTYPE4: THUAI6.TrickerBuffType.Clairaudience,
MessageType.INVISIBLE: THUAI6.TrickerBuffType.Invisible, }
MessageType.TRICKER_ADD_SPEED: THUAI6.TrickerBuffType.AddSpeed,
MessageType.SPEAR: THUAI6.TrickerBuffType.Spear,
MessageType.ADD_AP: THUAI6.TrickerBuffType.AddAp,
MessageType.CLAIRAUDIENCE: THUAI6.TrickerBuffType.Clairaudience,
MessageType.TRICKER_INVISIBLE: THUAI6.TrickerBuffType.Invisible, }

playerStateDict: Final[dict] = {
MessageType.NULL_STATUS: THUAI6.PlayerState.NullState,
@@ -156,9 +158,9 @@ class Proto2THUAI6(NoInstance):
bulletTypeDict: Final[dict] = {
MessageType.NULL_BULLET_TYPE: THUAI6.BulletType.NullBulletType,
MessageType.FLYING_KNIFE: THUAI6.BulletType.FlyingKnife,
MessageType.FAST_BULLET: THUAI6.BulletType.BombBomb,
MessageType.BOMB_BOMB: THUAI6.BulletType.BombBomb,
MessageType.COMMON_ATTACK_OF_TRICKER: THUAI6.BulletType.CommonAttackOfTricker,
MessageType.ORDINARY_BULLET: THUAI6.BulletType.JumpyDumpty,
MessageType.JUMPY_DUMPTY: THUAI6.BulletType.JumpyDumpty,
MessageType.ATOM_BOMB: THUAI6.BulletType.AtomBomb, }

# 用于将Proto的对象转为THUAI6的对象
@@ -303,14 +305,16 @@ class THUAI62Proto(NoInstance):
studentTypeDict: Final[dict] = {
THUAI6.StudentType.NullStudentType: MessageType.NULL_STUDENT_TYPE,
THUAI6.StudentType.Athlete: MessageType.ATHLETE,
THUAI6.StudentType.Teacher: MessageType.STUDENTTYPE2,
THUAI6.StudentType.StraightAStudent: MessageType.STUDENTTYPE3, }
THUAI6.StudentType.Teacher: MessageType.TEACHER,
THUAI6.StudentType.StraightAStudent: MessageType.STRAIGHT_A_STUDENT,
THUAI6.StudentType.Robot: MessageType.ROBOT,
THUAI6.StudentType.TechOtaku: MessageType.TECH_OTAKU, }

trickerTypeDict: Final[dict] = {
THUAI6.TrickerType.NullTrickerType: MessageType.NULL_TRICKER_TYPE,
THUAI6.TrickerType.Assassin: MessageType.ASSASSIN,
THUAI6.TrickerType.Klee: MessageType.TRICKERTYPE2,
THUAI6.TrickerType.ANoisyPerson: MessageType.TRICKERTYPE3, }
THUAI6.TrickerType.Klee: MessageType.KLEE,
THUAI6.TrickerType.ANoisyPerson: MessageType.A_NOISY_PERSON, }

propTypeDict: Final[dict] = {
THUAI6.PropType.NullPropType: MessageType.NULL_PROP_TYPE,
@@ -318,7 +322,7 @@ class THUAI62Proto(NoInstance):
THUAI6.PropType.Key5: MessageType.KEY5,
THUAI6.PropType.Key6: MessageType.KEY6,
THUAI6.PropType.AddHpOrAp: MessageType.ADD_HP_OR_AP,
THUAI6.PropType.AddLifeOrAp: MessageType.ADD_LIFE_OR_AP,
THUAI6.PropType.AddLifeOrClairaudience: MessageType.ADD_LIFE_OR_CLAIRAUDIENCE,
THUAI6.PropType.AddSpeed: MessageType.ADD_SPEED,
THUAI6.PropType.ShieldOrSpear: MessageType.SHIELD_OR_SPEAR, }



+ 12
- 0
CAPI/python/generate_proto.sh View File

@@ -0,0 +1,12 @@
#!/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



+ 2
- 2
CAPI/python/requirements.txt View File

@@ -1,2 +1,2 @@
grpcio==1.53.0
grpcio-tools==1.53.0
grpcio==1.52.0
grpcio-tools==1.52.0

+ 3
- 1
CAPI/python/run.sh View File

@@ -1,2 +1,4 @@
#!/usr/bin/env bash

python PyAPI/main.py -I 172.22.32.1 -P 8888 -p 0 -d -o &
python PyAPI/main.py -I 172.22.32.1 -P 8888 -p 1 -d -o &
# python PyAPI/main.py -I 172.22.32.1 -P 8888 -p 1 -d -o &

Loading…
Cancel
Save