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

View file

@ -82,7 +82,7 @@ public:
void incTxBytes(quint64 num);
void incRxBytes(quint64 num);
int getUserIdInDB(const QString &name);
void storeGameInformation(int secondsElapsed, const QSet<QString> &allPlayersEver, const QSet<QString> &allSpectatorsEver, const GameReplay &replay);
void storeGameInformation(int secondsElapsed, const QSet<QString> &allPlayersEver, const QSet<QString> &allSpectatorsEver, const QList<GameReplay *> &replays);
protected:
int startSession(const QString &userName, const QString &address);
void endSession(int sessionId);

View file

@ -503,23 +503,35 @@ Response::ResponseCode ServerSocketInterface::cmdReplayList(const Command_Replay
query1.bindValue(":id_player", userInfo->id());
servatrice->execSqlQuery(query1);
while (query1.next()) {
ServerInfo_Replay *replayInfo = re->add_replay_list();
ServerInfo_ReplayMatch *matchInfo = re->add_match_list();
const int gameId = query1.value(0).toInt();
replayInfo->set_game_id(gameId);
replayInfo->set_room_name(query1.value(2).toString().toStdString());
matchInfo->set_game_id(gameId);
matchInfo->set_room_name(query1.value(2).toString().toStdString());
const int timeStarted = query1.value(3).toDateTime().toTime_t();
const int timeFinished = query1.value(4).toDateTime().toTime_t();
replayInfo->set_time_started(timeStarted);
replayInfo->set_length(timeFinished - timeStarted);
replayInfo->set_game_name(query1.value(5).toString().toStdString());
replayInfo->set_replay_name(query1.value(1).toString().toStdString());
matchInfo->set_time_started(timeStarted);
matchInfo->set_length(timeFinished - timeStarted);
matchInfo->set_game_name(query1.value(5).toString().toStdString());
const QString replayName = query1.value(1).toString();
QSqlQuery query2;
query2.prepare("select player_name from cockatrice_games_players where id_game = :id_game");
query2.bindValue(":id_game", gameId);
servatrice->execSqlQuery(query2);
while (query2.next())
replayInfo->add_player_names(query2.value(0).toString().toStdString());
matchInfo->add_player_names(query2.value(0).toString().toStdString());
QSqlQuery query3;
query3.prepare("select id, duration from " + servatrice->getDbPrefix() + "_replays where id_game = :id_game");
query3.bindValue(":id_game", gameId);
servatrice->execSqlQuery(query3);
while (query3.next()) {
ServerInfo_Replay *replayInfo = matchInfo->add_replay_list();
replayInfo->set_replay_id(query3.value(0).toInt());
replayInfo->set_replay_name(replayName.toStdString());
replayInfo->set_duration(query3.value(1).toInt());
}
}
servatrice->dbMutex.unlock();
@ -535,8 +547,8 @@ Response::ResponseCode ServerSocketInterface::cmdReplayDownload(const Command_Re
QMutexLocker dbLocker(&servatrice->dbMutex);
QSqlQuery query1;
query1.prepare("select 1 from " + servatrice->getDbPrefix() + "_replays_access where id_game = :id_game and id_player = :id_player");
query1.bindValue(":id_game", cmd.game_id());
query1.prepare("select 1 from " + servatrice->getDbPrefix() + "_replays_access a left join " + servatrice->getDbPrefix() + "_replays b on a.id_game = b.id_game where b.id = :id_replay and a.id_player = :id_player");
query1.bindValue(":id_replay", cmd.replay_id());
query1.bindValue(":id_player", userInfo->id());
if (!servatrice->execSqlQuery(query1))
return Response::RespInternalError;
@ -544,8 +556,8 @@ Response::ResponseCode ServerSocketInterface::cmdReplayDownload(const Command_Re
return Response::RespAccessDenied;
QSqlQuery query2;
query2.prepare("select replay from " + servatrice->getDbPrefix() + "_games where id = :id_game");
query2.bindValue(":id_game", cmd.game_id());
query2.prepare("select replay from " + servatrice->getDbPrefix() + "_replays where id = :id_replay");
query2.bindValue(":id_replay", cmd.replay_id());
if (!servatrice->execSqlQuery(query2))
return Response::RespInternalError;
if (!query2.next())