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
This commit is contained in:
DawnFire42 2026-05-16 13:19:53 -04:00 committed by GitHub
parent 7153f7d4c1
commit aadee34238
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
173 changed files with 2725 additions and 1461 deletions

View file

@ -391,8 +391,9 @@ void Server_AbstractPlayer::processMoveCard(GameEventStorage &ges,
for (auto *player : game->getPlayers().values()) {
QList<int> arrowsToDelete;
for (Server_Arrow *arrow : player->getArrows()) {
if ((arrow->getStartCard() == card) || (arrow->getTargetItem() == card))
if ((arrow->getStartCard() == card) || (arrow->getTargetItem() == card)) {
arrowsToDelete.append(arrow->getId());
}
}
for (int j : arrowsToDelete) {
player->deleteArrow(j);
@ -1472,8 +1473,9 @@ Server_AbstractPlayer::cmdRevealCards(const Command_RevealCards &cmd, ResponseCo
if (cmd.has_player_id()) {
Server_AbstractPlayer *otherPlayer = game->getPlayer(cmd.player_id());
if (!otherPlayer)
if (!otherPlayer) {
return Response::RespNameNotFound;
}
}
Server_CardZone *zone = zones.value(nameFromStdString(cmd.zone_name()));
if (!zone) {

View file

@ -30,6 +30,7 @@ void Server_Arrow::getInfo(ServerInfo_Arrow *info)
info->set_target_player_id(targetCard->getZone()->getPlayer()->getPlayerId());
info->set_target_zone(targetCard->getZone()->getName().toStdString());
info->set_target_card_id(targetCard->getId());
} else
} else {
info->set_target_player_id(static_cast<Server_Player *>(targetItem)->getPlayerId());
}
}

View file

@ -36,11 +36,13 @@ Server_Card::Server_Card(const CardRef &cardRef, int _id, int _coord_x, int _coo
Server_Card::~Server_Card()
{
// setParentCard(0) leads to the item being removed from our list, so we can't iterate properly
while (!attachedCards.isEmpty())
while (!attachedCards.isEmpty()) {
attachedCards.first()->setParentCard(0);
}
if (parentCard)
if (parentCard) {
parentCard->removeAttachedCard(this);
}
if (stashedCard) {
stashedCard->deleteLater();
@ -62,16 +64,18 @@ void Server_Card::resetState(bool keepAnnotations)
QString Server_Card::setAttribute(CardAttribute attribute, const QString &avalue, bool allCards)
{
if (attribute == AttrTapped && avalue != "1" && allCards && doesntUntap)
if (attribute == AttrTapped && avalue != "1" && allCards && doesntUntap) {
return QVariant(tapped).toString();
}
return setAttribute(attribute, avalue);
}
QString Server_Card::setAttribute(CardAttribute attribute, const QString &avalue, Event_SetCardAttr *event)
{
if (event)
if (event) {
event->set_attribute(attribute);
}
switch (attribute) {
case AttrTapped: {
@ -89,8 +93,9 @@ QString Server_Card::setAttribute(CardAttribute attribute, const QString &avalue
break;
case AttrPT:
setPT(avalue);
if (event)
if (event) {
event->set_attr_value(getPT().toStdString());
}
return getPT();
case AttrAnnotation:
setAnnotation(avalue);
@ -99,17 +104,19 @@ QString Server_Card::setAttribute(CardAttribute attribute, const QString &avalue
setDoesntUntap(avalue == "1");
break;
}
if (event)
if (event) {
event->set_attr_value(avalue.toStdString());
}
return avalue;
}
void Server_Card::setCounter(int _id, int value, Event_SetCardCounter *event)
{
if (value)
if (value) {
counters.insert(_id, value);
else
} else {
counters.remove(_id);
}
if (event) {
event->set_counter_id(_id);
@ -119,11 +126,13 @@ void Server_Card::setCounter(int _id, int value, Event_SetCardCounter *event)
void Server_Card::setParentCard(Server_Card *_parentCard)
{
if (parentCard)
if (parentCard) {
parentCard->removeAttachedCard(this);
}
parentCard = _parentCard;
if (parentCard)
if (parentCard) {
parentCard->addAttachedCard(this);
}
}
void Server_Card::getInfo(ServerInfo_Card *info)

View file

@ -47,19 +47,23 @@ void Server_CardZone::shuffle(int start, int end)
cardsBeingLookedAt = 0;
// Size 0 or 1 decks are sorted
if (cards.size() < 2)
if (cards.size() < 2) {
return;
}
// Negative numbers signify positions starting at the end of the
// zone convert these to actual indexes.
if (end < 0)
if (end < 0) {
end += cards.size();
}
if (start < 0)
if (start < 0) {
start += cards.size();
}
if (start < 0 || end < 0 || start >= cards.size() || end >= cards.size())
if (start < 0 || end < 0 || start >= cards.size() || end >= cards.size()) {
return;
}
for (int i = end; i > start; i--) {
int j = rng->rand(start, i);
@ -70,40 +74,47 @@ void Server_CardZone::shuffle(int start, int end)
void Server_CardZone::removeCardFromCoordMap(Server_Card *card, int oldX, int oldY)
{
if (oldX < 0)
if (oldX < 0) {
return;
}
const int baseX = (oldX / 3) * 3;
QMap<int, Server_Card *> &coordMap = coordinateMap[oldY];
if (coordMap.contains(baseX) && coordMap.contains(baseX + 1) && coordMap.contains(baseX + 2))
if (coordMap.contains(baseX) && coordMap.contains(baseX + 1) && coordMap.contains(baseX + 2)) {
// If the removal of this card has opened up a previously full pile...
freePilesMap[oldY].insert(coordMap.value(baseX)->getName(), baseX);
}
coordMap.remove(oldX);
if (!(coordMap.contains(baseX) && coordMap.value(baseX)->getName() == card->getName()) &&
!(coordMap.contains(baseX + 1) && coordMap.value(baseX + 1)->getName() == card->getName()) &&
!(coordMap.contains(baseX + 2) && coordMap.value(baseX + 2)->getName() == card->getName()))
!(coordMap.contains(baseX + 2) && coordMap.value(baseX + 2)->getName() == card->getName())) {
// If this card was the last one with this name...
freePilesMap[oldY].remove(card->getName(), baseX);
}
if (!coordMap.contains(baseX) && !coordMap.contains(baseX + 1) && !coordMap.contains(baseX + 2)) {
// If the removal of this card has freed a whole pile, i.e. it was the last card in it...
if (baseX < freeSpaceMap[oldY])
if (baseX < freeSpaceMap[oldY]) {
freeSpaceMap[oldY] = baseX;
}
}
}
void Server_CardZone::insertCardIntoCoordMap(Server_Card *card, int x, int y)
{
if (x < 0)
if (x < 0) {
return;
}
coordinateMap[y].insert(x, card);
if (!(x % 3)) {
if (!card->getFaceDown() && !freePilesMap[y].contains(card->getName(), x) && card->getAttachedCards().isEmpty())
if (!card->getFaceDown() && !freePilesMap[y].contains(card->getName(), x) &&
card->getAttachedCards().isEmpty()) {
freePilesMap[y].insert(card->getName(), x);
}
if (freeSpaceMap[y] == x) {
int nextFreeX = x;
do {
@ -146,8 +157,9 @@ Server_Card *Server_CardZone::getCard(int id, int *position, bool remove)
for (int i = 0; i < cards.size(); ++i) {
Server_Card *tmp = cards[i];
if (tmp->getId() == id) {
if (position)
if (position) {
*position = i;
}
if (remove) {
cards.removeAt(i);
tmp->setZone(nullptr);
@ -157,11 +169,13 @@ Server_Card *Server_CardZone::getCard(int id, int *position, bool remove)
}
return nullptr;
} else {
if ((id >= cards.size()) || (id < 0))
if ((id >= cards.size()) || (id < 0)) {
return nullptr;
}
Server_Card *tmp = cards[id];
if (position)
if (position) {
*position = id;
}
if (remove) {
cards.removeAt(id);
tmp->setZone(nullptr);
@ -184,32 +198,35 @@ int Server_CardZone::getFreeGridColumn(int x, int y, const QString &cardName, bo
if (coordMap.contains(x) && (coordMap[x]->getFaceDown() || !coordMap[x]->getAttachedCards().isEmpty())) {
// don't pile up on: 1. facedown cards 2. cards with attached cards
} else if (!coordMap.contains(x))
} else if (!coordMap.contains(x)) {
return x;
else if (!coordMap.contains(x + 1))
} else if (!coordMap.contains(x + 1)) {
return x + 1;
else
} else {
return x + 2;
}
}
} else if (x >= 0) {
int resultX = 0;
x = (x / 3) * 3;
if (!coordMap.contains(x))
if (!coordMap.contains(x)) {
resultX = x;
else if (!coordMap.value(x)->getAttachedCards().isEmpty()) {
} else if (!coordMap.value(x)->getAttachedCards().isEmpty()) {
resultX = x;
x = -1;
} else if (!coordMap.contains(x + 1))
} else if (!coordMap.contains(x + 1)) {
resultX = x + 1;
else if (!coordMap.contains(x + 2))
} else if (!coordMap.contains(x + 2)) {
resultX = x + 2;
else {
} else {
resultX = x;
x = -1;
}
if (x < 0)
while (coordMap.contains(resultX))
if (x < 0) {
while (coordMap.contains(resultX)) {
resultX += 3;
}
}
return resultX;
}
@ -219,16 +236,18 @@ int Server_CardZone::getFreeGridColumn(int x, int y, const QString &cardName, bo
bool Server_CardZone::isColumnStacked(int x, int y) const
{
if (!has_coords)
if (!has_coords) {
return false;
}
return coordinateMap[y].contains((x / 3) * 3 + 1);
}
bool Server_CardZone::isColumnEmpty(int x, int y) const
{
if (!has_coords)
if (!has_coords) {
return true;
}
return !coordinateMap[y].contains((x / 3) * 3);
}
@ -243,12 +262,14 @@ void Server_CardZone::moveCardInRow(GameEventStorage &ges, Server_Card *card, in
void Server_CardZone::fixFreeSpaces(GameEventStorage &ges)
{
if (!has_coords)
if (!has_coords) {
return;
}
QSet<QPair<int, int>> placesToLook;
for (auto &card : cards)
for (auto &card : cards) {
placesToLook.insert(QPair<int, int>((card->getX() / 3) * 3, card->getY()));
}
QSetIterator<QPair<int, int>> placeIterator(placesToLook);
while (placeIterator.hasNext()) {
@ -257,26 +278,30 @@ void Server_CardZone::fixFreeSpaces(GameEventStorage &ges)
int y = foo.second;
if (!coordinateMap[y].contains(baseX)) {
if (coordinateMap[y].contains(baseX + 1))
if (coordinateMap[y].contains(baseX + 1)) {
moveCardInRow(ges, coordinateMap[y].value(baseX + 1), baseX, y);
else if (coordinateMap[y].contains(baseX + 2)) {
} else if (coordinateMap[y].contains(baseX + 2)) {
moveCardInRow(ges, coordinateMap[y].value(baseX + 2), baseX, y);
continue;
} else
} else {
continue;
}
}
if (!coordinateMap[y].contains(baseX + 1) && coordinateMap[y].contains(baseX + 2))
if (!coordinateMap[y].contains(baseX + 1) && coordinateMap[y].contains(baseX + 2)) {
moveCardInRow(ges, coordinateMap[y].value(baseX + 2), baseX + 1, y);
}
}
}
void Server_CardZone::updateCardCoordinates(Server_Card *card, int oldX, int oldY)
{
if (!has_coords)
if (!has_coords) {
return;
}
if (oldX != -1)
if (oldX != -1) {
removeCardFromCoordMap(card, oldX, oldY);
}
insertCardIntoCoordMap(card, card->getX(), card->getY());
}
@ -299,8 +324,9 @@ void Server_CardZone::insertCard(Server_Card *card, int x, int y)
void Server_CardZone::clear()
{
for (auto card : cards)
for (auto card : cards) {
delete card;
}
cards.clear();
coordinateMap.clear();
freePilesMap.clear();
@ -329,7 +355,8 @@ void Server_CardZone::getInfo(ServerInfo_Zone *info, Server_AbstractParticipant
const bool zonesOthersCanSee = type == ServerInfo_Zone::PublicZone;
if ((selfPlayerAsking && zonesSelfCanSee) || (otherPlayerAsking && zonesOthersCanSee)) {
QListIterator<Server_Card *> cardIterator(cards);
while (cardIterator.hasNext())
while (cardIterator.hasNext()) {
cardIterator.next()->getInfo(info->add_card_list());
}
}
}

View file

@ -147,8 +147,9 @@ void Server_Game::storeGameInformation()
const QStringList &allGameTypes = room->getGameTypes();
QStringList _gameTypes;
for (int i = gameInfo.game_types_size() - 1; i >= 0; --i)
for (int i = gameInfo.game_types_size() - 1; i >= 0; --i) {
_gameTypes.append(allGameTypes[gameInfo.game_types(i)]);
}
for (const auto &playerName : allPlayersEver) {
replayMatchInfo->add_player_names(playerName.toStdString());
@ -166,8 +167,9 @@ void Server_Game::storeGameInformation()
server->clientsLock.lockForRead();
for (auto userName : allPlayersEver + allSpectatorsEver) {
Server_AbstractUserInterface *userHandler = server->findUser(userName);
if (userHandler && server->getStoreReplaysEnabled())
if (userHandler && server->getStoreReplaysEnabled()) {
userHandler->sendProtocolItem(*sessionEvent);
}
}
server->clientsLock.unlock();
delete sessionEvent;
@ -189,8 +191,9 @@ void Server_Game::pingClockTimeout()
bool allPlayersInactive = true;
int playerCount = 0;
for (auto *participant : participants) {
if (participant == nullptr)
if (participant == nullptr) {
continue;
}
if (!participant->isSpectator()) {
++playerCount;
@ -253,8 +256,9 @@ int Server_Game::getSpectatorCount() const
int result = 0;
for (Server_AbstractParticipant *participant : participants.values()) {
if (participant->isSpectator())
if (participant->isSpectator()) {
++result;
}
}
return result;
}
@ -269,8 +273,9 @@ void Server_Game::createGameStateChangedEvent(Event_GameStateChanged *event,
event->set_game_started(true);
event->set_active_player_id(0);
event->set_active_phase(0);
} else
} else {
event->set_game_started(false);
}
for (Server_AbstractParticipant *participant : participants.values()) {
participant->getInfo(event->add_player_list(), recipient, omniscient, withUserInfo);
@ -367,8 +372,9 @@ void Server_Game::doStartGameIfReady(bool forceStartGame)
delete replayCont;
startTimeOfThisGame = secondsElapsed;
} else
} else {
firstGameStarted = true;
}
sendGameStateToPlayers();
@ -396,11 +402,13 @@ void Server_Game::stopGameIfFinished()
int playing = 0;
auto players = getPlayers();
for (auto *player : players.values()) {
if (!player->getConceded())
if (!player->getConceded()) {
++playing;
}
}
if (playing > 1)
if (playing > 1) {
return;
}
gameStarted = false;
@ -428,32 +436,40 @@ Response::ResponseCode Server_Game::checkJoin(ServerInfo_User *user,
{
Server_DatabaseInterface *databaseInterface = room->getServer()->getDatabaseInterface();
for (auto *participant : participants.values()) {
if (participant->getUserInfo()->name() == user->name())
if (participant->getUserInfo()->name() == user->name()) {
return Response::RespContextError;
}
}
if (asJudge && !(user->user_level() & ServerInfo_User::IsJudge)) {
return Response::RespUserLevelTooLow;
}
if (!(overrideRestrictions && (user->user_level() & ServerInfo_User::IsModerator))) {
if ((_password != password) && !(spectator && !spectatorsNeedPassword))
if ((_password != password) && !(spectator && !spectatorsNeedPassword)) {
return Response::RespWrongPassword;
if (!(user->user_level() & ServerInfo_User::IsRegistered) && onlyRegistered)
}
if (!(user->user_level() & ServerInfo_User::IsRegistered) && onlyRegistered) {
return Response::RespUserLevelTooLow;
if (onlyBuddies && (user->name() != creatorInfo->name()))
}
if (onlyBuddies && (user->name() != creatorInfo->name())) {
if (!databaseInterface->isInBuddyList(QString::fromStdString(creatorInfo->name()),
QString::fromStdString(user->name())))
QString::fromStdString(user->name()))) {
return Response::RespOnlyBuddies;
}
}
if (databaseInterface->isInIgnoreList(QString::fromStdString(creatorInfo->name()),
QString::fromStdString(user->name())))
QString::fromStdString(user->name()))) {
return Response::RespInIgnoreList;
}
if (spectator) {
if (!spectatorsAllowed)
if (!spectatorsAllowed) {
return Response::RespSpectatorsNotAllowed;
}
}
}
if (!spectator && (gameStarted || (getPlayerCount() >= getMaxPlayers())))
if (!spectator && (gameStarted || (getPlayerCount() >= getMaxPlayers()))) {
return Response::RespGameFull;
}
return Response::RespOk;
}
@ -463,8 +479,9 @@ bool Server_Game::containsUser(const QString &userName) const
QMutexLocker locker(&gameMutex);
for (auto *participant : participants.values()) {
if (participant->getUserInfo()->name() == userName.toStdString())
if (participant->getUserInfo()->name() == userName.toStdString()) {
return true;
}
}
return false;
}
@ -516,8 +533,9 @@ void Server_Game::addPlayer(Server_AbstractUserInterface *userInterface,
emit gameInfoChanged(gameInfo);
}
if ((newParticipant->getUserInfo()->user_level() & ServerInfo_User::IsRegistered) && !spectator)
if ((newParticipant->getUserInfo()->user_level() & ServerInfo_User::IsRegistered) && !spectator) {
room->getServer()->addPersistentPlayer(playerName, room->getId(), gameId, newParticipant->getPlayerId());
}
userInterface->playerAddedToGame(gameId, room->getId(), newParticipant->getPlayerId());
@ -564,8 +582,9 @@ void Server_Game::removeParticipant(Server_AbstractParticipant *participant, Eve
}
if (!spectator) {
stopGameIfFinished();
if (gameStarted && playerActive)
if (gameStarted && playerActive) {
nextTurn();
}
}
ServerInfo_Game gameInfo;
@ -588,15 +607,18 @@ void Server_Game::removeArrowsRelatedToPlayer(GameEventStorage &ges, Server_Abst
for (auto *arrow : anyPlayer->getArrows().values()) {
auto *targetCard = qobject_cast<Server_Card *>(arrow->getTargetItem());
if (targetCard) {
if (targetCard->getZone() != nullptr && targetCard->getZone()->getPlayer() == player)
if (targetCard->getZone() != nullptr && targetCard->getZone()->getPlayer() == player) {
toDelete.append(arrow);
}
} else if (arrow->getTargetItem() == player) {
toDelete.append(arrow);
}
// Don't use else here! It has to happen regardless of whether targetCard == 0.
if (arrow->getStartCard()->getZone() != nullptr && arrow->getStartCard()->getZone()->getPlayer() == player)
if (arrow->getStartCard()->getZone() != nullptr &&
arrow->getStartCard()->getZone()->getPlayer() == player) {
toDelete.append(arrow);
}
}
for (auto *arrow : toDelete) {
Event_DeleteArrow event;
@ -635,8 +657,9 @@ bool Server_Game::kickParticipant(int playerId)
QMutexLocker locker(&gameMutex);
auto *participant = participants.value(playerId);
if (!participant)
if (!participant) {
return false;
}
GameEventContainer *gec = prepareGameEvent(Event_Kicked(), -1);
participant->sendGameEvent(*gec);
@ -769,8 +792,9 @@ void Server_Game::sendGameEventContainer(GameEventContainer *cont,
const bool playerPrivate = (participant->getPlayerId() == privatePlayerId) || participant->isJudge() ||
(participant->isSpectator() && spectatorsSeeEverything);
if ((recipients.testFlag(GameEventStorageItem::SendToPrivate) && playerPrivate) ||
(recipients.testFlag(GameEventStorageItem::SendToOthers) && !playerPrivate))
(recipients.testFlag(GameEventStorageItem::SendToOthers) && !playerPrivate)) {
participant->sendGameEvent(*cont);
}
}
if (recipients.testFlag(GameEventStorageItem::SendToPrivate)) {
cont->set_seconds_elapsed(secondsElapsed - startTimeOfThisGame);
@ -786,11 +810,13 @@ Server_Game::prepareGameEvent(const ::google::protobuf::Message &gameEvent, int
{
auto *cont = new GameEventContainer;
cont->set_game_id(gameId);
if (context)
if (context) {
cont->mutable_context()->CopyFrom(*context);
}
GameEvent *event = cont->add_event_list();
if (playerId != -1)
if (playerId != -1) {
event->set_player_id(playerId);
}
event->GetReflection()
->MutableMessage(event, gameEvent.GetDescriptor()->FindExtensionByName("ext"))
->CopyFrom(gameEvent);