mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2025-12-22 07:10:25 -08:00
improved code for automatic joining of persistent games; disconnecting + reconnecting works as it should with ISL
This commit is contained in:
@@ -1,7 +1,15 @@
|
||||
#include <QList>
|
||||
#include <QPair>
|
||||
#include <QDebug>
|
||||
#include "server_abstractuserinterface.h"
|
||||
#include "server_game.h"
|
||||
#include "server_response_containers.h"
|
||||
#include "server_player_reference.h"
|
||||
#include "server.h"
|
||||
#include "server_room.h"
|
||||
#include "server_game.h"
|
||||
#include "pb/event_game_joined.pb.h"
|
||||
#include "pb/event_game_state_changed.pb.h"
|
||||
#include <google/protobuf/descriptor.h>
|
||||
|
||||
void Server_AbstractUserInterface::sendProtocolItemByType(ServerMessage::MessageType type, const ::google::protobuf::Message &item)
|
||||
@@ -27,15 +35,61 @@ void Server_AbstractUserInterface::sendResponseContainer(const ResponseContainer
|
||||
for (int i = 0; i < preResponseQueue.size(); ++i)
|
||||
sendProtocolItemByType(preResponseQueue[i].first, *preResponseQueue[i].second);
|
||||
|
||||
Response response;
|
||||
response.set_cmd_id(responseContainer.getCmdId());
|
||||
response.set_response_code(responseCode);
|
||||
::google::protobuf::Message *responseExtension = responseContainer.getResponseExtension();
|
||||
if (responseExtension)
|
||||
response.GetReflection()->MutableMessage(&response, responseExtension->GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(*responseExtension);
|
||||
sendProtocolItem(response);
|
||||
if (responseCode != Response::RespNothing) {
|
||||
Response response;
|
||||
response.set_cmd_id(responseContainer.getCmdId());
|
||||
response.set_response_code(responseCode);
|
||||
::google::protobuf::Message *responseExtension = responseContainer.getResponseExtension();
|
||||
if (responseExtension)
|
||||
response.GetReflection()->MutableMessage(&response, responseExtension->GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(*responseExtension);
|
||||
sendProtocolItem(response);
|
||||
}
|
||||
|
||||
const QList<QPair<ServerMessage::MessageType, ::google::protobuf::Message *> > &postResponseQueue = responseContainer.getPostResponseQueue();
|
||||
for (int i = 0; i < postResponseQueue.size(); ++i)
|
||||
sendProtocolItemByType(postResponseQueue[i].first, *postResponseQueue[i].second);
|
||||
}
|
||||
|
||||
void Server_AbstractUserInterface::playerRemovedFromGame(Server_Game *game)
|
||||
{
|
||||
qDebug() << "Server_AbstractUserInterface::playerRemovedFromGame(): gameId =" << game->getGameId();
|
||||
|
||||
QMutexLocker locker(&gameListMutex);
|
||||
games.remove(game->getGameId());
|
||||
}
|
||||
|
||||
void Server_AbstractUserInterface::playerAddedToGame(int gameId, int roomId, int playerId)
|
||||
{
|
||||
qDebug() << "Server_AbstractUserInterface::playerAddedToGame(): gameId =" << gameId;
|
||||
|
||||
QMutexLocker locker(&gameListMutex);
|
||||
games.insert(gameId, QPair<int, int>(roomId, playerId));
|
||||
}
|
||||
|
||||
void Server_AbstractUserInterface::joinPersistentGames(ResponseContainer &rc)
|
||||
{
|
||||
QList<PlayerReference> gamesToJoin = server->getPersistentPlayerReferences(QString::fromStdString(userInfo->name()));
|
||||
|
||||
server->roomsLock.lockForRead();
|
||||
for (int i = 0; i < gamesToJoin.size(); ++i) {
|
||||
const PlayerReference &pr = gamesToJoin.at(i);
|
||||
|
||||
Server_Room *room = server->getRooms().value(pr.getRoomId());
|
||||
if (!room)
|
||||
continue;
|
||||
QMutexLocker roomGamesLocker(&room->gamesMutex);
|
||||
|
||||
Server_Game *game = room->getGames().value(pr.getGameId());
|
||||
if (!game)
|
||||
continue;
|
||||
QMutexLocker gameLocker(&game->gameMutex);
|
||||
|
||||
Server_Player *player = game->getPlayers().value(pr.getPlayerId());
|
||||
|
||||
player->setUserInterface(this);
|
||||
playerAddedToGame(game->getGameId(), room->getId(), player->getPlayerId());
|
||||
|
||||
game->createGameJoinedEvent(player, rc, true);
|
||||
}
|
||||
server->roomsLock.unlock();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user