WorldWeb.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "../World.h"
  2. #include "../WorldDatabase.h"
  3. #include "../LoginServer.h"
  4. #include <boost/property_tree/ptree.hpp>
  5. #include <boost/property_tree/json_parser.hpp>
  6. #include <sstream>
  7. extern ZoneList zone_list;
  8. extern World world;
  9. extern LoginServer loginserver;
  10. extern sint32 numclients;
  11. extern WorldDatabase database;
  12. void World::Web_worldhandle_status(const http::request<http::string_body>& req, http::response<http::string_body>& res) {
  13. res.set(http::field::content_type, "application/json");
  14. boost::property_tree::ptree pt;
  15. pt.put("web_status", "online");
  16. bool world_online = world.world_loaded;
  17. pt.put("world_status", world.world_loaded ? "online" : "offline");
  18. pt.put("world_uptime", (getCurrentTimestamp() - world.world_uptime));
  19. auto [days, hours, minutes, seconds] = convertTimestampDuration((getCurrentTimestamp() - world.world_uptime));
  20. std::string uptime_str("Days: " + std::to_string(days) + ", " + "Hours: " + std::to_string(hours) + ", " + "Minutes: " + std::to_string(minutes) + ", " + "Seconds: " + std::to_string(seconds));
  21. pt.put("world_uptime_string", uptime_str);
  22. pt.put("login_connected", loginserver.Connected() ? "connected" : "disconnected");
  23. pt.put("player_count", zone_list.GetZonesPlayersCount());
  24. pt.put("client_count", numclients);
  25. pt.put("zones_connected", zone_list.Count());
  26. pt.put("world_reloading", world.IsReloadingSubsystems() ? "yes" : "no");
  27. std::ostringstream oss;
  28. boost::property_tree::write_json(oss, pt);
  29. std::string json = oss.str();
  30. res.body() = json;
  31. res.prepare_payload();
  32. }
  33. void World::Web_worldhandle_clients(const http::request<http::string_body>& req, http::response<http::string_body>& res) {
  34. zone_list.PopulateClientList(res);
  35. }
  36. void ZoneList::PopulateClientList(http::response<http::string_body>& res) {
  37. res.set(http::field::content_type, "application/json");
  38. boost::property_tree::ptree maintree;
  39. std::ostringstream oss;
  40. MClientList.lock();
  41. map<string,Client*>::iterator itr;
  42. for(itr = client_map.begin(); itr != client_map.end(); itr++){
  43. if(itr->second){
  44. Client* cur = (Client*)itr->second;
  45. boost::property_tree::ptree pt;
  46. pt.put("character_id", cur->GetCharacterID());
  47. pt.put("character_name", cur->GetPlayer() ? cur->GetPlayer()->GetName() : "N/A");
  48. pt.put("class1", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_class1() : 0);
  49. pt.put("class2", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_class2() : 0);
  50. pt.put("class3", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_class3() : 0);
  51. pt.put("tradeskill_class1", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_tradeskill_class1() : 0);
  52. pt.put("tradeskill_class2", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_tradeskill_class2() : 0);
  53. pt.put("tradeskill_class3", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_tradeskill_class3() : 0);
  54. pt.put("race", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_race() : 0);
  55. pt.put("level", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_level() : 0);
  56. pt.put("effective_level", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_effective_level() : 0);
  57. pt.put("tradeskill_level", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_tradeskill_level() : 0);
  58. pt.put("account_age", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_account_age_base() : 0);
  59. pt.put("account_id", cur->GetAccountID());
  60. pt.put("version", cur->GetVersion());
  61. pt.put("status", cur->GetAdminStatus());
  62. pt.put("is_zoning", (cur->IsZoning() || !cur->IsReadyForUpdates()));
  63. bool linkdead = cur->GetPlayer() ? (((cur->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) > 0)) : false;
  64. pt.put("is_linkdead", linkdead);
  65. pt.put("in_zone", cur->IsReadyForUpdates());
  66. pt.put("zonename", (cur->GetPlayer() && cur->GetPlayer()->GetZone()) ? cur->GetPlayer()->GetZone()->GetZoneName() : "N/A");
  67. maintree.push_back(std::make_pair("", pt));
  68. }
  69. }
  70. MClientList.unlock();
  71. boost::property_tree::ptree result;
  72. result.add_child("Clients", maintree);
  73. boost::property_tree::write_json(oss, result);
  74. std::string json = oss.str();
  75. res.body() = json;
  76. res.prepare_payload();
  77. }
  78. void World::Web_worldhandle_setadminstatus(const http::request<http::string_body>& req, http::response<http::string_body>& res) {
  79. res.set(http::field::content_type, "application/json");
  80. boost::property_tree::ptree pt, json_tree;
  81. std::istringstream json_stream(req.body());
  82. boost::property_tree::read_json(json_stream, json_tree);
  83. sint16 status = 0;
  84. std::string charname("");
  85. bool got_status_field = false;
  86. if (auto name = json_tree.get_optional<std::string>("character_name")) {
  87. charname = name.get();
  88. }
  89. if (auto new_status = json_tree.get_optional<sint16>("new_status")) {
  90. status = new_status.get();
  91. got_status_field = true;
  92. }
  93. sint32 success = 0;
  94. if(got_status_field && charname.size() > 0 && database.UpdateAdminStatus((char*)charname.c_str(),status)) {
  95. Client* target = zone_list.GetClientByCharName(charname.c_str());
  96. if (target) {
  97. target->SetAdminStatus(status);
  98. target->Message(CHANNEL_COLOR_YELLOW, "Your admin status has been set to %i.", status);
  99. }
  100. success = 1;
  101. }
  102. else if(!got_status_field || charname.size() < 1) {
  103. success = -1;
  104. }
  105. pt.put("success", success);
  106. std::ostringstream oss;
  107. boost::property_tree::write_json(oss, pt);
  108. std::string json = oss.str();
  109. res.body() = json;
  110. res.prepare_payload();
  111. }