diff --git a/CAPI/cpp/API/include/logic.h b/CAPI/cpp/API/include/logic.h index 5ca41ea..23098db 100644 --- a/CAPI/cpp/API/include/logic.h +++ b/CAPI/cpp/API/include/logic.h @@ -49,9 +49,6 @@ private: THUAI6::TrickerType trickerType; THUAI6::StudentType studentType; - // GUID信息 - std::vector playerGUIDs; - std::unique_ptr timer; std::thread tAI; // 用于运行AI的线程 diff --git a/CAPI/cpp/API/src/logic.cpp b/CAPI/cpp/API/src/logic.cpp index c06eb09..54ec745 100644 --- a/CAPI/cpp/API/src/logic.cpp +++ b/CAPI/cpp/API/src/logic.cpp @@ -323,17 +323,6 @@ void Logic::ProcessMessage() case THUAI6::GameState::GameStart: logger->info("Game Start!"); - // 重新读取玩家的guid,保证人类在前屠夫在后 - playerGUIDs.clear(); - for (const auto& obj : clientMsg.obj_message()) - if (Proto2THUAI6::messageOfObjDict[obj.message_of_obj_case()] == THUAI6::MessageOfObj::StudentMessage) - playerGUIDs.push_back(obj.student_message().guid()); - for (const auto& obj : clientMsg.obj_message()) - if (Proto2THUAI6::messageOfObjDict[obj.message_of_obj_case()] == THUAI6::MessageOfObj::TrickerMessage) - playerGUIDs.push_back(obj.tricker_message().guid()); - currentState->guids = playerGUIDs; - bufferState->guids = playerGUIDs; - // 读取地图 for (const auto& item : clientMsg.obj_message()) if (Proto2THUAI6::messageOfObjDict[item.message_of_obj_case()] == THUAI6::MessageOfObj::MapMessage) @@ -368,16 +357,6 @@ void Logic::ProcessMessage() break; case THUAI6::GameState::GameRunning: - // 重新读取玩家的guid,guid确保人类在前屠夫在后 - playerGUIDs.clear(); - for (const auto& obj : clientMsg.obj_message()) - if (Proto2THUAI6::messageOfObjDict[obj.message_of_obj_case()] == THUAI6::MessageOfObj::StudentMessage) - playerGUIDs.push_back(obj.student_message().guid()); - for (const auto& obj : clientMsg.obj_message()) - if (Proto2THUAI6::messageOfObjDict[obj.message_of_obj_case()] == THUAI6::MessageOfObj::TrickerMessage) - playerGUIDs.push_back(obj.tricker_message().guid()); - currentState->guids = playerGUIDs; - bufferState->guids = playerGUIDs; LoadBuffer(clientMsg); break; @@ -605,9 +584,16 @@ void Logic::LoadBuffer(const protobuf::MessageToClient& message) bufferState->props.clear(); bufferState->bullets.clear(); bufferState->bombedBullets.clear(); + bufferState->guids.clear(); logger->debug("Buffer cleared!"); // 读取新的信息 + for (const auto& obj : message.obj_message()) + if (Proto2THUAI6::messageOfObjDict[obj.message_of_obj_case()] == THUAI6::MessageOfObj::StudentMessage) + bufferState->guids.push_back(obj.student_message().guid()); + for (const auto& obj : message.obj_message()) + if (Proto2THUAI6::messageOfObjDict[obj.message_of_obj_case()] == THUAI6::MessageOfObj::TrickerMessage) + bufferState->guids.push_back(obj.tricker_message().guid()); bufferState->gameInfo = Proto2THUAI6::Protobuf2THUAI6GameInfo(message.all_message()); LoadBufferSelf(message); for (const auto& item : message.obj_message()) @@ -689,6 +675,7 @@ bool Logic::TryConnection() bool Logic::HaveView(int gridX, int gridY, int selfX, int selfY, int viewRange) const { + std::unique_lock lock(mtxState); return AssistFunction::HaveView(viewRange, selfX, selfY, gridX, gridY, currentState->gameMap); } diff --git a/CAPI/python/PyAPI/logic.py b/CAPI/python/PyAPI/logic.py index b3763c6..e1dcf7f 100644 --- a/CAPI/python/PyAPI/logic.py +++ b/CAPI/python/PyAPI/logic.py @@ -3,6 +3,7 @@ from typing import List, Union, Callable, Tuple import threading import logging import copy +import platform import proto.MessageType_pb2 as MessageType import proto.Message2Server_pb2 as Message2Server import proto.Message2Clients_pb2 as Message2Clients @@ -22,7 +23,6 @@ class Logic(ILogic): # ID self.__playerID: int = playerID - self.__playerGUIDs: List[int] = [] self.__playerType: THUAI6.PlayerType = playerType @@ -214,7 +214,7 @@ class Logic(ILogic): def GetPlayerGUIDs(self) -> List[int]: with self.__mtxState: - return copy.deepcopy(self.__playerGUIDs) + return copy.deepcopy(self.__currentState.guids) # IStudentAPI使用的接口 @@ -263,7 +263,8 @@ class Logic(ILogic): return self.__comm.EndAllAction(self.__playerID) def HaveView(self, gridX: int, gridY: int, selfX: int, selfY: int, viewRange: int) -> bool: - return AssistFunction.HaveView(viewRange, selfX, selfY, gridX, gridY, self.__currentState.gameMap) + with self.__mtxState: + return AssistFunction.HaveView(viewRange, selfX, selfY, gridX, gridY, self.__currentState.gameMap) # Logic内部逻辑 def __TryConnection(self) -> bool: @@ -286,15 +287,6 @@ class Logic(ILogic): if self.__gameState == THUAI6.GameState.GameStart: # 读取玩家的GUID self.__logger.info("Game start!") - self.__playerGUIDs.clear() - for obj in clientMsg.obj_message: - if obj.WhichOneof("message_of_obj") == "student_message": - self.__playerGUIDs.append(obj.student_message.guid) - for obj in clientMsg.obj_message: - if obj.WhichOneof("message_of_obj") == "tricker_message": - self.__playerGUIDs.append(obj.tricker_message.guid) - self.__currentState.guids = self.__playerGUIDs - self.__bufferState.guids = self.__playerGUIDs for obj in clientMsg.obj_message: if obj.WhichOneof("message_of_obj") == "map_message": @@ -318,15 +310,6 @@ class Logic(ILogic): elif self.__gameState == THUAI6.GameState.GameRunning: # 读取玩家的GUID - self.__playerGUIDs.clear() - for obj in clientMsg.obj_message: - if obj.WhichOneof("message_of_obj") == "student_message": - self.__playerGUIDs.append(obj.student_message.guid) - for obj in clientMsg.obj_message: - if obj.WhichOneof("message_of_obj") == "tricker_message": - self.__playerGUIDs.append(obj.tricker_message.guid) - self.__currentState.guids = self.__playerGUIDs - self.__bufferState.guids = self.__playerGUIDs self.__LoadBuffer(clientMsg) else: self.__logger.error("Unknown GameState!") @@ -467,9 +450,21 @@ class Logic(ILogic): self.__bufferState.students.clear() self.__bufferState.trickers.clear() self.__bufferState.props.clear() + self.__bufferState.bullets.clear() + self.__bufferState.bombedBullets.clear() + self.__bufferState.guids.clear() self.__logger.debug("Buffer cleared!") + + for obj in message.obj_message: + if obj.WhichOneof("message_of_obj") == "student_message": + self.__bufferState.guids.append(obj.student_message.guid) + for obj in message.obj_message: + if obj.WhichOneof("message_of_obj") == "tricker_message": + self.__bufferState.guids.append(obj.tricker_message.guid) + self.__bufferState.gameInfo = Proto2THUAI6.Protobuf2THUAI6GameInfo( message.all_message) + self.__LoadBufferSelf(message) for item in message.obj_message: self.__LoadBufferCase(item) @@ -511,9 +506,16 @@ class Logic(ILogic): formatter = logging.Formatter( "[%(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( - os.path.realpath(__file__))) + "/logs") + # if not os.path.exists(os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/logs"): + # os.makedirs(os.path.dirname(os.path.dirname( + # os.path.realpath(__file__))) + "/logs") + + if platform.system().lower() == "windows": + os.system( + f"mkdir {os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/logs'}") + else: + os.system( + f"mkdir -p {os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/logs'}") fileHandler = logging.FileHandler(os.path.dirname( os.path.dirname(os.path.realpath(__file__))) + "/logs/logic" + str(self.__playerID) + "-log.txt", "w+", encoding="utf-8") diff --git a/docs/CAPI接口(cpp).md b/docs/CAPI接口(cpp).md index 70bb297..8fba77c 100644 --- a/docs/CAPI接口(cpp).md +++ b/docs/CAPI接口(cpp).md @@ -59,13 +59,13 @@ 下面的 CellX 和 CellY 指的是地图格数,而非绝对坐标。 - `THUAI6::PlaceType GetPlaceType(int32_t cellX, int32_t cellY)` :返回某一位置场地种类信息。场地种类详见 structure.h 。 - - `bool IsDoorOpen(int32_t cellX, int32_t cellY) const`:查询特定位置门是否开启,没有门也返回false - 以下指令,若查询物品当前在视野内,则返回最新进度;若物品当前不在视野内、但曾经出现在视野内,则返回最后一次看到时的进度;若物品从未出现在视野内,或查询位置没有对应的物品,则返回 -1。 - `int32_t GetChestProgress(int32_t cellX, int32_t cellY) const`:查询特定位置箱子开启进度 - `int32_t GetGateProgress(int32_t cellX, int32_t cellY) const`:查询特定位置校门开启进度 - `int32_t GetClassroomProgress(int32_t cellX, int32_t cellY) const`:查询特定位置教室作业完成进度 - `int32_t GetDoorProgress(int32_t cellX, int32_t cellY) const`:查询特定位置门开启状态 - - `THUAI6::HiddenGateState GetHiddenGateState(int32_t cellX, int32_t cellY) const`::查询特定位置隐藏校门状态,没有隐藏校门返回THUAI6::HiddenGateState::Null + - `bool IsDoorOpen(int32_t cellX, int32_t cellY) const`:查询特定位置门是否开启,没有门/不在视野内也返回false + - `THUAI6::HiddenGateState GetHiddenGateState(int32_t cellX, int32_t cellY) const`::查询特定位置隐藏校门状态,没有隐藏校门/不在视野内返回THUAI6::HiddenGateState::Null #### 其他 - `std::shared_ptr GetGameInfo() const`:查询当前游戏状态 diff --git a/docs/CAPI接口(python).md b/docs/CAPI接口(python).md index 1b36a28..bfbf84d 100644 --- a/docs/CAPI接口(python).md +++ b/docs/CAPI接口(python).md @@ -72,13 +72,13 @@ 下面的 CellX 和 CellY 指的是地图格数,而非绝对坐标。 - `def GetPlaceType(self, cellX: int, cellY: int) -> THUAI6.PlaceType` :返回某一位置场地种类信息。场地种类详见 structure.h 。 - - `def IsDoorOpen(self, cellX: int, cellY: int) -> bool`:查询特定位置门是否开启,没有门也返回false - 以下指令,若查询物品当前在视野内,则返回最新进度;若物品当前不在视野内、但曾经出现在视野内,则返回最后一次看到时的进度;若物品从未出现在视野内,或查询位置没有对应的物品,则返回 -1。 - `def GetChestProgress(self, cellX: int, cellY: int) -> int`:查询特定位置箱子开启进度 - `def GetGateProgress(self, cellX: int, cellY: int) -> int`:查询特定位置校门开启进度 - `def GetClassroomProgress(self, cellX: int, cellY: int) -> int`:查询特定位置教室作业完成进度 - `def GetDoorProgress(self, cellX: int, cellY: int) -> int`:查询特定位置门开启状态 - - `def GetHiddenGateState(self, cellX: int, cellY: int) -> THUAI6.HiddenGateState`::查询特定位置隐藏校门状态,没有隐藏校门返回THUAI6::HiddenGateState::Null + - `def IsDoorOpen(self, cellX: int, cellY: int) -> bool`:查询特定位置门是否开启,没有门/不在视野内也返回false + - `def GetHiddenGateState(self, cellX: int, cellY: int) -> THUAI6.HiddenGateState`::查询特定位置隐藏校门状态,没有隐藏校门/不在视野内返回THUAI6::HiddenGateState::Null #### 其他