forked from TLeonardUK/ds3os
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can now send management messages to users from the webui.
- Loading branch information
1 parent
752260a
commit 3c1ad1f
Showing
9 changed files
with
277 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
Source/Server/Server/WebUIService/Handlers/MessageHandler.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Dark Souls 3 - Open Server | ||
* Copyright (C) 2021 Tim Leonard | ||
* | ||
* This program is free software; licensed under the MIT license. | ||
* You should have received a copy of the license along with this program. | ||
* If not, see <https://opensource.org/licenses/MIT>. | ||
*/ | ||
|
||
#include "Server/Server.h" | ||
#include "Server/GameService/GameService.h" | ||
#include "Server/GameService/GameClient.h" | ||
#include "Server/WebUIService/Handlers/MessageHandler.h" | ||
#include "Server/Core/Network/NetConnection.h" | ||
|
||
#include "Core/Utils/Logging.h" | ||
#include "Core/Utils/Strings.h" | ||
|
||
MessageHandler::MessageHandler(WebUIService* InService) | ||
: WebUIHandler(InService) | ||
{ | ||
} | ||
|
||
void MessageHandler::Register(CivetServer* Server) | ||
{ | ||
Server->addHandler("/message", this); | ||
} | ||
|
||
bool MessageHandler::handlePost(CivetServer* Server, struct mg_connection* Connection) | ||
{ | ||
if (!Service->IsAuthenticated(Connection)) | ||
{ | ||
mg_send_http_error(Connection, 401, "Token invalid."); | ||
return true; | ||
} | ||
|
||
nlohmann::json json; | ||
if (!ReadJson(Server, Connection, json) || | ||
!json.contains("playerId") || | ||
!json.contains("message")) | ||
{ | ||
mg_send_http_error(Connection, 400, "Malformed body."); | ||
return true; | ||
} | ||
|
||
uint32_t playerId = json["playerId"]; | ||
std::string message = json["message"]; | ||
|
||
std::shared_ptr<GameService> Game = Service->GetServer()->GetService<GameService>(); | ||
if (playerId == 0) | ||
{ | ||
LogS("WebUI", "Sending message to all players: %s", message.c_str()); | ||
for (auto Client : Game->GetClients()) | ||
{ | ||
Client->SendTextMessage(message); | ||
} | ||
} | ||
else | ||
{ | ||
if (std::shared_ptr<GameClient> Client = Game->FindClientByPlayerId(playerId)) | ||
{ | ||
LogS("WebUI", "Sending message to %s: %s", Client->GetName().c_str(), message.c_str()); | ||
Client->SendTextMessage(message); | ||
} | ||
} | ||
|
||
nlohmann::json responseJson; | ||
RespondJson(Connection, responseJson); | ||
|
||
return true; | ||
} |
32 changes: 32 additions & 0 deletions
32
Source/Server/Server/WebUIService/Handlers/MessageHandler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Dark Souls 3 - Open Server | ||
* Copyright (C) 2021 Tim Leonard | ||
* | ||
* This program is free software; licensed under the MIT license. | ||
* You should have received a copy of the license along with this program. | ||
* If not, see <https://opensource.org/licenses/MIT>. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "Server/WebUIService/Handlers/WebUIHandler.h" | ||
#include "Server/GameService/PlayerState.h" | ||
|
||
#include <mutex> | ||
|
||
// /message | ||
// | ||
// POST - Sends a message to a player (or all players) | ||
|
||
class MessageHandler : public WebUIHandler | ||
{ | ||
public: | ||
MessageHandler(WebUIService* InService); | ||
|
||
virtual bool handlePost(CivetServer* Server, struct mg_connection* Connection) override; | ||
|
||
virtual void Register(CivetServer* Server) override; | ||
|
||
protected: | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.