always get next game/replay id from database to avoid id collisions in multi-server mode

This commit is contained in:
Max-Wilhelm Bruker 2012-03-17 23:01:56 +01:00
parent 9706ecd98a
commit c9a8429044
5 changed files with 38 additions and 25 deletions

View file

@ -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; }

View file

@ -38,8 +38,8 @@ public:
~Server();
AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password, QString &reason);
const QMap<int, Server_Room *> &getRooms() { return rooms; }
int getNextGameId() { return nextGameId++; }
int getNextReplayId() { return nextReplayId++; }
virtual int getNextGameId() { return nextGameId++; }
virtual int getNextReplayId() { return nextReplayId++; }
const QMap<QString, Server_ProtocolHandler *> &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<QString> &allPlayersEver, const QSet<QString> &allSpectatorsEver, const QList<GameReplay *> &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);

View file

@ -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;

View file

@ -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<QString> &allPlayersEver, const QSet<QString> &allSpectatorsEver, const QList<GameReplay *> &replayList)
{
const ServerInfo_Game &gameInfo = replayList.first()->game_info();
@ -782,7 +792,7 @@ void Servatrice::storeGameInformation(int secondsElapsed, const QSet<QString> &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<QString> &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<QString> &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);

View file

@ -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<QString> &allPlayersEver, const QSet<QString> &allSpectatorsEver, const QList<GameReplay *> &replays);
DeckList *getDeckFromDatabase(int deckId, const QString &userName);