cardMgr.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #ifndef __INCLUDE_CARD_MGR__
  2. #define __INCLUDE_CARD_MGR__
  3. #include <memory>
  4. #include <thread>
  5. #include <boost/config.hpp>
  6. #include <boost/graph/adjacency_list.hpp>
  7. #include <boost/archive/text_oarchive.hpp>
  8. #include <boost/graph/vector_as_graph.hpp>
  9. #include "tqueue.h"
  10. #include "log.h"
  11. #include "geo_hash.h"
  12. struct card_graph
  13. {
  14. typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::bidirectionalS> myGraph_vv;
  15. myGraph_vv m_g;
  16. uint32_t _count=0;
  17. void add_edge(uint32_t c1,uint32_t c2)
  18. {
  19. log_info("add_edge:%d,%d",c1,c2);
  20. boost::add_edge(c1,c2,m_g);
  21. boost::add_edge(c2,c1,m_g);
  22. _count=boost::num_vertices(m_g);
  23. }
  24. void clear_vertex(uint32_t c)
  25. {
  26. if(c>=_count) return;
  27. boost::clear_vertex(c,m_g);
  28. }
  29. void remove_edge(uint32_t c1,uint32_t c2)
  30. {
  31. if(c1>=_count || c2>=_count) return;
  32. boost::remove_edge(c1,c2,m_g);
  33. boost::remove_edge(c2,c1,m_g);
  34. }
  35. std::vector<uint32_t> getcard(uint32_t c)
  36. {
  37. std::vector<uint32_t> tmp;
  38. if(c>=_count)return tmp;
  39. boost::graph_traits<myGraph_vv>::adjacency_iterator ai_it, ai_it_end;
  40. boost::tie(ai_it, ai_it_end) = boost::adjacent_vertices(c, m_g);
  41. while(ai_it != ai_it_end)
  42. {
  43. log_info("get_card%d:",c);
  44. tmp.push_back(*ai_it);
  45. ai_it++;
  46. }
  47. return std::move(tmp);
  48. }
  49. };
  50. enum{
  51. CMD_CLEAR,
  52. CMD_HANDLE
  53. };
  54. struct Msg
  55. {
  56. int cmd;
  57. int x;
  58. int y;
  59. uint64_t cardid;
  60. int type;
  61. };
  62. class CardFactory;
  63. struct cardMgr
  64. {
  65. static cardMgr* instance();
  66. void run();
  67. void onMessage(const Msg &m);
  68. void clear(uint64_t);
  69. void handleMessage(const Msg &m);
  70. void erase(const std::map<uint64_t ,std::string>&);
  71. void erase(uint64_t);
  72. void tryPut(Msg &m)
  73. {
  74. if(!m_queue.tryPut(m))
  75. log_error("m_queue tryPut makes error.");
  76. }
  77. void addVertex(uint64_t c1,uint64_t c2)
  78. {
  79. m_cgraph.add_edge(c1,c2);
  80. }
  81. std::vector<uint64_t> getcard(uint64_t cid)
  82. {
  83. std::vector<uint64_t> tmpv;
  84. auto v= m_cgraph.getcard(cid);
  85. std::for_each(v.begin(),v.end(),[cid,&tmpv](uint32_t cardid){uint64_t id=(cid>>32<<32)|cardid;tmpv.push_back(id);});
  86. return std::move(tmpv);
  87. }
  88. void remove_edge(uint64_t c1, uint64_t c2)
  89. {
  90. m_cgraph.remove_edge(c1,c2);
  91. }
  92. ~cardMgr();
  93. private:
  94. cardMgr();
  95. BoundedQueue<Msg> m_queue;
  96. bool m_bstop;
  97. geo_list m_glist;
  98. card_graph m_cgraph;
  99. std::unique_ptr<std::thread> m_pThread;
  100. std::array<std::shared_ptr<CardFactory>,2> m_cf;
  101. };
  102. #endif