mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-10 16:24:45 -07:00
interface & client tab for replay transfer
This commit is contained in:
parent
faf6b2c5cb
commit
c6c6a970c6
36 changed files with 702 additions and 52 deletions
|
|
@ -323,29 +323,31 @@ bool Servatrice::isInIgnoreList(const QString &whoseList, const QString &who)
|
|||
return query.next();
|
||||
}
|
||||
|
||||
ServerInfo_User Servatrice::evalUserQueryResult(const QSqlQuery &query, bool complete)
|
||||
ServerInfo_User Servatrice::evalUserQueryResult(const QSqlQuery &query, bool complete, bool withId)
|
||||
{
|
||||
ServerInfo_User result;
|
||||
|
||||
result.set_name(query.value(0).toString().toStdString());
|
||||
if (withId)
|
||||
result.set_id(query.value(0).toInt());
|
||||
result.set_name(query.value(1).toString().toStdString());
|
||||
|
||||
const QString country = query.value(4).toString();
|
||||
const QString country = query.value(5).toString();
|
||||
if (!country.isEmpty())
|
||||
result.set_country(country.toStdString());
|
||||
|
||||
if (complete) {
|
||||
const QByteArray avatarBmp = query.value(5).toByteArray();
|
||||
const QByteArray avatarBmp = query.value(6).toByteArray();
|
||||
if (avatarBmp.size())
|
||||
result.set_avatar_bmp(avatarBmp.data(), avatarBmp.size());
|
||||
}
|
||||
|
||||
const QString genderStr = query.value(3).toString();
|
||||
const QString genderStr = query.value(4).toString();
|
||||
if (genderStr == "m")
|
||||
result.set_gender(ServerInfo_User::Male);
|
||||
else if (genderStr == "f")
|
||||
result.set_gender(ServerInfo_User::Female);
|
||||
|
||||
const int is_admin = query.value(1).toInt();
|
||||
const int is_admin = query.value(2).toInt();
|
||||
int userLevel = ServerInfo_User::IsUser | ServerInfo_User::IsRegistered;
|
||||
if (is_admin == 1)
|
||||
userLevel |= ServerInfo_User::IsAdmin | ServerInfo_User::IsModerator;
|
||||
|
|
@ -353,14 +355,14 @@ ServerInfo_User Servatrice::evalUserQueryResult(const QSqlQuery &query, bool com
|
|||
userLevel |= ServerInfo_User::IsModerator;
|
||||
result.set_user_level(userLevel);
|
||||
|
||||
const QString realName = query.value(2).toString();
|
||||
const QString realName = query.value(3).toString();
|
||||
if (!realName.isEmpty())
|
||||
result.set_real_name(realName.toStdString());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ServerInfo_User Servatrice::getUserData(const QString &name)
|
||||
ServerInfo_User Servatrice::getUserData(const QString &name, bool withId)
|
||||
{
|
||||
ServerInfo_User result;
|
||||
result.set_name(name.toStdString());
|
||||
|
|
@ -372,13 +374,13 @@ ServerInfo_User Servatrice::getUserData(const QString &name)
|
|||
return result;
|
||||
|
||||
QSqlQuery query;
|
||||
query.prepare("select name, admin, realname, gender, country, avatar_bmp from " + dbPrefix + "_users where name = :name and active = 1");
|
||||
query.prepare("select id, name, admin, realname, gender, country, avatar_bmp from " + dbPrefix + "_users where name = :name and active = 1");
|
||||
query.bindValue(":name", name);
|
||||
if (!execSqlQuery(query))
|
||||
return result;
|
||||
|
||||
if (query.next())
|
||||
return evalUserQueryResult(query, true);
|
||||
return evalUserQueryResult(query, true, withId);
|
||||
else
|
||||
return result;
|
||||
} else
|
||||
|
|
@ -447,7 +449,7 @@ QMap<QString, ServerInfo_User> Servatrice::getBuddyList(const QString &name)
|
|||
checkSql();
|
||||
|
||||
QSqlQuery query;
|
||||
query.prepare("select a.name, a.admin, a.realname, a.gender, a.country from " + dbPrefix + "_users a left join " + dbPrefix + "_buddylist b on a.id = b.id_user2 left join " + dbPrefix + "_users c on b.id_user1 = c.id where c.name = :name");
|
||||
query.prepare("select a.id, a.name, a.admin, a.realname, a.gender, a.country from " + dbPrefix + "_users a left join " + dbPrefix + "_buddylist b on a.id = b.id_user2 left join " + dbPrefix + "_users c on b.id_user1 = c.id where c.name = :name");
|
||||
query.bindValue(":name", name);
|
||||
if (!execSqlQuery(query))
|
||||
return result;
|
||||
|
|
@ -469,7 +471,7 @@ QMap<QString, ServerInfo_User> Servatrice::getIgnoreList(const QString &name)
|
|||
checkSql();
|
||||
|
||||
QSqlQuery query;
|
||||
query.prepare("select a.name, a.admin, a.realname, a.gender, a.country from " + dbPrefix + "_users a left join " + dbPrefix + "_ignorelist b on a.id = b.id_user2 left join " + dbPrefix + "_users c on b.id_user1 = c.id where c.name = :name");
|
||||
query.prepare("select a.id, a.name, a.admin, a.realname, a.gender, a.country from " + dbPrefix + "_users a left join " + dbPrefix + "_ignorelist b on a.id = b.id_user2 left join " + dbPrefix + "_users c on b.id_user1 = c.id where c.name = :name");
|
||||
query.bindValue(":name", name);
|
||||
if (!execSqlQuery(query))
|
||||
return result;
|
||||
|
|
@ -536,11 +538,14 @@ void Servatrice::statusUpdate()
|
|||
execSqlQuery(query);
|
||||
}
|
||||
|
||||
void Servatrice::storeGameInformation(int secondsElapsed, const QStringList &allPlayersEver, const GameReplay &replay)
|
||||
void Servatrice::storeGameInformation(int secondsElapsed, const QSet<QString> &allPlayersEver, const QSet<QString> &allSpectatorsEver, const GameReplay &replay)
|
||||
{
|
||||
Server_Room *room = rooms.value(replay.game_info().room_id());
|
||||
|
||||
const QStringList &allGameTypes = room->getGameTypes();
|
||||
QStringList gameTypes;
|
||||
for (int i = replay.game_info().game_types_size() - 1; i >= 0; --i)
|
||||
gameTypes.append(QString::number(replay.game_info().game_types(i)));
|
||||
gameTypes.append(allGameTypes[replay.game_info().game_types(i)]);
|
||||
|
||||
QByteArray replayBlob;
|
||||
const unsigned int size = replay.ByteSize();
|
||||
|
|
@ -552,29 +557,48 @@ void Servatrice::storeGameInformation(int secondsElapsed, const QStringList &all
|
|||
return;
|
||||
|
||||
QSqlQuery query1;
|
||||
query1.prepare("insert into " + dbPrefix + "_games (id_room, 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(":id_room", replay.game_info().room_id());
|
||||
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(":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;
|
||||
|
||||
QVariantList gameIds1, playerNames, gameIds2, userIds, replayNames;
|
||||
QSetIterator<QString> playerIterator(allPlayersEver);
|
||||
while (playerIterator.hasNext()) {
|
||||
gameIds1.append(replay.game_info().game_id());
|
||||
playerNames.append(playerIterator.next());
|
||||
}
|
||||
QSet<QString> allUsersInGame = allPlayersEver + allSpectatorsEver;
|
||||
QSetIterator<QString> allUsersIterator(allUsersInGame);
|
||||
while (allUsersIterator.hasNext()) {
|
||||
int id = getUserIdInDB(allUsersIterator.next());
|
||||
if (id == -1)
|
||||
continue;
|
||||
gameIds2.append(replay.game_info().game_id());
|
||||
userIds.append(id);
|
||||
replayNames.append(QString::fromStdString(replay.game_info().description()));
|
||||
}
|
||||
|
||||
QSqlQuery query2;
|
||||
query2.prepare("insert into " + dbPrefix + "_games_players (id_game, player_name) values (:id_game, :player_name)");
|
||||
QVariantList gameIds, playerNames;
|
||||
for (int i = allPlayersEver.size() - 1; i >= 0; --i) {
|
||||
gameIds.append(replay.game_info().game_id());
|
||||
playerNames.append(allPlayersEver[i]);
|
||||
}
|
||||
query2.bindValue(":id_game", gameIds);
|
||||
query2.bindValue(":id_game", gameIds1);
|
||||
query2.bindValue(":player_name", playerNames);
|
||||
query2.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);
|
||||
query3.bindValue(":id_player", userIds);
|
||||
query3.bindValue(":replay_name", replayNames);
|
||||
query3.execBatch();
|
||||
}
|
||||
|
||||
void Servatrice::scheduleShutdown(const QString &reason, int minutes)
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ public:
|
|||
bool getThreaded() const { return threaded; }
|
||||
QString getDbPrefix() const { return dbPrefix; }
|
||||
void updateLoginMessage();
|
||||
ServerInfo_User getUserData(const QString &name);
|
||||
ServerInfo_User getUserData(const QString &name, bool withId = false);
|
||||
int getUsersWithAddress(const QHostAddress &address) const;
|
||||
QList<ServerSocketInterface *> getUsersWithAddressAsList(const QHostAddress &address) const;
|
||||
QMap<QString, ServerInfo_User> getBuddyList(const QString &name);
|
||||
|
|
@ -82,7 +82,7 @@ public:
|
|||
void incTxBytes(quint64 num);
|
||||
void incRxBytes(quint64 num);
|
||||
int getUserIdInDB(const QString &name);
|
||||
void storeGameInformation(int secondsElapsed, const QStringList &allPlayersEver, const GameReplay &replay);
|
||||
void storeGameInformation(int secondsElapsed, const QSet<QString> &allPlayersEver, const QSet<QString> &allSpectatorsEver, const GameReplay &replay);
|
||||
protected:
|
||||
int startSession(const QString &userName, const QString &address);
|
||||
void endSession(int sessionId);
|
||||
|
|
@ -106,7 +106,7 @@ private:
|
|||
quint64 txBytes, rxBytes;
|
||||
int maxGameInactivityTime, maxPlayerInactivityTime;
|
||||
int maxUsersPerAddress, messageCountingInterval, maxMessageCountPerInterval, maxMessageSizePerInterval, maxGamesPerUser;
|
||||
ServerInfo_User evalUserQueryResult(const QSqlQuery &query, bool complete);
|
||||
ServerInfo_User evalUserQueryResult(const QSqlQuery &query, bool complete, bool withId = false);
|
||||
|
||||
QString shutdownReason;
|
||||
int shutdownMinutes;
|
||||
|
|
|
|||
|
|
@ -34,6 +34,8 @@
|
|||
#include "pb/command_deck_new_dir.pb.h"
|
||||
#include "pb/command_deck_del_dir.pb.h"
|
||||
#include "pb/command_deck_del.pb.h"
|
||||
#include "pb/command_replay_list.pb.h"
|
||||
#include "pb/command_replay_download.pb.h"
|
||||
#include "pb/event_connection_closed.pb.h"
|
||||
#include "pb/event_server_message.pb.h"
|
||||
#include "pb/event_server_identification.pb.h"
|
||||
|
|
@ -42,6 +44,9 @@
|
|||
#include "pb/response_deck_list.pb.h"
|
||||
#include "pb/response_deck_download.pb.h"
|
||||
#include "pb/response_deck_upload.pb.h"
|
||||
#include "pb/response_replay_list.pb.h"
|
||||
#include "pb/response_replay_download.pb.h"
|
||||
#include "pb/serverinfo_replay.pb.h"
|
||||
#include "pb/serverinfo_user.pb.h"
|
||||
#include "pb/serverinfo_deckstorage.pb.h"
|
||||
|
||||
|
|
@ -175,7 +180,7 @@ Response::ResponseCode ServerSocketInterface::cmdAddToList(const Command_AddToLi
|
|||
if (servatrice->isInIgnoreList(QString::fromStdString(userInfo->name()), user))
|
||||
return Response::RespContextError;
|
||||
|
||||
int id1 = servatrice->getUserIdInDB(QString::fromStdString(userInfo->name()));
|
||||
int id1 = userInfo->id();
|
||||
int id2 = servatrice->getUserIdInDB(user);
|
||||
if (id2 < 0)
|
||||
return Response::RespNameNotFound;
|
||||
|
|
@ -216,7 +221,7 @@ Response::ResponseCode ServerSocketInterface::cmdRemoveFromList(const Command_Re
|
|||
if (!servatrice->isInIgnoreList(QString::fromStdString(userInfo->name()), user))
|
||||
return Response::RespContextError;
|
||||
|
||||
int id1 = servatrice->getUserIdInDB(QString::fromStdString(userInfo->name()));
|
||||
int id1 = userInfo->id();
|
||||
int id2 = servatrice->getUserIdInDB(user);
|
||||
if (id2 < 0)
|
||||
return Response::RespNameNotFound;
|
||||
|
|
@ -485,6 +490,77 @@ Response::ResponseCode ServerSocketInterface::cmdDeckDownload(const Command_Deck
|
|||
return Response::RespOk;
|
||||
}
|
||||
|
||||
Response::ResponseCode ServerSocketInterface::cmdReplayList(const Command_ReplayList & /*cmd*/, ResponseContainer &rc)
|
||||
{
|
||||
if (authState != PasswordRight)
|
||||
return Response::RespFunctionNotAllowed;
|
||||
|
||||
Response_ReplayList *re = new Response_ReplayList;
|
||||
|
||||
servatrice->dbMutex.lock();
|
||||
QSqlQuery query1;
|
||||
query1.prepare("select a.id_game, a.replay_name, b.room_name, b.time_started, b.time_finished, b.descr from cockatrice_replays_access a left join cockatrice_games b on b.id = a.id_game where a.id_player = :id_player");
|
||||
query1.bindValue(":id_player", userInfo->id());
|
||||
servatrice->execSqlQuery(query1);
|
||||
while (query1.next()) {
|
||||
ServerInfo_Replay *replayInfo = re->add_replay_list();
|
||||
const int gameId = query1.value(0).toInt();
|
||||
replayInfo->set_game_id(gameId);
|
||||
replayInfo->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());
|
||||
|
||||
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());
|
||||
}
|
||||
servatrice->dbMutex.unlock();
|
||||
|
||||
rc.setResponseExtension(re);
|
||||
return Response::RespOk;
|
||||
}
|
||||
|
||||
Response::ResponseCode ServerSocketInterface::cmdReplayDownload(const Command_ReplayDownload &cmd, ResponseContainer &rc)
|
||||
{
|
||||
if (authState != PasswordRight)
|
||||
return Response::RespFunctionNotAllowed;
|
||||
|
||||
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.bindValue(":id_player", userInfo->id());
|
||||
if (!servatrice->execSqlQuery(query1))
|
||||
return Response::RespInternalError;
|
||||
if (!query1.next())
|
||||
return Response::RespAccessDenied;
|
||||
|
||||
QSqlQuery query2;
|
||||
query2.prepare("select replay from " + servatrice->getDbPrefix() + "_games where id = :id_game");
|
||||
query2.bindValue(":id_game", cmd.game_id());
|
||||
if (!servatrice->execSqlQuery(query2))
|
||||
return Response::RespInternalError;
|
||||
if (!query2.next())
|
||||
return Response::RespNameNotFound;
|
||||
|
||||
QByteArray data = query2.value(0).toByteArray();
|
||||
|
||||
Response_ReplayDownload *re = new Response_ReplayDownload;
|
||||
re->set_replay_data(data.data(), data.size());
|
||||
rc.setResponseExtension(re);
|
||||
|
||||
return Response::RespOk;
|
||||
}
|
||||
|
||||
|
||||
// MODERATOR FUNCTIONS.
|
||||
// May be called by admins and moderators. Permission is checked by the calling function.
|
||||
|
||||
|
|
@ -499,7 +575,7 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban
|
|||
query.prepare("insert into " + servatrice->getDbPrefix() + "_bans (user_name, ip_address, id_admin, time_from, minutes, reason, visible_reason) values(:user_name, :ip_address, :id_admin, NOW(), :minutes, :reason, :visible_reason)");
|
||||
query.bindValue(":user_name", userName);
|
||||
query.bindValue(":ip_address", address);
|
||||
query.bindValue(":id_admin", servatrice->getUserIdInDB(QString::fromStdString(userInfo->name())));
|
||||
query.bindValue(":id_admin", userInfo->id());
|
||||
query.bindValue(":minutes", minutes);
|
||||
query.bindValue(":reason", QString::fromStdString(cmd.reason()) + "\n");
|
||||
query.bindValue(":visible_reason", QString::fromStdString(cmd.visible_reason()) + "\n");
|
||||
|
|
|
|||
|
|
@ -61,6 +61,8 @@ private:
|
|||
Response::ResponseCode cmdDeckUpload(const Command_DeckUpload &cmd, ResponseContainer &rc);
|
||||
DeckList *getDeckFromDatabase(int deckId);
|
||||
Response::ResponseCode cmdDeckDownload(const Command_DeckDownload &cmd, ResponseContainer &rc);
|
||||
Response::ResponseCode cmdReplayList(const Command_ReplayList &cmd, ResponseContainer &rc);
|
||||
Response::ResponseCode cmdReplayDownload(const Command_ReplayDownload &cmd, ResponseContainer &rc);
|
||||
Response::ResponseCode cmdBanFromServer(const Command_BanFromServer &cmd, ResponseContainer &rc);
|
||||
Response::ResponseCode cmdShutdownServer(const Command_ShutdownServer &cmd, ResponseContainer &rc);
|
||||
Response::ResponseCode cmdUpdateServerMessage(const Command_UpdateServerMessage &cmd, ResponseContainer &rc);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue