From c9a84290442c651da306673a4038033142476d71 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Sat, 17 Mar 2012 23:01:56 +0100 Subject: [PATCH] always get next game/replay id from database to avoid id collisions in multi-server mode --- cockatrice/src/localserverinterface.h | 1 - common/server.h | 6 ++-- common/server_player.cpp | 3 ++ servatrice/src/servatrice.cpp | 51 ++++++++++++++++----------- servatrice/src/servatrice.h | 2 ++ 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/cockatrice/src/localserverinterface.h b/cockatrice/src/localserverinterface.h index 5520a757..3620d1bb 100644 --- a/cockatrice/src/localserverinterface.h +++ b/cockatrice/src/localserverinterface.h @@ -9,7 +9,6 @@ class LocalServerInterface : public Server_ProtocolHandler { Q_OBJECT private: - DeckList *getDeckFromDatabase(int /*deckId*/) { return 0; } Response::ResponseCode cmdAddToList(const Command_AddToList & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; } Response::ResponseCode cmdRemoveFromList(const Command_RemoveFromList & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; } Response::ResponseCode cmdDeckList(const Command_DeckList & /*cmd*/, ResponseContainer & /*rc*/) { return Response::RespFunctionNotAllowed; } diff --git a/common/server.h b/common/server.h index aec64aee..8d937cad 100644 --- a/common/server.h +++ b/common/server.h @@ -38,8 +38,8 @@ public: ~Server(); AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password, QString &reason); const QMap &getRooms() { return rooms; } - int getNextGameId() { return nextGameId++; } - int getNextReplayId() { return nextReplayId++; } + virtual int getNextGameId() { return nextGameId++; } + virtual int getNextReplayId() { return nextReplayId++; } const QMap &getUsers() const { return users; } void addClient(Server_ProtocolHandler *player); @@ -61,7 +61,7 @@ public: virtual bool isInIgnoreList(const QString &whoseList, const QString &who) { return false; } virtual void storeGameInformation(int secondsElapsed, const QSet &allPlayersEver, const QSet &allSpectatorsEver, const QList &replays) { } - virtual DeckList *getDeckFromDatabase(int deckId, const QString &userName) = 0; + virtual DeckList *getDeckFromDatabase(int deckId, const QString &userName) { return 0; } void sendIslMessage(const Response &item, int serverId = -1); void sendIslMessage(const SessionEvent &item, int serverId = -1); diff --git a/common/server_player.cpp b/common/server_player.cpp index 7d341227..969debd6 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -649,6 +649,9 @@ Response::ResponseCode Server_Player::cmdDeckSelect(const Command_DeckSelect &cm } else newDeck = new DeckList(QString::fromStdString(cmd.deck())); + if (!newDeck) + return Response::RespInternalError; + delete deck; deck = newDeck; diff --git a/servatrice/src/servatrice.cpp b/servatrice/src/servatrice.cpp index 401eb021..b677c977 100644 --- a/servatrice/src/servatrice.cpp +++ b/servatrice/src/servatrice.cpp @@ -249,24 +249,6 @@ bool Servatrice::openDatabase() } std::cerr << "OK" << std::endl; - if (!nextGameId) { - QSqlQuery query; - if (!query.exec("select max(id) from " + dbPrefix + "_games")) - return false; - if (!query.next()) - return false; - nextGameId = query.value(0).toInt() + 1; - qDebug() << "set nextGameId to " << nextGameId; - } - if (!nextReplayId) { - QSqlQuery query; - if (!query.exec("select max(id) from " + dbPrefix + "_replays")) - return false; - if (!query.next()) - return false; - nextReplayId = query.value(0).toInt() + 1; - qDebug() << "set nextReplayId to " << nextReplayId; - } return true; } @@ -710,6 +692,34 @@ void Servatrice::statusUpdate() execSqlQuery(query); } +int Servatrice::getNextGameId() +{ + if (databaseType == DatabaseNone) + return Server::getNextGameId(); + + checkSql(); + + QSqlQuery query; + query.prepare("insert into " + dbPrefix + "_games (time_started) values (now())"); + execSqlQuery(query); + + return query.lastInsertId().toInt(); +} + +int Servatrice::getNextReplayId() +{ + if (databaseType == DatabaseNone) + return Server::getNextGameId(); + + checkSql(); + + QSqlQuery query; + query.prepare("insert into " + dbPrefix + "_replays () values ()"); + execSqlQuery(query); + + return query.lastInsertId().toInt(); +} + void Servatrice::storeGameInformation(int secondsElapsed, const QSet &allPlayersEver, const QSet &allSpectatorsEver, const QList &replayList) { const ServerInfo_Game &gameInfo = replayList.first()->game_info(); @@ -782,7 +792,7 @@ void Servatrice::storeGameInformation(int secondsElapsed, const QSet &a return; QSqlQuery query1; - query1.prepare("insert into " + dbPrefix + "_games (room_name, id, descr, creator_name, password, game_types, player_count, time_started, time_finished) values (:id_room, :id_game, :descr, :creator_name, :password, :game_types, :player_count, date_sub(now(), interval :seconds second), now())"); + query1.prepare("update " + dbPrefix + "_games set room_name=:room_name, descr=:descr, creator_name=:creator_name, password=:password, game_types=:game_types, player_count=:player_count, time_finished=now() where id=:id_game"); query1.bindValue(":room_name", room->getName()); query1.bindValue(":id_game", gameInfo.game_id()); query1.bindValue(":descr", QString::fromStdString(gameInfo.description())); @@ -790,7 +800,6 @@ void Servatrice::storeGameInformation(int secondsElapsed, const QSet &a query1.bindValue(":password", gameInfo.with_password() ? 1 : 0); query1.bindValue(":game_types", gameTypes.isEmpty() ? QString("") : gameTypes.join(", ")); query1.bindValue(":player_count", gameInfo.max_players()); - query1.bindValue(":seconds", secondsElapsed); if (!execSqlQuery(query1)) return; @@ -801,7 +810,7 @@ void Servatrice::storeGameInformation(int secondsElapsed, const QSet &a query2.execBatch(); QSqlQuery replayQuery1; - replayQuery1.prepare("insert into " + dbPrefix + "_replays (id, id_game, duration, replay) values (:id_replay, :id_game, :duration, :replay)"); + replayQuery1.prepare("update " + dbPrefix + "_replays set id_game=:id_game, duration=:duration, replay=:replay where id=:id_replay"); replayQuery1.bindValue(":id_replay", replayIds); replayQuery1.bindValue(":id_game", replayGameIds); replayQuery1.bindValue(":duration", replayDurations); diff --git a/servatrice/src/servatrice.h b/servatrice/src/servatrice.h index b6a6d827..52aa6650 100644 --- a/servatrice/src/servatrice.h +++ b/servatrice/src/servatrice.h @@ -115,6 +115,8 @@ public: void incTxBytes(quint64 num); void incRxBytes(quint64 num); int getUserIdInDB(const QString &name); + int getNextGameId(); + int getNextReplayId(); void storeGameInformation(int secondsElapsed, const QSet &allPlayersEver, const QSet &allSpectatorsEver, const QList &replays); DeckList *getDeckFromDatabase(int deckId, const QString &userName);