1
0

Web administration now has /setadminstatus support

This commit is contained in:
Emagi 2024-08-18 07:44:57 -04:00
parent 8d09f4e043
commit 4d659a2601
3 changed files with 46 additions and 0 deletions

View File

@ -1,4 +1,5 @@
#include "../World.h" #include "../World.h"
#include "../WorldDatabase.h"
#include "../LoginServer.h" #include "../LoginServer.h"
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
@ -9,6 +10,7 @@ extern ZoneList zone_list;
extern World world; extern World world;
extern LoginServer loginserver; extern LoginServer loginserver;
extern sint32 numclients; extern sint32 numclients;
extern WorldDatabase database;
void World::Web_worldhandle_status(const http::request<http::string_body>& req, http::response<http::string_body>& res) { void World::Web_worldhandle_status(const http::request<http::string_body>& req, http::response<http::string_body>& res) {
res.set(http::field::content_type, "application/json"); res.set(http::field::content_type, "application/json");
@ -65,6 +67,7 @@ void ZoneList::PopulateClientList(http::response<http::string_body>& res) {
pt.put("account_age", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_account_age_base() : 0); pt.put("account_age", cur->GetPlayer() ? cur->GetPlayer()->GetInfoStruct()->get_account_age_base() : 0);
pt.put("account_id", cur->GetAccountID()); pt.put("account_id", cur->GetAccountID());
pt.put("version", cur->GetVersion()); pt.put("version", cur->GetVersion());
pt.put("status", cur->GetAdminStatus());
pt.put("is_zoning", (cur->IsZoning() || !cur->IsReadyForUpdates())); pt.put("is_zoning", (cur->IsZoning() || !cur->IsReadyForUpdates()));
bool linkdead = cur->GetPlayer() ? (((cur->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) > 0)) : false; bool linkdead = cur->GetPlayer() ? (((cur->GetPlayer()->GetActivityStatus() & ACTIVITY_STATUS_LINKDEAD) > 0)) : false;
@ -84,3 +87,44 @@ void ZoneList::PopulateClientList(http::response<http::string_body>& res) {
res.prepare_payload(); res.prepare_payload();
} }
void World::Web_worldhandle_setadminstatus(const http::request<http::string_body>& req, http::response<http::string_body>& res) {
res.set(http::field::content_type, "application/json");
boost::property_tree::ptree pt, json_tree;
std::istringstream json_stream(req.body());
boost::property_tree::read_json(json_stream, json_tree);
sint16 status = 0;
std::string charname("");
bool got_status_field = false;
if (auto name = json_tree.get_optional<std::string>("character_name")) {
charname = name.get();
}
if (auto new_status = json_tree.get_optional<sint16>("new_status")) {
status = new_status.get();
got_status_field = true;
}
sint32 success = 0;
if(got_status_field && charname.size() > 0 && database.UpdateAdminStatus((char*)charname.c_str(),status)) {
Client* target = zone_list.GetClientByCharName(charname.c_str());
if (target) {
target->SetAdminStatus(status);
target->Message(CHANNEL_COLOR_YELLOW, "Your admin status has been set to %i.", status);
}
success = 1;
}
else if(!got_status_field || charname.size() < 1) {
success = -1;
}
pt.put("success", success);
std::ostringstream oss;
boost::property_tree::write_json(oss, pt);
std::string json = oss.str();
res.body() = json;
res.prepare_payload();
}

View File

@ -245,6 +245,7 @@ void World::init(std::string web_ipaddr, int16 web_port, std::string cert_file,
world_webserver->register_route("/status", World::Web_worldhandle_status); world_webserver->register_route("/status", World::Web_worldhandle_status);
world_webserver->register_route("/clients", World::Web_worldhandle_clients); world_webserver->register_route("/clients", World::Web_worldhandle_clients);
world_webserver->register_route("/setadminstatus", World::Web_worldhandle_setadminstatus);
world_webserver->run(); world_webserver->run();
LogWrite(INIT__INFO, 0, "Init", "World Web Server is listening on %s:%u..", web_ipaddr.c_str(), web_port); LogWrite(INIT__INFO, 0, "Init", "World Web Server is listening on %s:%u..", web_ipaddr.c_str(), web_port);
} }

View File

@ -651,6 +651,7 @@ public:
static void Web_worldhandle_status(const http::request<http::string_body>& req, http::response<http::string_body>& res); static void Web_worldhandle_status(const http::request<http::string_body>& req, http::response<http::string_body>& res);
static void Web_worldhandle_clients(const http::request<http::string_body>& req, http::response<http::string_body>& res); static void Web_worldhandle_clients(const http::request<http::string_body>& req, http::response<http::string_body>& res);
static void Web_worldhandle_setadminstatus(const http::request<http::string_body>& req, http::response<http::string_body>& res);
Mutex MVoiceOvers; Mutex MVoiceOvers;