Skip to content

Commit

Permalink
Refactored display of logged in players
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxcucus committed Jun 19, 2024
1 parent 7ff50e1 commit 41b636c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,22 @@ void PlayersListWidget::updatePlayers(const std::vector<UserWithLastLoginViewMod
QString player = playerModel.m_UserName;
if (player != m_GlobalData->m_UserData.m_UserName) {
m_PlayersListWidget->addItem(player);
m_PlayersListFromServer.insert(player);
m_PlayersListFromServer.insert(playerModel);
}
}

for (QString player: m_PlayersList) {
if (m_PlayersListFromServer.find(player) != m_PlayersListFromServer.end())
for (UserWithLastLoginViewModel player: m_PlayersList) {
auto it = m_PlayersListFromServer.begin();
while (it != m_PlayersListFromServer.end()) {
if (it->m_UserName == player.m_UserName)
break;
it++;
}

if (it != m_PlayersListFromServer.end())
continue;
if (player != m_GlobalData->m_UserData.m_UserName)
m_PlayersListWidget->addItem(player);
if (player.m_UserName != m_GlobalData->m_UserData.m_UserName)
m_PlayersListWidget->addItem(player.m_UserName);
}

//TODO: should one chat window be selected here ?
Expand All @@ -58,7 +65,9 @@ void PlayersListWidget::updatePlayers(const std::vector<UserWithLastLoginViewMod
}

void PlayersListWidget::addPlayer(const QString& player) {
auto res = m_PlayersList.insert(player);

UserWithLastLoginViewModel user(player);
auto res = m_PlayersList.insert(user);
if (res.second)
updatePlayersFromPlayersList();
}
Expand All @@ -82,27 +91,35 @@ void PlayersListWidget::itemDoubleClicked(QListWidgetItem* item) {
emit playerDoubleClicked(item->text());
}

void PlayersListWidget::updatePlayersList(const QStringList& players) {
/*void PlayersListWidget::updatePlayersList(const QStringList& players) {
for (QString player : players)
m_PlayersList.insert(player);
}
}*/

void PlayersListWidget::updatePlayersFromPlayersList() {
while (m_PlayersListWidget->count() > 0) {
m_PlayersListWidget->takeItem(0);
}

m_PlayersListFromServer.clear();
for (QString player: m_PlayersListFromServer) {
if (player != m_GlobalData->m_UserData.m_UserName) {
m_PlayersListWidget->addItem(player);
for (UserWithLastLoginViewModel player: m_PlayersListFromServer) {
if (player.m_UserName != m_GlobalData->m_UserData.m_UserName) {
m_PlayersListWidget->addItem(player.m_UserName);
}
}

for (QString player: m_PlayersList) {
if (m_PlayersListFromServer.find(player) != m_PlayersListFromServer.end())
for (UserWithLastLoginViewModel player: m_PlayersList) {
auto it = m_PlayersListFromServer.begin();
while (it != m_PlayersListFromServer.end()) {
if (it->m_UserName == player.m_UserName)
break;
it++;
}


if (it != m_PlayersListFromServer.end())
continue;
if (player != m_GlobalData->m_UserData.m_UserName)
m_PlayersListWidget->addItem(player);
if (player.m_UserName != m_GlobalData->m_UserData.m_UserName)
m_PlayersListWidget->addItem(player.m_UserName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ private slots:
QListWidget* m_PlayersListWidget = nullptr;
QTimer* m_RefreshPlayersListTimer = nullptr;

std::set<QString> m_PlayersList;
std::set<QString> m_PlayersListFromServer;
std::set<UserWithLastLoginViewModel> m_PlayersList;
std::set<UserWithLastLoginViewModel> m_PlayersListFromServer;
QString m_CurrentItemText;

bool m_IsActive = false;
Expand Down
22 changes: 22 additions & 0 deletions c_plus_plus/multiround/viewmodels/userwithlastloginviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ struct UserWithLastLoginViewModel {
m_LastLogin = parseDateFromString(lastLoginString);
}

explicit UserWithLastLoginViewModel(const QString& username) {
m_UserName = username;
m_LastLogin = QDateTime::currentDateTime();
}

UserWithLastLoginViewModel(const QString& username, const QDateTime& dateTime) {
m_UserName = username;
m_LastLogin = dateTime;
}

private:
QDateTime parseDateFromString(const QString& dateString) {
QStringList dateElements = dateString.split(" ", Qt::SkipEmptyParts);
Expand Down Expand Up @@ -46,6 +56,18 @@ struct UserWithLastLoginViewModel {
return QDateTime(retDate, retTime);
}

friend bool operator<(const UserWithLastLoginViewModel& user1, const UserWithLastLoginViewModel& user2) {
if (user1.m_UserName != user2.m_UserName) {
return user1.m_LastLogin < user2.m_LastLogin;
}

return user1.m_LastLogin < user2.m_LastLogin;
}

friend bool operator==(const UserWithLastLoginViewModel& user1, const UserWithLastLoginViewModel& user2) {
return user1.m_LastLogin == user2.m_LastLogin && user1.m_UserName == user2.m_UserName;
}

};

#endif

0 comments on commit 41b636c

Please sign in to comment.