Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
aronwk-aaron committed Jan 14, 2024
1 parent 1258954 commit 4529ae3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 96 deletions.
167 changes: 81 additions & 86 deletions dChatServer/ChatPacketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,33 @@ void ChatPacketHandler::HandleFriendRequest(Packet* packet) {
CINSTREAM_SKIP_HEADER;

LWOOBJID requestorPlayerID;
LUWString playerName;
LUWString LUplayerName;
char isBestFriendRequest{};

inStream.Read(requestorPlayerID);
inStream.IgnoreBytes(4);
inStream.Read(playerName);
inStream.Read(LUplayerName);
inStream.Read(isBestFriendRequest);

auto playerName = LUplayerName.GetAsString();

auto& requestor = Game::playerContainer.GetPlayerDataMutable(requestorPlayerID);
if (!requestor) {
LOG("No requestor player %llu sent to %s found.", requestorPlayerID, playerName.GetAsString().c_str());
LOG("No requestor player %llu sent to %s found.", requestorPlayerID, playerName.c_str());
return;
}

if (requestor.playerName == playerName.GetAsString()) {
if (requestor.playerName == playerName) {
SendFriendResponse(requestor, requestor, eAddFriendResponseType::MYTHRAN);
return;
};

auto& requestee = Game::playerContainer.GetPlayerDataMutable(playerName.GetAsString());
auto& requestee = Game::playerContainer.GetPlayerDataMutable(playerName);

// Check if player is online first
if (isBestFriendRequest && !requestee) {
for (auto& friendDataCandidate : requestor.friends) {
if (friendDataCandidate.friendName != playerName.GetAsString()) continue;
if (friendDataCandidate.friendName != playerName) continue;
// Setup the needed info since you can add a best friend offline.
requestee.playerID = friendDataCandidate.friendID;
requestee.playerName = friendDataCandidate.friendName;
Expand All @@ -125,8 +127,8 @@ void ChatPacketHandler::HandleFriendRequest(Packet* packet) {
// If at this point we dont have a target, then they arent online and we cant send the request.
// Send the response code that corresponds to what the error is.
if (!requestee) {
requestee.playerName = playerName.GetAsString();
auto responseType = Database::Get()->GetCharacterInfo(playerName.GetAsString())
requestee.playerName = playerName;
auto responseType = Database::Get()->GetCharacterInfo(playerName)
? eAddFriendResponseType::NOTONLINE
: eAddFriendResponseType::INVALIDCHARACTER;

Expand Down Expand Up @@ -285,15 +287,16 @@ void ChatPacketHandler::HandleFriendResponse(Packet* packet) {
void ChatPacketHandler::HandleRemoveFriend(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID;
LUWString friendName;
LUWString LUFriendName;
inStream.Read(playerID);
inStream.IgnoreBytes(4);
inStream.Read(friendName);
inStream.Read(LUFriendName);
auto friendName = LUFriendName.GetAsString();

//we'll have to query the db here to find the user, since you can delete them while they're offline.
//First, we need to find their ID:
LWOOBJID friendID = 0;
auto friendIdResult = Database::Get()->GetCharacterInfo(friendName.GetAsString());
auto friendIdResult = Database::Get()->GetCharacterInfo(friendName);
if (friendIdResult) {
friendID = friendIdResult->id;
}
Expand All @@ -315,8 +318,7 @@ void ChatPacketHandler::HandleRemoveFriend(Packet* packet) {
break;
}
}
auto name = friendName.GetAsString();
SendRemoveFriend(goonA, name, true);
SendRemoveFriend(goonA, friendName, true);
}

auto& goonB = Game::playerContainer.GetPlayerDataMutable(friendID);
Expand Down Expand Up @@ -346,45 +348,31 @@ void ChatPacketHandler::HandleChatMessage(Packet* packet) {
eChatChannel channel;
uint32_t size;

inStream.IgnoreBytes(4);
inStream.IgnoreBytes(4); // padding/garbage
inStream.Read(channel);
inStream.Read(size);
inStream.IgnoreBytes(77);
inStream.IgnoreBytes(77); // padding/garbage

LUWString message(size);
inStream.Read(message);

LOG("Got a message from (%s) via [%s]: %s", sender.playerName.c_str(), StringifiedEnum::ToString(channel).data(), message.GetAsString().c_str());

if (channel == eChatChannel::TEAM) {
switch (channel) {
case eChatChannel::TEAM: {
auto* team = Game::playerContainer.GetTeam(playerID);
if (team == nullptr) return;

for (const auto memberId : team->memberIDs) {
const auto& otherMember = Game::playerContainer.GetPlayerData(memberId);

if (!otherMember) return;

CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::ROUTE_TO_PLAYER);
bitStream.Write(otherMember.playerID);

BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::PRIVATE_CHAT_MESSAGE);
bitStream.Write(otherMember.playerID);
bitStream.Write(eChatChannel::TEAM);
bitStream.Write<uint32_t>(0); // not used
bitStream.Write(LUWString(sender.playerName));
bitStream.Write(sender.playerID);
bitStream.Write<uint16_t>(0); // sourceID
bitStream.Write(sender.GMLevel);
bitStream.Write(LUWString(otherMember.playerName));
bitStream.Write(otherMember.GMLevel);
bitStream.Write(eChatMessageResponseCode::Sent);
bitStream.Write(message);

SystemAddress sysAddr = otherMember.sysAddr;
SEND_PACKET;
SendPrivateChatMessage(sender, otherMember, otherMember, message, eChatMessageResponseCode::SENT);
}
break;
}
default:
LOG("Unhandled Chat channel [%s]", StringifiedEnum::ToString(channel).data());
break;
}
}

Expand All @@ -398,72 +386,79 @@ void ChatPacketHandler::HandlePrivateChatMessage(Packet* packet) {

eChatChannel channel;
uint32_t size;
LUWString receiverName;
LUWString LUReceiverName;

inStream.IgnoreBytes(4);
inStream.IgnoreBytes(4); // padding/garbage
inStream.Read(channel);
if (channel != eChatChannel::PRIVATE_CHAT) LOG("WARNING: Received Private chat with the wrong channel!");

inStream.Read(size);
inStream.IgnoreBytes(77);
inStream.Read(receiverName);
inStream.IgnoreBytes(2);
inStream.IgnoreBytes(77); // padding/garbage

inStream.Read(LUReceiverName);
auto receiverName = LUReceiverName.GetAsString();
inStream.IgnoreBytes(2); // padding/garbage

LUWString message(size);
inStream.Read(message);

LOG("Got a message from (%s) via [%s]: %s to %s", sender.playerName.c_str(), StringifiedEnum::ToString(channel).data(), message.GetAsString().c_str(), receiverName.GetAsString().c_str());

const auto& receiver = Game::playerContainer.GetPlayerData(receiverName.GetAsString());
if (!receiver) return; // TODO: respond with not online or error
LOG("Got a message from (%s) via [%s]: %s to %s", sender.playerName.c_str(), StringifiedEnum::ToString(channel).data(), message.GetAsString().c_str(), receiverName.c_str());

// TODO: check if they are friends, only friends should be able to whisper/private chat
const auto& receiver = Game::playerContainer.GetPlayerData(receiverName);
if (!receiver) {
PlayerData otherPlayer;
otherPlayer.playerName = receiverName;
auto responseType = Database::Get()->GetCharacterInfo(receiverName)
? eChatMessageResponseCode::NOTONLINE
: eChatMessageResponseCode::GENERALERROR;

//To the sender:
{
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::ROUTE_TO_PLAYER);
bitStream.Write(sender.playerID);
SendPrivateChatMessage(sender, otherPlayer, sender, message, responseType);
return;
}

BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::PRIVATE_CHAT_MESSAGE);
bitStream.Write(sender.playerID);
bitStream.Write(eChatChannel::PRIVATE_CHAT);
bitStream.Write<uint32_t>(0); // not used
bitStream.Write(LUWString(sender.playerName));
bitStream.Write(sender.playerID);
bitStream.Write<uint16_t>(0); // sourceID
bitStream.Write(sender.GMLevel);
bitStream.Write(LUWString(receiver.playerName));
bitStream.Write(receiver.GMLevel);
bitStream.Write(eChatMessageResponseCode::Sent);
bitStream.Write(message);
if (sender.isFTP) {
SendPrivateChatMessage(sender, receiver, sender, message, eChatMessageResponseCode::SENDERFREETRIAL);
return;
}

SystemAddress sysAddr = sender.sysAddr;
SEND_PACKET;
if (receiver.isFTP) {
SendPrivateChatMessage(sender, receiver, sender, message, eChatMessageResponseCode::RECEIVERFREETRIAL);
return;
}

//To the receiver:
{
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::ROUTE_TO_PLAYER);
bitStream.Write(receiver.playerID);
// Check to see if they are friends
if (std::find(sender.friends.begin(), sender.friends.end(), receiver.playerID) != sender.friends.end()) {
//To the sender:
SendPrivateChatMessage(sender, receiver, sender, message, eChatMessageResponseCode::SENT);
//To the receiver:
SendPrivateChatMessage(sender, receiver, receiver, message, eChatMessageResponseCode::RECEIVEDNEWWHISPER);
return; //we have this player as a friend, yeet this function so it doesn't send another request.
} else SendPrivateChatMessage(sender, receiver, sender, message, eChatMessageResponseCode::NOTFRIENDS);
}

BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::PRIVATE_CHAT_MESSAGE);
bitStream.Write(sender.playerID);
bitStream.Write(eChatChannel::PRIVATE_CHAT);
bitStream.Write<uint32_t>(0); // not used
bitStream.Write(LUWString(sender.playerName));
bitStream.Write(sender.playerID);
bitStream.Write<uint16_t>(0); // sourceID
bitStream.Write(sender.GMLevel);
bitStream.Write(LUWString(receiver.playerName));
bitStream.Write(receiver.GMLevel);
bitStream.Write(eChatMessageResponseCode::ReceivedNewWhisper);
bitStream.Write(message);
void ChatPacketHandler::SendPrivateChatMessage(const PlayerData& sender, const PlayerData& receiver, const PlayerData& routeTo, const LUWString& message, eChatMessageResponseCode responseCode) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::ROUTE_TO_PLAYER);
bitStream.Write(routeTo.playerID);

SystemAddress sysAddr = receiver.sysAddr;
SEND_PACKET;
}
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::PRIVATE_CHAT_MESSAGE);
bitStream.Write(sender.playerID);
bitStream.Write(eChatChannel::TEAM);
bitStream.Write<uint32_t>(0); // not used
bitStream.Write(LUWString(sender.playerName));
bitStream.Write(sender.playerID);
bitStream.Write<uint16_t>(0); // sourceID
bitStream.Write(sender.GMLevel);
bitStream.Write(LUWString(receiver.playerName));
bitStream.Write(receiver.GMLevel);
bitStream.Write(responseCode);
bitStream.Write(message);

SystemAddress sysAddr = routeTo.sysAddr;
SEND_PACKET;
}


void ChatPacketHandler::HandleTeamInvite(Packet* packet) {
CINSTREAM_SKIP_HEADER;

Expand Down
15 changes: 8 additions & 7 deletions dChatServer/ChatPacketHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ enum class eChatChannel : uint8_t {


enum class eChatMessageResponseCode : uint8_t {
Sent = 0,
NotOnline,
GeneralError,
ReceivedNewWhisper,
NotFriends,
SenderFreeTrial,
ReceiverFreeTrial,
SENT = 0,
NOTONLINE,
GENERALERROR,
RECEIVEDNEWWHISPER,
NOTFRIENDS,
SENDERFREETRIAL,
RECEIVERFREETRIAL,
};

namespace ChatPacketHandler {
Expand All @@ -52,6 +52,7 @@ namespace ChatPacketHandler {

void HandleChatMessage(Packet* packet);
void HandlePrivateChatMessage(Packet* packet);
void SendPrivateChatMessage(const PlayerData& sender, const PlayerData& receiver, const PlayerData& routeTo, const LUWString& message, eChatMessageResponseCode responseCode);

void HandleTeamInvite(Packet* packet);
void HandleTeamInviteResponse(Packet* packet);
Expand Down
54 changes: 52 additions & 2 deletions dChatServer/ChatServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,59 @@ void HandlePacket(Packet* packet) {
case eChatMessageType::TEAM_SET_LOOT:
ChatPacketHandler::HandleTeamLootOption(packet);
break;

case eChatMessageType::LOGIN_SESSION_NOTIFY:
case eChatMessageType::USER_CHANNEL_CHAT_MESSAGE:
case eChatMessageType::WORLD_DISCONNECT_REQUEST:
case eChatMessageType::WORLD_PROXIMITY_RESPONSE:
case eChatMessageType::WORLD_PARCEL_RESPONSE:
case eChatMessageType::TEAM_MISSED_INVITE_CHECK:
case eChatMessageType::GUILD_CREATE:
case eChatMessageType::GUILD_INVITE:
case eChatMessageType::GUILD_INVITE_RESPONSE:
case eChatMessageType::GUILD_LEAVE:
case eChatMessageType::GUILD_KICK:
case eChatMessageType::GUILD_GET_STATUS:
case eChatMessageType::GUILD_GET_ALL:
case eChatMessageType::SHOW_ALL:
case eChatMessageType::BLUEPRINT_MODERATED:
case eChatMessageType::BLUEPRINT_MODEL_READY:
case eChatMessageType::PROPERTY_READY_FOR_APPROVAL:
case eChatMessageType::PROPERTY_MODERATION_CHANGED:
case eChatMessageType::PROPERTY_BUILDMODE_CHANGED:
case eChatMessageType::PROPERTY_BUILDMODE_CHANGED_REPORT:
case eChatMessageType::MAIL:
case eChatMessageType::WORLD_INSTANCE_LOCATION_REQUEST:
case eChatMessageType::REPUTATION_UPDATE:
case eChatMessageType::SEND_CANNED_TEXT:
case eChatMessageType::GMLEVEL_UPDATE:
case eChatMessageType::CHARACTER_NAME_CHANGE_REQUEST:
case eChatMessageType::CSR_REQUEST:
case eChatMessageType::CSR_REPLY:
case eChatMessageType::GM_KICK:
case eChatMessageType::GM_ANNOUNCE:
case eChatMessageType::WORLD_ROUTE_PACKET:
case eChatMessageType::GET_ZONE_POPULATIONS:
case eChatMessageType::REQUEST_MINIMUM_CHAT_MODE:
case eChatMessageType::MATCH_REQUEST:
case eChatMessageType::UGCMANIFEST_REPORT_MISSING_FILE:
case eChatMessageType::UGCMANIFEST_REPORT_DONE_FILE:
case eChatMessageType::UGCMANIFEST_REPORT_DONE_BLUEPRINT:
case eChatMessageType::UGCC_REQUEST:
case eChatMessageType::WHO:
case eChatMessageType::WORLD_PLAYERS_PET_MODERATED_ACKNOWLEDGE:
case eChatMessageType::ACHIEVEMENT_NOTIFY:
case eChatMessageType::GM_CLOSE_PRIVATE_CHAT_WINDOW:
case eChatMessageType::UNEXPECTED_DISCONNECT:
case eChatMessageType::PLAYER_READY:
case eChatMessageType::GET_DONATION_TOTAL:
case eChatMessageType::UPDATE_DONATION:
case eChatMessageType::PRG_CSR_COMMAND:
case eChatMessageType::HEARTBEAT_REQUEST_FROM_WORLD:
case eChatMessageType::UPDATE_FREE_TRIAL_STATUS:
LOG("Unhandled CHAT Message id: %s (%i)", StringifiedEnum::ToString(chat_message_type).data(), chat_message_type);
break;
default:
LOG("Unknown/Unhandled CHAT Message id: %s (%i)", StringifiedEnum::ToString(chat_message_type).data(), chat_message_type);
LOG("Unknown CHAT Message id: %i", chat_message_type);
}
}

Expand Down
4 changes: 3 additions & 1 deletion dChatServer/PlayerContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Game.h"
#include "dServer.h"
#include <unordered_map>
#include "eGameMasterLevel.h"

struct IgnoreData {
IgnoreData(const std::string& name, const LWOOBJID& id) : playerName(name), playerId(id) {}
Expand Down Expand Up @@ -42,7 +43,8 @@ struct PlayerData {
std::string playerName;
std::vector<FriendData> friends;
std::vector<IgnoreData> ignoredPlayers;
uint8_t GMLevel = 0;
// Unused TODO: make use of this
eGameMasterLevel GMLevel = eGameMasterLevel::CIVILIAN;
bool isFTP = false;
};

Expand Down

0 comments on commit 4529ae3

Please sign in to comment.