From 33509e2169e53bb1ba4f2a0af0103a5d356da43c Mon Sep 17 00:00:00 2001 From: DragonAura Date: Sat, 10 Dec 2022 14:02:19 +0800 Subject: [PATCH 1/4] feat(proto): :sparkles: add game state enum --- CAPI/proto/Message2Clients.pb.cc | 69 ++++++++++++++++++++------ CAPI/proto/Message2Clients.pb.h | 36 ++++++++++++++ CAPI/proto/MessageType.pb.cc | 27 ++++++++-- CAPI/proto/MessageType.pb.h | 40 +++++++++++++++ dependency/proto/Message2Clients.proto | 1 + dependency/proto/MessageType.proto | 37 +++----------- 6 files changed, 163 insertions(+), 47 deletions(-) diff --git a/CAPI/proto/Message2Clients.pb.cc b/CAPI/proto/Message2Clients.pb.cc index 51b174f..4932e35 100644 --- a/CAPI/proto/Message2Clients.pb.cc +++ b/CAPI/proto/Message2Clients.pb.cc @@ -158,7 +158,7 @@ namespace protobuf ::_pbi::ConstantInitialized ) : _impl_{ - /*decltype(_impl_.human_message_)*/ {}, /*decltype(_impl_.butcher_message_)*/ {}, /*decltype(_impl_.prop_message_)*/ {}, /*decltype(_impl_.map_message_)*/ nullptr, /*decltype(_impl_._cached_size_)*/ {}} + /*decltype(_impl_.human_message_)*/ {}, /*decltype(_impl_.butcher_message_)*/ {}, /*decltype(_impl_.prop_message_)*/ {}, /*decltype(_impl_.map_message_)*/ nullptr, /*decltype(_impl_.game_state_)*/ 0, /*decltype(_impl_._cached_size_)*/ {}} { } struct MessageToClientDefaultTypeInternal @@ -340,6 +340,7 @@ const uint32_t TableStruct_Message2Clients_2eproto::offsets[] PROTOBUF_SECTION_V PROTOBUF_FIELD_OFFSET(::protobuf::MessageToClient, _impl_.butcher_message_), PROTOBUF_FIELD_OFFSET(::protobuf::MessageToClient, _impl_.prop_message_), PROTOBUF_FIELD_OFFSET(::protobuf::MessageToClient, _impl_.map_message_), + PROTOBUF_FIELD_OFFSET(::protobuf::MessageToClient, _impl_.game_state_), ~0u, // no _has_bits_ PROTOBUF_FIELD_OFFSET(::protobuf::MoveRes, _internal_metadata_), ~0u, // no _extensions_ @@ -374,9 +375,9 @@ static const ::_pbi::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protode {68, -1, -1, sizeof(::protobuf::MessageOfMap_Row)}, {75, -1, -1, sizeof(::protobuf::MessageOfMap)}, {82, -1, -1, sizeof(::protobuf::MessageToClient)}, - {92, -1, -1, sizeof(::protobuf::MoveRes)}, - {101, -1, -1, sizeof(::protobuf::BoolRes)}, - {108, -1, -1, sizeof(::protobuf::MsgRes)}, + {93, -1, -1, sizeof(::protobuf::MoveRes)}, + {102, -1, -1, sizeof(::protobuf::BoolRes)}, + {109, -1, -1, sizeof(::protobuf::MsgRes)}, }; static const ::_pb::Message* const file_default_instances[] = { @@ -423,18 +424,19 @@ const char descriptor_table_protodef_Message2Clients_2eproto[] PROTOBUF_SECTION_ "\t\n\001y\030\003 \001(\005\022\030\n\020facing_direction\030\004 \001(\001\022\022\n\n" "mapping_id\030\005 \001(\003\"`\n\014MessageOfMap\022\'\n\003row\030" "\002 \003(\0132\032.protobuf.MessageOfMap.Row\032\'\n\003Row" - "\022 \n\003col\030\001 \003(\0162\023.protobuf.PlaceType\"\323\001\n\017M" + "\022 \n\003col\030\001 \003(\0162\023.protobuf.PlaceType\"\374\001\n\017M" "essageToClient\022/\n\rhuman_message\030\001 \003(\0132\030." "protobuf.MessageOfHuman\0223\n\017butcher_messa" "ge\030\002 \003(\0132\032.protobuf.MessageOfButcher\022-\n\014" "prop_message\030\003 \003(\0132\027.protobuf.MessageOfP" "rop\022+\n\013map_message\030\004 \001(\0132\026.protobuf.Mess" - "ageOfMap\"J\n\007MoveRes\022\024\n\014actual_speed\030\001 \001(" - "\003\022\024\n\014actual_angle\030\002 \001(\001\022\023\n\013act_success\030\003" - " \001(\010\"\036\n\007BoolRes\022\023\n\013act_success\030\001 \001(\010\"P\n\006" - "MsgRes\022\024\n\014have_message\030\001 \001(\010\022\026\n\016from_pla" - "yer_id\030\002 \001(\003\022\030\n\020message_received\030\003 \001(\tb\006" - "proto3"; + "ageOfMap\022\'\n\ngame_state\030\005 \001(\0162\023.protobuf." + "GameState\"J\n\007MoveRes\022\024\n\014actual_speed\030\001 \001" + "(\003\022\024\n\014actual_angle\030\002 \001(\001\022\023\n\013act_success\030" + "\003 \001(\010\"\036\n\007BoolRes\022\023\n\013act_success\030\001 \001(\010\"P\n" + "\006MsgRes\022\024\n\014have_message\030\001 \001(\010\022\026\n\016from_pl" + "ayer_id\030\002 \001(\003\022\030\n\020message_received\030\003 \001(\tb" + "\006proto3"; static const ::_pbi::DescriptorTable* const descriptor_table_Message2Clients_2eproto_deps[1] = { &::descriptor_table_MessageType_2eproto, }; @@ -442,7 +444,7 @@ static ::_pbi::once_flag descriptor_table_Message2Clients_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_Message2Clients_2eproto = { false, false, - 1646, + 1687, descriptor_table_protodef_Message2Clients_2eproto, "Message2Clients.proto", &descriptor_table_Message2Clients_2eproto_once, @@ -3044,13 +3046,14 @@ namespace protobuf MessageToClient* const _this = this; (void)_this; new (&_impl_) Impl_{ - decltype(_impl_.human_message_){from._impl_.human_message_}, decltype(_impl_.butcher_message_){from._impl_.butcher_message_}, decltype(_impl_.prop_message_){from._impl_.prop_message_}, decltype(_impl_.map_message_){nullptr}, /*decltype(_impl_._cached_size_)*/ {}}; + decltype(_impl_.human_message_){from._impl_.human_message_}, decltype(_impl_.butcher_message_){from._impl_.butcher_message_}, decltype(_impl_.prop_message_){from._impl_.prop_message_}, decltype(_impl_.map_message_){nullptr}, decltype(_impl_.game_state_){}, /*decltype(_impl_._cached_size_)*/ {}}; _internal_metadata_.MergeFrom<::PROTOBUF_NAMESPACE_ID::UnknownFieldSet>(from._internal_metadata_); if (from._internal_has_map_message()) { _this->_impl_.map_message_ = new ::protobuf::MessageOfMap(*from._impl_.map_message_); } + _this->_impl_.game_state_ = from._impl_.game_state_; // @@protoc_insertion_point(copy_constructor:protobuf.MessageToClient) } @@ -3061,7 +3064,7 @@ namespace protobuf (void)arena; (void)is_message_owned; new (&_impl_) Impl_{ - decltype(_impl_.human_message_){arena}, decltype(_impl_.butcher_message_){arena}, decltype(_impl_.prop_message_){arena}, decltype(_impl_.map_message_){nullptr}, /*decltype(_impl_._cached_size_)*/ {}}; + decltype(_impl_.human_message_){arena}, decltype(_impl_.butcher_message_){arena}, decltype(_impl_.prop_message_){arena}, decltype(_impl_.map_message_){nullptr}, decltype(_impl_.game_state_){0}, /*decltype(_impl_._cached_size_)*/ {}}; } MessageToClient::~MessageToClient() @@ -3105,6 +3108,7 @@ namespace protobuf delete _impl_.map_message_; } _impl_.map_message_ = nullptr; + _impl_.game_state_ = 0; _internal_metadata_.Clear<::PROTOBUF_NAMESPACE_ID::UnknownFieldSet>(); } @@ -3180,6 +3184,17 @@ namespace protobuf else goto handle_unusual; continue; + // .protobuf.GameState game_state = 5; + case 5: + if (PROTOBUF_PREDICT_TRUE(static_cast(tag) == 40)) + { + uint64_t val = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint64(&ptr); + CHK_(ptr); + _internal_set_game_state(static_cast<::protobuf::GameState>(val)); + } + else + goto handle_unusual; + continue; default: goto handle_unusual; } // switch @@ -3254,6 +3269,15 @@ namespace protobuf InternalWriteMessage(4, _Internal::map_message(this), _Internal::map_message(this).GetCachedSize(), target, stream); } + // .protobuf.GameState game_state = 5; + if (this->_internal_game_state() != 0) + { + target = stream->EnsureSpace(target); + target = ::_pbi::WireFormatLite::WriteEnumToArray( + 5, this->_internal_game_state(), target + ); + } + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { target = ::_pbi::WireFormat::InternalSerializeUnknownFieldsToArray( @@ -3306,6 +3330,13 @@ namespace protobuf ); } + // .protobuf.GameState game_state = 5; + if (this->_internal_game_state() != 0) + { + total_size += 1 + + ::_pbi::WireFormatLite::EnumSize(this->_internal_game_state()); + } + return MaybeComputeUnknownFieldsSize(total_size, &_impl_._cached_size_); } @@ -3335,6 +3366,10 @@ namespace protobuf from._internal_map_message() ); } + if (from._internal_game_state() != 0) + { + _this->_internal_set_game_state(from._internal_game_state()); + } _this->_internal_metadata_.MergeFrom<::PROTOBUF_NAMESPACE_ID::UnknownFieldSet>(from._internal_metadata_); } @@ -3359,7 +3394,11 @@ namespace protobuf _impl_.human_message_.InternalSwap(&other->_impl_.human_message_); _impl_.butcher_message_.InternalSwap(&other->_impl_.butcher_message_); _impl_.prop_message_.InternalSwap(&other->_impl_.prop_message_); - swap(_impl_.map_message_, other->_impl_.map_message_); + ::PROTOBUF_NAMESPACE_ID::internal::memswap< + PROTOBUF_FIELD_OFFSET(MessageToClient, _impl_.game_state_) + sizeof(MessageToClient::_impl_.game_state_) - PROTOBUF_FIELD_OFFSET(MessageToClient, _impl_.map_message_)>( + reinterpret_cast(&_impl_.map_message_), + reinterpret_cast(&other->_impl_.map_message_) + ); } ::PROTOBUF_NAMESPACE_ID::Metadata MessageToClient::GetMetadata() const diff --git a/CAPI/proto/Message2Clients.pb.h b/CAPI/proto/Message2Clients.pb.h index dba5aca..6f95e7d 100644 --- a/CAPI/proto/Message2Clients.pb.h +++ b/CAPI/proto/Message2Clients.pb.h @@ -1951,6 +1951,7 @@ namespace protobuf kButcherMessageFieldNumber = 2, kPropMessageFieldNumber = 3, kMapMessageFieldNumber = 4, + kGameStateFieldNumber = 5, }; // repeated .protobuf.MessageOfHuman human_message = 1; int human_message_size() const; @@ -2041,6 +2042,16 @@ namespace protobuf ); ::protobuf::MessageOfMap* unsafe_arena_release_map_message(); + // .protobuf.GameState game_state = 5; + void clear_game_state(); + ::protobuf::GameState game_state() const; + void set_game_state(::protobuf::GameState value); + + private: + ::protobuf::GameState _internal_game_state() const; + void _internal_set_game_state(::protobuf::GameState value); + + public: // @@protoc_insertion_point(class_scope:protobuf.MessageToClient) private: @@ -2056,6 +2067,7 @@ namespace protobuf ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField<::protobuf::MessageOfButcher> butcher_message_; ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField<::protobuf::MessageOfProp> prop_message_; ::protobuf::MessageOfMap* map_message_; + int game_state_; mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_; }; union @@ -4208,6 +4220,30 @@ namespace protobuf // @@protoc_insertion_point(field_set_allocated:protobuf.MessageToClient.map_message) } + // .protobuf.GameState game_state = 5; + inline void MessageToClient::clear_game_state() + { + _impl_.game_state_ = 0; + } + inline ::protobuf::GameState MessageToClient::_internal_game_state() const + { + return static_cast<::protobuf::GameState>(_impl_.game_state_); + } + inline ::protobuf::GameState MessageToClient::game_state() const + { + // @@protoc_insertion_point(field_get:protobuf.MessageToClient.game_state) + return _internal_game_state(); + } + inline void MessageToClient::_internal_set_game_state(::protobuf::GameState value) + { + _impl_.game_state_ = value; + } + inline void MessageToClient::set_game_state(::protobuf::GameState value) + { + _internal_set_game_state(value); + // @@protoc_insertion_point(field_set:protobuf.MessageToClient.game_state) + } + // ------------------------------------------------------------------- // MoveRes diff --git a/CAPI/proto/MessageType.pb.cc b/CAPI/proto/MessageType.pb.cc index b5fe3d7..65265e8 100644 --- a/CAPI/proto/MessageType.pb.cc +++ b/CAPI/proto/MessageType.pb.cc @@ -23,7 +23,7 @@ namespace _pbi = _pb::internal; namespace protobuf { } // namespace protobuf -static const ::_pb::EnumDescriptor* file_level_enum_descriptors_MessageType_2eproto[9]; +static const ::_pb::EnumDescriptor* file_level_enum_descriptors_MessageType_2eproto[10]; static constexpr ::_pb::ServiceDescriptor const** file_level_service_descriptors_MessageType_2eproto = nullptr; const uint32_t TableStruct_MessageType_2eproto::offsets[1] = {}; static constexpr ::_pbi::MigrationSchema* schemas = nullptr; @@ -51,12 +51,14 @@ const char descriptor_table_protodef_MessageType_2eproto[] PROTOBUF_SECTION_VARI "\020\003\022\016\n\nHUMANTYPE4\020\004*l\n\013ButcherType\022\025\n\021NUL" "L_BUTCHER_TYPE\020\000\022\020\n\014BUTCHERTYPE1\020\001\022\020\n\014BU" "TCHERTYPE2\020\002\022\020\n\014BUTCHERTYPE3\020\003\022\020\n\014BUTCHE" - "RTYPE4\020\004b\006proto3"; + "RTYPE4\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\010" + "GAME_END\020\003b\006proto3"; static ::_pbi::once_flag descriptor_table_MessageType_2eproto_once; const ::_pbi::DescriptorTable descriptor_table_MessageType_2eproto = { false, false, - 856, + 938, descriptor_table_protodef_MessageType_2eproto, "MessageType.proto", &descriptor_table_MessageType_2eproto_once, @@ -258,6 +260,25 @@ namespace protobuf } } + const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* GameState_descriptor() + { + ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&descriptor_table_MessageType_2eproto); + return file_level_enum_descriptors_MessageType_2eproto[9]; + } + bool GameState_IsValid(int value) + { + switch (value) + { + case 0: + case 1: + case 2: + case 3: + return true; + default: + return false; + } + } + // @@protoc_insertion_point(namespace_scope) } // namespace protobuf PROTOBUF_NAMESPACE_OPEN diff --git a/CAPI/proto/MessageType.pb.h b/CAPI/proto/MessageType.pb.h index 3c1d55c..0a6ee42 100644 --- a/CAPI/proto/MessageType.pb.h +++ b/CAPI/proto/MessageType.pb.h @@ -337,6 +337,37 @@ namespace protobuf ButcherType_descriptor(), name, value ); } + enum GameState : int + { + NULL_GAME_STATE = 0, + GAME_START = 1, + GAME_RUNNING = 2, + GAME_END = 3, + GameState_INT_MIN_SENTINEL_DO_NOT_USE_ = std::numeric_limits::min(), + GameState_INT_MAX_SENTINEL_DO_NOT_USE_ = std::numeric_limits::max() + }; + bool GameState_IsValid(int value); + constexpr GameState GameState_MIN = NULL_GAME_STATE; + constexpr GameState GameState_MAX = GAME_END; + constexpr int GameState_ARRAYSIZE = GameState_MAX + 1; + + const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* GameState_descriptor(); + template + inline const std::string& GameState_Name(T enum_t_value) + { + static_assert(::std::is_same::value || ::std::is_integral::value, "Incorrect type passed to function GameState_Name."); + return ::PROTOBUF_NAMESPACE_ID::internal::NameOfEnum( + GameState_descriptor(), enum_t_value + ); + } + inline bool GameState_Parse( + ::PROTOBUF_NAMESPACE_ID::ConstStringParam name, GameState* value + ) + { + return ::PROTOBUF_NAMESPACE_ID::internal::ParseNamedEnum( + GameState_descriptor(), name, value + ); + } // =================================================================== // =================================================================== @@ -438,6 +469,15 @@ inline const EnumDescriptor* GetEnumDescriptor<::protobuf::ButcherType>() { return ::protobuf::ButcherType_descriptor(); } +template<> +struct is_proto_enum<::protobuf::GameState> : ::std::true_type +{ +}; +template<> +inline const EnumDescriptor* GetEnumDescriptor<::protobuf::GameState>() +{ + return ::protobuf::GameState_descriptor(); +} PROTOBUF_NAMESPACE_CLOSE diff --git a/dependency/proto/Message2Clients.proto b/dependency/proto/Message2Clients.proto index 7925964..7c52af3 100755 --- a/dependency/proto/Message2Clients.proto +++ b/dependency/proto/Message2Clients.proto @@ -79,6 +79,7 @@ message MessageToClient repeated MessageOfButcher butcher_message = 2; // 是否真正repeated待定 repeated MessageOfProp prop_message = 3; MessageOfMap map_message = 4; + GameState game_state = 5; } message MoveRes // 如果打算设计撞墙保留平行速度分量,且需要返回值则可用这个(大概没啥用) diff --git a/dependency/proto/MessageType.proto b/dependency/proto/MessageType.proto index f92708f..01b42a0 100755 --- a/dependency/proto/MessageType.proto +++ b/dependency/proto/MessageType.proto @@ -89,32 +89,11 @@ enum ButcherType BUTCHERTYPE4 = 4; } -// 取消了大包之后,MessageType的枚举是否有必要保留还有待商榷 -// enum MessageType -// { -// // 公共信息类型 -// Move = 0; -// PickProp = 1; -// UseProp = 2; -// UseSkill = 3; -// Map = 4; -// Send = 5; - -// // 人类限定信息类型 -// FixMachine = 6; -// SaveHuman = 7; - -// // 屠夫限定信息类型 -// Attack = 8; -// CarryHuman = 9; -// ReleaseHuman = 10; -// HangHuman = 11; - -// // 游戏相关信息类型 -// AddPlayer = 12; -// InvalidPlayer = 13; -// ValidPlayer = 14; -// StartGame = 15; -// Gaming = 16; -// EndGame = 17; -// } +// 游戏进行状态 +enum GameState +{ + NULL_GAME_STATE = 0; + GAME_START = 1; + GAME_RUNNING = 2; + GAME_END = 3; +} From cd6747e6f8620b714fa65f9246d12efd61117578 Mon Sep 17 00:00:00 2001 From: DragonAura Date: Sat, 10 Dec 2022 20:45:23 +0800 Subject: [PATCH 2/4] feat(CAPI): :sparkles: complete basic logic --- CAPI/API/include/API.h | 63 +++++++++----------------- CAPI/API/include/logic.h | 10 ++--- CAPI/API/include/structures.h | 9 ++++ CAPI/API/include/utils.hpp | 7 +++ CAPI/API/src/API.cpp | 50 ++++++++++++++++----- CAPI/API/src/DebugAPI.cpp | 70 ++++++++++++++++++++++++----- CAPI/API/src/logic.cpp | 83 +++++++++++++++++++++++++++++------ 7 files changed, 210 insertions(+), 82 deletions(-) diff --git a/CAPI/API/include/API.h b/CAPI/API/include/API.h index 0fb0f4b..191960a 100644 --- a/CAPI/API/include/API.h +++ b/CAPI/API/include/API.h @@ -63,6 +63,8 @@ public: virtual bool CarryHuman() = 0; virtual bool ReleaseHuman() = 0; virtual bool HangHuman() = 0; + + virtual const std::vector GetPlayerGUIDs() const = 0; }; class IAPI @@ -171,11 +173,9 @@ public: } void Play(IAI& ai) override; - std::future Move(int64_t timeInMilliseconds, double angleInRadian) override; + [[nodiscard]] int GetFrameCount() const override; - [[nodiscard]] int GetFrameCount() const override - { - } + std::future Move(int64_t timeInMilliseconds, double angleInRadian) override; std::future MoveRight(int64_t timeInMilliseconds) override; std::future MoveUp(int64_t timeInMilliseconds) override; @@ -190,9 +190,7 @@ public: [[nodiscard]] std::future HaveMessage() override; [[nodiscard]] std::future> GetMessage() override; - std::future Wait() override - { - } + std::future Wait() override; [[nodiscard]] std::vector> GetHuman() const override; [[nodiscard]] std::vector> GetButcher() const override; @@ -230,12 +228,9 @@ public: } void Play(IAI& ai) override; - std::future Move(int64_t timeInMilliseconds, double angleInRadian) override; - - [[nodiscard]] int GetFrameCount() const override - { - } + [[nodiscard]] int GetFrameCount() const override; + std::future Move(int64_t timeInMilliseconds, double angleInRadian) override; std::future MoveRight(int64_t timeInMilliseconds) override; std::future MoveUp(int64_t timeInMilliseconds) override; std::future MoveLeft(int64_t timeInMilliseconds) override; @@ -249,9 +244,7 @@ public: [[nodiscard]] std::future HaveMessage() override; [[nodiscard]] std::future> GetMessage() override; - std::future Wait() override - { - } + std::future Wait() override; [[nodiscard]] std::vector> GetHuman() const override; [[nodiscard]] std::vector> GetButcher() const override; @@ -280,20 +273,13 @@ public: logic(logic) { } - void StartTimer() override - { - } - void EndTimer() override - { - } + void StartTimer() override; + void EndTimer() override; void Play(IAI& ai) override; - std::future Move(int64_t timeInMilliseconds, double angleInRadian) override; - - [[nodiscard]] int GetFrameCount() const override - { - } + [[nodiscard]] int GetFrameCount() const override; + std::future Move(int64_t timeInMilliseconds, double angleInRadian) override; std::future MoveRight(int64_t timeInMilliseconds) override; std::future MoveUp(int64_t timeInMilliseconds) override; std::future MoveLeft(int64_t timeInMilliseconds) override; @@ -307,9 +293,7 @@ public: [[nodiscard]] std::future HaveMessage() override; [[nodiscard]] std::future> GetMessage() override; - std::future Wait() override - { - } + std::future Wait() override; [[nodiscard]] std::vector> GetHuman() const override; [[nodiscard]] std::vector> GetButcher() const override; @@ -329,6 +313,7 @@ public: [[nodiscard]] virtual std::shared_ptr GetSelfInfo() const override; private: + std::chrono::system_clock::time_point StartPoint; ILogic& logic; }; @@ -339,20 +324,13 @@ public: logic(logic) { } - void StartTimer() override - { - } - void EndTimer() override - { - } + void StartTimer() override; + void EndTimer() override; void Play(IAI& ai) override; - std::future Move(int64_t timeInMilliseconds, double angleInRadian) override; - - [[nodiscard]] int GetFrameCount() const override - { - } + [[nodiscard]] int GetFrameCount() const override; + std::future Move(int64_t timeInMilliseconds, double angleInRadian) override; std::future MoveRight(int64_t timeInMilliseconds) override; std::future MoveUp(int64_t timeInMilliseconds) override; std::future MoveLeft(int64_t timeInMilliseconds) override; @@ -366,9 +344,7 @@ public: [[nodiscard]] std::future HaveMessage() override; [[nodiscard]] std::future> GetMessage() override; - std::future Wait() override - { - } + std::future Wait() override; [[nodiscard]] std::vector> GetHuman() const override; [[nodiscard]] std::vector> GetButcher() const override; @@ -387,6 +363,7 @@ public: [[nodiscard]] std::shared_ptr GetSelfInfo() const override; private: + std::chrono::system_clock::time_point StartPoint; ILogic& logic; }; diff --git a/CAPI/API/include/logic.h b/CAPI/API/include/logic.h index b11fd78..a772504 100644 --- a/CAPI/API/include/logic.h +++ b/CAPI/API/include/logic.h @@ -43,10 +43,6 @@ private: // GUID信息 std::vector playerGUIDs; - // THUAI5中的通信组件可以完全被我们的stub取代,故无须再写 - - std::unique_ptr pAI; - std::unique_ptr timer; std::thread tAI; // 用于运行AI的线程 @@ -70,6 +66,8 @@ private: int counterState = 0; int counterBuffer = 0; + THUAI6::GameState gameState = THUAI6::GameState::NullGameState; + // 是否应该执行player() std::atomic_bool AILoop = true; @@ -84,8 +82,6 @@ private: // 提供给API使用的函数 - // 获取服务器发来的消息 - std::vector> GetButchers() const override; std::vector> GetHumans() const override; std::vector> GetProps() const override; @@ -121,6 +117,8 @@ private: int GetCounter() const override; + const std::vector GetPlayerGUIDs() const override; + bool TryConnection(); // THUAI5中的一系列用于处理信息的函数可能也不会再用 diff --git a/CAPI/API/include/structures.h b/CAPI/API/include/structures.h index 436e1e7..648aa89 100644 --- a/CAPI/API/include/structures.h +++ b/CAPI/API/include/structures.h @@ -8,6 +8,15 @@ namespace THUAI6 { + + // 游戏状态 + enum class GameState : unsigned char + { + NullGameState = 0, + GameStart = 1, + GameRunning = 2, + GameEnd = 3, + }; // 所有NullXXXType均为错误类型,其余为可能出现的正常类型 // 位置标志 diff --git a/CAPI/API/include/utils.hpp b/CAPI/API/include/utils.hpp index d73d333..99c0170 100644 --- a/CAPI/API/include/utils.hpp +++ b/CAPI/API/include/utils.hpp @@ -85,6 +85,13 @@ namespace Proto2THUAI6 {protobuf::HumanState::DEAD, THUAI6::HumanState::Dead}, }; + inline std::map gameStateDict{ + {protobuf::GameState::NULL_GAME_STATE, THUAI6::GameState::NullGameState}, + {protobuf::GameState::GAME_START, THUAI6::GameState::GameStart}, + {protobuf::GameState::GAME_RUNNING, THUAI6::GameState::GameRunning}, + {protobuf::GameState::GAME_END, THUAI6::GameState::GameEnd}, + }; + // 用于将Protobuf中的类转换为THUAI6的类 inline std::shared_ptr Protobuf2THUAI6Butcher(const protobuf::MessageOfButcher& butcherMsg) { diff --git a/CAPI/API/src/API.cpp b/CAPI/API/src/API.cpp index 9273fca..8a7d168 100644 --- a/CAPI/API/src/API.cpp +++ b/CAPI/API/src/API.cpp @@ -2,6 +2,16 @@ #include "API.h" #define PI 3.14159265358979323846 +int HumanAPI::GetFrameCount() const +{ + return logic.GetCounter(); +} + +int ButcherAPI::GetFrameCount() const +{ + return logic.GetCounter(); +} + std::future HumanAPI::Move(int64_t timeInMilliseconds, double angleInRadian) { return std::async(std::launch::async, [&]() @@ -126,6 +136,26 @@ std::future> ButcherAPI::GetMessage() { return logic.GetMessage(); }); } +std::future HumanAPI::Wait() +{ + if (logic.GetCounter() == -1) + return std::async(std::launch::async, [&]() + { return false; }); + else + return std::async(std::launch::async, [&]() + { return logic.WaitThread(); }); +} + +std::future ButcherAPI::Wait() +{ + if (logic.GetCounter() == -1) + return std::async(std::launch::async, [&]() + { return false; }); + else + return std::async(std::launch::async, [&]() + { return logic.WaitThread(); }); +} + std::vector> HumanAPI::GetButcher() const { return logic.GetButchers(); @@ -178,36 +208,36 @@ std::vector> ButcherAPI::GetFullMap() const const std::vector HumanAPI::GetPlayerGUIDs() const { - // todo + return logic.GetPlayerGUIDs(); } const std::vector ButcherAPI::GetPlayerGUIDs() const { - // todo + return logic.GetPlayerGUIDs(); } std::future HumanAPI::StartFixMachine() { - std::async(std::launch::async, [&]() - { return logic.StartFixMachine(); }); + return std::async(std::launch::async, [&]() + { return logic.StartFixMachine(); }); } std::future HumanAPI::EndFixMachine() { - std::async(std::launch::async, [&]() - { return logic.EndFixMachine(); }); + return std::async(std::launch::async, [&]() + { return logic.EndFixMachine(); }); } std::future HumanAPI::StartSaveHuman() { - std::async(std::launch::async, [&]() - { return logic.StartSaveHuman(); }); + return std::async(std::launch::async, [&]() + { return logic.StartSaveHuman(); }); } std::future HumanAPI::EndSaveHuman() { - std::async(std::launch::async, [&]() - { return logic.EndSaveHuman(); }); + return std::async(std::launch::async, [&]() + { return logic.EndSaveHuman(); }); } std::future HumanAPI::Escape() diff --git a/CAPI/API/src/DebugAPI.cpp b/CAPI/API/src/DebugAPI.cpp index 5be7dba..57d6431 100644 --- a/CAPI/API/src/DebugAPI.cpp +++ b/CAPI/API/src/DebugAPI.cpp @@ -2,6 +2,36 @@ #include "API.h" #define PI 3.14159265358979323846 +void HumanDebugAPI::StartTimer() +{ + StartPoint = std::chrono::system_clock::now(); + std::time_t t = std::chrono::system_clock::to_time_t(StartPoint); +} + +void ButcherDebugAPI::StartTimer() +{ + StartPoint = std::chrono::system_clock::now(); + std::time_t t = std::chrono::system_clock::to_time_t(StartPoint); +} + +void HumanDebugAPI::EndTimer() +{ +} + +void ButcherDebugAPI::EndTimer() +{ +} + +int HumanDebugAPI::GetFrameCount() const +{ + return logic.GetCounter(); +} + +int ButcherDebugAPI::GetFrameCount() const +{ + return logic.GetCounter(); +} + std::future HumanDebugAPI::Move(int64_t timeInMilliseconds, double angleInRadian) { return std::async(std::launch::async, [&]() @@ -126,6 +156,26 @@ std::future> ButcherDebugAPI::GetMessage() { return logic.GetMessage(); }); } +std::future HumanDebugAPI::Wait() +{ + if (logic.GetCounter() == -1) + return std::async(std::launch::async, [&]() + { return false; }); + else + return std::async(std::launch::async, [&]() + { return logic.WaitThread(); }); +} + +std::future ButcherDebugAPI::Wait() +{ + if (logic.GetCounter() == -1) + return std::async(std::launch::async, [&]() + { return false; }); + else + return std::async(std::launch::async, [&]() + { return logic.WaitThread(); }); +} + std::vector> HumanDebugAPI::GetButcher() const { return logic.GetButchers(); @@ -178,36 +228,36 @@ std::vector> ButcherDebugAPI::GetFullMap() const const std::vector HumanDebugAPI::GetPlayerGUIDs() const { - // todo + return logic.GetPlayerGUIDs(); } const std::vector ButcherDebugAPI::GetPlayerGUIDs() const { - // todo + return logic.GetPlayerGUIDs(); } std::future HumanDebugAPI::StartFixMachine() { - std::async(std::launch::async, [&]() - { return logic.StartFixMachine(); }); + return std::async(std::launch::async, [&]() + { return logic.StartFixMachine(); }); } std::future HumanDebugAPI::EndFixMachine() { - std::async(std::launch::async, [&]() - { return logic.EndFixMachine(); }); + return std::async(std::launch::async, [&]() + { return logic.EndFixMachine(); }); } std::future HumanDebugAPI::StartSaveHuman() { - std::async(std::launch::async, [&]() - { return logic.StartSaveHuman(); }); + return std::async(std::launch::async, [&]() + { return logic.StartSaveHuman(); }); } std::future HumanDebugAPI::EndSaveHuman() { - std::async(std::launch::async, [&]() - { return logic.EndSaveHuman(); }); + return std::async(std::launch::async, [&]() + { return logic.EndSaveHuman(); }); } std::future HumanDebugAPI::Escape() diff --git a/CAPI/API/src/logic.cpp b/CAPI/API/src/logic.cpp index 9b93b96..dc427a6 100644 --- a/CAPI/API/src/logic.cpp +++ b/CAPI/API/src/logic.cpp @@ -158,13 +158,58 @@ void Logic::ProcessMessage() { std::cout << "Join Player!" << std::endl; pComm->AddPlayer(playerID, playerType, humanType, butcherType); - while (true) + while (gameState != THUAI6::GameState::GameEnd) { if (pComm->HaveMessage2Client()) { std::cout << "Get Message!" << std::endl; auto clientMsg = pComm->GetMessage2Client(); - LoadBuffer(clientMsg); + gameState = Proto2THUAI6::gameStateDict[clientMsg.game_state()]; + switch (gameState) + { + case THUAI6::GameState::GameStart: + std::cout << "Game Start!" << std::endl; + + // 重新读取玩家的guid,guid确保人类在前屠夫在后 + playerGUIDs.clear(); + for (auto human : clientMsg.human_message()) + playerGUIDs.push_back(human.guid()); + for (auto butcher : clientMsg.butcher_message()) + playerGUIDs.push_back(butcher.guid()); + currentState->guids = playerGUIDs; + bufferState->guids = playerGUIDs; + + LoadBuffer(clientMsg); + + AILoop = true; + UnBlockAI(); + + break; + case THUAI6::GameState::GameRunning: + // 重新读取玩家的guid,guid确保人类在前屠夫在后 + playerGUIDs.clear(); + for (auto human : clientMsg.human_message()) + playerGUIDs.push_back(human.guid()); + for (auto butcher : clientMsg.butcher_message()) + playerGUIDs.push_back(butcher.guid()); + currentState->guids = playerGUIDs; + bufferState->guids = playerGUIDs; + + LoadBuffer(clientMsg); + break; + case THUAI6::GameState::GameEnd: + AILoop = false; + { + std::lock_guard lock(mtxBuffer); + bufferUpdated = true; + counterBuffer = -1; + } + cvBuffer.notify_one(); + std::cout << "Game End!" << std::endl; + break; + default: + std::cerr << "Invalid GameState!" << std::endl; + } } } }; @@ -277,6 +322,12 @@ int Logic::GetCounter() const return counterState; } +const std::vector Logic::GetPlayerGUIDs() const +{ + std::unique_lock lock(mtxState); + return currentState->guids; +} + bool Logic::TryConnection() { std::cout << "Trying to connect to server..." << std::endl; @@ -286,9 +337,6 @@ bool Logic::TryConnection() void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port) { - // 构造AI - pAI = createAI(); - // 建立与服务器之间通信的组件 pComm = std::make_unique(IP, port); @@ -306,15 +354,24 @@ void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port) cvAI.wait(lock, [this]() { return AIStart; }); } - std::cout << "AI Start!" << std::endl; auto ai = createAI(); - ProcessMessage(); + while (AILoop) { - Update(); - timer->StartTimer(); - timer->Play(*ai); - timer->EndTimer(); + if (asynchronous) + { + Wait(); + timer->StartTimer(); + timer->Play(*ai); + timer->EndTimer(); + } + else + { + Update(); + timer->StartTimer(); + timer->Play(*ai); + timer->EndTimer(); + } } }; @@ -327,8 +384,8 @@ void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port) if (tAI.joinable()) { std::cout << "Join the AI thread." << std::endl; - AIStart = true; - cvAI.notify_one(); + // 首先开启处理消息的线程 + ProcessMessage(); tAI.join(); } } From 90ee637d7a669a922c9b2872e8b4c084ddb0657a Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Sat, 10 Dec 2022 14:11:38 +0000 Subject: [PATCH 3/4] chore(deps): update dependency frameratetask to v1.2.0 --- logic/GameEngine/GameEngine.csproj | 2 +- logic/Server/Server.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/logic/GameEngine/GameEngine.csproj b/logic/GameEngine/GameEngine.csproj index 2adc507..f622778 100644 --- a/logic/GameEngine/GameEngine.csproj +++ b/logic/GameEngine/GameEngine.csproj @@ -7,7 +7,7 @@ - + diff --git a/logic/Server/Server.csproj b/logic/Server/Server.csproj index 62045cf..cf3afc3 100644 --- a/logic/Server/Server.csproj +++ b/logic/Server/Server.csproj @@ -8,7 +8,7 @@ - + From e1feed853ede30ef2b3db90bdc602ff3bdf35196 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Sat, 10 Dec 2022 14:11:44 +0000 Subject: [PATCH 4/4] chore(deps): update dependency grpc.tools to v2.51.0 --- dependency/proto/Protos.csproj | 2 +- logic/Server/Server.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dependency/proto/Protos.csproj b/dependency/proto/Protos.csproj index 5377978..d03c1fe 100755 --- a/dependency/proto/Protos.csproj +++ b/dependency/proto/Protos.csproj @@ -18,7 +18,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/logic/Server/Server.csproj b/logic/Server/Server.csproj index 62045cf..57fb2e9 100644 --- a/logic/Server/Server.csproj +++ b/logic/Server/Server.csproj @@ -12,7 +12,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive