Merge pull request #224 from Salanto/addClientIDtoArea

Change broadcast to area to use userIDs instead of iterating over all joined clients.
This commit is contained in:
Rosemary Witchaven 2022-01-24 13:49:54 -06:00 committed by GitHub
commit d6a4e64070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 18 deletions

View File

@ -215,8 +215,11 @@ class AreaData : public QObject {
* *
* @param f_charId The character ID of the client who left. The default value is `-1`. If it is left at that, * @param f_charId The character ID of the client who left. The default value is `-1`. If it is left at that,
* the area will not try to remove any character from the list of characters taken. * the area will not try to remove any character from the list of characters taken.
*
* @param f_userId The user ID of the client who left. The default value is '-1', This ID is technically
* impossible.
*/ */
void clientLeftArea(int f_charId = -1); void clientLeftArea(int f_charId = -1, int f_userId = -1);
/** /**
* @brief A client in the area joined recently. * @brief A client in the area joined recently.
@ -225,8 +228,11 @@ class AreaData : public QObject {
* *
* @param f_charId The character ID of the client who joined. The default value is `-1`. If it is left at that, * @param f_charId The character ID of the client who joined. The default value is `-1`. If it is left at that,
* the area will not add any character to the list of characters taken. * the area will not add any character to the list of characters taken.
*
* @param f_userId The user ID of the client who left. The default value is '-1', This ID is technically
* impossible.
*/ */
void clientJoinedArea(int f_charId = -1); void clientJoinedArea(int f_charId = -1, int f_userId = -1);
/** /**
* @brief Returns a copy of the list of owners of this area. * @brief Returns a copy of the list of owners of this area.
@ -844,6 +850,11 @@ class AreaData : public QObject {
*/ */
QString addJukeboxSong(QString f_song); QString addJukeboxSong(QString f_song);
/**
* @brief Returns a constant that includes all currently joined userids.
*/
QVector<int> joinedIDs() const;
public slots: public slots:
/** /**
@ -1060,6 +1071,11 @@ private:
*/ */
bool m_send_area_message; bool m_send_area_message;
/**
* @brief Collection of joined IDs to this area.
*/
QVector<int> m_joined_ids;
// Jukebox specific members // Jukebox specific members
/** /**
* @brief Stores the songs added to the jukebox to be played. * @brief Stores the songs added to the jukebox to be played.

View File

@ -55,7 +55,7 @@ void AOClient::clientDisconnected()
if (m_joined) { if (m_joined) {
server->m_player_count--; server->m_player_count--;
emit server->updatePlayerCount(server->m_player_count); emit server->updatePlayerCount(server->m_player_count);
server->m_areas[m_current_area]->clientLeftArea(server->getCharID(m_current_char)); server->m_areas[m_current_area]->clientLeftArea(server->getCharID(m_current_char), m_id);
arup(ARUPType::PLAYER_COUNT, true); arup(ARUPType::PLAYER_COUNT, true);
} }
@ -124,14 +124,14 @@ void AOClient::changeArea(int new_area)
server->m_areas[m_current_area]->changeCharacter(server->getCharID(m_current_char), -1); server->m_areas[m_current_area]->changeCharacter(server->getCharID(m_current_char), -1);
server->updateCharsTaken(server->m_areas[m_current_area]); server->updateCharsTaken(server->m_areas[m_current_area]);
} }
server->m_areas[m_current_area]->clientLeftArea(m_char_id); server->m_areas[m_current_area]->clientLeftArea(m_char_id, m_id);
bool l_character_taken = false; bool l_character_taken = false;
if (server->m_areas[new_area]->charactersTaken().contains(server->getCharID(m_current_char))) { if (server->m_areas[new_area]->charactersTaken().contains(server->getCharID(m_current_char))) {
m_current_char = ""; m_current_char = "";
m_char_id = -1; m_char_id = -1;
l_character_taken = true; l_character_taken = true;
} }
server->m_areas[new_area]->clientJoinedArea(m_char_id); server->m_areas[new_area]->clientJoinedArea(m_char_id, m_id);
m_current_area = new_area; m_current_area = new_area;
arup(ARUPType::PLAYER_COUNT, true); arup(ARUPType::PLAYER_COUNT, true);
sendEvidenceList(server->m_areas[new_area]); sendEvidenceList(server->m_areas[new_area]);

View File

@ -75,22 +75,24 @@ const QMap<QString, AreaData::Status> AreaData::map_statuses = {
{"gaming", AreaData::Status::GAMING }, {"gaming", AreaData::Status::GAMING },
}; };
void AreaData::clientLeftArea(int f_charId) void AreaData::clientLeftArea(int f_charId, int f_userId)
{ {
--m_playerCount; --m_playerCount;
if (f_charId != -1) { if (f_charId != -1) {
m_charactersTaken.removeAll(f_charId); m_charactersTaken.removeAll(f_charId);
} }
m_joined_ids.removeAll(f_userId);
} }
void AreaData::clientJoinedArea(int f_charId) void AreaData::clientJoinedArea(int f_charId, int f_userId)
{ {
++m_playerCount; ++m_playerCount;
if (f_charId != -1) { if (f_charId != -1) {
m_charactersTaken.append(f_charId); m_charactersTaken.append(f_charId);
} }
m_joined_ids.append(f_userId);
} }
QList<int> AreaData::owners() const QList<int> AreaData::owners() const
@ -582,6 +584,11 @@ QString AreaData::addJukeboxSong(QString f_song)
return "Unable to add song. Song already in Jukebox."; return "Unable to add song. Song already in Jukebox.";
} }
QVector<int> AreaData::joinedIDs() const
{
return m_joined_ids;
}
void AreaData::switchJukeboxSong() void AreaData::switchJukeboxSong()
{ {
QString l_song_name; QString l_song_name;

View File

@ -134,7 +134,7 @@ void AOClient::pktLoadingDone(AreaData* area, int argc, QStringList argv, AOPack
server->m_player_count++; server->m_player_count++;
emit server->updatePlayerCount(server->m_player_count); emit server->updatePlayerCount(server->m_player_count);
area->clientJoinedArea(); area->clientJoinedArea(-1, m_id);
m_joined = true; m_joined = true;
server->updateCharsTaken(area); server->updateCharsTaken(area);

View File

@ -253,9 +253,9 @@ void Server::reloadSettings()
void Server::broadcast(AOPacket packet, int area_index) void Server::broadcast(AOPacket packet, int area_index)
{ {
for (AOClient* client : qAsConst(m_clients)) { QVector<int> l_client_ids = m_areas.value(area_index)->joinedIDs();
if (client->m_current_area == area_index) for (const int l_client_id : qAsConst(l_client_ids)) {
client->sendPacket(packet); getClientByID(l_client_id)->sendPacket(packet);
} }
} }

View File

@ -77,17 +77,17 @@ void Area::cleanup()
void Area::clientJoinLeave() void Area::clientJoinLeave()
{ {
{ {
// There must be exactly one client in the area, and it must have a charid of 5. // There must be exactly one client in the area, and it must have a charid of 5 and userid 0.
m_area->clientJoinedArea(5); m_area->clientJoinedArea(5,0);
QCOMPARE(m_area->charactersTaken().size(), 1); QCOMPARE(m_area->joinedIDs().size(), 1);
QCOMPARE(m_area->charactersTaken().at(0), 5); QCOMPARE(m_area->charactersTaken().at(0), 5);
} }
{ {
// No clients must be left in the area. // No clients must be left in the area.
m_area->clientLeftArea(5); m_area->clientLeftArea(5,0);
QCOMPARE(m_area->charactersTaken().size(), 0); QCOMPARE(m_area->joinedIDs().size(), 0);
} }
} }
@ -151,8 +151,8 @@ void Area::changeHP()
void Area::changeCharacter() void Area::changeCharacter()
{ {
{ {
// A client with a charid of 6 joins. There's only them in there. // A client with a charid of 6 and userid 0 joins. There's only them in there.
m_area->clientJoinedArea(6); m_area->clientJoinedArea(6,0);
QCOMPARE(m_area->charactersTaken().size(), 1); QCOMPARE(m_area->charactersTaken().size(), 1);
QCOMPARE(m_area->charactersTaken().at(0), 6); QCOMPARE(m_area->charactersTaken().at(0), 6);