Browse Source

feat(CAPI): 🎨 change team selection logic

tags/0.1.0
DragonAura 2 years ago
parent
commit
f694212734
17 changed files with 3253 additions and 3283 deletions
  1. +7
    -3
      CAPI/cpp/API/include/AI.h
  2. +26
    -6
      CAPI/cpp/API/src/AI.cpp
  3. +1
    -1
      CAPI/cpp/API/src/logic.cpp
  4. +10
    -5
      CAPI/cpp/API/src/main.cpp
  5. +1533
    -1721
      CAPI/cpp/proto/Message2Clients.pb.cc
  6. +912
    -805
      CAPI/cpp/proto/Message2Clients.pb.h
  7. +467
    -503
      CAPI/cpp/proto/Message2Server.pb.cc
  8. +223
    -182
      CAPI/cpp/proto/Message2Server.pb.h
  9. +10
    -6
      CAPI/cpp/proto/MessageType.pb.cc
  10. +2
    -8
      CAPI/cpp/proto/MessageType.pb.h
  11. +13
    -9
      CAPI/cpp/proto/Services.pb.cc
  12. +2
    -8
      CAPI/cpp/proto/Services.pb.h
  13. +19
    -11
      CAPI/python/PyAPI/AI.py
  14. +6
    -2
      CAPI/python/PyAPI/Communication.py
  15. +9
    -7
      CAPI/python/PyAPI/logic.py
  16. +9
    -3
      CAPI/python/PyAPI/main.py
  17. +4
    -3
      CAPI/python/run.sh

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

@@ -13,17 +13,21 @@ public:
virtual void play(ITrickerAPI& api) = 0;
};

using CreateAIFunc = std::unique_ptr<IAI> (*)();
using CreateAIFunc = std::unique_ptr<IAI> (*)(int64_t playerID);

class AI : public IAI
{
public:
AI() :
IAI()
AI(int64_t pID) :
IAI(),
playerID(pID)
{
}
void play(IStudentAPI& api) override;
void play(ITrickerAPI& api) override;

private:
int64_t playerID;
};

#endif

+ 26
- 6
CAPI/cpp/API/src/AI.cpp View File

@@ -1,24 +1,44 @@
#include <vector>
#include <thread>
#include <array>
#include "AI.h"
#include "constants.h"

// 为假则play()期间确保游戏状态不更新,为真则只保证游戏状态在调用相关方法时不更新
extern const bool asynchronous = false;

// 选手必须定义该变量来选择自己的阵营
extern const THUAI6::PlayerType playerType = THUAI6::PlayerType::TrickerPlayer;
// 选手需要依次将player0到player4的职业在这里定义

// 选手需要将两个都定义,本份代码中不选择的阵营任意定义即可
extern const THUAI6::TrickerType trickerType = THUAI6::TrickerType::Assassin;
extern const std::array<THUAI6::StudentType, 4> studentType = {
THUAI6::StudentType::Athlete,
THUAI6::StudentType::Teacher,
THUAI6::StudentType::StraightAStudent,
THUAI6::StudentType::Sunshine};

extern const THUAI6::StudentType studentType = THUAI6::StudentType::Athlete;
extern const THUAI6::TrickerType trickerType = THUAI6::TrickerType::Assassin;

// 选手只需写一个即可,为了调试方便写了两个的话也不会有影响

void AI::play(IStudentAPI& api)
{
auto self = api.GetSelfInfo();
// 公共操作
if (this->playerID == 0)
{
// 玩家0执行操作
}
else if (this->playerID == 1)
{
// 玩家1执行操作
}
else if (this->playerID == 2)
{
// 玩家2执行操作
}
else if (this->playerID == 3)
{
// 玩家3执行操作
}
// 公共操作
}

void AI::play(ITrickerAPI& api)


+ 1
- 1
CAPI/cpp/API/src/logic.cpp View File

@@ -863,7 +863,7 @@ void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port, bool f
cvAI.wait(lock, [this]()
{ return AIStart; });
}
auto ai = createAI();
auto ai = createAI(playerID);

while (AILoop)
{


+ 10
- 5
CAPI/cpp/API/src/main.cpp View File

@@ -2,6 +2,7 @@
#include "logic.h"
#include "structures.h"
#include <tclap/CmdLine.h>
#include <array>

#ifdef _MSC_VER
#pragma warning(disable : 4996)
@@ -15,9 +16,8 @@ int THUAI6Main(int argc, char** argv, CreateAIFunc AIBuilder)
bool file = false;
bool print = false;
bool warnOnly = false;
extern const THUAI6::PlayerType playerType;
extern const THUAI6::TrickerType trickerType;
extern const THUAI6::StudentType studentType;
extern const std::array<THUAI6::StudentType, 4> studentType;
// {
// file = true;
// print = true;
@@ -71,7 +71,12 @@ int THUAI6Main(int argc, char** argv, CreateAIFunc AIBuilder)
}
try
{
Logic logic(playerType, pID, trickerType, studentType);
THUAI6::PlayerType playerType;
if (pID == 4)
playerType = THUAI6::PlayerType::TrickerPlayer;
else
playerType = THUAI6::PlayerType::StudentPlayer;
Logic logic(playerType, pID, trickerType, studentType[pID]);
logic.Main(AIBuilder, sIP, sPort, file, print, warnOnly);
}
catch (const std::exception& e)
@@ -81,9 +86,9 @@ int THUAI6Main(int argc, char** argv, CreateAIFunc AIBuilder)
return 0;
}

std::unique_ptr<IAI> CreateAI()
std::unique_ptr<IAI> CreateAI(int64_t pID)
{
return std::make_unique<AI>();
return std::make_unique<AI>(pID);
}

int main(int argc, char* argv[])


+ 1533
- 1721
CAPI/cpp/proto/Message2Clients.pb.cc
File diff suppressed because it is too large
View File


+ 912
- 805
CAPI/cpp/proto/Message2Clients.pb.h
File diff suppressed because it is too large
View File


+ 467
- 503
CAPI/cpp/proto/Message2Server.pb.cc
File diff suppressed because it is too large
View File


+ 223
- 182
CAPI/cpp/proto/Message2Server.pb.h
File diff suppressed because it is too large
View File


+ 10
- 6
CAPI/cpp/proto/MessageType.pb.cc View File

@@ -16,14 +16,18 @@
#include <google/protobuf/port_def.inc>

PROTOBUF_PRAGMA_INIT_SEG

namespace _pb = ::PROTOBUF_NAMESPACE_ID;
namespace _pbi = _pb::internal;

namespace protobuf
{
} // namespace protobuf
static const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* file_level_enum_descriptors_MessageType_2eproto[11];
static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_MessageType_2eproto = nullptr;
static const ::_pb::EnumDescriptor* file_level_enum_descriptors_MessageType_2eproto[11];
static constexpr ::_pb::ServiceDescriptor const** file_level_service_descriptors_MessageType_2eproto = nullptr;
const uint32_t TableStruct_MessageType_2eproto::offsets[1] = {};
static constexpr ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema* schemas = nullptr;
static constexpr ::PROTOBUF_NAMESPACE_ID::Message* const* file_default_instances = nullptr;
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*\202\001\n\nBullet"
@@ -82,13 +86,13 @@ const ::_pbi::DescriptorTable descriptor_table_MessageType_2eproto = {
file_level_enum_descriptors_MessageType_2eproto,
file_level_service_descriptors_MessageType_2eproto,
};
PROTOBUF_ATTRIBUTE_WEAK const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable* descriptor_table_MessageType_2eproto_getter()
PROTOBUF_ATTRIBUTE_WEAK const ::_pbi::DescriptorTable* descriptor_table_MessageType_2eproto_getter()
{
return &descriptor_table_MessageType_2eproto;
}

// Force running AddDescriptors() at dynamic initialization time.
PROTOBUF_ATTRIBUTE_INIT_PRIORITY static ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptorsRunner dynamic_init_dummy_MessageType_2eproto(&descriptor_table_MessageType_2eproto);
PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 static ::_pbi::AddDescriptorsRunner dynamic_init_dummy_MessageType_2eproto(&descriptor_table_MessageType_2eproto);
namespace protobuf
{
const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* BulletType_descriptor()


+ 2
- 8
CAPI/cpp/proto/MessageType.pb.h View File

@@ -8,12 +8,12 @@
#include <string>

#include <google/protobuf/port_def.inc>
#if PROTOBUF_VERSION < 3019000
#if PROTOBUF_VERSION < 3021000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.
#endif
#if 3019004 < PROTOBUF_MIN_PROTOC_VERSION
#if 3021005 < PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
@@ -23,7 +23,6 @@
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/arena.h>
#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_table_driven.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/metadata_lite.h>
#include <google/protobuf/generated_message_reflection.h>
@@ -43,11 +42,6 @@ PROTOBUF_NAMESPACE_CLOSE
// Internal implementation detail -- do not use these members.
struct TableStruct_MessageType_2eproto
{
static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] PROTOBUF_SECTION_VARIABLE(protodesc_cold);
static const ::PROTOBUF_NAMESPACE_ID::internal::AuxiliaryParseTableField aux[] PROTOBUF_SECTION_VARIABLE(protodesc_cold);
static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] PROTOBUF_SECTION_VARIABLE(protodesc_cold);
static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[];
static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[];
static const uint32_t offsets[];
};
extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_MessageType_2eproto;


+ 13
- 9
CAPI/cpp/proto/Services.pb.cc View File

@@ -16,14 +16,18 @@
#include <google/protobuf/port_def.inc>

PROTOBUF_PRAGMA_INIT_SEG

namespace _pb = ::PROTOBUF_NAMESPACE_ID;
namespace _pbi = _pb::internal;

namespace protobuf
{
} // namespace protobuf
static constexpr ::PROTOBUF_NAMESPACE_ID::EnumDescriptor const** file_level_enum_descriptors_Services_2eproto = nullptr;
static constexpr ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor const** file_level_service_descriptors_Services_2eproto = nullptr;
static constexpr ::_pb::EnumDescriptor const** file_level_enum_descriptors_Services_2eproto = nullptr;
static constexpr ::_pb::ServiceDescriptor const** file_level_service_descriptors_Services_2eproto = nullptr;
const uint32_t TableStruct_Services_2eproto::offsets[1] = {};
static constexpr ::PROTOBUF_NAMESPACE_ID::internal::MigrationSchema* schemas = nullptr;
static constexpr ::PROTOBUF_NAMESPACE_ID::Message* const* file_default_instances = nullptr;
static constexpr ::_pbi::MigrationSchema* schemas = nullptr;
static constexpr ::_pb::Message* const* file_default_instances = nullptr;

const char descriptor_table_protodef_Services_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) =
"\n\016Services.proto\022\010protobuf\032\025Message2Clie"
@@ -54,12 +58,12 @@ const char descriptor_table_protodef_Services_2eproto[] PROTOBUF_SECTION_VARIABL
"tOpenChest\022\017.protobuf.IDMsg\032\021.protobuf.B"
"oolRes\0222\n\014EndAllAction\022\017.protobuf.IDMsg\032"
"\021.protobuf.BoolResb\006proto3";
static const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable* const descriptor_table_Services_2eproto_deps[2] = {
static const ::_pbi::DescriptorTable* const descriptor_table_Services_2eproto_deps[2] = {
&::descriptor_table_Message2Clients_2eproto,
&::descriptor_table_Message2Server_2eproto,
};
static ::PROTOBUF_NAMESPACE_ID::internal::once_flag descriptor_table_Services_2eproto_once;
const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_Services_2eproto = {
static ::_pbi::once_flag descriptor_table_Services_2eproto_once;
const ::_pbi::DescriptorTable descriptor_table_Services_2eproto = {
false,
false,
1106,
@@ -76,13 +80,13 @@ const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_Servic
file_level_enum_descriptors_Services_2eproto,
file_level_service_descriptors_Services_2eproto,
};
PROTOBUF_ATTRIBUTE_WEAK const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable* descriptor_table_Services_2eproto_getter()
PROTOBUF_ATTRIBUTE_WEAK const ::_pbi::DescriptorTable* descriptor_table_Services_2eproto_getter()
{
return &descriptor_table_Services_2eproto;
}

// Force running AddDescriptors() at dynamic initialization time.
PROTOBUF_ATTRIBUTE_INIT_PRIORITY static ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptorsRunner dynamic_init_dummy_Services_2eproto(&descriptor_table_Services_2eproto);
PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 static ::_pbi::AddDescriptorsRunner dynamic_init_dummy_Services_2eproto(&descriptor_table_Services_2eproto);
namespace protobuf
{



+ 2
- 8
CAPI/cpp/proto/Services.pb.h View File

@@ -8,12 +8,12 @@
#include <string>

#include <google/protobuf/port_def.inc>
#if PROTOBUF_VERSION < 3019000
#if PROTOBUF_VERSION < 3021000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.
#endif
#if 3019004 < PROTOBUF_MIN_PROTOC_VERSION
#if 3021005 < PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
@@ -23,7 +23,6 @@
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/arena.h>
#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_table_driven.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/metadata_lite.h>
#include <google/protobuf/generated_message_reflection.h>
@@ -44,11 +43,6 @@ PROTOBUF_NAMESPACE_CLOSE
// Internal implementation detail -- do not use these members.
struct TableStruct_Services_2eproto
{
static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTableField entries[] PROTOBUF_SECTION_VARIABLE(protodesc_cold);
static const ::PROTOBUF_NAMESPACE_ID::internal::AuxiliaryParseTableField aux[] PROTOBUF_SECTION_VARIABLE(protodesc_cold);
static const ::PROTOBUF_NAMESPACE_ID::internal::ParseTable schema[1] PROTOBUF_SECTION_VARIABLE(protodesc_cold);
static const ::PROTOBUF_NAMESPACE_ID::internal::FieldMetadata field_metadata[];
static const ::PROTOBUF_NAMESPACE_ID::internal::SerializationTable serialization_table[];
static const uint32_t offsets[];
};
extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_Services_2eproto;


+ 19
- 11
CAPI/python/PyAPI/AI.py View File

@@ -1,6 +1,6 @@
import PyAPI.structures as THUAI6
from PyAPI.Interface import IStudentAPI, ITrickerAPI, IAI
from typing import Union, Final, cast
from typing import Union, Final, cast, List
from PyAPI.constants import Constants
import queue

@@ -13,15 +13,10 @@ class Setting:
def asynchronous() -> bool:
return True

# 选手必须修改该函数的返回值来选择自己的阵营
# 选手需要依次将player0到player4的职业都定义
@staticmethod
def playerType() -> THUAI6.PlayerType:
return THUAI6.PlayerType.StudentPlayer

# 选手需要将两个都定义,本份代码中不选择的阵营任意定义即可
@staticmethod
def studentType() -> THUAI6.StudentType:
return THUAI6.StudentType.Athlete
def studentType() -> List[THUAI6.StudentType]:
return [THUAI6.StudentType.Athlete, THUAI6.StudentType.Teacher, THUAI6.StudentType.StraightAStudent, THUAI6.StudentType.Sunshine]

@staticmethod
def trickerType() -> THUAI6.TrickerType:
@@ -44,10 +39,23 @@ class AssistFunction:


class AI(IAI):
def __init__(self, pID: int):
self.__playerID = pID
# 选手在这里实现自己的逻辑,要求和上面选择的阵营保持一致

def StudentPlay(self, api: IStudentAPI) -> None:
selfInfo = api.GetSelfInfo()
api.PrintSelfInfo()
if self.__playerID == 0:
# 玩家0执行操作
return
elif self.__playerID == 1:
# 玩家1执行操作
return
elif self.__playerID == 2:
# 玩家2执行操作
return
elif self.__playerID == 3:
# 玩家3执行操作
return
return

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


+ 6
- 2
CAPI/python/PyAPI/Communication.py View File

@@ -198,11 +198,15 @@ class Communication:
self.__haveNewMessage = False
return self.__message2Client

def AddPlayer(self, playerID: int) -> None:
def AddPlayer(self, playerID: int, playerType: THUAI6.PlayerType) -> None:
def tMessage():
try:
if playerType == THUAI6.PlayerType.StudentPlayer:
studentType = Setting.studentType()[playerID]
else:
studentType = THUAI6.StudentType.NullStudentType
playerMsg = THUAI62Proto.THUAI62ProtobufPlayer(
playerID, Setting.playerType(), Setting.studentType(), Setting.trickerType())
playerID, playerType, studentType, Setting.trickerType())
for msg in self.__THUAI6Stub.AddPlayer(playerMsg):
with self.__cvMessage:
self.__haveNewMessage = True


+ 9
- 7
CAPI/python/PyAPI/logic.py View File

@@ -18,12 +18,14 @@ from PyAPI.Interface import ILogic, IGameTimer


class Logic(ILogic):
def __init__(self, playerID: int) -> None:
def __init__(self, playerID: int, playerType: THUAI6.PlayerType) -> None:

# ID
self.__playerID: int = playerID
self.__playerGUIDs: List[int] = []

self.__playerType: THUAI6.PlayerType = playerType

# 通信
self.__comm: Communication

@@ -268,7 +270,7 @@ class Logic(ILogic):
def __ProcessMessage(self) -> None:
def messageThread():
self.__logger.info("Message thread start!")
self.__comm.AddPlayer(self.__playerID)
self.__comm.AddPlayer(self.__playerID, self.__playerType)
self.__logger.info("Join the player!")

while self.__gameState != THUAI6.GameState.GameEnd:
@@ -344,7 +346,7 @@ class Logic(ILogic):
self.__logger.debug("Buffer cleared!")
self.__bufferState.gameInfo = Proto2THUAI6.Protobuf2THUAI6GameInfo(
message.all_message)
if Setting.playerType() == THUAI6.PlayerType.StudentPlayer:
if self.__playerType == THUAI6.PlayerType.StudentPlayer:
for item in message.obj_message:
if item.WhichOneof("message_of_obj") == "student_message":
if item.student_message.player_id == self.__playerID:
@@ -590,20 +592,20 @@ class Logic(ILogic):
self.__logger.info("asynchronous: %s", Setting.asynchronous())
self.__logger.info("server: %s:%s", IP, port)
self.__logger.info("playerID: %s", self.__playerID)
self.__logger.info("player type: %s", Setting.playerType().name)
self.__logger.info("player type: %s", self.__playerType.name)
self.__logger.info("****************************")

# 建立通信组件
self.__comm = Communication(IP, port)

# 构造timer
if Setting.playerType() == THUAI6.PlayerType.StudentPlayer:
if self.__playerType == THUAI6.PlayerType.StudentPlayer:
if not file and not screen:
self.__timer = StudentAPI(self)
else:
self.__timer = StudentDebugAPI(
self, file, screen, warnOnly, self.__playerID)
elif Setting.playerType() == THUAI6.PlayerType.TrickerPlayer:
elif self.__playerType == THUAI6.PlayerType.TrickerPlayer:
if not file and not screen:
self.__timer = TrickerAPI(self)
else:
@@ -615,7 +617,7 @@ class Logic(ILogic):
with self.__cvAI:
self.__cvAI.wait_for(lambda: self.__AIStart)

ai = createAI()
ai = createAI(self.__playerID)
while self.__AILoop:
if Setting.asynchronous():
self.__Wait()


+ 9
- 3
CAPI/python/PyAPI/main.py View File

@@ -9,6 +9,7 @@ from PyAPI.AI import AI
from PyAPI.logic import Logic
from typing import List, Callable
import argparse
import PyAPI.structures as THUAI6


def THUAI6Main(argv: List[str], AIBuilder: Callable) -> None:
@@ -39,12 +40,17 @@ def THUAI6Main(argv: List[str], AIBuilder: Callable) -> None:
file = args.file
screen = args.screen
warnOnly = args.warnOnly
logic = Logic(pID)
playerType = THUAI6.PlayerType.NullPlayerType
if pID == 4:
playerType = THUAI6.PlayerType.TrickerPlayer
else:
playerType = THUAI6.PlayerType.StudentPlayer
logic = Logic(pID, playerType)
logic.Main(AIBuilder, sIP, sPort, file, screen, warnOnly)


def CreateAI() -> IAI:
return AI()
def CreateAI(pID: int) -> IAI:
return AI(pID)


if __name__ == '__main__':


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

@@ -1,6 +1,7 @@
#!/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 &
# python PyAPI/main.py -I 172.22.32.1 -P 8888 -p 2 -d &
# python PyAPI/main.py -I 172.22.32.1 -P 8888 -p 3 -d &
python PyAPI/main.py -I 172.22.32.1 -P 8888 -p 1 -d &
python PyAPI/main.py -I 172.22.32.1 -P 8888 -p 2 -d &
python PyAPI/main.py -I 172.22.32.1 -P 8888 -p 3 -d &
python PyAPI/main.py -I 172.22.32.1 -P 8888 -p 4 -d &

Loading…
Cancel
Save