cardMgr.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. void add_edge(uint32_t c1,uint32_t c2)
  17. {
  18. boost::add_edge(c1,c2,m_g);
  19. boost::add_edge(c2,c1,m_g);
  20. }
  21. void clear_vertex(uint32_t c)
  22. {
  23. if(c>boost::num_vertices(m_g)) return;
  24. boost::clear_vertex(c,m_g);
  25. }
  26. void remove_edge(uint32_t c1,uint32_t c2)
  27. {
  28. uint64_t count = boost::num_vertices(m_g);
  29. if(c1>count || c2>count) return;
  30. boost::remove_edge(c1,c2,m_g);
  31. boost::remove_edge(c2,c1,m_g);
  32. }
  33. std::vector<uint32_t> getcard(uint32_t c)
  34. {
  35. std::vector<uint32_t> tmp;
  36. if(c>boost::num_vertices(m_g))return tmp;
  37. boost::graph_traits<myGraph_vv>::adjacency_iterator ai_it, ai_it_end;
  38. boost::tie(ai_it, ai_it_end) = boost::adjacent_vertices(c, m_g);
  39. while(ai_it != ai_it_end)
  40. {
  41. tmp.push_back(*ai_it);
  42. ai_it++;
  43. }
  44. return std::move(tmp);
  45. }
  46. };
  47. enum{
  48. CMD_CLEAR,
  49. CMD_HANDLE
  50. };
  51. struct Msg
  52. {
  53. int cmd;
  54. int x;
  55. int y;
  56. uint64_t cardid;
  57. };
  58. class CardFactory;
  59. struct cardMgr
  60. {
  61. cardMgr();
  62. static cardMgr* instance();
  63. void run();
  64. void onMessage(const Msg &m);
  65. void clear(uint64_t);
  66. void handleMessage(const Msg &m);
  67. void erase(const std::map<uint64_t ,std::string>&);
  68. void erase(uint64_t);
  69. void tryPut(Msg &m)
  70. {
  71. if(!m_queue.tryPut(m))
  72. log_error("m_queue tryPut makes error.");
  73. }
  74. void addVertex(uint64_t c1,uint64_t c2)
  75. {
  76. m_cgraph.add_edge(c1,c2);
  77. }
  78. std::vector<uint64_t> getcard(uint64_t cid)
  79. {
  80. std::vector<uint64_t> tmpv;
  81. auto v= m_cgraph.getcard(cid);
  82. std::for_each(v.begin(),v.end(),[cid,&tmpv](uint32_t cardid){uint64_t id=(cid>>32<<32)|cardid;tmpv.push_back(id);});
  83. return std::move(tmpv);
  84. }
  85. void remove_edge(uint64_t c1, uint64_t c2)
  86. {
  87. m_cgraph.remove_edge(c1,c2);
  88. }
  89. ~cardMgr();
  90. private:
  91. BoundedQueue<Msg> m_queue;
  92. bool m_bstop;
  93. geo_list m_glist;
  94. card_graph m_cgraph;
  95. std::unique_ptr<std::thread> m_pThread;
  96. std::array<std::shared_ptr<CardFactory>,2> m_cf;
  97. };
  98. #endif