LoginWeb.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "../net.h"
  2. #include "../LWorld.h"
  3. #include <boost/algorithm/string.hpp>
  4. #include <boost/property_tree/ptree.hpp>
  5. #include <boost/property_tree/json_parser.hpp>
  6. extern ClientList client_list;
  7. extern LWorldList world_list;
  8. extern NetConnection net;
  9. void NetConnection::Web_loginhandle_status(const http::request<http::string_body>& req, http::response<http::string_body>& res) {
  10. res.set(http::field::content_type, "application/json");
  11. boost::property_tree::ptree pt;
  12. pt.put("web_status", "online");
  13. pt.put("login_status", net.login_running ? "online" : "offline");
  14. pt.put("login_uptime", (getCurrentTimestamp() - net.login_uptime));
  15. auto [days, hours, minutes, seconds] = convertTimestampDuration((getCurrentTimestamp() - net.login_uptime));
  16. std::string uptime_str("Days: " + std::to_string(days) + ", " + "Hours: " + std::to_string(hours) + ", " + "Minutes: " + std::to_string(minutes) + ", " + "Seconds: " + std::to_string(seconds));
  17. pt.put("login_uptime_string", uptime_str);
  18. pt.put("world_count", world_list.GetCount(ConType::World));
  19. pt.put("client_count", net.numclients);
  20. std::ostringstream oss;
  21. boost::property_tree::write_json(oss, pt);
  22. std::string json = oss.str();
  23. res.body() = json;
  24. res.prepare_payload();
  25. }
  26. void NetConnection::Web_loginhandle_worlds(const http::request<http::string_body>& req, http::response<http::string_body>& res) {
  27. world_list.PopulateWorldList(res);
  28. }
  29. void LWorldList::PopulateWorldList(http::response<http::string_body>& res) {
  30. struct in_addr in;
  31. res.set(http::field::content_type, "application/json");
  32. boost::property_tree::ptree maintree;
  33. std::ostringstream oss;
  34. map<int32,LWorld*>::iterator map_list;
  35. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  36. LWorld* world = map_list->second;
  37. in.s_addr = world->GetIP();
  38. if (world->GetType() == World) {
  39. boost::property_tree::ptree pt;
  40. pt.put("id", world->GetID());
  41. pt.put("world_name", world->GetName());
  42. pt.put("status", (world->GetStatus() == 1) ? "online" : "offline");
  43. pt.put("ip_addr", inet_ntoa(in));
  44. maintree.add_child("WorldServer", pt);
  45. }
  46. }
  47. boost::property_tree::write_json(oss, maintree);
  48. std::string json = oss.str();
  49. res.body() = json;
  50. res.prepare_payload();
  51. }