Skip to content

Commit

Permalink
Can now send management messages to users from the webui.
Browse files Browse the repository at this point in the history
  • Loading branch information
TLeonardUK committed Feb 1, 2022
1 parent 752260a commit 3c1ad1f
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Source/Server/Server.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ copy $(SolutionDir)..\Resources\steam_appid.txt $(OutputPath)steam_appid.txt</Co
<ClInclude Include="Server\Streams\Frpg2UdpPacket.h" />
<ClInclude Include="Server\Streams\Frpg2UdpPacketStream.h" />
<ClInclude Include="Server\WebUIService\Handlers\AuthHandler.h" />
<ClInclude Include="Server\WebUIService\Handlers\MessageHandler.h" />
<ClInclude Include="Server\WebUIService\Handlers\PlayersHandler.h" />
<ClInclude Include="Server\WebUIService\Handlers\SettingsHandler.h" />
<ClInclude Include="Server\WebUIService\Handlers\StatisticsHandler.h" />
Expand Down Expand Up @@ -252,6 +253,7 @@ copy $(SolutionDir)..\Resources\steam_appid.txt $(OutputPath)steam_appid.txt</Co
<ClCompile Include="Server\Streams\Frpg2ReliableUdpPacketStream.cpp" />
<ClCompile Include="Server\Streams\Frpg2UdpPacketStream.cpp" />
<ClCompile Include="Server\WebUIService\Handlers\AuthHandler.cpp" />
<ClCompile Include="Server\WebUIService\Handlers\MessageHandler.cpp" />
<ClCompile Include="Server\WebUIService\Handlers\PlayersHandler.cpp" />
<ClCompile Include="Server\WebUIService\Handlers\SettingsHandler.cpp" />
<ClCompile Include="Server\WebUIService\Handlers\StatisticsHandler.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions Source/Server/Server.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@
<ClInclude Include="Server\WebUIService\Handlers\SettingsHandler.h">
<Filter>Server\WebUIService\Handlers</Filter>
</ClInclude>
<ClInclude Include="Server\WebUIService\Handlers\MessageHandler.h">
<Filter>Server\WebUIService\Handlers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Server\Server.cpp">
Expand Down Expand Up @@ -489,6 +492,9 @@
<ClCompile Include="Server\WebUIService\Handlers\SettingsHandler.cpp">
<Filter>Server\WebUIService\Handlers</Filter>
</ClCompile>
<ClCompile Include="Server\WebUIService\Handlers\MessageHandler.cpp">
<Filter>Server\WebUIService\Handlers</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="Directory.Build.props" />
Expand Down
25 changes: 25 additions & 0 deletions Source/Server/Server/GameService/GameClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Platform/Platform.h"

#include "Core/Utils/Logging.h"
#include "Core/Utils/Strings.h"
#include "Core/Network/NetConnection.h"

#include "Config/BuildConfig.h"
Expand Down Expand Up @@ -127,4 +128,28 @@ bool GameClient::HandleMessage(const Frpg2ReliableUdpMessage& Message)
std::string GameClient::GetName()
{
return Connection->GetName();
}

void GameClient::SendTextMessage(const std::string& TextMessage)
{
Frpg2RequestMessage::ManagementTextMessage Message;
Message.set_push_message_id(Frpg2RequestMessage::PushID_ManagementTextMessage);
Message.set_unknown_2(TextMessage);
Message.set_unknown_4(0);
Message.set_unknown_5(0);

// Date makes no difference, just hard-code for now.
Frpg2PlayerData::DateTime* DateTime = Message.mutable_unknown_3();
DateTime->set_year(2021);
DateTime->set_month(1);
DateTime->set_day(1);
DateTime->set_hours(0);
DateTime->set_minutes(0);
DateTime->set_seconds(0);
DateTime->set_tzdiff(0);

if (!MessageStream->Send(&Message))
{
WarningS(GetName().c_str(), "Failed to send game client text message.");
}
}
3 changes: 3 additions & 0 deletions Source/Server/Server/GameService/GameClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class GameClient

double GetConnectionDuration() { return GetSeconds() - ConnectTime; }

// Sends a text message displayed at the top of the users screen.
void SendTextMessage(const std::string& Message);

public:

std::shared_ptr<NetConnection> Connection;
Expand Down
71 changes: 71 additions & 0 deletions Source/Server/Server/WebUIService/Handlers/MessageHandler.cpp
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 Source/Server/Server/WebUIService/Handlers/MessageHandler.h
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:

};
2 changes: 2 additions & 0 deletions Source/Server/Server/WebUIService/WebUIService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Server/WebUIService/Handlers/PlayersHandler.h"
#include "Server/WebUIService/Handlers/StatisticsHandler.h"
#include "Server/WebUIService/Handlers/SettingsHandler.h"
#include "Server/WebUIService/Handlers/MessageHandler.h"

#include "Server/Server.h"
#include "Core/Utils/Logging.h"
Expand All @@ -29,6 +30,7 @@ WebUIService::WebUIService(Server* OwningServer)
Handlers.push_back(std::make_shared<PlayersHandler>(this));
Handlers.push_back(std::make_shared<StatisticsHandler>(this));
Handlers.push_back(std::make_shared<SettingsHandler>(this));
Handlers.push_back(std::make_shared<MessageHandler>(this));
}

WebUIService::~WebUIService()
Expand Down
68 changes: 46 additions & 22 deletions Source/WebUI/Static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<meta name="theme-color" content="#263238">
</head>
<body>

<!-- Authentication dialog -->
<dialog class="mdl-dialog authDialog" id="auth-dialog">
<h4 class="mdl-dialog__title">Login</h4>
Expand All @@ -43,6 +44,24 @@ <h4 class="mdl-dialog__title">Login</h4>
<button type="button" id="auth-login-button" class="mdl-button close">Login</button>
</div>
</dialog>

<!-- Send message dialog -->
<dialog class="mdl-dialog authDialog" id="send-message-dialog">
<h4 class="mdl-dialog__title">Send Message</h4>
<div class="mdl-dialog__content">
<p>
This message will be sent and displayed at the top of the players screen for around 15 seconds.
</p>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label fullWidth">
<input class="mdl-textfield__input" type="text" id="send-message-text">
<label class="mdl-textfield__label" for="send-message-text">Message</label>
</div>
</div>
<div class="mdl-dialog__actions">
<button type="button" id="send-message-button" class="mdl-button close">Send</button>
<button type="button" id="cancel-send-message-button" class="mdl-button close">Cancel</button>
</div>
</dialog>

<!-- Main Page -->
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
Expand Down Expand Up @@ -114,29 +133,34 @@ <h4 class="mdl-dialog__title">Login</h4>
<section class="mdl-layout__tab-panel" id="players-tab">
<div class="page-content">

<div class="elementPaddingMargin">
<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp fullwidth">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric">Steam Id</th>
<th class="mdl-data-table__cell--non-numeric">Character Name</th>
<th>Soul Level</th>
<th>Souls</th>
<th>Soul Memory</th>
<th>Death Count</th>
<th>Multiplayer Count</th>
<th>Covenant</th>
<th>Status</th>
<th>Location</th>
<th>Play Time</th>
<th>Connection Time</th>
<th>Options</th>
</tr>
</thead>
<tbody id="players-table-body">
</tbody>
</table>
<div class="elementPaddingMargin" style="padding:0px;">
<div style="text-align:right; padding-bottom: 20px;">
<button class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored" onclick="sendMessageToAllUsers()">
Send All Message
</button>
</div>
<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp fullwidth">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric">Steam Id</th>
<th class="mdl-data-table__cell--non-numeric">Character Name</th>
<th>Soul Level</th>
<th>Souls</th>
<th>Soul Memory</th>
<th>Death Count</th>
<th>Multiplayer Count</th>
<th>Covenant</th>
<th>Status</th>
<th>Location</th>
<th>Play Time</th>
<th>Connection Time</th>
<th>Options</th>
</tr>
</thead>
<tbody id="players-table-body">
</tbody>
</table>
</div>

</div>
</section>
Expand Down
Loading

0 comments on commit 3c1ad1f

Please sign in to comment.