Replays split up, one for each subgame

This commit is contained in:
Max-Wilhelm Bruker 2012-03-02 20:45:04 +01:00
parent 4dc712286f
commit 8481e61e8f
15 changed files with 278 additions and 119 deletions

View file

@ -538,42 +538,21 @@ void Servatrice::statusUpdate()
execSqlQuery(query);
}
void Servatrice::storeGameInformation(int secondsElapsed, const QSet<QString> &allPlayersEver, const QSet<QString> &allSpectatorsEver, const GameReplay &replay)
void Servatrice::storeGameInformation(int secondsElapsed, const QSet<QString> &allPlayersEver, const QSet<QString> &allSpectatorsEver, const QList<GameReplay *> &replayList)
{
Server_Room *room = rooms.value(replay.game_info().room_id());
const ServerInfo_Game &gameInfo = replayList.first()->game_info();
Server_Room *room = rooms.value(gameInfo.room_id());
const QStringList &allGameTypes = room->getGameTypes();
QStringList gameTypes;
for (int i = replay.game_info().game_types_size() - 1; i >= 0; --i)
gameTypes.append(allGameTypes[replay.game_info().game_types(i)]);
QByteArray replayBlob;
const unsigned int size = replay.ByteSize();
replayBlob.resize(size);
replay.SerializeToArray(replayBlob.data(), size);
QMutexLocker locker(&dbMutex);
if (!checkSql())
return;
QSqlQuery query1;
query1.prepare("insert into " + dbPrefix + "_games (room_name, id, descr, creator_name, password, game_types, player_count, time_started, time_finished, replay) values (:id_room, :id_game, :descr, :creator_name, :password, :game_types, :player_count, date_sub(now(), interval :seconds second), now(), :replay)");
query1.bindValue(":room_name", room->getName());
query1.bindValue(":id_game", replay.game_info().game_id());
query1.bindValue(":descr", QString::fromStdString(replay.game_info().description()));
query1.bindValue(":creator_name", QString::fromStdString(replay.game_info().creator_info().name()));
query1.bindValue(":password", replay.game_info().with_password() ? 1 : 0);
query1.bindValue(":game_types", gameTypes.isEmpty() ? QString("") : gameTypes.join(", "));
query1.bindValue(":player_count", replay.game_info().max_players());
query1.bindValue(":seconds", secondsElapsed);
query1.bindValue(":replay", replayBlob);
if (!execSqlQuery(query1))
return;
for (int i = gameInfo.game_types_size() - 1; i >= 0; --i)
gameTypes.append(allGameTypes[gameInfo.game_types(i)]);
QVariantList gameIds1, playerNames, gameIds2, userIds, replayNames;
QSetIterator<QString> playerIterator(allPlayersEver);
while (playerIterator.hasNext()) {
gameIds1.append(replay.game_info().game_id());
gameIds1.append(gameInfo.game_id());
playerNames.append(playerIterator.next());
}
QSet<QString> allUsersInGame = allPlayersEver + allSpectatorsEver;
@ -582,17 +561,53 @@ void Servatrice::storeGameInformation(int secondsElapsed, const QSet<QString> &a
int id = getUserIdInDB(allUsersIterator.next());
if (id == -1)
continue;
gameIds2.append(replay.game_info().game_id());
gameIds2.append(gameInfo.game_id());
userIds.append(id);
replayNames.append(QString::fromStdString(replay.game_info().description()));
replayNames.append(QString::fromStdString(gameInfo.description()));
}
QVariantList replayGameIds, replayDurations, replayBlobs;
for (int i = 0; i < replayList.size(); ++i) {
QByteArray blob;
const unsigned int size = replayList[i]->ByteSize();
blob.resize(size);
replayList[i]->SerializeToArray(blob.data(), size);
replayGameIds.append(gameInfo.game_id());
replayDurations.append(replayList[i]->duration_seconds());
replayBlobs.append(blob);
}
QMutexLocker locker(&dbMutex);
if (!checkSql())
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.bindValue(":room_name", room->getName());
query1.bindValue(":id_game", gameInfo.game_id());
query1.bindValue(":descr", QString::fromStdString(gameInfo.description()));
query1.bindValue(":creator_name", QString::fromStdString(gameInfo.creator_info().name()));
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;
QSqlQuery query2;
query2.prepare("insert into " + dbPrefix + "_games_players (id_game, player_name) values (:id_game, :player_name)");
query2.bindValue(":id_game", gameIds1);
query2.bindValue(":player_name", playerNames);
query2.execBatch();
QSqlQuery replayQuery1;
replayQuery1.prepare("insert into " + dbPrefix + "_replays (id_game, duration, replay) values (:id_game, :duration, :replay)");
replayQuery1.bindValue(":id_game", replayGameIds);
replayQuery1.bindValue(":duration", replayDurations);
replayQuery1.bindValue(":replay", replayBlobs);
replayQuery1.execBatch();
QSqlQuery query3;
query3.prepare("insert into " + dbPrefix + "_replays_access (id_game, id_player, replay_name) values (:id_game, :id_player, :replay_name)");
query3.bindValue(":id_game", gameIds2);