test.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "logger.hpp"
  2. #include "util.hpp"
  3. #include "db.hpp"
  4. #include "online.hpp"
  5. #include "room.hpp"
  6. #include "session.hpp"
  7. #include "matcher.hpp"
  8. using std::cout;
  9. using std::endl;
  10. #define HOST "127.0.0.1"
  11. #define USER "thj"
  12. #define PASSWD "Abcd1234@"
  13. #define DB "test_connection"
  14. #define PORT 4106
  15. #define FILENAME "./makefile"
  16. void log_test()
  17. {
  18. LOG(NORMAL, "%s-%d", "hello", 10);
  19. LOG(DEBUG, "hello world");
  20. }
  21. void mysql_test()
  22. {
  23. MYSQL *mysql = mysql_util::mysql_create(HOST, USER, PASSWD, DB, PORT);
  24. const std::string sql = "insert into user values(null, '王五', 25, '11133334444')";
  25. mysql_util::mysql_execute(mysql, sql);
  26. mysql_util::mysql_destroy(mysql);
  27. }
  28. void json_test()
  29. {
  30. Json::Value root;
  31. root["姓名"] = "小明";
  32. root["年龄"] = 18;
  33. root["成绩"].append(80);
  34. root["成绩"].append(90);
  35. root["成绩"].append(100);
  36. std::string str;
  37. json_util::serialize(root, str);
  38. cout << str << endl;
  39. Json::Value newroot;
  40. json_util::deserialize(str, newroot);
  41. cout << "姓名: " << newroot["姓名"].asString() << endl;
  42. cout << "年龄: " << newroot["年龄"].asInt() << endl;
  43. int size = newroot["成绩"].size();
  44. for(int i = 0; i < size; i++)
  45. {
  46. cout << "成绩: " << newroot["成绩"][i].asFloat() << endl;
  47. }
  48. }
  49. void string_test()
  50. {
  51. std::string src = " abc dfef fe";
  52. std::string sep = " ";
  53. std::vector<std::string> res;
  54. string_util::split(src, sep, res);
  55. for(auto s : res)
  56. cout << s << endl;
  57. cout << endl;
  58. }
  59. void fileRead_test()
  60. {
  61. std::string data;
  62. file_util::read(FILENAME, data);
  63. cout << data << endl;
  64. }
  65. void db_test()
  66. {
  67. user_table ut(HOST, USER, PASSWD);
  68. Json::Value user;
  69. user["username"] = "zhangsan";
  70. user["password"] = "123456";
  71. // register
  72. // ut.registers(user);
  73. // user["username"] = "lisi";
  74. // user["password"] = "123321";
  75. // ut.registers(user);
  76. // login
  77. // ut.login(user);
  78. // user["password"] = "123321";
  79. // ut.login(user);
  80. // win and lose
  81. ut.win(1);
  82. ut.lose(2);
  83. // select
  84. Json::Value data;
  85. if(ut.select_by_name("zhangsan", data)) {
  86. std::string msg;
  87. json_util::serialize(data, msg);
  88. LOG(DEBUG, "%s", msg.c_str());
  89. }
  90. if(ut.select_by_id(2, data)) {
  91. std::string msg;
  92. json_util::serialize(data, msg);
  93. LOG(DEBUG, "%s", msg.c_str());
  94. }
  95. }
  96. void online_test()
  97. {
  98. uint64_t id = 1;
  99. Json::Value user;
  100. user_table ut(HOST, USER, PASSWD);
  101. ut.select_by_id(id, user);
  102. std::string name = user["username"].asString();
  103. online_manager om;
  104. wsserver_t::connection_ptr conn;
  105. om.enter_game_hall(id, conn);
  106. if(om.is_in_game_hall(id)) LOG(DEBUG, "%s enter game hall", name.c_str());
  107. om.enter_game_room(id, conn);
  108. if(om.is_in_game_room(id)) LOG(DEBUG, "%s enter game room", name.c_str());
  109. om.exit_game_room(id);
  110. if(om.is_in_game_room(id) == false) LOG(DEBUG, "%s exit game room", name.c_str());
  111. om.exit_game_hall(id);
  112. if(om.is_in_game_hall(id) == false) LOG(DEBUG, "%s exit game hall", name.c_str());
  113. }
  114. void room_test() {
  115. // 游戏房间
  116. user_table ut(HOST, USER, PASSWD);
  117. online_manager om;
  118. // room r(10, &ut, &om);
  119. // 游戏房间管理
  120. room_manager rm(&ut, &om);
  121. room_ptr rp = rm.create_room(10, 20);
  122. }
  123. void matcher_test() {
  124. user_table ut(HOST, USER, PASSWD);
  125. online_manager om;
  126. room_manager rm(&ut, &om);
  127. matcher mt(&ut, &om, &rm);
  128. }
  129. int main()
  130. {
  131. // log_test();
  132. // mysql_test();
  133. // json_test();
  134. // string_test();
  135. // fileRead_test();
  136. // db_test();
  137. // online_test();
  138. // room_test();
  139. matcher_test();
  140. return 0;
  141. }