9
3

WorldWeb.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "../World.h"
  2. #include "../LoginServer.h"
  3. #include <boost/property_tree/ptree.hpp>
  4. #include <boost/property_tree/json_parser.hpp>
  5. #include <sstream>
  6. extern ZoneList zone_list;
  7. extern World world;
  8. extern LoginServer loginserver;
  9. extern sint32 numclients;
  10. void World::Web_worldhandle_status(const http::request<http::string_body>& req, http::response<http::string_body>& res) {
  11. res.set(http::field::content_type, "application/json");
  12. boost::property_tree::ptree pt;
  13. pt.put("web_status", "online");
  14. bool world_online = world.world_loaded;
  15. pt.put("world_status", world.world_loaded ? "online" : "offline");
  16. pt.put("world_uptime", (getCurrentTimestamp() - world.world_uptime));
  17. auto [days, hours, minutes, seconds] = convertTimestampDuration((getCurrentTimestamp() - world.world_uptime));
  18. std::string uptime_str("Days: " + std::to_string(days) + ", " + "Hours: " + std::to_string(hours) + ", " + "Minutes: " + std::to_string(minutes) + ", " + "Seconds: " + std::to_string(seconds));
  19. pt.put("world_uptime_string", uptime_str);
  20. pt.put("login_connected", loginserver.Connected() ? "connected" : "disconnected");
  21. pt.put("player_count", zone_list.GetZonesPlayersCount());
  22. pt.put("client_count", numclients);
  23. pt.put("zones_connected", zone_list.Count());
  24. pt.put("world_reloading", world.IsReloadingSubsystems() ? "yes" : "no");
  25. std::ostringstream oss;
  26. boost::property_tree::write_json(oss, pt);
  27. std::string json = oss.str();
  28. res.body() = json;
  29. res.prepare_payload();
  30. }
  31. void World::Web_worldhandle_clients(const http::request<http::string_body>& req, http::response<http::string_body>& res) {
  32. zone_list.PopulateClientList(res);
  33. }
  34. void ZoneList::PopulateClientList(http::response<http::string_body>& res) {
  35. res.set(http::field::content_type, "application/json");
  36. boost::property_tree::ptree maintree;
  37. std::ostringstream oss;
  38. MClientList.lock();
  39. map<string,Client*>::iterator itr;
  40. for(itr = client_map.begin(); itr != client_map.end(); itr++){
  41. if(itr->second){
  42. Client* cur = (Client*)itr->second;
  43. boost::property_tree::ptree pt;
  44. pt.put("character_id", cur->GetCharacterID());
  45. pt.put("character_name", cur->GetPlayer() ? cur->GetPlayer()->GetName() : "N/A");
  46. pt.put("class1", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_class1() : 0);
  47. pt.put("class2", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_class2() : 0);
  48. pt.put("class3", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_class3() : 0);
  49. pt.put("tradeskill_class1", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_tradeskill_class1() : 0);
  50. pt.put("tradeskill_class2", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_tradeskill_class2() : 0);
  51. pt.put("tradeskill_class3", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_tradeskill_class3() : 0);
  52. pt.put("race", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_race() : 0);
  53. pt.put("level", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_level() : 0);
  54. pt.put("effective_level", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_effective_level() : 0);
  55. pt.put("tradeskill_level", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_tradeskill_level() : 0);
  56. pt.put("account_age", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_account_age_base() : 0);
  57. pt.put("account_id", cur->GetAccountID());
  58. pt.put("version", cur->GetVersion());
  59. pt.put("is_zoning", (cur->IsZoning() || !cur->IsReadyForUpdates()));
  60. bool linkdead = cur->GetPlayer() ? (((cur->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) > 0)) : false;
  61. pt.put("is_linkdead", linkdead);
  62. pt.put("in_zone", cur->IsReadyForUpdates());
  63. pt.put("zonename", (cur->GetPlayer() && cur->GetPlayer()->GetZone()) ? cur->GetPlayer()->GetZone()->GetZoneName() : "N/A");
  64. maintree.add_child("Client", pt);
  65. }
  66. }
  67. MClientList.unlock();
  68. boost::property_tree::write_json(oss, maintree);
  69. std::string json = oss.str();
  70. res.body() = json;
  71. res.prepare_payload();
  72. }