fix(CAPI): 🐛 fix various of bugs
tags/0.1.0
| @@ -49,9 +49,6 @@ private: | |||||
| THUAI6::TrickerType trickerType; | THUAI6::TrickerType trickerType; | ||||
| THUAI6::StudentType studentType; | THUAI6::StudentType studentType; | ||||
| // GUID信息 | |||||
| std::vector<int64_t> playerGUIDs; | |||||
| std::unique_ptr<IGameTimer> timer; | std::unique_ptr<IGameTimer> timer; | ||||
| std::thread tAI; // 用于运行AI的线程 | std::thread tAI; // 用于运行AI的线程 | ||||
| @@ -323,17 +323,6 @@ void Logic::ProcessMessage() | |||||
| case THUAI6::GameState::GameStart: | case THUAI6::GameState::GameStart: | ||||
| logger->info("Game Start!"); | 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()) | for (const auto& item : clientMsg.obj_message()) | ||||
| if (Proto2THUAI6::messageOfObjDict[item.message_of_obj_case()] == THUAI6::MessageOfObj::MapMessage) | if (Proto2THUAI6::messageOfObjDict[item.message_of_obj_case()] == THUAI6::MessageOfObj::MapMessage) | ||||
| @@ -368,16 +357,6 @@ void Logic::ProcessMessage() | |||||
| break; | break; | ||||
| case THUAI6::GameState::GameRunning: | 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); | LoadBuffer(clientMsg); | ||||
| break; | break; | ||||
| @@ -605,9 +584,16 @@ void Logic::LoadBuffer(const protobuf::MessageToClient& message) | |||||
| bufferState->props.clear(); | bufferState->props.clear(); | ||||
| bufferState->bullets.clear(); | bufferState->bullets.clear(); | ||||
| bufferState->bombedBullets.clear(); | bufferState->bombedBullets.clear(); | ||||
| bufferState->guids.clear(); | |||||
| logger->debug("Buffer cleared!"); | 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()); | bufferState->gameInfo = Proto2THUAI6::Protobuf2THUAI6GameInfo(message.all_message()); | ||||
| LoadBufferSelf(message); | LoadBufferSelf(message); | ||||
| for (const auto& item : message.obj_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 | bool Logic::HaveView(int gridX, int gridY, int selfX, int selfY, int viewRange) const | ||||
| { | { | ||||
| std::unique_lock<std::mutex> lock(mtxState); | |||||
| return AssistFunction::HaveView(viewRange, selfX, selfY, gridX, gridY, currentState->gameMap); | return AssistFunction::HaveView(viewRange, selfX, selfY, gridX, gridY, currentState->gameMap); | ||||
| } | } | ||||
| @@ -3,6 +3,7 @@ from typing import List, Union, Callable, Tuple | |||||
| import threading | import threading | ||||
| import logging | import logging | ||||
| import copy | import copy | ||||
| import platform | |||||
| import proto.MessageType_pb2 as MessageType | import proto.MessageType_pb2 as MessageType | ||||
| import proto.Message2Server_pb2 as Message2Server | import proto.Message2Server_pb2 as Message2Server | ||||
| import proto.Message2Clients_pb2 as Message2Clients | import proto.Message2Clients_pb2 as Message2Clients | ||||
| @@ -22,7 +23,6 @@ class Logic(ILogic): | |||||
| # ID | # ID | ||||
| self.__playerID: int = playerID | self.__playerID: int = playerID | ||||
| self.__playerGUIDs: List[int] = [] | |||||
| self.__playerType: THUAI6.PlayerType = playerType | self.__playerType: THUAI6.PlayerType = playerType | ||||
| @@ -214,7 +214,7 @@ class Logic(ILogic): | |||||
| def GetPlayerGUIDs(self) -> List[int]: | def GetPlayerGUIDs(self) -> List[int]: | ||||
| with self.__mtxState: | with self.__mtxState: | ||||
| return copy.deepcopy(self.__playerGUIDs) | |||||
| return copy.deepcopy(self.__currentState.guids) | |||||
| # IStudentAPI使用的接口 | # IStudentAPI使用的接口 | ||||
| @@ -263,7 +263,8 @@ class Logic(ILogic): | |||||
| return self.__comm.EndAllAction(self.__playerID) | return self.__comm.EndAllAction(self.__playerID) | ||||
| def HaveView(self, gridX: int, gridY: int, selfX: int, selfY: int, viewRange: int) -> bool: | 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内部逻辑 | # Logic内部逻辑 | ||||
| def __TryConnection(self) -> bool: | def __TryConnection(self) -> bool: | ||||
| @@ -286,15 +287,6 @@ class Logic(ILogic): | |||||
| if self.__gameState == THUAI6.GameState.GameStart: | if self.__gameState == THUAI6.GameState.GameStart: | ||||
| # 读取玩家的GUID | # 读取玩家的GUID | ||||
| self.__logger.info("Game start!") | 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: | for obj in clientMsg.obj_message: | ||||
| if obj.WhichOneof("message_of_obj") == "map_message": | if obj.WhichOneof("message_of_obj") == "map_message": | ||||
| @@ -318,15 +310,6 @@ class Logic(ILogic): | |||||
| elif self.__gameState == THUAI6.GameState.GameRunning: | elif self.__gameState == THUAI6.GameState.GameRunning: | ||||
| # 读取玩家的GUID | # 读取玩家的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) | self.__LoadBuffer(clientMsg) | ||||
| else: | else: | ||||
| self.__logger.error("Unknown GameState!") | self.__logger.error("Unknown GameState!") | ||||
| @@ -467,9 +450,21 @@ class Logic(ILogic): | |||||
| self.__bufferState.students.clear() | self.__bufferState.students.clear() | ||||
| self.__bufferState.trickers.clear() | self.__bufferState.trickers.clear() | ||||
| self.__bufferState.props.clear() | self.__bufferState.props.clear() | ||||
| self.__bufferState.bullets.clear() | |||||
| self.__bufferState.bombedBullets.clear() | |||||
| self.__bufferState.guids.clear() | |||||
| self.__logger.debug("Buffer cleared!") | 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( | self.__bufferState.gameInfo = Proto2THUAI6.Protobuf2THUAI6GameInfo( | ||||
| message.all_message) | message.all_message) | ||||
| self.__LoadBufferSelf(message) | self.__LoadBufferSelf(message) | ||||
| for item in message.obj_message: | for item in message.obj_message: | ||||
| self.__LoadBufferCase(item) | self.__LoadBufferCase(item) | ||||
| @@ -511,9 +506,16 @@ class Logic(ILogic): | |||||
| formatter = logging.Formatter( | formatter = logging.Formatter( | ||||
| "[%(name)s] [%(asctime)s.%(msecs)03d] [%(levelname)s] %(message)s", '%H:%M:%S') | "[%(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( | fileHandler = logging.FileHandler(os.path.dirname( | ||||
| os.path.dirname(os.path.realpath(__file__))) + "/logs/logic" + str(self.__playerID) + "-log.txt", "w+", encoding="utf-8") | os.path.dirname(os.path.realpath(__file__))) + "/logs/logic" + str(self.__playerID) + "-log.txt", "w+", encoding="utf-8") | ||||
| @@ -59,13 +59,13 @@ | |||||
| 下面的 CellX 和 CellY 指的是地图格数,而非绝对坐标。 | 下面的 CellX 和 CellY 指的是地图格数,而非绝对坐标。 | ||||
| - `THUAI6::PlaceType GetPlaceType(int32_t cellX, int32_t cellY)` :返回某一位置场地种类信息。场地种类详见 structure.h 。 | - `THUAI6::PlaceType GetPlaceType(int32_t cellX, int32_t cellY)` :返回某一位置场地种类信息。场地种类详见 structure.h 。 | ||||
| - `bool IsDoorOpen(int32_t cellX, int32_t cellY) const`:查询特定位置门是否开启,没有门也返回false | |||||
| - 以下指令,若查询物品当前在视野内,则返回最新进度;若物品当前不在视野内、但曾经出现在视野内,则返回最后一次看到时的进度;若物品从未出现在视野内,或查询位置没有对应的物品,则返回 -1。 | - 以下指令,若查询物品当前在视野内,则返回最新进度;若物品当前不在视野内、但曾经出现在视野内,则返回最后一次看到时的进度;若物品从未出现在视野内,或查询位置没有对应的物品,则返回 -1。 | ||||
| - `int32_t GetChestProgress(int32_t cellX, int32_t cellY) const`:查询特定位置箱子开启进度 | - `int32_t GetChestProgress(int32_t cellX, int32_t cellY) const`:查询特定位置箱子开启进度 | ||||
| - `int32_t GetGateProgress(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 GetClassroomProgress(int32_t cellX, int32_t cellY) const`:查询特定位置教室作业完成进度 | ||||
| - `int32_t GetDoorProgress(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<const THUAI6::GameInfo> GetGameInfo() const`:查询当前游戏状态 | - `std::shared_ptr<const THUAI6::GameInfo> GetGameInfo() const`:查询当前游戏状态 | ||||
| @@ -72,13 +72,13 @@ | |||||
| 下面的 CellX 和 CellY 指的是地图格数,而非绝对坐标。 | 下面的 CellX 和 CellY 指的是地图格数,而非绝对坐标。 | ||||
| - `def GetPlaceType(self, cellX: int, cellY: int) -> THUAI6.PlaceType` :返回某一位置场地种类信息。场地种类详见 structure.h 。 | - `def GetPlaceType(self, cellX: int, cellY: int) -> THUAI6.PlaceType` :返回某一位置场地种类信息。场地种类详见 structure.h 。 | ||||
| - `def IsDoorOpen(self, cellX: int, cellY: int) -> bool`:查询特定位置门是否开启,没有门也返回false | |||||
| - 以下指令,若查询物品当前在视野内,则返回最新进度;若物品当前不在视野内、但曾经出现在视野内,则返回最后一次看到时的进度;若物品从未出现在视野内,或查询位置没有对应的物品,则返回 -1。 | - 以下指令,若查询物品当前在视野内,则返回最新进度;若物品当前不在视野内、但曾经出现在视野内,则返回最后一次看到时的进度;若物品从未出现在视野内,或查询位置没有对应的物品,则返回 -1。 | ||||
| - `def GetChestProgress(self, cellX: int, cellY: int) -> int`:查询特定位置箱子开启进度 | - `def GetChestProgress(self, cellX: int, cellY: int) -> int`:查询特定位置箱子开启进度 | ||||
| - `def GetGateProgress(self, cellX: int, cellY: int) -> int`:查询特定位置校门开启进度 | - `def GetGateProgress(self, cellX: int, cellY: int) -> int`:查询特定位置校门开启进度 | ||||
| - `def GetClassroomProgress(self, cellX: int, cellY: int) -> int`:查询特定位置教室作业完成进度 | - `def GetClassroomProgress(self, cellX: int, cellY: int) -> int`:查询特定位置教室作业完成进度 | ||||
| - `def GetDoorProgress(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 | |||||
| #### 其他 | #### 其他 | ||||