miscellaneous refactors (#4521)

This commit is contained in:
ebbit1q
2022-01-16 23:58:53 +01:00
committed by GitHub
parent 994704d353
commit ae9b8b8f34
8 changed files with 32 additions and 50 deletions

View File

@@ -44,7 +44,7 @@ If you'd like to help contribute to Cockatrice in any way, check out our [README
> ⚠️ **With this release, we no longer provide a ready-to-install binary for:**
> --DEPRECATED-OS-HERE--
-->
- Run the internal software updater: <kbd>Help → Check for Client Updates</kbd>
Don't forget to update your card database right after! (<kbd>Help → Check for Card Updates...</kbd>)

View File

@@ -2615,15 +2615,6 @@ void Player::clearArrows()
void Player::rearrangeCounters()
{
qreal marginTop = 80;
// Determine total height of bounding rectangles
qreal totalHeight = 0;
for (const auto &counter : counters) {
if (counter->getShownInCounterArea()) {
totalHeight += counter->boundingRect().height();
}
}
const qreal padding = 5;
qreal ySize = boundingRect().y() + marginTop;

View File

@@ -4,11 +4,6 @@
#include <QCryptographicHash>
void PasswordHasher::initialize()
{
// dummy
}
QString PasswordHasher::computeHash(const QString &password, const QString &salt)
{
QCryptographicHash::Algorithm algo = QCryptographicHash::Sha512;
@@ -41,4 +36,4 @@ QString PasswordHasher::generateRandomSalt(const int len)
QString PasswordHasher::generateActivationToken()
{
return QCryptographicHash::hash(generateRandomSalt().toUtf8(), QCryptographicHash::Md5).toBase64().left(16);
}
}

View File

@@ -6,7 +6,6 @@
class PasswordHasher
{
public:
static void initialize();
static QString computeHash(const QString &password, const QString &salt);
static QString generateRandomSalt(const int len = 16);
static QString generateActivationToken();

View File

@@ -50,9 +50,9 @@ void Server_ProtocolHandler::prepareDestroy()
return;
deleted = true;
QMapIterator<int, Server_Room *> roomIterator(rooms);
while (roomIterator.hasNext())
roomIterator.next().value()->removeClient(this);
for (auto *room : rooms.values()) {
room->removeClient(this);
}
QMap<int, QPair<int, int>> tempGames(getGames());
@@ -61,27 +61,27 @@ void Server_ProtocolHandler::prepareDestroy()
while (gameIterator.hasNext()) {
gameIterator.next();
Server_Room *r = server->getRooms().value(gameIterator.value().first);
if (!r)
Server_Room *room = server->getRooms().value(gameIterator.value().first);
if (!room)
continue;
r->gamesLock.lockForRead();
Server_Game *g = r->getGames().value(gameIterator.key());
if (!g) {
r->gamesLock.unlock();
room->gamesLock.lockForRead();
Server_Game *game = room->getGames().value(gameIterator.key());
if (!game) {
room->gamesLock.unlock();
continue;
}
g->gameMutex.lock();
Server_Player *p = g->getPlayers().value(gameIterator.value().second);
game->gameMutex.lock();
Server_Player *p = game->getPlayers().value(gameIterator.value().second);
if (!p) {
g->gameMutex.unlock();
r->gamesLock.unlock();
game->gameMutex.unlock();
room->gamesLock.unlock();
continue;
}
p->disconnectClient();
g->gameMutex.unlock();
r->gamesLock.unlock();
game->gameMutex.unlock();
room->gamesLock.unlock();
}
server->roomsLock.unlock();
@@ -671,24 +671,24 @@ Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoo
return Response::RespContextError;
QReadLocker serverLocker(&server->roomsLock);
Server_Room *r = server->getRooms().value(cmd.room_id(), 0);
if (!r)
Server_Room *room = server->getRooms().value(cmd.room_id(), 0);
if (!room)
return Response::RespNameNotFound;
if (!(userInfo->user_level() & ServerInfo_User::IsModerator))
if (!(r->userMayJoin(*userInfo)))
if (!(room->userMayJoin(*userInfo)))
return Response::RespUserLevelTooLow;
r->addClient(this);
rooms.insert(r->getId(), r);
room->addClient(this);
rooms.insert(room->getId(), room);
Event_RoomSay joinMessageEvent;
joinMessageEvent.set_message(r->getJoinMessage().toStdString());
joinMessageEvent.set_message(room->getJoinMessage().toStdString());
joinMessageEvent.set_message_type(Event_RoomSay::Welcome);
rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(joinMessageEvent));
rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, room->prepareRoomEvent(joinMessageEvent));
QReadLocker chatHistoryLocker(&r->historyLock);
QList<ServerInfo_ChatMessage> chatHistory = r->getChatHistory();
QReadLocker chatHistoryLocker(&room->historyLock);
QList<ServerInfo_ChatMessage> chatHistory = room->getChatHistory();
ServerInfo_ChatMessage chatMessage;
for (int i = 0; i < chatHistory.size(); ++i) {
chatMessage = chatHistory.at(i);
@@ -697,11 +697,11 @@ Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoo
roomChatHistory.set_message_type(Event_RoomSay::ChatHistory);
roomChatHistory.set_time_of(
QDateTime::fromString(QString::fromStdString(chatMessage.time())).toMSecsSinceEpoch());
rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(roomChatHistory));
rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, room->prepareRoomEvent(roomChatHistory));
}
Response_JoinRoom *re = new Response_JoinRoom;
r->getInfo(*re->mutable_room_info(), true);
room->getInfo(*re->mutable_room_info(), true);
rc.setResponseExtension(re);
return Response::RespOk;

View File

@@ -171,8 +171,6 @@ int main(int argc, char *argv[])
std::cerr << "Servatrice " << VERSION_STRING << " starting." << std::endl;
std::cerr << "-------------------------" << std::endl;
PasswordHasher::initialize();
if (testRandom) {
testRNG();
}

View File

@@ -206,8 +206,9 @@ Servatrice::~Servatrice()
// clients live in other threads, we need to lock them
clientsLock.lockForRead();
for (auto client : clients)
for (auto *client : clients) {
QMetaObject::invokeMethod(client, "prepareDestroy", Qt::QueuedConnection);
}
clientsLock.unlock();
// client destruction is asynchronous, wait for all clients to be gone

View File

@@ -893,8 +893,7 @@ Response::ResponseCode AbstractServerSocketInterface::cmdWarnUser(const Command_
delete se;
}
QListIterator<QString> modIterator(moderatorList);
foreach (QString moderator, moderatorList) {
for (QString &moderator : moderatorList) {
QString notificationMessage = sendingModerator + " has sent a warning with the following information";
notificationMessage.append("\n Username: " + userName);
notificationMessage.append("\n Reason: " + warningReason);
@@ -987,8 +986,7 @@ Response::ResponseCode AbstractServerSocketInterface::cmdBanFromServer(const Com
}
}
QListIterator<QString> modIterator(moderatorList);
foreach (QString moderator, moderatorList) {
for (QString &moderator : moderatorList) {
QString notificationMessage =
QString::fromStdString(userInfo->name()).simplified() + " has placed a ban with the following information";
if (!userName.isEmpty())