mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-30 10:33:54 -07:00
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:
parent
7153f7d4c1
commit
aadee34238
173 changed files with 2725 additions and 1461 deletions
|
|
@ -66,8 +66,9 @@ void AbstractClient::processProtocolItem(const ServerMessage &item)
|
|||
const int cmdId = response.cmd_id();
|
||||
|
||||
PendingCommand *pend = pendingCommands.value(cmdId, 0);
|
||||
if (!pend)
|
||||
if (!pend) {
|
||||
return;
|
||||
}
|
||||
pendingCommands.remove(cmdId);
|
||||
|
||||
pend->processResponse(response);
|
||||
|
|
|
|||
|
|
@ -213,8 +213,9 @@ Command_Login RemoteClient::generateCommandLogin()
|
|||
|
||||
if (!clientFeatures.isEmpty()) {
|
||||
QMap<QString, bool>::iterator i;
|
||||
for (i = clientFeatures.begin(); i != clientFeatures.end(); ++i)
|
||||
for (i = clientFeatures.begin(); i != clientFeatures.end(); ++i) {
|
||||
cmdLogin.add_clientfeatures(i.key().toStdString().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
return cmdLogin;
|
||||
|
|
@ -284,8 +285,9 @@ void RemoteClient::loginResponse(const Response &response)
|
|||
|
||||
QString possibleMissingFeatures;
|
||||
if (resp.missing_features_size() > 0) {
|
||||
for (int i = 0; i < resp.missing_features_size(); ++i)
|
||||
for (int i = 0; i < resp.missing_features_size(); ++i) {
|
||||
possibleMissingFeatures.append("," + QString::fromStdString(resp.missing_features(i)));
|
||||
}
|
||||
}
|
||||
|
||||
if (response.response_code() == Response::RespOk) {
|
||||
|
|
@ -293,13 +295,15 @@ void RemoteClient::loginResponse(const Response &response)
|
|||
emit userInfoChanged(resp.user_info());
|
||||
|
||||
QList<ServerInfo_User> buddyList;
|
||||
for (int i = resp.buddy_list_size() - 1; i >= 0; --i)
|
||||
for (int i = resp.buddy_list_size() - 1; i >= 0; --i) {
|
||||
buddyList.append(resp.buddy_list(i));
|
||||
}
|
||||
emit buddyListReceived(buddyList);
|
||||
|
||||
QList<ServerInfo_User> ignoreList;
|
||||
for (int i = resp.ignore_list_size() - 1; i >= 0; --i)
|
||||
for (int i = resp.ignore_list_size() - 1; i >= 0; --i) {
|
||||
ignoreList.append(resp.ignore_list(i));
|
||||
}
|
||||
emit ignoreListReceived(ignoreList);
|
||||
|
||||
if (newMissingFeatureFound(possibleMissingFeatures) && resp.missing_features_size() > 0 &&
|
||||
|
|
@ -311,8 +315,9 @@ void RemoteClient::loginResponse(const Response &response)
|
|||
} else if (response.response_code() != Response::RespNotConnected) {
|
||||
QList<QString> missingFeatures;
|
||||
if (resp.missing_features_size() > 0) {
|
||||
for (int i = 0; i < resp.missing_features_size(); ++i)
|
||||
for (int i = 0; i < resp.missing_features_size(); ++i) {
|
||||
missingFeatures << QString::fromStdString(resp.missing_features(i));
|
||||
}
|
||||
}
|
||||
emit loginError(response.response_code(), QString::fromStdString(resp.denied_reason_str()),
|
||||
static_cast<quint32>(resp.denied_end_time()), missingFeatures);
|
||||
|
|
@ -383,11 +388,13 @@ void RemoteClient::readData()
|
|||
inputBuffer.remove(0, 4);
|
||||
messageInProgress = true;
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (inputBuffer.size() < messageLength)
|
||||
if (inputBuffer.size() < messageLength) {
|
||||
return;
|
||||
}
|
||||
|
||||
ServerMessage newServerMessage;
|
||||
bool ok = newServerMessage.ParseFromArray(inputBuffer.data(), messageLength);
|
||||
|
|
@ -403,8 +410,9 @@ void RemoteClient::readData()
|
|||
qCDebug(RemoteClientLog) << "parsing error!";
|
||||
}
|
||||
|
||||
if (getStatus() == StatusDisconnecting) // use thread-safe getter
|
||||
if (getStatus() == StatusDisconnecting) { // use thread-safe getter
|
||||
doDisconnectFromServer();
|
||||
}
|
||||
} while (!inputBuffer.isEmpty());
|
||||
}
|
||||
|
||||
|
|
@ -537,8 +545,9 @@ void RemoteClient::doDisconnectFromServer()
|
|||
pendingCommands.clear();
|
||||
|
||||
setStatus(StatusDisconnected);
|
||||
if (websocket->isValid())
|
||||
if (websocket->isValid()) {
|
||||
websocket->close();
|
||||
}
|
||||
socket->close();
|
||||
}
|
||||
|
||||
|
|
@ -615,8 +624,9 @@ bool RemoteClient::newMissingFeatureFound(const QString &_serversMissingFeatures
|
|||
QStringList serversMissingFeaturesList = _serversMissingFeatures.split(",");
|
||||
for (const QString &feature : serversMissingFeaturesList) {
|
||||
if (!feature.isEmpty()) {
|
||||
if (!networkSettingsProvider->getKnownMissingFeatures().contains(feature))
|
||||
if (!networkSettingsProvider->getKnownMissingFeatures().contains(feature)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return newMissingFeature;
|
||||
|
|
@ -628,8 +638,9 @@ void RemoteClient::clearNewClientFeatures()
|
|||
QStringList existingKnownMissingFeatures = networkSettingsProvider->getKnownMissingFeatures().split(",");
|
||||
for (const QString &existingKnownFeature : existingKnownMissingFeatures) {
|
||||
if (!existingKnownFeature.isEmpty()) {
|
||||
if (!clientFeatures.contains(existingKnownFeature))
|
||||
if (!clientFeatures.contains(existingKnownFeature)) {
|
||||
newKnownMissingFeatures.append("," + existingKnownFeature);
|
||||
}
|
||||
}
|
||||
}
|
||||
networkSettingsProvider->setKnownMissingFeatures(newKnownMissingFeatures);
|
||||
|
|
@ -667,10 +678,12 @@ void RemoteClient::requestForgotPasswordResponse(const Response &response)
|
|||
if (response.response_code() == Response::RespOk) {
|
||||
if (resp.challenge_email()) {
|
||||
emit sigPromptForForgotPasswordChallenge();
|
||||
} else
|
||||
} else {
|
||||
emit sigPromptForForgotPasswordReset();
|
||||
} else
|
||||
}
|
||||
} else {
|
||||
emit sigForgotPasswordError();
|
||||
}
|
||||
|
||||
doDisconnectFromServer();
|
||||
}
|
||||
|
|
@ -698,8 +711,9 @@ void RemoteClient::submitForgotPasswordResetResponse(const Response &response)
|
|||
{
|
||||
if (response.response_code() == Response::RespOk) {
|
||||
emit sigForgotPasswordSuccess();
|
||||
} else
|
||||
} else {
|
||||
emit sigForgotPasswordError();
|
||||
}
|
||||
|
||||
doDisconnectFromServer();
|
||||
}
|
||||
|
|
@ -732,8 +746,9 @@ void RemoteClient::submitForgotPasswordChallengeResponse(const Response &respons
|
|||
{
|
||||
if (response.response_code() == Response::RespOk) {
|
||||
emit sigPromptForForgotPasswordReset();
|
||||
} else
|
||||
} else {
|
||||
emit sigForgotPasswordError();
|
||||
}
|
||||
|
||||
doDisconnectFromServer();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -56,8 +56,9 @@ void Server::prepareDestroy()
|
|||
{
|
||||
roomsLock.lockForWrite();
|
||||
QMapIterator<int, Server_Room *> roomIterator(rooms);
|
||||
while (roomIterator.hasNext())
|
||||
while (roomIterator.hasNext()) {
|
||||
delete roomIterator.next().value();
|
||||
}
|
||||
rooms.clear();
|
||||
roomsLock.unlock();
|
||||
}
|
||||
|
|
@ -86,22 +87,25 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session,
|
|||
bool hasClientId = false;
|
||||
if (clientid.isEmpty()) {
|
||||
// client id is empty, either out dated client or client has been modified
|
||||
if (getClientIDRequiredEnabled())
|
||||
if (getClientIDRequiredEnabled()) {
|
||||
return ClientIdRequired;
|
||||
}
|
||||
} else {
|
||||
hasClientId = true;
|
||||
}
|
||||
|
||||
if (name.size() > 35)
|
||||
if (name.size() > 35) {
|
||||
name = name.left(35);
|
||||
}
|
||||
|
||||
Server_DatabaseInterface *databaseInterface = getDatabaseInterface();
|
||||
|
||||
AuthenticationResult authState = databaseInterface->checkUserPassword(session, name, password, clientid, reasonStr,
|
||||
secondsLeft, passwordNeedsHash);
|
||||
if (authState == NotLoggedIn || authState == UserIsBanned || authState == UsernameInvalid ||
|
||||
authState == UserIsInactive)
|
||||
authState == UserIsInactive) {
|
||||
return authState;
|
||||
}
|
||||
|
||||
ServerInfo_User data = databaseInterface->getUserData(name, true);
|
||||
data.set_address(session->getAddress().toStdString());
|
||||
|
|
@ -140,8 +144,9 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session,
|
|||
QString tempName = name;
|
||||
int i = 0;
|
||||
while (users.contains(tempName) || databaseInterface->activeUserExists(tempName) ||
|
||||
databaseInterface->userSessionExists(tempName))
|
||||
databaseInterface->userSessionExists(tempName)) {
|
||||
tempName = name + "_" + QString::number(++i);
|
||||
}
|
||||
name = tempName;
|
||||
data.set_name(name.toStdString());
|
||||
}
|
||||
|
|
@ -163,9 +168,11 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session,
|
|||
Event_UserJoined event;
|
||||
event.mutable_user_info()->CopyFrom(session->copyUserInfo(false));
|
||||
SessionEvent *se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||
for (auto &client : clients)
|
||||
if (client->getAcceptsUserListChanges())
|
||||
for (auto &client : clients) {
|
||||
if (client->getAcceptsUserListChanges()) {
|
||||
client->sendProtocolItem(*se);
|
||||
}
|
||||
}
|
||||
delete se;
|
||||
|
||||
event.mutable_user_info()->CopyFrom(session->copyUserInfo(true, true, true));
|
||||
|
|
@ -206,19 +213,22 @@ Server_AbstractUserInterface *Server::findUser(const QString &userName) const
|
|||
// Call this only with clientsLock set.
|
||||
|
||||
Server_AbstractUserInterface *userHandler = users.value(userName);
|
||||
if (userHandler)
|
||||
if (userHandler) {
|
||||
return userHandler;
|
||||
else
|
||||
} else {
|
||||
return externalUsers.value(userName);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::addClient(Server_ProtocolHandler *client)
|
||||
{
|
||||
if (client->getConnectionType() == "tcp")
|
||||
if (client->getConnectionType() == "tcp") {
|
||||
tcpUserCount++;
|
||||
}
|
||||
|
||||
if (client->getConnectionType() == "websocket")
|
||||
if (client->getConnectionType() == "websocket") {
|
||||
webSocketUserCount++;
|
||||
}
|
||||
|
||||
QWriteLocker locker(&clientsLock);
|
||||
clients << client;
|
||||
|
|
@ -232,11 +242,13 @@ void Server::removeClient(Server_ProtocolHandler *client)
|
|||
return;
|
||||
}
|
||||
|
||||
if (client->getConnectionType() == "tcp")
|
||||
if (client->getConnectionType() == "tcp") {
|
||||
tcpUserCount--;
|
||||
}
|
||||
|
||||
if (client->getConnectionType() == "websocket")
|
||||
if (client->getConnectionType() == "websocket") {
|
||||
webSocketUserCount--;
|
||||
}
|
||||
|
||||
QWriteLocker locker(&clientsLock);
|
||||
clients.removeAt(clientIndex);
|
||||
|
|
@ -245,9 +257,11 @@ void Server::removeClient(Server_ProtocolHandler *client)
|
|||
Event_UserLeft event;
|
||||
event.set_name(data->name());
|
||||
SessionEvent *se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||
for (auto &_client : clients)
|
||||
if (_client->getAcceptsUserListChanges())
|
||||
for (auto &_client : clients) {
|
||||
if (_client->getAcceptsUserListChanges()) {
|
||||
_client->sendProtocolItem(*se);
|
||||
}
|
||||
}
|
||||
sendIsl_SessionEvent(*se);
|
||||
delete se;
|
||||
|
||||
|
|
@ -274,8 +288,9 @@ QList<QString> Server::getOnlineModeratorList() const
|
|||
|
||||
// TODO: this line should be updated in the event there is any type of new user level created
|
||||
if (data &&
|
||||
(data->user_level() & ServerInfo_User::IsModerator || data->user_level() & ServerInfo_User::IsAdmin))
|
||||
(data->user_level() & ServerInfo_User::IsModerator || data->user_level() & ServerInfo_User::IsAdmin)) {
|
||||
results << QString::fromStdString(data->name()).simplified();
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
|
@ -293,9 +308,11 @@ void Server::externalUserJoined(const ServerInfo_User &userInfo)
|
|||
event.mutable_user_info()->CopyFrom(userInfo);
|
||||
|
||||
SessionEvent *se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||
for (auto &client : clients)
|
||||
if (client->getAcceptsUserListChanges())
|
||||
for (auto &client : clients) {
|
||||
if (client->getAcceptsUserListChanges()) {
|
||||
client->sendProtocolItem(*se);
|
||||
}
|
||||
}
|
||||
delete se;
|
||||
clientsLock.unlock();
|
||||
|
||||
|
|
@ -319,18 +336,21 @@ void Server::externalUserLeft(const QString &userName)
|
|||
while (userGamesIterator.hasNext()) {
|
||||
userGamesIterator.next();
|
||||
Server_Room *room = rooms.value(userGamesIterator.value().first);
|
||||
if (!room)
|
||||
if (!room) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QReadLocker roomGamesLocker(&room->gamesLock);
|
||||
Server_Game *game = room->getGames().value(userGamesIterator.key());
|
||||
if (!game)
|
||||
if (!game) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QMutexLocker gameLocker(&game->gameMutex);
|
||||
auto *participant = game->getParticipants().value(userGamesIterator.value().second);
|
||||
if (!participant)
|
||||
if (!participant) {
|
||||
continue;
|
||||
}
|
||||
|
||||
participant->disconnectClient();
|
||||
}
|
||||
|
|
@ -343,9 +363,11 @@ void Server::externalUserLeft(const QString &userName)
|
|||
|
||||
SessionEvent *se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||
clientsLock.lockForRead();
|
||||
for (auto &client : clients)
|
||||
if (client->getAcceptsUserListChanges())
|
||||
for (auto &client : clients) {
|
||||
if (client->getAcceptsUserListChanges()) {
|
||||
client->sendProtocolItem(*se);
|
||||
}
|
||||
}
|
||||
clientsLock.unlock();
|
||||
delete se;
|
||||
}
|
||||
|
|
@ -492,8 +514,9 @@ void Server::externalGameCommandContainerReceived(const CommandContainer &cont,
|
|||
|
||||
Response::ResponseCode resp = participant->processGameCommand(sc, responseContainer, ges);
|
||||
|
||||
if (resp != Response::RespOk)
|
||||
if (resp != Response::RespOk) {
|
||||
finalResponseCode = resp;
|
||||
}
|
||||
}
|
||||
ges.sendToGame(game);
|
||||
|
||||
|
|
@ -547,13 +570,16 @@ void Server::broadcastRoomUpdate(const ServerInfo_Room &roomInfo, bool sendToIsl
|
|||
SessionEvent *se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||
|
||||
clientsLock.lockForRead();
|
||||
for (auto &client : clients)
|
||||
if (client->getAcceptsRoomListChanges())
|
||||
for (auto &client : clients) {
|
||||
if (client->getAcceptsRoomListChanges()) {
|
||||
client->sendProtocolItem(*se);
|
||||
}
|
||||
}
|
||||
clientsLock.unlock();
|
||||
|
||||
if (sendToIsl)
|
||||
if (sendToIsl) {
|
||||
sendIsl_SessionEvent(*se);
|
||||
}
|
||||
|
||||
delete se;
|
||||
}
|
||||
|
|
@ -591,8 +617,9 @@ void Server::sendIsl_Response(const Response &item, int serverId, qint64 session
|
|||
{
|
||||
IslMessage msg;
|
||||
msg.set_message_type(IslMessage::RESPONSE);
|
||||
if (sessionId != -1)
|
||||
if (sessionId != -1) {
|
||||
msg.set_session_id(static_cast<google::protobuf::uint64>(sessionId));
|
||||
}
|
||||
msg.mutable_response()->CopyFrom(item);
|
||||
|
||||
emit sigSendIslMessage(msg, serverId);
|
||||
|
|
@ -602,8 +629,9 @@ void Server::sendIsl_SessionEvent(const SessionEvent &item, int serverId, qint64
|
|||
{
|
||||
IslMessage msg;
|
||||
msg.set_message_type(IslMessage::SESSION_EVENT);
|
||||
if (sessionId != -1)
|
||||
if (sessionId != -1) {
|
||||
msg.set_session_id(static_cast<google::protobuf::uint64>(sessionId));
|
||||
}
|
||||
msg.mutable_session_event()->CopyFrom(item);
|
||||
|
||||
emit sigSendIslMessage(msg, serverId);
|
||||
|
|
@ -613,8 +641,9 @@ void Server::sendIsl_GameEventContainer(const GameEventContainer &item, int serv
|
|||
{
|
||||
IslMessage msg;
|
||||
msg.set_message_type(IslMessage::GAME_EVENT_CONTAINER);
|
||||
if (sessionId != -1)
|
||||
if (sessionId != -1) {
|
||||
msg.set_session_id(static_cast<google::protobuf::uint64>(sessionId));
|
||||
}
|
||||
msg.mutable_game_event_container()->CopyFrom(item);
|
||||
|
||||
emit sigSendIslMessage(msg, serverId);
|
||||
|
|
@ -624,8 +653,9 @@ void Server::sendIsl_RoomEvent(const RoomEvent &item, int serverId, qint64 sessi
|
|||
{
|
||||
IslMessage msg;
|
||||
msg.set_message_type(IslMessage::ROOM_EVENT);
|
||||
if (sessionId != -1)
|
||||
if (sessionId != -1) {
|
||||
msg.set_session_id(static_cast<google::protobuf::uint64>(sessionId));
|
||||
}
|
||||
msg.mutable_room_event()->CopyFrom(item);
|
||||
|
||||
emit sigSendIslMessage(msg, serverId);
|
||||
|
|
|
|||
|
|
@ -45,25 +45,28 @@ void Server_AbstractUserInterface::sendResponseContainer(const ResponseContainer
|
|||
{
|
||||
const QList<QPair<ServerMessage::MessageType, ::google::protobuf::Message *>> &preResponseQueue =
|
||||
responseContainer.getPreResponseQueue();
|
||||
for (int i = 0; i < preResponseQueue.size(); ++i)
|
||||
for (int i = 0; i < preResponseQueue.size(); ++i) {
|
||||
sendProtocolItemByType(preResponseQueue[i].first, *preResponseQueue[i].second);
|
||||
}
|
||||
|
||||
if (responseCode != Response::RespNothing) {
|
||||
Response response;
|
||||
response.set_cmd_id(responseContainer.getCmdId());
|
||||
response.set_response_code(responseCode);
|
||||
::google::protobuf::Message *responseExtension = responseContainer.getResponseExtension();
|
||||
if (responseExtension)
|
||||
if (responseExtension) {
|
||||
response.GetReflection()
|
||||
->MutableMessage(&response, responseExtension->GetDescriptor()->FindExtensionByName("ext"))
|
||||
->CopyFrom(*responseExtension);
|
||||
}
|
||||
sendProtocolItem(response);
|
||||
}
|
||||
|
||||
const QList<QPair<ServerMessage::MessageType, ::google::protobuf::Message *>> &postResponseQueue =
|
||||
responseContainer.getPostResponseQueue();
|
||||
for (int i = 0; i < postResponseQueue.size(); ++i)
|
||||
for (int i = 0; i < postResponseQueue.size(); ++i) {
|
||||
sendProtocolItemByType(postResponseQueue[i].first, *postResponseQueue[i].second);
|
||||
}
|
||||
}
|
||||
|
||||
void Server_AbstractUserInterface::playerRemovedFromGame(Server_Game *game)
|
||||
|
|
@ -92,18 +95,21 @@ void Server_AbstractUserInterface::joinPersistentGames(ResponseContainer &rc)
|
|||
const PlayerReference &pr = gamesToJoin.at(i);
|
||||
|
||||
Server_Room *room = server->getRooms().value(pr.getRoomId());
|
||||
if (!room)
|
||||
if (!room) {
|
||||
continue;
|
||||
}
|
||||
QReadLocker roomGamesLocker(&room->gamesLock);
|
||||
|
||||
Server_Game *game = room->getGames().value(pr.getGameId());
|
||||
if (!game)
|
||||
if (!game) {
|
||||
continue;
|
||||
}
|
||||
QMutexLocker gameLocker(&game->gameMutex);
|
||||
|
||||
auto *participant = game->getParticipants().value(pr.getPlayerId());
|
||||
if (!participant)
|
||||
if (!participant) {
|
||||
continue;
|
||||
}
|
||||
|
||||
participant->setUserInterface(this);
|
||||
playerAddedToGame(game->getGameId(), room->getId(), participant->getPlayerId());
|
||||
|
|
|
|||
|
|
@ -48,8 +48,9 @@ Server_ProtocolHandler::~Server_ProtocolHandler()
|
|||
// The thread must not hold any server locks when calling this (e.g. clientsLock, roomsLock).
|
||||
void Server_ProtocolHandler::prepareDestroy()
|
||||
{
|
||||
if (deleted)
|
||||
if (deleted) {
|
||||
return;
|
||||
}
|
||||
deleted = true;
|
||||
|
||||
for (auto *room : rooms.values()) {
|
||||
|
|
@ -64,8 +65,9 @@ void Server_ProtocolHandler::prepareDestroy()
|
|||
gameIterator.next();
|
||||
|
||||
Server_Room *room = server->getRooms().value(gameIterator.value().first);
|
||||
if (!room)
|
||||
if (!room) {
|
||||
continue;
|
||||
}
|
||||
room->gamesLock.lockForRead();
|
||||
Server_Game *game = room->getGames().value(gameIterator.key());
|
||||
if (!game) {
|
||||
|
|
@ -167,8 +169,9 @@ Response::ResponseCode Server_ProtocolHandler::processSessionCommandContainer(co
|
|||
default:
|
||||
resp = processExtendedSessionCommand(num, sc, rc);
|
||||
}
|
||||
if (resp != Response::RespOk)
|
||||
if (resp != Response::RespOk) {
|
||||
finalResponseCode = resp;
|
||||
}
|
||||
}
|
||||
return finalResponseCode;
|
||||
}
|
||||
|
|
@ -176,13 +179,15 @@ Response::ResponseCode Server_ProtocolHandler::processSessionCommandContainer(co
|
|||
Response::ResponseCode Server_ProtocolHandler::processRoomCommandContainer(const CommandContainer &cont,
|
||||
ResponseContainer &rc)
|
||||
{
|
||||
if (authState == NotLoggedIn)
|
||||
if (authState == NotLoggedIn) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
QReadLocker locker(&server->roomsLock);
|
||||
Server_Room *room = rooms.value(cont.room_id(), 0);
|
||||
if (!room)
|
||||
if (!room) {
|
||||
return Response::RespNotInRoom;
|
||||
}
|
||||
|
||||
resetIdleTimer();
|
||||
|
||||
|
|
@ -206,8 +211,9 @@ Response::ResponseCode Server_ProtocolHandler::processRoomCommandContainer(const
|
|||
resp = cmdJoinGame(sc.GetExtension(Command_JoinGame::ext), room, rc);
|
||||
break;
|
||||
}
|
||||
if (resp != Response::RespOk)
|
||||
if (resp != Response::RespOk) {
|
||||
finalResponseCode = resp;
|
||||
}
|
||||
}
|
||||
return finalResponseCode;
|
||||
}
|
||||
|
|
@ -232,18 +238,21 @@ Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const
|
|||
// allows a user to sideboard without receiving flooding message
|
||||
<< GameCommand::MOVE_CARD;
|
||||
|
||||
if (authState == NotLoggedIn)
|
||||
if (authState == NotLoggedIn) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
QMap<int, QPair<int, int>> gameMap = getGames();
|
||||
if (!gameMap.contains(cont.game_id()))
|
||||
if (!gameMap.contains(cont.game_id())) {
|
||||
return Response::RespNotInRoom;
|
||||
}
|
||||
const QPair<int, int> roomIdAndPlayerId = gameMap.value(cont.game_id());
|
||||
|
||||
QReadLocker roomsLocker(&server->roomsLock);
|
||||
Server_Room *room = server->getRooms().value(roomIdAndPlayerId.first);
|
||||
if (!room)
|
||||
if (!room) {
|
||||
return Response::RespNotInRoom;
|
||||
}
|
||||
|
||||
QReadLocker roomGamesLocker(&room->gamesLock);
|
||||
Server_Game *game = room->getGames().value(cont.game_id());
|
||||
|
|
@ -258,8 +267,9 @@ Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const
|
|||
|
||||
QMutexLocker gameLocker(&game->gameMutex);
|
||||
auto *participant = game->getParticipants().value(roomIdAndPlayerId.second);
|
||||
if (!participant)
|
||||
if (!participant) {
|
||||
return Response::RespNotInRoom;
|
||||
}
|
||||
|
||||
resetIdleTimer();
|
||||
|
||||
|
|
@ -274,11 +284,13 @@ Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const
|
|||
|
||||
if (commandCountingInterval > 0) {
|
||||
int totalCount = 0;
|
||||
if (commandCountOverTime.isEmpty())
|
||||
if (commandCountOverTime.isEmpty()) {
|
||||
commandCountOverTime.prepend(0);
|
||||
}
|
||||
|
||||
if (!antifloodCommandsWhiteList.contains((GameCommand::GameCommandType)getPbExtension(sc)))
|
||||
if (!antifloodCommandsWhiteList.contains((GameCommand::GameCommandType)getPbExtension(sc))) {
|
||||
++commandCountOverTime[0];
|
||||
}
|
||||
|
||||
for (int count : commandCountOverTime) {
|
||||
totalCount += count;
|
||||
|
|
@ -291,8 +303,9 @@ Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const
|
|||
|
||||
Response::ResponseCode resp = participant->processGameCommand(sc, rc, ges);
|
||||
|
||||
if (resp != Response::RespOk)
|
||||
if (resp != Response::RespOk) {
|
||||
finalResponseCode = resp;
|
||||
}
|
||||
}
|
||||
ges.sendToGame(game);
|
||||
|
||||
|
|
@ -302,10 +315,12 @@ Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const
|
|||
Response::ResponseCode Server_ProtocolHandler::processModeratorCommandContainer(const CommandContainer &cont,
|
||||
ResponseContainer &rc)
|
||||
{
|
||||
if (!userInfo)
|
||||
if (!userInfo) {
|
||||
return Response::RespLoginNeeded;
|
||||
if (!(userInfo->user_level() & ServerInfo_User::IsModerator))
|
||||
}
|
||||
if (!(userInfo->user_level() & ServerInfo_User::IsModerator)) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
resetIdleTimer();
|
||||
|
||||
|
|
@ -317,8 +332,9 @@ Response::ResponseCode Server_ProtocolHandler::processModeratorCommandContainer(
|
|||
logDebugMessage(getSafeDebugString(sc));
|
||||
|
||||
resp = processExtendedModeratorCommand(num, sc, rc);
|
||||
if (resp != Response::RespOk)
|
||||
if (resp != Response::RespOk) {
|
||||
finalResponseCode = resp;
|
||||
}
|
||||
}
|
||||
return finalResponseCode;
|
||||
}
|
||||
|
|
@ -326,10 +342,12 @@ Response::ResponseCode Server_ProtocolHandler::processModeratorCommandContainer(
|
|||
Response::ResponseCode Server_ProtocolHandler::processAdminCommandContainer(const CommandContainer &cont,
|
||||
ResponseContainer &rc)
|
||||
{
|
||||
if (!userInfo)
|
||||
if (!userInfo) {
|
||||
return Response::RespLoginNeeded;
|
||||
if (!(userInfo->user_level() & ServerInfo_User::IsAdmin))
|
||||
}
|
||||
if (!(userInfo->user_level() & ServerInfo_User::IsAdmin)) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
resetIdleTimer();
|
||||
|
||||
|
|
@ -341,8 +359,9 @@ Response::ResponseCode Server_ProtocolHandler::processAdminCommandContainer(cons
|
|||
logDebugMessage(getSafeDebugString(sc));
|
||||
|
||||
resp = processExtendedAdminCommand(num, sc, rc);
|
||||
if (resp != Response::RespOk)
|
||||
if (resp != Response::RespOk) {
|
||||
finalResponseCode = resp;
|
||||
}
|
||||
}
|
||||
return finalResponseCode;
|
||||
}
|
||||
|
|
@ -350,29 +369,32 @@ Response::ResponseCode Server_ProtocolHandler::processAdminCommandContainer(cons
|
|||
void Server_ProtocolHandler::processCommandContainer(const CommandContainer &cont)
|
||||
{
|
||||
// Command processing must be disabled after prepareDestroy() has been called.
|
||||
if (deleted)
|
||||
if (deleted) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastDataReceived = timeRunning;
|
||||
|
||||
ResponseContainer responseContainer(cont.has_cmd_id() ? cont.cmd_id() : -1);
|
||||
Response::ResponseCode finalResponseCode;
|
||||
|
||||
if (cont.game_command_size())
|
||||
if (cont.game_command_size()) {
|
||||
finalResponseCode = processGameCommandContainer(cont, responseContainer);
|
||||
else if (cont.room_command_size())
|
||||
} else if (cont.room_command_size()) {
|
||||
finalResponseCode = processRoomCommandContainer(cont, responseContainer);
|
||||
else if (cont.session_command_size())
|
||||
} else if (cont.session_command_size()) {
|
||||
finalResponseCode = processSessionCommandContainer(cont, responseContainer);
|
||||
else if (cont.moderator_command_size())
|
||||
} else if (cont.moderator_command_size()) {
|
||||
finalResponseCode = processModeratorCommandContainer(cont, responseContainer);
|
||||
else if (cont.admin_command_size())
|
||||
} else if (cont.admin_command_size()) {
|
||||
finalResponseCode = processAdminCommandContainer(cont, responseContainer);
|
||||
else
|
||||
} else {
|
||||
finalResponseCode = Response::RespInvalidCommand;
|
||||
}
|
||||
|
||||
if ((finalResponseCode != Response::RespNothing))
|
||||
if ((finalResponseCode != Response::RespNothing)) {
|
||||
sendResponseContainer(responseContainer, finalResponseCode);
|
||||
}
|
||||
}
|
||||
|
||||
void Server_ProtocolHandler::pingClockTimeout()
|
||||
|
|
@ -386,11 +408,13 @@ void Server_ProtocolHandler::pingClockTimeout()
|
|||
if (interval > 0) {
|
||||
if (pingclockinterval > 0) {
|
||||
messageSizeOverTime.prepend(0);
|
||||
if (messageSizeOverTime.size() > (msgcountinterval / pingclockinterval))
|
||||
if (messageSizeOverTime.size() > (msgcountinterval / pingclockinterval)) {
|
||||
messageSizeOverTime.removeLast();
|
||||
}
|
||||
messageCountOverTime.prepend(0);
|
||||
if (messageCountOverTime.size() > (msgcountinterval / pingclockinterval))
|
||||
if (messageCountOverTime.size() > (msgcountinterval / pingclockinterval)) {
|
||||
messageCountOverTime.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -398,13 +422,15 @@ void Server_ProtocolHandler::pingClockTimeout()
|
|||
if (interval > 0) {
|
||||
if (pingclockinterval > 0) {
|
||||
commandCountOverTime.prepend(0);
|
||||
if (commandCountOverTime.size() > (cmdcountinterval / pingclockinterval))
|
||||
if (commandCountOverTime.size() > (cmdcountinterval / pingclockinterval)) {
|
||||
commandCountOverTime.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (timeRunning - lastDataReceived > server->getMaxPlayerInactivityTime())
|
||||
if (timeRunning - lastDataReceived > server->getMaxPlayerInactivityTime()) {
|
||||
prepareDestroy();
|
||||
}
|
||||
|
||||
// PrivLevel users, Moderators, and Admins are not subject to the server idle timeout policy
|
||||
const bool hasPrivLevel = userInfo && QString::fromStdString(userInfo->privlevel()).toLower() != "none";
|
||||
|
|
@ -444,8 +470,9 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd
|
|||
QString password;
|
||||
bool needsHash = false;
|
||||
if (cmd.has_password()) {
|
||||
if (cmd.password().length() > MAX_NAME_LENGTH)
|
||||
if (cmd.password().length() > MAX_NAME_LENGTH) {
|
||||
return Response::RespWrongPassword;
|
||||
}
|
||||
password = QString::fromStdString(cmd.password());
|
||||
needsHash = true;
|
||||
} else if (cmd.hashed_password().length() > MAX_NAME_LENGTH) {
|
||||
|
|
@ -493,8 +520,9 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd
|
|||
case UserIsBanned: {
|
||||
auto *re = new Response_Login;
|
||||
re->set_denied_reason_str(reasonStr.toStdString());
|
||||
if (banSecondsLeft != 0)
|
||||
if (banSecondsLeft != 0) {
|
||||
re->set_denied_end_time(QDateTime::currentDateTime().addSecs(banSecondsLeft).toSecsSinceEpoch());
|
||||
}
|
||||
rc.setResponseExtension(re);
|
||||
return Response::RespUserIsBanned;
|
||||
}
|
||||
|
|
@ -539,19 +567,22 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd
|
|||
|
||||
if (authState == PasswordRight) {
|
||||
QMapIterator<QString, ServerInfo_User> buddyIterator(databaseInterface->getBuddyList(userName));
|
||||
while (buddyIterator.hasNext())
|
||||
while (buddyIterator.hasNext()) {
|
||||
re->add_buddy_list()->CopyFrom(buddyIterator.next().value());
|
||||
}
|
||||
|
||||
QMapIterator<QString, ServerInfo_User> ignoreIterator(databaseInterface->getIgnoreList(userName));
|
||||
while (ignoreIterator.hasNext())
|
||||
while (ignoreIterator.hasNext()) {
|
||||
re->add_ignore_list()->CopyFrom(ignoreIterator.next().value());
|
||||
}
|
||||
}
|
||||
|
||||
// return to client any missing features the server has that the client does not
|
||||
if (!missingClientFeatures.isEmpty()) {
|
||||
QMap<QString, bool>::iterator i;
|
||||
for (i = missingClientFeatures.begin(); i != missingClientFeatures.end(); ++i)
|
||||
for (i = missingClientFeatures.begin(); i != missingClientFeatures.end(); ++i) {
|
||||
re->add_missing_features(i.key().toStdString().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
joinPersistentGames(rc);
|
||||
|
|
@ -562,8 +593,9 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd
|
|||
|
||||
Response::ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message &cmd, ResponseContainer &rc)
|
||||
{
|
||||
if (authState == NotLoggedIn)
|
||||
if (authState == NotLoggedIn) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
QReadLocker locker(&server->clientsLock);
|
||||
|
||||
|
|
@ -599,8 +631,9 @@ Response::ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message
|
|||
Response::ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_GetGamesOfUser &cmd,
|
||||
ResponseContainer &rc)
|
||||
{
|
||||
if (authState == NotLoggedIn)
|
||||
if (authState == NotLoggedIn) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
// Do not show games to someone on the ignore list of that user, except for mods
|
||||
QString target_user = nameFromStdString(cmd.user_name());
|
||||
|
|
@ -624,8 +657,9 @@ Response::ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_G
|
|||
room->gamesLock.lockForRead();
|
||||
room->getInfo(*re->add_room_list(), false, true);
|
||||
QListIterator<ServerInfo_Game> gameIterator(room->getGamesOfUser(nameFromStdString(cmd.user_name())));
|
||||
while (gameIterator.hasNext())
|
||||
while (gameIterator.hasNext()) {
|
||||
re->add_game_list()->CopyFrom(gameIterator.next());
|
||||
}
|
||||
room->gamesLock.unlock();
|
||||
}
|
||||
server->roomsLock.unlock();
|
||||
|
|
@ -636,14 +670,15 @@ Response::ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_G
|
|||
|
||||
Response::ResponseCode Server_ProtocolHandler::cmdGetUserInfo(const Command_GetUserInfo &cmd, ResponseContainer &rc)
|
||||
{
|
||||
if (authState == NotLoggedIn)
|
||||
if (authState == NotLoggedIn) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
QString userName = nameFromStdString(cmd.user_name());
|
||||
auto *re = new Response_GetUserInfo;
|
||||
if (userName.isEmpty())
|
||||
if (userName.isEmpty()) {
|
||||
re->mutable_user_info()->CopyFrom(*userInfo);
|
||||
else {
|
||||
} else {
|
||||
|
||||
QReadLocker locker(&server->clientsLock);
|
||||
|
||||
|
|
@ -662,13 +697,15 @@ Response::ResponseCode Server_ProtocolHandler::cmdGetUserInfo(const Command_GetU
|
|||
|
||||
Response::ResponseCode Server_ProtocolHandler::cmdListRooms(const Command_ListRooms & /*cmd*/, ResponseContainer &rc)
|
||||
{
|
||||
if (authState == NotLoggedIn)
|
||||
if (authState == NotLoggedIn) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
Event_ListRooms event;
|
||||
QMapIterator<int, Server_Room *> roomIterator(server->getRooms());
|
||||
while (roomIterator.hasNext())
|
||||
while (roomIterator.hasNext()) {
|
||||
roomIterator.next().value()->getInfo(*event.add_room_list(), false);
|
||||
}
|
||||
rc.enqueuePreResponseItem(ServerMessage::SESSION_EVENT, prepareSessionEvent(event));
|
||||
|
||||
acceptsRoomListChanges = true;
|
||||
|
|
@ -677,20 +714,25 @@ Response::ResponseCode Server_ProtocolHandler::cmdListRooms(const Command_ListRo
|
|||
|
||||
Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc)
|
||||
{
|
||||
if (authState == NotLoggedIn)
|
||||
if (authState == NotLoggedIn) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
if (rooms.contains(cmd.room_id()))
|
||||
if (rooms.contains(cmd.room_id())) {
|
||||
return Response::RespContextError;
|
||||
}
|
||||
|
||||
QReadLocker serverLocker(&server->roomsLock);
|
||||
Server_Room *room = server->getRooms().value(cmd.room_id(), 0);
|
||||
if (!room)
|
||||
if (!room) {
|
||||
return Response::RespNameNotFound;
|
||||
}
|
||||
|
||||
if (!(userInfo->user_level() & ServerInfo_User::IsModerator))
|
||||
if (!(room->userMayJoin(*userInfo)))
|
||||
if (!(userInfo->user_level() & ServerInfo_User::IsModerator)) {
|
||||
if (!(room->userMayJoin(*userInfo))) {
|
||||
return Response::RespUserLevelTooLow;
|
||||
}
|
||||
}
|
||||
|
||||
room->addClient(this);
|
||||
rooms.insert(room->getId(), room);
|
||||
|
|
@ -722,17 +764,20 @@ Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoo
|
|||
|
||||
Response::ResponseCode Server_ProtocolHandler::cmdListUsers(const Command_ListUsers & /*cmd*/, ResponseContainer &rc)
|
||||
{
|
||||
if (authState == NotLoggedIn)
|
||||
if (authState == NotLoggedIn) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
auto *re = new Response_ListUsers;
|
||||
server->clientsLock.lockForRead();
|
||||
QMapIterator<QString, Server_ProtocolHandler *> userIterator = server->getUsers();
|
||||
while (userIterator.hasNext())
|
||||
while (userIterator.hasNext()) {
|
||||
re->add_user_list()->CopyFrom(userIterator.next().value()->copyUserInfo(false));
|
||||
}
|
||||
QMapIterator<QString, Server_AbstractUserInterface *> extIterator = server->getExternalUsers();
|
||||
while (extIterator.hasNext())
|
||||
while (extIterator.hasNext()) {
|
||||
re->add_user_list()->CopyFrom(extIterator.next().value()->copyUserInfo(false));
|
||||
}
|
||||
|
||||
acceptsUserListChanges = true;
|
||||
server->clientsLock.unlock();
|
||||
|
|
@ -799,10 +844,12 @@ Server_ProtocolHandler::cmdRoomSay(const Command_RoomSay &cmd, Server_Room *room
|
|||
Response::ResponseCode
|
||||
Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room *room, ResponseContainer &rc)
|
||||
{
|
||||
if (authState == NotLoggedIn)
|
||||
if (authState == NotLoggedIn) {
|
||||
return Response::RespLoginNeeded;
|
||||
if (cmd.password().length() > MAX_NAME_LENGTH)
|
||||
}
|
||||
if (cmd.password().length() > MAX_NAME_LENGTH) {
|
||||
return Response::RespContextError;
|
||||
}
|
||||
|
||||
auto level = userInfo->user_level();
|
||||
bool isJudge = level & ServerInfo_User::IsJudge;
|
||||
|
|
@ -852,8 +899,9 @@ Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room
|
|||
Response::ResponseCode
|
||||
Server_ProtocolHandler::cmdJoinGame(const Command_JoinGame &cmd, Server_Room *room, ResponseContainer &rc)
|
||||
{
|
||||
if (authState == NotLoggedIn)
|
||||
if (authState == NotLoggedIn) {
|
||||
return Response::RespLoginNeeded;
|
||||
}
|
||||
|
||||
return room->processJoinGameCommand(cmd, rc, this);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,8 +25,9 @@ GameEventStorage::GameEventStorage() : gameEventContext(0), privatePlayerId(0)
|
|||
GameEventStorage::~GameEventStorage()
|
||||
{
|
||||
delete gameEventContext;
|
||||
for (int i = 0; i < gameEventList.size(); ++i)
|
||||
for (int i = 0; i < gameEventList.size(); ++i) {
|
||||
delete gameEventList[i];
|
||||
}
|
||||
}
|
||||
|
||||
void GameEventStorage::setGameEventContext(const ::google::protobuf::Message &_gameEventContext)
|
||||
|
|
@ -44,14 +45,16 @@ void GameEventStorage::enqueueGameEvent(const ::google::protobuf::Message &event
|
|||
int _privatePlayerId)
|
||||
{
|
||||
gameEventList.append(new GameEventStorageItem(event, playerId, recipients));
|
||||
if (_privatePlayerId != -1)
|
||||
if (_privatePlayerId != -1) {
|
||||
privatePlayerId = _privatePlayerId;
|
||||
}
|
||||
}
|
||||
|
||||
void GameEventStorage::sendToGame(Server_Game *game)
|
||||
{
|
||||
if (gameEventList.isEmpty())
|
||||
if (gameEventList.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto *contPrivate = new GameEventContainer;
|
||||
auto *contOthers = new GameEventContainer;
|
||||
|
|
@ -68,10 +71,12 @@ void GameEventStorage::sendToGame(Server_Game *game)
|
|||
for (const auto &i : gameEventList) {
|
||||
const GameEvent &event = i->getGameEvent();
|
||||
const GameEventStorageItem::EventRecipients recipients = i->getRecipients();
|
||||
if (recipients.testFlag(GameEventStorageItem::SendToPrivate))
|
||||
if (recipients.testFlag(GameEventStorageItem::SendToPrivate)) {
|
||||
contPrivate->add_event_list()->CopyFrom(event);
|
||||
if (recipients.testFlag(GameEventStorageItem::SendToOthers))
|
||||
}
|
||||
if (recipients.testFlag(GameEventStorageItem::SendToOthers)) {
|
||||
contOthers->add_event_list()->CopyFrom(event);
|
||||
}
|
||||
}
|
||||
if (gameEventContext) {
|
||||
contPrivate->mutable_context()->CopyFrom(*gameEventContext);
|
||||
|
|
@ -88,8 +93,10 @@ ResponseContainer::ResponseContainer(int _cmdId) : cmdId(_cmdId), responseExtens
|
|||
ResponseContainer::~ResponseContainer()
|
||||
{
|
||||
delete responseExtension;
|
||||
for (int i = 0; i < preResponseQueue.size(); ++i)
|
||||
for (int i = 0; i < preResponseQueue.size(); ++i) {
|
||||
delete preResponseQueue[i].second;
|
||||
for (int i = 0; i < postResponseQueue.size(); ++i)
|
||||
}
|
||||
for (int i = 0; i < postResponseQueue.size(); ++i) {
|
||||
delete postResponseQueue[i].second;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,8 +42,9 @@ Server_Room::~Server_Room()
|
|||
|
||||
gamesLock.lockForWrite();
|
||||
const QList<Server_Game *> gameList = games.values();
|
||||
for (int i = 0; i < gameList.size(); ++i)
|
||||
for (int i = 0; i < gameList.size(); ++i) {
|
||||
delete gameList[i];
|
||||
}
|
||||
games.clear();
|
||||
gamesLock.unlock();
|
||||
|
||||
|
|
@ -55,19 +56,23 @@ Server_Room::~Server_Room()
|
|||
bool Server_Room::userMayJoin(const ServerInfo_User &userInfo)
|
||||
{
|
||||
|
||||
if (permissionLevel.toLower() == "administrator" || permissionLevel.toLower() == "moderator")
|
||||
if (permissionLevel.toLower() == "administrator" || permissionLevel.toLower() == "moderator") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (permissionLevel.toLower() == "registered" && !(userInfo.user_level() & ServerInfo_User::IsRegistered))
|
||||
if (permissionLevel.toLower() == "registered" && !(userInfo.user_level() & ServerInfo_User::IsRegistered)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (privilegeLevel.toLower() != "none") {
|
||||
if (privilegeLevel.toLower() == "privileged") {
|
||||
if (privilegeLevel.toLower() == "none")
|
||||
if (privilegeLevel.toLower() == "none") {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (privilegeLevel.toLower() != QString::fromStdString(userInfo.privlevel()).toLower())
|
||||
if (privilegeLevel.toLower() != QString::fromStdString(userInfo.privlevel()).toLower()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
|
@ -92,12 +97,14 @@ Server_Room::getInfo(ServerInfo_Room &result, bool complete, bool showGameTypes,
|
|||
result.set_game_count(games.size() + externalGames.size());
|
||||
if (complete) {
|
||||
QMapIterator<int, Server_Game *> gameIterator(games);
|
||||
while (gameIterator.hasNext())
|
||||
while (gameIterator.hasNext()) {
|
||||
gameIterator.next().value()->getInfo(*result.add_game_list());
|
||||
}
|
||||
if (includeExternalData) {
|
||||
QMapIterator<int, ServerInfo_Game> externalGameIterator(externalGames);
|
||||
while (externalGameIterator.hasNext())
|
||||
while (externalGameIterator.hasNext()) {
|
||||
result.add_game_list()->CopyFrom(externalGameIterator.next().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
gamesLock.unlock();
|
||||
|
|
@ -106,22 +113,25 @@ Server_Room::getInfo(ServerInfo_Room &result, bool complete, bool showGameTypes,
|
|||
result.set_player_count(users.size() + externalUsers.size());
|
||||
if (complete) {
|
||||
QMapIterator<QString, Server_ProtocolHandler *> userIterator(users);
|
||||
while (userIterator.hasNext())
|
||||
while (userIterator.hasNext()) {
|
||||
result.add_user_list()->CopyFrom(userIterator.next().value()->copyUserInfo(false));
|
||||
}
|
||||
if (includeExternalData) {
|
||||
QMapIterator<QString, ServerInfo_User_Container> externalUserIterator(externalUsers);
|
||||
while (externalUserIterator.hasNext())
|
||||
while (externalUserIterator.hasNext()) {
|
||||
result.add_user_list()->CopyFrom(externalUserIterator.next().value().copyUserInfo(false));
|
||||
}
|
||||
}
|
||||
}
|
||||
usersLock.unlock();
|
||||
|
||||
if (complete || showGameTypes)
|
||||
if (complete || showGameTypes) {
|
||||
for (int i = 0; i < gameTypes.size(); ++i) {
|
||||
ServerInfo_GameType *gameTypeInfo = result.add_gametype_list();
|
||||
gameTypeInfo->set_game_type_id(i);
|
||||
gameTypeInfo->set_description(gameTypes[i].toStdString());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -208,8 +218,9 @@ void Server_Room::removeExternalUser(const QString &_name)
|
|||
roomInfo.set_room_id(id);
|
||||
|
||||
usersLock.lockForWrite();
|
||||
if (externalUsers.contains(_name))
|
||||
if (externalUsers.contains(_name)) {
|
||||
externalUsers.remove(_name);
|
||||
}
|
||||
roomInfo.set_player_count(users.size() + externalUsers.size());
|
||||
usersLock.unlock();
|
||||
|
||||
|
|
@ -227,10 +238,11 @@ void Server_Room::updateExternalGameList(const ServerInfo_Game &gameInfo)
|
|||
roomInfo.set_room_id(id);
|
||||
|
||||
gamesLock.lockForWrite();
|
||||
if (!gameInfo.has_player_count() && externalGames.contains(gameInfo.game_id()))
|
||||
if (!gameInfo.has_player_count() && externalGames.contains(gameInfo.game_id())) {
|
||||
externalGames.remove(gameInfo.game_id());
|
||||
else
|
||||
} else {
|
||||
externalGames.insert(gameInfo.game_id(), gameInfo);
|
||||
}
|
||||
roomInfo.set_game_count(games.size() + externalGames.size());
|
||||
gamesLock.unlock();
|
||||
|
||||
|
|
@ -242,8 +254,9 @@ Response::ResponseCode Server_Room::processJoinGameCommand(const Command_JoinGam
|
|||
ResponseContainer &rc,
|
||||
Server_AbstractUserInterface *userInterface)
|
||||
{
|
||||
if (cmd.password().length() > MAX_NAME_LENGTH)
|
||||
if (cmd.password().length() > MAX_NAME_LENGTH) {
|
||||
return Response::RespWrongPassword;
|
||||
}
|
||||
// This function is called from the Server thread and from the S_PH thread.
|
||||
// server->roomsMutex is always locked.
|
||||
|
||||
|
|
@ -271,8 +284,9 @@ Response::ResponseCode Server_Room::processJoinGameCommand(const Command_JoinGam
|
|||
Response::ResponseCode result =
|
||||
game->checkJoin(userInterface->getUserInfo(), QString::fromStdString(cmd.password()), cmd.spectator(),
|
||||
cmd.override_restrictions(), cmd.join_as_judge());
|
||||
if (result == Response::RespOk)
|
||||
if (result == Response::RespOk) {
|
||||
game->addPlayer(userInterface, rc, cmd.spectator(), cmd.join_as_judge());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -329,13 +343,15 @@ void Server_Room::sendRoomEvent(RoomEvent *event, bool sendToIsl)
|
|||
usersLock.lockForRead();
|
||||
{
|
||||
QMapIterator<QString, Server_ProtocolHandler *> userIterator(users);
|
||||
while (userIterator.hasNext())
|
||||
while (userIterator.hasNext()) {
|
||||
userIterator.next().value()->sendProtocolItem(*event);
|
||||
}
|
||||
}
|
||||
usersLock.unlock();
|
||||
|
||||
if (sendToIsl)
|
||||
if (sendToIsl) {
|
||||
static_cast<Server *>(parent())->sendIsl_RoomEvent(*event);
|
||||
}
|
||||
|
||||
delete event;
|
||||
}
|
||||
|
|
@ -405,9 +421,11 @@ int Server_Room::getGamesCreatedByUser(const QString &userName) const
|
|||
|
||||
QMapIterator<int, Server_Game *> gamesIterator(games);
|
||||
int result = 0;
|
||||
while (gamesIterator.hasNext())
|
||||
if (gamesIterator.next().value()->getCreatorInfo()->name() == userName.toStdString())
|
||||
while (gamesIterator.hasNext()) {
|
||||
if (gamesIterator.next().value()->getCreatorInfo()->name() == userName.toStdString()) {
|
||||
++result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,10 +13,11 @@ ServerInfo_User_Container::ServerInfo_User_Container(const ServerInfo_User &_use
|
|||
|
||||
ServerInfo_User_Container::ServerInfo_User_Container(const ServerInfo_User_Container &other)
|
||||
{
|
||||
if (other.userInfo)
|
||||
if (other.userInfo) {
|
||||
userInfo = new ServerInfo_User(*other.userInfo);
|
||||
else
|
||||
} else {
|
||||
userInfo = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
ServerInfo_User_Container::~ServerInfo_User_Container()
|
||||
|
|
@ -45,8 +46,9 @@ ServerInfo_User &ServerInfo_User_Container::copyUserInfo(ServerInfo_User &result
|
|||
result.clear_id();
|
||||
result.clear_email();
|
||||
}
|
||||
if (!complete)
|
||||
if (!complete) {
|
||||
result.clear_avatar_bmp();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue