mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-02 19:03:43 +00:00
Every packet of the chat service is now an LUBitStream struct in dNet/ChatPackets.h (docs/PacketArchitecture.md, PR 13), and nothing in the chat server or the chat side of the world server reads or writes a packet by hand any more. - World <-> chat: LoginSessionNotify, UnexpectedDisconnect, GMLevelUpdate, GMMute, Announcement (GM announce), CreateTeam, TeamUpdate (TEAM_GET_STATUS to worlds), AchievementNotify, ShowAllRequest, FindPlayerRequest. - Client -> world -> chat (friends, ignore list, teams, general and private chat): GetFriendsList, AddFriendRequest, AddFriendResponse, RemoveFriend, GetIgnoreList, AddIgnore, RemoveIgnore, GeneralChatMessage, PrivateChatMessage, TeamInvite, TeamInviteResponse, TeamLeave, TeamKick, TeamSetLeader, TeamSetLoot, TeamGetStatus. The 77 bytes the handlers skipped are named fields now (the sender block the client fills in); the 4 unused bytes after the player ID are kept as `unknown`. - What the client receives. Chat service (ChatPackets::Client): GeneralChatMessage (replaces SendChatMessage; SendSystemMessage stays as a helper built on it), PrivateChatMessage. Client service (ClientPackets, as structs go in the file of the ServiceType in their header): SendCannedText (replaces SendMessageFail), GetFriendsListResponse, AddFriendRequest, AddFriendResponse, RemoveFriendResponse, UpdateFriendNotify, WhoResponse, ShowAllResponse, Get/Add/RemoveIgnoreResponse, TeamInvite, TeamInviteInitialResponse, and the team game messages chat writes (TeamInviteConfirm, TeamGetStatusResponse, TeamSetLeader, TeamAddPlayer, TeamRemovePlayer, TeamSetOffWorldFlag) as TeamGameMsg structs, since chat doesn't link dGame's NetGameMsg. - WORLD_ROUTE_PACKET is ChatPackets::WorldRoutePacket (its own file, dNet/WorldRoutePacket.h, since it carries packets of other services): the target and the inner packet. ChatPacketHandler::SendRouted replaces the per-function route headers and SendRoutedMsg. Dispatch: dNet/PacketDispatcher.h is a dispatch map from packet ID to (struct, handler function); packets that fail to Deserialize are logged and dropped. The chat server's switch and the world's HandlePacketChat switch are now maps. The handlers take the structs; their logic is unchanged. World code sends to chat with ChatServerLink::Send (dGame) instead of writing to Game::chatServer by hand. eChatChannel, eChatMessageResponseCode and eAddIgnoreResponse moved to dCommon/dEnums so the structs can use them. Verified: tests/dGameTests/dNetTests/Legacy/ChatPacketsLegacy.h is a verbatim copy of the old senders and readers; ChatPacketsTests (25 tests) sends a grid of inputs through both and requires identical bits, bytes and destination, checks the old readers read what the structs write, round trips every struct, checks truncated packets are refused, and has hand written golden packets for general chat, canned text, GM mute and a routed team game message. Breaking one field width (UpdateFriendNotify) and the TeamAddPlayer zone flag made the tests fail. No wire bytes changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
139 lines
4.9 KiB
C++
139 lines
4.9 KiB
C++
#include "ChatIgnoreList.h"
|
|
#include "PlayerContainer.h"
|
|
#include "MessageType/Chat.h"
|
|
#include "BitStreamUtils.h"
|
|
#include "Game.h"
|
|
#include "dConfig.h"
|
|
#include "Logger.h"
|
|
#include "eObjectBits.h"
|
|
|
|
#include "Database.h"
|
|
#include "ChatPacketHandler.h"
|
|
|
|
// A note to future readers, The client handles all the actual ignoring logic:
|
|
// not allowing teams, rejecting DMs, friends requets etc.
|
|
// The only thing not auto-handled is instance activities force joining the team on the server.
|
|
|
|
void ChatIgnoreList::GetIgnoreList(const ChatPackets::GetIgnoreList& request, const SystemAddress& sysAddr) {
|
|
const LWOOBJID playerId = request.playerID;
|
|
|
|
auto& receiver = Game::playerContainer.GetPlayerDataMutable(playerId);
|
|
if (!receiver) {
|
|
LOG("Tried to get ignore list, but player %llu not found in container", playerId);
|
|
return;
|
|
}
|
|
|
|
if (!receiver.ignoredPlayers.empty()) {
|
|
LOG_DEBUG("Player %llu already has an ignore list, but is requesting it again.", playerId);
|
|
} else {
|
|
auto ignoreList = Database::Get()->GetIgnoreList(playerId);
|
|
if (ignoreList.empty()) {
|
|
LOG_DEBUG("Player %llu has no ignores", playerId);
|
|
return;
|
|
}
|
|
|
|
for (auto& ignoredPlayer : ignoreList) {
|
|
receiver.ignoredPlayers.emplace_back(ignoredPlayer.name, ignoredPlayer.id);
|
|
GeneralUtils::SetBit(receiver.ignoredPlayers.back().playerId, eObjectBits::CHARACTER);
|
|
}
|
|
}
|
|
|
|
ClientPackets::GetIgnoreListResponse response;
|
|
response.isFreeTrial = false; // Is Free Trial, but we don't care about that
|
|
for (const auto& ignoredPlayer : receiver.ignoredPlayers) {
|
|
response.ignored.push_back({ ignoredPlayer.playerId, LUWString(ignoredPlayer.playerName, 36) });
|
|
}
|
|
|
|
ChatPacketHandler::SendRouted(receiver.playerID, sysAddr, response);
|
|
}
|
|
|
|
void ChatIgnoreList::AddIgnore(const ChatPackets::AddIgnore& request, const SystemAddress& sysAddr) {
|
|
const LWOOBJID playerId = request.playerID;
|
|
|
|
auto& receiver = Game::playerContainer.GetPlayerDataMutable(playerId);
|
|
if (!receiver) {
|
|
LOG("Tried to get ignore list, but player %llu not found in container", playerId);
|
|
return;
|
|
}
|
|
|
|
const int32_t MAX_IGNORES = Game::config->GetValue("max_ignores", 32);
|
|
if (receiver.ignoredPlayers.size() >= MAX_IGNORES) {
|
|
LOG_DEBUG("Player %llu has too many ignores", playerId);
|
|
return;
|
|
}
|
|
|
|
std::string toIgnoreStr = request.playerName.GetAsString();
|
|
|
|
ClientPackets::AddIgnoreResponse response;
|
|
|
|
// Check if the player exists
|
|
LWOOBJID ignoredPlayerId = LWOOBJID_EMPTY;
|
|
if (toIgnoreStr == receiver.playerName || toIgnoreStr.find("[GM]") == 0) {
|
|
LOG_DEBUG("Player %llu tried to ignore themselves", playerId);
|
|
|
|
response.responseCode = eAddIgnoreResponse::GENERAL_ERROR;
|
|
} else if (std::count(receiver.ignoredPlayers.begin(), receiver.ignoredPlayers.end(), toIgnoreStr) > 0) {
|
|
LOG_DEBUG("Player %llu is already ignoring %s", playerId, toIgnoreStr.c_str());
|
|
|
|
response.responseCode = eAddIgnoreResponse::ALREADY_IGNORED;
|
|
} else {
|
|
// Get the playerId falling back to query if not online
|
|
const auto& playerData = Game::playerContainer.GetPlayerData(toIgnoreStr);
|
|
if (!playerData) {
|
|
// Fall back to query
|
|
auto player = Database::Get()->GetCharacterInfo(toIgnoreStr);
|
|
if (!player || player->name != toIgnoreStr) {
|
|
LOG_DEBUG("Player %s not found", toIgnoreStr.c_str());
|
|
} else {
|
|
ignoredPlayerId = player->id;
|
|
}
|
|
} else {
|
|
ignoredPlayerId = playerData.playerID;
|
|
}
|
|
|
|
if (ignoredPlayerId != LWOOBJID_EMPTY) {
|
|
Database::Get()->AddIgnore(playerId, ignoredPlayerId);
|
|
GeneralUtils::SetBit(ignoredPlayerId, eObjectBits::CHARACTER);
|
|
|
|
receiver.ignoredPlayers.emplace_back(toIgnoreStr, ignoredPlayerId);
|
|
LOG_DEBUG("Player %llu is ignoring %s", playerId, toIgnoreStr.c_str());
|
|
|
|
response.responseCode = eAddIgnoreResponse::SUCCESS;
|
|
} else {
|
|
response.responseCode = eAddIgnoreResponse::PLAYER_NOT_FOUND;
|
|
}
|
|
}
|
|
|
|
response.playerName = LUWString(toIgnoreStr, 33);
|
|
response.playerID = ignoredPlayerId;
|
|
|
|
ChatPacketHandler::SendRouted(receiver.playerID, sysAddr, response);
|
|
}
|
|
|
|
void ChatIgnoreList::RemoveIgnore(const ChatPackets::RemoveIgnore& request, const SystemAddress& sysAddr) {
|
|
const LWOOBJID playerId = request.playerID;
|
|
|
|
auto& receiver = Game::playerContainer.GetPlayerDataMutable(playerId);
|
|
if (!receiver) {
|
|
LOG("Tried to get ignore list, but player %llu not found in container", playerId);
|
|
return;
|
|
}
|
|
|
|
std::string removedIgnoreStr = request.playerName.GetAsString();
|
|
|
|
auto toRemove = std::remove(receiver.ignoredPlayers.begin(), receiver.ignoredPlayers.end(), removedIgnoreStr);
|
|
if (toRemove == receiver.ignoredPlayers.end()) {
|
|
LOG_DEBUG("Player %llu is not ignoring %s", playerId, removedIgnoreStr.c_str());
|
|
return;
|
|
}
|
|
|
|
Database::Get()->RemoveIgnore(playerId, toRemove->playerId);
|
|
receiver.ignoredPlayers.erase(toRemove, receiver.ignoredPlayers.end());
|
|
|
|
ClientPackets::RemoveIgnoreResponse response;
|
|
response.responseCode = 0;
|
|
response.playerName = LUWString(removedIgnoreStr, 33);
|
|
|
|
ChatPacketHandler::SendRouted(receiver.playerID, sysAddr, response);
|
|
}
|