You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

logic.h 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #ifndef LOGIC_H
  3. #define LOGIC_H
  4. #ifdef _MSC_VER
  5. #pragma warning(disable : 4996)
  6. #endif
  7. #include <iostream>
  8. #include <vector>
  9. #include <thread>
  10. #include <mutex>
  11. #include <condition_variable>
  12. #include <atomic>
  13. #include <Message2Server.pb.h>
  14. #include <Message2Clients.pb.h>
  15. #include <MessageType.pb.h>
  16. #include <Message2Server.grpc.pb.h>
  17. #include <Message2Clients.grpc.pb.h>
  18. #include <MessageType.grpc.pb.h>
  19. #include "API.h"
  20. #include "AI.h"
  21. // 封装了通信组件和对AI对象进行操作
  22. class Logic : public ILogic
  23. {
  24. private:
  25. // gRPC客户端的stub,所有与服务端之间的通信操作都需要基于stub完成。
  26. std::unique_ptr<Protobuf::AvailableService::Stub> THUAI6Stub;
  27. // ID、阵营记录
  28. int playerID;
  29. THUAI6::PlayerType playerType;
  30. // 类型记录
  31. THUAI6::HumanType humanType;
  32. THUAI6::ButcherType butcherType;
  33. // GUID信息
  34. std::vector<int64_t> playerGUIDs;
  35. // THUAI5中的通信组件可以完全被我们的stub取代,故无须再写
  36. std::unique_ptr<IAI> pAI;
  37. std::shared_ptr<IAPIForLogic> pAPI;
  38. std::thread tAI;
  39. mutable std::mutex mtxAI;
  40. mutable std::mutex mtxState;
  41. mutable std::mutex mtxBuffer;
  42. std::condition_variable cvBuffer;
  43. std::condition_variable cvAI;
  44. // 信息队列目前可能会不用?具体待定
  45. // 存储状态,分别是现在的状态和缓冲区的状态。
  46. State state[2];
  47. State* currentState;
  48. State* bufferState;
  49. // 是否应该执行player()
  50. std::atomic_bool AILoop = true;
  51. // buffer是否更新完毕
  52. bool bufferUpdated = true;
  53. // 是否可以启用当前状态
  54. bool currentStateAccessed = false;
  55. // 是否应当启动AI
  56. bool AIStart = false;
  57. // 控制内容更新的变量
  58. std::atomic_bool freshed = false;
  59. // 所有API中声明的函数都需要在此处重写,先暂时略过,等到之后具体实现时再考虑
  60. // 执行AI线程
  61. void PlayerWrapper(std::function<void()> player);
  62. // THUAI5中的一系列用于处理信息的函数可能也不会再用
  63. // 将信息加载到buffer
  64. void LoadBuffer(std::shared_ptr<Protobuf::MessageToClient>);
  65. // 解锁状态更新线程
  66. void UnBlockBuffer();
  67. // 解锁AI线程
  68. void UnBlockAI();
  69. // 更新状态
  70. void Update() noexcept;
  71. // 等待
  72. void Wait() noexcept;
  73. public:
  74. // 构造函数还需要传更多参数,有待补充
  75. Logic(std::shared_ptr<grpc::Channel> channel);
  76. ~Logic() = default;
  77. // Main函数同上
  78. void Main();
  79. };
  80. #endif