Cockatrice/cockatrice/src/interface/widgets/server/games_model.cpp
DawnFire42 aadee34238
style: Add braces to all control flow statements (#6887)
* style: Add braces to all control flow statements

  Standardize code style by adding explicit braces to all single-statement
  control flow blocks (if, else, for, while) across the entire codebase.

  Also documents the InsertBraces clang-format option (requires v15+) for
  future automated enforcement.

* InsertBraces-check-enabled
2026-05-16 19:19:53 +02:00

500 lines
18 KiB
C++

#include "games_model.h"
#include "../../../client/settings/cache_settings.h"
#include "../interface/pixel_map_generator.h"
#include "../interface/widgets/tabs/tab_account.h"
#include "user/user_list_manager.h"
#include "user/user_list_widget.h"
#include <QDateTime>
#include <QIcon>
#include <QTimeZone>
#include <libcockatrice/protocol/pb/serverinfo_game.pb.h>
enum GameListColumn
{
ROOM,
CREATED,
DESCRIPTION,
CREATOR,
GAME_TYPE,
RESTRICTIONS,
PLAYERS,
SPECTATORS
};
const QString GamesModel::getGameCreatedString(const int secs)
{
static const QTime zeroTime{0, 0};
static const int halfHourSecs = zeroTime.secsTo(QTime(1, 0)) / 2;
static const int halfMinSecs = zeroTime.secsTo(QTime(0, 1)) / 2;
static const int wrapSeconds = zeroTime.secsTo(zeroTime.addSecs(-halfHourSecs)); // round up
if (secs >= wrapSeconds) { // QTime wraps after a day
return tr(">1 day");
}
QTime total = zeroTime.addSecs(secs);
QTime totalRounded = total.addSecs(halfMinSecs); // round up
QString form;
int amount;
if (totalRounded.hour()) {
amount = total.addSecs(halfHourSecs).hour(); // round up separately
form = tr("%1%2 hr", "short age in hours", amount);
} else if (total.minute() < 2) { // games are new during their first minute
return tr("new");
} else {
amount = totalRounded.minute();
form = tr("%1%2 min", "short age in minutes", amount);
}
for (int aggregate : {40, 20, 10, 5}) { // floor to values in this list
if (amount >= aggregate) {
return form.arg(">").arg(aggregate);
}
}
return form.arg("").arg(amount);
}
GamesModel::GamesModel(const QMap<int, QString> &_rooms, const QMap<int, GameTypeMap> &_gameTypes, QObject *parent)
: QAbstractTableModel(parent), rooms(_rooms), gameTypes(_gameTypes)
{
}
QVariant GamesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
if (role == Qt::UserRole) {
return index.row();
}
if (role != Qt::DisplayRole && role != SORT_ROLE && role != Qt::DecorationRole && role != Qt::TextAlignmentRole) {
return QVariant();
}
if ((index.row() >= gameList.size()) || (index.column() >= columnCount())) {
return QVariant();
}
const ServerInfo_Game &gameentry = gameList[index.row()];
switch (index.column()) {
case ROOM:
return rooms.value(gameentry.room_id());
case CREATED: {
switch (role) {
case Qt::DisplayRole: {
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
auto then = QDateTime::fromSecsSinceEpoch(gameentry.start_time(), QTimeZone::UTC);
#else
auto then = QDateTime::fromSecsSinceEpoch(gameentry.start_time(), Qt::UTC);
#endif
int secs = then.secsTo(QDateTime::currentDateTimeUtc());
return getGameCreatedString(secs);
}
case SORT_ROLE:
return QVariant(-static_cast<qint64>(gameentry.start_time()));
case Qt::TextAlignmentRole:
return Qt::AlignCenter;
default:
return QVariant();
}
}
case DESCRIPTION:
switch (role) {
case SORT_ROLE:
case Qt::DisplayRole:
return QString::fromStdString(gameentry.description());
case Qt::TextAlignmentRole:
return Qt::AlignLeft;
default:
return QVariant();
}
case CREATOR: {
switch (role) {
case SORT_ROLE:
case Qt::DisplayRole:
return QString::fromStdString(gameentry.creator_info().name());
case Qt::DecorationRole: {
return UserLevelPixmapGenerator::generateIcon(
13, UserLevelFlags(gameentry.creator_info().user_level()),
gameentry.creator_info().pawn_colors(), false,
QString::fromStdString(gameentry.creator_info().privlevel()));
}
default:
return QVariant();
}
}
case GAME_TYPE:
switch (role) {
case SORT_ROLE:
case Qt::DisplayRole: {
QStringList result;
GameTypeMap gameTypeMap = gameTypes.value(gameentry.room_id());
for (int i = gameentry.game_types_size() - 1; i >= 0; --i) {
result.append(gameTypeMap.value(gameentry.game_types(i)));
}
return result.join(", ");
}
case Qt::TextAlignmentRole:
return Qt::AlignLeft;
default:
return QVariant();
}
case RESTRICTIONS:
switch (role) {
case SORT_ROLE:
case Qt::DisplayRole: {
QStringList result;
if (gameentry.with_password()) {
result.append(tr("password"));
}
if (gameentry.only_buddies()) {
result.append(tr("buddies only"));
}
if (gameentry.only_registered()) {
result.append(tr("reg. users only"));
}
if (gameentry.share_decklists_on_load()) {
result.append(tr("open decklists"));
}
return result.join(", ");
}
case Qt::DecorationRole: {
return gameentry.with_password() ? QIcon(LockPixmapGenerator::generatePixmap(13)) : QVariant();
case Qt::TextAlignmentRole:
return Qt::AlignLeft;
default:
return QVariant();
}
}
case PLAYERS:
switch (role) {
case SORT_ROLE:
case Qt::DisplayRole:
return QString("%1/%2").arg(gameentry.player_count()).arg(gameentry.max_players());
case Qt::TextAlignmentRole:
return Qt::AlignCenter;
default:
return QVariant();
}
case SPECTATORS:
switch (role) {
case SORT_ROLE:
case Qt::DisplayRole: {
if (gameentry.spectators_allowed()) {
QString result;
result.append(QString::number(gameentry.spectators_count()));
if (gameentry.spectators_can_chat() && gameentry.spectators_omniscient()) {
result.append(" (")
.append(tr("can chat"))
.append(" & ")
.append(tr("see hands"))
.append(")");
} else if (gameentry.spectators_can_chat()) {
result.append(" (").append(tr("can chat")).append(")");
} else if (gameentry.spectators_omniscient()) {
result.append(" (").append(tr("can see hands")).append(")");
}
return result;
}
return QVariant(tr("not allowed"));
}
case Qt::TextAlignmentRole:
return Qt::AlignLeft;
default:
return QVariant();
}
default:
return QVariant();
}
}
QVariant GamesModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const
{
if ((role != Qt::DisplayRole) && (role != Qt::TextAlignmentRole)) {
return QVariant();
}
switch (section) {
case ROOM:
return tr("Room");
case CREATED: {
switch (role) {
case Qt::DisplayRole:
return tr("Age");
case Qt::TextAlignmentRole:
return Qt::AlignCenter;
default:
return QVariant();
}
}
case DESCRIPTION:
return tr("Description");
case CREATOR:
return tr("Creator");
case GAME_TYPE:
return tr("Type");
case RESTRICTIONS:
return tr("Restrictions");
case PLAYERS: {
switch (role) {
case Qt::DisplayRole:
return tr("Players");
case Qt::TextAlignmentRole:
return Qt::AlignCenter;
default:
return QVariant();
}
}
case SPECTATORS:
return tr("Spectators");
default:
return QVariant();
}
}
const ServerInfo_Game &GamesModel::getGame(int row)
{
Q_ASSERT(row < gameList.size());
return gameList[row];
}
void GamesModel::updateGameList(const ServerInfo_Game &game)
{
for (int i = 0; i < gameList.size(); i++) {
if (gameList[i].game_id() == game.game_id()) {
if (game.closed()) {
beginRemoveRows(QModelIndex(), i, i);
gameList.removeAt(i);
endRemoveRows();
} else {
gameList[i].MergeFrom(game);
emit dataChanged(index(i, 0), index(i, NUM_COLS - 1));
}
return;
}
}
beginInsertRows(QModelIndex(), gameList.size(), gameList.size());
gameList.append(game);
endInsertRows();
}
GamesProxyModel::GamesProxyModel(QObject *parent, const UserListProxy *_userListProxy)
: QSortFilterProxyModel(parent), userListProxy(_userListProxy)
{
resetFilterParameters();
setSortRole(GamesModel::SORT_ROLE);
setDynamicSortFilter(true);
}
void GamesProxyModel::setGameFilters(const GameFilterConfigs &_filters)
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0))
beginFilterChange();
#endif
filters = _filters;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 10, 0))
endFilterChange(QSortFilterProxyModel::Direction::Rows);
#else
invalidateFilter();
#endif
emit filtersChanged();
}
int GamesProxyModel::getNumFilteredGames() const
{
auto *model = qobject_cast<GamesModel *>(sourceModel());
if (!model) {
return 0;
}
int numFilteredGames = 0;
for (int row = 0; row < model->rowCount(); ++row) {
if (!filterAcceptsRow(row)) {
++numFilteredGames;
}
}
return numFilteredGames;
}
void GamesProxyModel::resetFilterParameters()
{
setGameFilters({});
}
bool GamesProxyModel::areFilterParametersSetToDefaults() const
{
return filters.isDefault();
}
void GamesProxyModel::loadFilterParameters(const QMap<int, QString> &allGameTypes)
{
GameFiltersSettings &gameFilters = SettingsCache::instance().gameFilters();
QSet<int> newGameTypeFilter;
QMapIterator<int, QString> gameTypesIterator(allGameTypes);
while (gameTypesIterator.hasNext()) {
gameTypesIterator.next();
if (gameFilters.isGameTypeEnabled(gameTypesIterator.value())) {
newGameTypeFilter.insert(gameTypesIterator.key());
}
}
setGameFilters({gameFilters.isHideBuddiesOnlyGames(), gameFilters.isHideIgnoredUserGames(),
gameFilters.isHideFullGames(), gameFilters.isHideGamesThatStarted(),
gameFilters.isHidePasswordProtectedGames(), gameFilters.isHideNotBuddyCreatedGames(),
gameFilters.isHideOpenDecklistGames(), gameFilters.getGameNameFilter(),
gameFilters.getCreatorNameFilters(), newGameTypeFilter, gameFilters.getMinPlayers(),
gameFilters.getMaxPlayers(), gameFilters.getMaxGameAge(),
gameFilters.isShowOnlyIfSpectatorsCanWatch(), gameFilters.isShowSpectatorPasswordProtected(),
gameFilters.isShowOnlyIfSpectatorsCanChat(), gameFilters.isShowOnlyIfSpectatorsCanSeeHands()});
}
void GamesProxyModel::saveFilterParameters(const QMap<int, QString> &allGameTypes)
{
GameFiltersSettings &gameFilters = SettingsCache::instance().gameFilters();
gameFilters.setHideBuddiesOnlyGames(filters.hideBuddiesOnlyGames);
gameFilters.setHideFullGames(filters.hideFullGames);
gameFilters.setHideGamesThatStarted(filters.hideGamesThatStarted);
gameFilters.setHidePasswordProtectedGames(filters.hidePasswordProtectedGames);
gameFilters.setHideIgnoredUserGames(filters.hideIgnoredUserGames);
gameFilters.setHideNotBuddyCreatedGames(filters.hideNotBuddyCreatedGames);
gameFilters.setHideOpenDecklistGames(filters.hideOpenDecklistGames);
gameFilters.setGameNameFilter(filters.gameNameFilter);
gameFilters.setCreatorNameFilters(filters.creatorNameFilters);
QMapIterator<int, QString> gameTypeIterator(allGameTypes);
while (gameTypeIterator.hasNext()) {
gameTypeIterator.next();
bool enabled = filters.gameTypeFilter.contains(gameTypeIterator.key());
gameFilters.setGameTypeEnabled(gameTypeIterator.value(), enabled);
}
gameFilters.setMinPlayers(filters.maxPlayersFilterMin);
gameFilters.setMaxPlayers(filters.maxPlayersFilterMax);
gameFilters.setMaxGameAge(filters.maxGameAge);
gameFilters.setShowOnlyIfSpectatorsCanWatch(filters.showOnlyIfSpectatorsCanWatch);
gameFilters.setShowSpectatorPasswordProtected(filters.showSpectatorPasswordProtected);
gameFilters.setShowOnlyIfSpectatorsCanChat(filters.showOnlyIfSpectatorsCanChat);
gameFilters.setShowOnlyIfSpectatorsCanSeeHands(filters.showOnlyIfSpectatorsCanSeeHands);
}
bool GamesProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & /*sourceParent*/) const
{
return filterAcceptsRow(sourceRow);
}
bool GamesProxyModel::filterAcceptsRow(int sourceRow) const
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
static const QDate epochDate = QDateTime::fromSecsSinceEpoch(0, QTimeZone::UTC).date();
#else
static const QDate epochDate = QDateTime::fromSecsSinceEpoch(0, Qt::UTC).date();
#endif
auto *model = qobject_cast<GamesModel *>(sourceModel());
if (!model) {
return false;
}
const ServerInfo_Game &game = model->getGame(sourceRow);
if (filters.hideBuddiesOnlyGames && game.only_buddies()) {
return false;
}
if (filters.hideOpenDecklistGames && game.share_decklists_on_load()) {
return false;
}
if (filters.hideIgnoredUserGames &&
userListProxy->isUserIgnored(QString::fromStdString(game.creator_info().name()))) {
return false;
}
if (filters.hideNotBuddyCreatedGames &&
!userListProxy->isUserBuddy(QString::fromStdString(game.creator_info().name()))) {
return false;
}
if (filters.hideFullGames && game.player_count() == game.max_players()) {
return false;
}
if (filters.hideGamesThatStarted && game.started()) {
return false;
}
if (!userListProxy->isOwnUserRegistered()) {
if (game.only_registered()) {
return false;
}
}
if (filters.hidePasswordProtectedGames && game.with_password()) {
return false;
}
if (!filters.gameNameFilter.isEmpty()) {
if (!QString::fromStdString(game.description()).contains(filters.gameNameFilter, Qt::CaseInsensitive)) {
return false;
}
}
if (!filters.creatorNameFilters.isEmpty()) {
bool found = false;
for (const auto &createNameFilter : filters.creatorNameFilters) {
if (QString::fromStdString(game.creator_info().name()).contains(createNameFilter, Qt::CaseInsensitive)) {
found = true;
}
}
if (!found) {
return false;
}
}
QSet<int> gameTypes;
for (int i = 0; i < game.game_types_size(); ++i) {
gameTypes.insert(game.game_types(i));
}
if (!filters.gameTypeFilter.isEmpty() && gameTypes.intersect(filters.gameTypeFilter).isEmpty()) {
return false;
}
if (static_cast<int>(game.max_players()) < filters.maxPlayersFilterMin) {
return false;
}
if (static_cast<int>(game.max_players()) > filters.maxPlayersFilterMax) {
return false;
}
if (filters.maxGameAge.isValid()) {
QDateTime now = QDateTime::currentDateTimeUtc();
qint64 signed_start_time = game.start_time(); // cast to 64 bit value to allow signed value
QDateTime total = now.addSecs(-signed_start_time); // a 32 bit value would wrap at 2038-1-19
// games shouldn't have negative ages but we'll not filter them
// because qtime wraps after a day we consider all games older than a day to be too old
if (total.isValid() && total.date() >= epochDate &&
(total.date() > epochDate || total.time() > filters.maxGameAge)) {
return false;
}
}
if (filters.showOnlyIfSpectatorsCanWatch) {
if (!game.spectators_allowed()) {
return false;
}
if (!filters.showSpectatorPasswordProtected && game.spectators_need_password()) {
return false;
}
if (filters.showOnlyIfSpectatorsCanChat && !game.spectators_can_chat()) {
return false;
}
if (filters.showOnlyIfSpectatorsCanSeeHands && !game.spectators_omniscient()) {
return false;
}
}
return true;
}
void GamesProxyModel::refresh()
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 10, 0))
endFilterChange(QSortFilterProxyModel::Direction::Rows);
#else
invalidateFilter();
#endif
}