Add game filtering for spectator attributes (#4127)

This commit is contained in:
Kaitlin 2020-10-27 15:49:02 -04:00 committed by GitHub
parent 1a94261490
commit a49c4865bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 291 additions and 62 deletions

View file

@ -23,10 +23,15 @@ enum GameListColumn
SPECTATORS
};
const bool DEFAULT_UNAVAILABLE_GAMES_VISIBLE = false;
const bool DEFAULT_SHOW_FULL_GAMES = false;
const bool DEFAULT_SHOW_GAMES_THAT_STARTED = false;
const bool DEFAULT_SHOW_PASSWORD_PROTECTED_GAMES = true;
const bool DEFAULT_SHOW_BUDDIES_ONLY_GAMES = true;
const bool DEFAULT_HIDE_IGNORED_USER_GAMES = false;
const bool DEFAULT_SHOW_ONLY_IF_SPECTATORS_CAN_WATCH = false;
const bool DEFAULT_SHOW_SPECTATOR_PASSWORD_PROTECTED = true;
const bool DEFAULT_SHOW_ONLY_IF_SPECTATORS_CAN_CHAT = false;
const bool DEFAULT_SHOW_ONLY_IF_SPECTATORS_CAN_SEE_HANDS = false;
const int DEFAULT_MAX_PLAYERS_MIN = 1;
const int DEFAULT_MAX_PLAYERS_MAX = 99;
constexpr QTime DEFAULT_MAX_GAME_AGE = QTime();
@ -288,9 +293,15 @@ void GamesProxyModel::setHideIgnoredUserGames(bool _hideIgnoredUserGames)
invalidateFilter();
}
void GamesProxyModel::setUnavailableGamesVisible(bool _unavailableGamesVisible)
void GamesProxyModel::setShowFullGames(bool _showFullGames)
{
unavailableGamesVisible = _unavailableGamesVisible;
showFullGames = _showFullGames;
invalidateFilter();
}
void GamesProxyModel::setShowGamesThatStarted(bool _showGamesThatStarted)
{
showGamesThatStarted = _showGamesThatStarted;
invalidateFilter();
}
@ -331,6 +342,30 @@ void GamesProxyModel::setMaxGameAge(const QTime &_maxGameAge)
invalidateFilter();
}
void GamesProxyModel::setShowOnlyIfSpectatorsCanWatch(bool _showOnlyIfSpectatorsCanWatch)
{
showOnlyIfSpectatorsCanWatch = _showOnlyIfSpectatorsCanWatch;
invalidateFilter();
}
void GamesProxyModel::setShowSpectatorPasswordProtected(bool _showSpectatorPasswordProtected)
{
showSpectatorPasswordProtected = _showSpectatorPasswordProtected;
invalidateFilter();
}
void GamesProxyModel::setShowOnlyIfSpectatorsCanChat(bool _showOnlyIfSpectatorsCanChat)
{
showOnlyIfSpectatorsCanChat = _showOnlyIfSpectatorsCanChat;
invalidateFilter();
}
void GamesProxyModel::setShowOnlyIfSpectatorsCanSeeHands(bool _showOnlyIfSpectatorsCanSeeHands)
{
showOnlyIfSpectatorsCanSeeHands = _showOnlyIfSpectatorsCanSeeHands;
invalidateFilter();
}
int GamesProxyModel::getNumFilteredGames() const
{
GamesModel *model = qobject_cast<GamesModel *>(sourceModel());
@ -348,7 +383,8 @@ int GamesProxyModel::getNumFilteredGames() const
void GamesProxyModel::resetFilterParameters()
{
unavailableGamesVisible = DEFAULT_UNAVAILABLE_GAMES_VISIBLE;
showFullGames = DEFAULT_SHOW_FULL_GAMES;
showGamesThatStarted = DEFAULT_SHOW_GAMES_THAT_STARTED;
showPasswordProtectedGames = DEFAULT_SHOW_PASSWORD_PROTECTED_GAMES;
showBuddiesOnlyGames = DEFAULT_SHOW_BUDDIES_ONLY_GAMES;
hideIgnoredUserGames = DEFAULT_HIDE_IGNORED_USER_GAMES;
@ -358,24 +394,33 @@ void GamesProxyModel::resetFilterParameters()
maxPlayersFilterMin = DEFAULT_MAX_PLAYERS_MIN;
maxPlayersFilterMax = DEFAULT_MAX_PLAYERS_MAX;
maxGameAge = DEFAULT_MAX_GAME_AGE;
showOnlyIfSpectatorsCanWatch = DEFAULT_SHOW_ONLY_IF_SPECTATORS_CAN_WATCH;
showSpectatorPasswordProtected = DEFAULT_SHOW_SPECTATOR_PASSWORD_PROTECTED;
showOnlyIfSpectatorsCanChat = DEFAULT_SHOW_ONLY_IF_SPECTATORS_CAN_CHAT;
showOnlyIfSpectatorsCanSeeHands = DEFAULT_SHOW_ONLY_IF_SPECTATORS_CAN_SEE_HANDS;
invalidateFilter();
}
bool GamesProxyModel::areFilterParametersSetToDefaults() const
{
return unavailableGamesVisible == DEFAULT_UNAVAILABLE_GAMES_VISIBLE &&
return showFullGames == DEFAULT_SHOW_FULL_GAMES && showGamesThatStarted == DEFAULT_SHOW_GAMES_THAT_STARTED &&
showPasswordProtectedGames == DEFAULT_SHOW_PASSWORD_PROTECTED_GAMES &&
showBuddiesOnlyGames == DEFAULT_SHOW_BUDDIES_ONLY_GAMES &&
hideIgnoredUserGames == DEFAULT_HIDE_IGNORED_USER_GAMES && gameNameFilter.isEmpty() &&
creatorNameFilter.isEmpty() && gameTypeFilter.isEmpty() && maxPlayersFilterMin == DEFAULT_MAX_PLAYERS_MIN &&
maxPlayersFilterMax == DEFAULT_MAX_PLAYERS_MAX && maxGameAge == DEFAULT_MAX_GAME_AGE;
maxPlayersFilterMax == DEFAULT_MAX_PLAYERS_MAX && maxGameAge == DEFAULT_MAX_GAME_AGE &&
showOnlyIfSpectatorsCanWatch == DEFAULT_SHOW_ONLY_IF_SPECTATORS_CAN_WATCH &&
showSpectatorPasswordProtected == DEFAULT_SHOW_SPECTATOR_PASSWORD_PROTECTED &&
showOnlyIfSpectatorsCanChat == DEFAULT_SHOW_ONLY_IF_SPECTATORS_CAN_CHAT &&
showOnlyIfSpectatorsCanSeeHands == DEFAULT_SHOW_ONLY_IF_SPECTATORS_CAN_SEE_HANDS;
}
void GamesProxyModel::loadFilterParameters(const QMap<int, QString> &allGameTypes)
{
GameFiltersSettings &gameFilters = SettingsCache::instance().gameFilters();
unavailableGamesVisible = gameFilters.isUnavailableGamesVisible();
showFullGames = gameFilters.isShowFullGames();
showGamesThatStarted = gameFilters.isShowGamesThatStarted();
showPasswordProtectedGames = gameFilters.isShowPasswordProtectedGames();
hideIgnoredUserGames = gameFilters.isHideIgnoredUserGames();
showBuddiesOnlyGames = gameFilters.isShowBuddiesOnlyGames();
@ -384,6 +429,10 @@ void GamesProxyModel::loadFilterParameters(const QMap<int, QString> &allGameType
maxPlayersFilterMin = gameFilters.getMinPlayers();
maxPlayersFilterMax = gameFilters.getMaxPlayers();
maxGameAge = gameFilters.getMaxGameAge();
showOnlyIfSpectatorsCanWatch = gameFilters.isShowOnlyIfSpectatorsCanWatch();
showSpectatorPasswordProtected = gameFilters.isShowSpectatorPasswordProtected();
showOnlyIfSpectatorsCanChat = gameFilters.isShowOnlyIfSpectatorsCanChat();
showOnlyIfSpectatorsCanSeeHands = gameFilters.isShowOnlyIfSpectatorsCanSeeHands();
QMapIterator<int, QString> gameTypesIterator(allGameTypes);
while (gameTypesIterator.hasNext()) {
@ -400,7 +449,8 @@ void GamesProxyModel::saveFilterParameters(const QMap<int, QString> &allGameType
{
GameFiltersSettings &gameFilters = SettingsCache::instance().gameFilters();
gameFilters.setShowBuddiesOnlyGames(showBuddiesOnlyGames);
gameFilters.setUnavailableGamesVisible(unavailableGamesVisible);
gameFilters.setShowFullGames(showFullGames);
gameFilters.setShowGamesThatStarted(showGamesThatStarted);
gameFilters.setShowPasswordProtectedGames(showPasswordProtectedGames);
gameFilters.setHideIgnoredUserGames(hideIgnoredUserGames);
gameFilters.setGameNameFilter(gameNameFilter);
@ -416,6 +466,11 @@ void GamesProxyModel::saveFilterParameters(const QMap<int, QString> &allGameType
gameFilters.setMinPlayers(maxPlayersFilterMin);
gameFilters.setMaxPlayers(maxPlayersFilterMax);
gameFilters.setMaxGameAge(maxGameAge);
gameFilters.setShowOnlyIfSpectatorsCanWatch(showOnlyIfSpectatorsCanWatch);
gameFilters.setShowSpectatorPasswordProtected(showSpectatorPasswordProtected);
gameFilters.setShowOnlyIfSpectatorsCanChat(showOnlyIfSpectatorsCanChat);
gameFilters.setShowOnlyIfSpectatorsCanSeeHands(showOnlyIfSpectatorsCanSeeHands);
}
bool GamesProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & /*sourceParent*/) const
@ -430,7 +485,7 @@ bool GamesProxyModel::filterAcceptsRow(int sourceRow) const
#else
static const QDate epochDate = QDateTime::fromTime_t(0, Qt::UTC).date();
#endif
GamesModel *model = qobject_cast<GamesModel *>(sourceModel());
auto *model = qobject_cast<GamesModel *>(sourceModel());
if (!model)
return false;
@ -443,15 +498,13 @@ bool GamesProxyModel::filterAcceptsRow(int sourceRow) const
QString::fromStdString(game.creator_info().name()))) {
return false;
}
if (!unavailableGamesVisible) {
if (game.player_count() == game.max_players())
if (!showFullGames && game.player_count() == game.max_players())
return false;
if (!showGamesThatStarted && game.started())
return false;
if (!ownUserIsRegistered)
if (game.only_registered())
return false;
if (game.started())
return false;
if (!ownUserIsRegistered)
if (game.only_registered())
return false;
}
if (!showPasswordProtectedGames && game.with_password())
return false;
if (!gameNameFilter.isEmpty())
@ -482,6 +535,17 @@ bool GamesProxyModel::filterAcceptsRow(int sourceRow) const
return false;
}
}
if (showOnlyIfSpectatorsCanWatch) {
if (!game.spectators_allowed())
return false;
if (!showSpectatorPasswordProtected && game.spectators_need_password())
return false;
if (showOnlyIfSpectatorsCanChat && !game.spectators_can_chat())
return false;
if (showOnlyIfSpectatorsCanSeeHands && !game.spectators_omniscient())
return false;
}
return true;
}