2021-12-05 17:54:36 +00:00
|
|
|
#include "PlayerContainer.h"
|
|
|
|
#include "dNetCommon.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include "Game.h"
|
|
|
|
#include "dLogger.h"
|
|
|
|
#include "ChatPacketHandler.h"
|
|
|
|
#include "GeneralUtils.h"
|
2023-09-21 01:06:28 +00:00
|
|
|
#include "BitStreamUtils.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "Database.h"
|
2023-05-03 21:38:32 +00:00
|
|
|
#include "eConnectionType.h"
|
|
|
|
#include "eChatInternalMessageType.h"
|
2023-08-18 23:58:27 +00:00
|
|
|
#include "ChatPackets.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
PlayerContainer::PlayerContainer() {
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerContainer::~PlayerContainer() {
|
|
|
|
mPlayers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlayerContainer::InsertPlayer(Packet* packet) {
|
2023-05-08 11:31:10 +00:00
|
|
|
CINSTREAM_SKIP_HEADER;
|
2021-12-05 17:54:36 +00:00
|
|
|
PlayerData* data = new PlayerData();
|
|
|
|
inStream.Read(data->playerID);
|
2022-07-17 03:40:46 +00:00
|
|
|
|
|
|
|
uint32_t len;
|
|
|
|
inStream.Read<uint32_t>(len);
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
char character; inStream.Read<char>(character);
|
2022-07-25 02:26:51 +00:00
|
|
|
data->playerName += character;
|
2022-07-17 03:40:46 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
inStream.Read(data->zoneID);
|
|
|
|
inStream.Read(data->muteExpire);
|
|
|
|
data->sysAddr = packet->systemAddress;
|
|
|
|
|
2022-08-02 13:56:20 +00:00
|
|
|
mNames[data->playerID] = GeneralUtils::UTF8ToUTF16(data->playerName);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
mPlayers.insert(std::make_pair(data->playerID, data));
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("PlayerContainer", "Added user: %s (%llu), zone: %i", data->playerName.c_str(), data->playerID, data->zoneID.GetMapID());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-10-10 00:40:48 +00:00
|
|
|
Database::Connection->InsertIntoActivityLog(data->playerID, 0, time(nullptr), data->zoneID.GetMapID());
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlayerContainer::RemovePlayer(Packet* packet) {
|
2023-05-08 11:31:10 +00:00
|
|
|
CINSTREAM_SKIP_HEADER;
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID playerID;
|
|
|
|
inStream.Read(playerID);
|
|
|
|
|
|
|
|
//Before they get kicked, we need to also send a message to their friends saying that they disconnected.
|
2022-07-13 16:11:24 +00:00
|
|
|
std::unique_ptr<PlayerData> player(this->GetPlayerData(playerID));
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (player == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& fr : player->friends) {
|
|
|
|
auto fd = this->GetPlayerData(fr.friendID);
|
2022-07-13 16:11:24 +00:00
|
|
|
if (fd) ChatPacketHandler::SendFriendUpdate(fd, player.get(), 0, fr.isBestFriend);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto* team = GetTeam(playerID);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (team != nullptr) {
|
2022-08-02 13:56:20 +00:00
|
|
|
const auto memberName = GeneralUtils::UTF8ToUTF16(std::string(player->playerName.c_str()));
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto memberId : team->memberIDs) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* otherMember = GetPlayerData(memberId);
|
|
|
|
|
|
|
|
if (otherMember == nullptr) continue;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
ChatPacketHandler::SendTeamSetOffWorldFlag(otherMember, playerID, { 0, 0, 0 });
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("PlayerContainer", "Removed user: %llu", playerID);
|
2021-12-05 17:54:36 +00:00
|
|
|
mPlayers.erase(playerID);
|
|
|
|
|
2023-10-10 00:40:48 +00:00
|
|
|
Database::Connection->InsertIntoActivityLog(playerID, 1, time(nullptr), player->zoneID.GetMapID());
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void PlayerContainer::MuteUpdate(Packet* packet) {
|
2023-05-08 11:31:10 +00:00
|
|
|
CINSTREAM_SKIP_HEADER;
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID playerID;
|
|
|
|
inStream.Read(playerID);
|
|
|
|
time_t expire = 0;
|
|
|
|
inStream.Read(expire);
|
|
|
|
|
|
|
|
auto* player = this->GetPlayerData(playerID);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (player == nullptr) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("PlayerContainer", "Failed to find user: %llu", playerID);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
player->muteExpire = expire;
|
|
|
|
|
|
|
|
BroadcastMuteUpdate(playerID, expire);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void PlayerContainer::CreateTeamServer(Packet* packet) {
|
2023-05-08 11:31:10 +00:00
|
|
|
CINSTREAM_SKIP_HEADER;
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID playerID;
|
|
|
|
inStream.Read(playerID);
|
|
|
|
size_t membersSize = 0;
|
|
|
|
inStream.Read(membersSize);
|
|
|
|
|
|
|
|
std::vector<LWOOBJID> members;
|
|
|
|
|
|
|
|
members.reserve(membersSize);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (size_t i = 0; i < membersSize; i++) {
|
2021-12-05 17:54:36 +00:00
|
|
|
LWOOBJID member;
|
|
|
|
inStream.Read(member);
|
|
|
|
members.push_back(member);
|
|
|
|
}
|
|
|
|
|
|
|
|
LWOZONEID zoneId;
|
|
|
|
|
|
|
|
inStream.Read(zoneId);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* team = CreateLocalTeam(members);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (team != nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
team->zoneId = zoneId;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateTeamsOnWorld(team, false);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void PlayerContainer::BroadcastMuteUpdate(LWOOBJID player, time_t time) {
|
2021-12-05 17:54:36 +00:00
|
|
|
CBITSTREAM;
|
2023-09-21 01:06:28 +00:00
|
|
|
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::MUTE_UPDATE);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
bitStream.Write(player);
|
|
|
|
bitStream.Write(time);
|
|
|
|
|
|
|
|
Game::server->Send(&bitStream, UNASSIGNED_SYSTEM_ADDRESS, true);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
TeamData* PlayerContainer::CreateLocalTeam(std::vector<LWOOBJID> members) {
|
|
|
|
if (members.empty()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TeamData* newTeam = nullptr;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto member : members) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* team = GetTeam(member);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (team != nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
RemoveMember(team, member, false, false, true);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (newTeam == nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
newTeam = CreateTeam(member, true);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
AddMember(newTeam, member);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newTeam->lootFlag = 1;
|
|
|
|
|
|
|
|
TeamStatusUpdate(newTeam);
|
|
|
|
|
|
|
|
return newTeam;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
TeamData* PlayerContainer::CreateTeam(LWOOBJID leader, bool local) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* team = new TeamData();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
team->teamID = ++mTeamIDCounter;
|
|
|
|
team->leaderID = leader;
|
|
|
|
team->local = local;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
mTeams.push_back(team);
|
|
|
|
|
|
|
|
AddMember(team, leader);
|
|
|
|
|
|
|
|
return team;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
TeamData* PlayerContainer::GetTeam(LWOOBJID playerID) {
|
|
|
|
for (auto* team : mTeams) {
|
2021-12-05 17:54:36 +00:00
|
|
|
if (std::find(team->memberIDs.begin(), team->memberIDs.end(), playerID) == team->memberIDs.end()) continue;
|
|
|
|
|
|
|
|
return team;
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void PlayerContainer::AddMember(TeamData* team, LWOOBJID playerID) {
|
2023-08-18 23:58:27 +00:00
|
|
|
if (team->memberIDs.size() >= 4){
|
|
|
|
Game::logger->Log("PlayerContainer", "Tried to add player to team that already had 4 players");
|
|
|
|
auto* player = GetPlayerData(playerID);
|
|
|
|
if (!player) return;
|
|
|
|
ChatPackets::SendSystemMessage(player->sysAddr, u"The teams is full! You have not been added to a team!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto index = std::find(team->memberIDs.begin(), team->memberIDs.end(), playerID);
|
|
|
|
|
|
|
|
if (index != team->memberIDs.end()) return;
|
|
|
|
|
|
|
|
team->memberIDs.push_back(playerID);
|
|
|
|
|
|
|
|
auto* leader = GetPlayerData(team->leaderID);
|
|
|
|
auto* member = GetPlayerData(playerID);
|
|
|
|
|
|
|
|
if (leader == nullptr || member == nullptr) return;
|
|
|
|
|
2022-08-02 13:56:20 +00:00
|
|
|
const auto leaderName = GeneralUtils::UTF8ToUTF16(leader->playerName);
|
|
|
|
const auto memberName = GeneralUtils::UTF8ToUTF16(member->playerName);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
ChatPacketHandler::SendTeamInviteConfirm(member, false, leader->playerID, leader->zoneID, team->lootFlag, 0, 0, leaderName);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!team->local) {
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::SendTeamSetLeader(member, leader->playerID);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::SendTeamSetLeader(member, LWOOBJID_EMPTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateTeamsOnWorld(team, false);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto memberId : team->memberIDs) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* otherMember = GetPlayerData(memberId);
|
|
|
|
|
|
|
|
if (otherMember == member) continue;
|
|
|
|
|
|
|
|
const auto otherMemberName = GetName(memberId);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::SendTeamAddPlayer(member, false, team->local, false, memberId, otherMemberName, otherMember != nullptr ? otherMember->zoneID : LWOZONEID(0, 0, 0));
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (otherMember != nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::SendTeamAddPlayer(otherMember, false, team->local, false, member->playerID, memberName, member->zoneID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void PlayerContainer::RemoveMember(TeamData* team, LWOOBJID playerID, bool disband, bool kicked, bool leaving, bool silent) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto index = std::find(team->memberIDs.begin(), team->memberIDs.end(), playerID);
|
|
|
|
|
|
|
|
if (index == team->memberIDs.end()) return;
|
|
|
|
|
|
|
|
auto* member = GetPlayerData(playerID);
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (member != nullptr && !silent) {
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::SendTeamSetLeader(member, LWOOBJID_EMPTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto memberName = GetName(playerID);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto memberId : team->memberIDs) {
|
|
|
|
if (silent && memberId == playerID) {
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* otherMember = GetPlayerData(memberId);
|
|
|
|
|
|
|
|
if (otherMember == nullptr) continue;
|
|
|
|
|
|
|
|
ChatPacketHandler::SendTeamRemovePlayer(otherMember, disband, kicked, leaving, false, team->leaderID, playerID, memberName);
|
|
|
|
}
|
|
|
|
|
|
|
|
team->memberIDs.erase(index);
|
|
|
|
|
|
|
|
UpdateTeamsOnWorld(team, false);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (team->memberIDs.size() <= 1) {
|
2021-12-05 17:54:36 +00:00
|
|
|
DisbandTeam(team);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
|
|
|
if (playerID == team->leaderID) {
|
2021-12-05 17:54:36 +00:00
|
|
|
PromoteMember(team, team->memberIDs[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void PlayerContainer::PromoteMember(TeamData* team, LWOOBJID newLeader) {
|
2021-12-05 17:54:36 +00:00
|
|
|
team->leaderID = newLeader;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto memberId : team->memberIDs) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* otherMember = GetPlayerData(memberId);
|
|
|
|
|
|
|
|
if (otherMember == nullptr) continue;
|
|
|
|
|
|
|
|
ChatPacketHandler::SendTeamSetLeader(otherMember, newLeader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void PlayerContainer::DisbandTeam(TeamData* team) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto index = std::find(mTeams.begin(), mTeams.end(), team);
|
|
|
|
|
|
|
|
if (index == mTeams.end()) return;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto memberId : team->memberIDs) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* otherMember = GetPlayerData(memberId);
|
|
|
|
|
|
|
|
if (otherMember == nullptr) continue;
|
|
|
|
|
2022-08-02 13:56:20 +00:00
|
|
|
const auto memberName = GeneralUtils::UTF8ToUTF16(otherMember->playerName);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
ChatPacketHandler::SendTeamSetLeader(otherMember, LWOOBJID_EMPTY);
|
|
|
|
ChatPacketHandler::SendTeamRemovePlayer(otherMember, true, false, false, team->local, team->leaderID, otherMember->playerID, memberName);
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
UpdateTeamsOnWorld(team, true);
|
|
|
|
|
|
|
|
mTeams.erase(index);
|
|
|
|
|
|
|
|
delete team;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void PlayerContainer::TeamStatusUpdate(TeamData* team) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto index = std::find(mTeams.begin(), mTeams.end(), team);
|
|
|
|
|
|
|
|
if (index == mTeams.end()) return;
|
|
|
|
|
|
|
|
auto* leader = GetPlayerData(team->leaderID);
|
|
|
|
|
|
|
|
if (leader == nullptr) return;
|
|
|
|
|
2022-08-02 13:56:20 +00:00
|
|
|
const auto leaderName = GeneralUtils::UTF8ToUTF16(leader->playerName);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto memberId : team->memberIDs) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* otherMember = GetPlayerData(memberId);
|
|
|
|
|
|
|
|
if (otherMember == nullptr) continue;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!team->local) {
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::SendTeamStatus(otherMember, team->leaderID, leader->zoneID, team->lootFlag, 0, leaderName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateTeamsOnWorld(team, false);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void PlayerContainer::UpdateTeamsOnWorld(TeamData* team, bool deleteTeam) {
|
2021-12-05 17:54:36 +00:00
|
|
|
CBITSTREAM;
|
2023-09-21 01:06:28 +00:00
|
|
|
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::TEAM_UPDATE);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
bitStream.Write(team->teamID);
|
|
|
|
bitStream.Write(deleteTeam);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!deleteTeam) {
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Write(team->lootFlag);
|
|
|
|
bitStream.Write(static_cast<char>(team->memberIDs.size()));
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto memberID : team->memberIDs) {
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream.Write(memberID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::server->Send(&bitStream, UNASSIGNED_SYSTEM_ADDRESS, true);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::u16string PlayerContainer::GetName(LWOOBJID playerID) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto& pair = mNames.find(playerID);
|
|
|
|
|
|
|
|
if (pair == mNames.end()) return u"";
|
|
|
|
|
|
|
|
return pair->second;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
LWOOBJID PlayerContainer::GetId(const std::u16string& playerName) {
|
|
|
|
for (const auto& pair : mNames) {
|
|
|
|
if (pair.second == playerName) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return pair.first;
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
return LWOOBJID_EMPTY;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool PlayerContainer::GetIsMuted(PlayerData* data) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return data->muteExpire == 1 || data->muteExpire > time(NULL);
|
|
|
|
}
|