mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-11 00:24:47 -07:00
minor improvements
This commit is contained in:
parent
3388804e8f
commit
99ff7fd15f
23 changed files with 114 additions and 95 deletions
|
|
@ -25,6 +25,8 @@
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
app.setOrganizationName("Cockatrice");
|
||||
app.setApplicationName("Servatrice");
|
||||
|
||||
Server server;
|
||||
if (!server.openDatabase()) {
|
||||
|
|
|
|||
|
|
@ -23,10 +23,12 @@
|
|||
#include "serversocket.h"
|
||||
#include "counter.h"
|
||||
#include <QtSql>
|
||||
#include <QSettings>
|
||||
|
||||
Server::Server(QObject *parent)
|
||||
: QTcpServer(parent)
|
||||
: QTcpServer(parent), nextGameId(1)
|
||||
{
|
||||
settings = new QSettings("servatrice.ini", QSettings::IniFormat, this);
|
||||
}
|
||||
|
||||
Server::~Server()
|
||||
|
|
@ -35,11 +37,14 @@ Server::~Server()
|
|||
|
||||
bool Server::openDatabase()
|
||||
{
|
||||
settings->beginGroup("database");
|
||||
QSqlDatabase sqldb = QSqlDatabase::addDatabase("QMYSQL");
|
||||
sqldb.setHostName("localhost");
|
||||
sqldb.setDatabaseName("cockatrice");
|
||||
sqldb.setUserName("cockatrice");
|
||||
sqldb.setPassword("45CdX6rmd");
|
||||
sqldb.setHostName(settings->value("hostname").toString());
|
||||
sqldb.setDatabaseName(settings->value("database").toString());
|
||||
sqldb.setUserName(settings->value("user").toString());
|
||||
sqldb.setPassword(settings->value("password").toString());
|
||||
settings->endGroup();
|
||||
|
||||
return sqldb.open();
|
||||
}
|
||||
|
||||
|
|
@ -50,9 +55,9 @@ void Server::gameCreated(ServerGame *_game, ServerSocket *_creator)
|
|||
_game->addPlayer(_creator);
|
||||
}
|
||||
|
||||
void Server::addGame(const QString name, const QString description, const QString password, const int maxPlayers, ServerSocket *creator)
|
||||
void Server::addGame(const QString description, const QString password, const int maxPlayers, ServerSocket *creator)
|
||||
{
|
||||
ServerGameThread *newThread = new ServerGameThread(name, description, password, maxPlayers, creator);
|
||||
ServerGameThread *newThread = new ServerGameThread(nextGameId++, description, password, maxPlayers, creator);
|
||||
connect(newThread, SIGNAL(gameCreated(ServerGame *, ServerSocket *)), this, SLOT(gameCreated(ServerGame *, ServerSocket *)));
|
||||
connect(newThread, SIGNAL(finished()), this, SLOT(gameClosed()));
|
||||
newThread->start();
|
||||
|
|
@ -62,8 +67,8 @@ void Server::incomingConnection(int socketId)
|
|||
{
|
||||
ServerSocket *socket = new ServerSocket(this);
|
||||
socket->setSocketDescriptor(socketId);
|
||||
connect(socket, SIGNAL(createGame(const QString, const QString, const QString, const int, ServerSocket *)), this, SLOT(addGame(const QString, const QString, const QString, const int, ServerSocket *)));
|
||||
connect(socket, SIGNAL(joinGame(const QString, ServerSocket *)), this, SLOT(addClientToGame(const QString, ServerSocket *)));
|
||||
connect(socket, SIGNAL(createGame(const QString, const QString, const int, ServerSocket *)), this, SLOT(addGame(const QString, const QString, const int, ServerSocket *)));
|
||||
connect(socket, SIGNAL(joinGame(int, ServerSocket *)), this, SLOT(addClientToGame(int, ServerSocket *)));
|
||||
socket->initConnection();
|
||||
}
|
||||
|
||||
|
|
@ -85,12 +90,12 @@ AuthenticationResult Server::checkUserPassword(const QString &user, const QStrin
|
|||
return UnknownUser;
|
||||
}
|
||||
|
||||
ServerGame *Server::getGame(const QString &name)
|
||||
ServerGame *Server::getGame(int gameId)
|
||||
{
|
||||
QListIterator<ServerGame *> i(games);
|
||||
while (i.hasNext()) {
|
||||
ServerGame *tmp = i.next();
|
||||
if ((!tmp->name.compare(name, Qt::CaseSensitive)) && !tmp->getGameStarted())
|
||||
if ((tmp->gameId == gameId) && !tmp->getGameStarted())
|
||||
return tmp;
|
||||
}
|
||||
return NULL;
|
||||
|
|
@ -111,10 +116,10 @@ QList<ServerGame *> Server::listOpenGames()
|
|||
return result;
|
||||
}
|
||||
|
||||
bool Server::checkGamePassword(const QString &name, const QString &password)
|
||||
bool Server::checkGamePassword(int gameId, const QString &password)
|
||||
{
|
||||
ServerGame *tmp;
|
||||
if ((tmp = getGame(name))) {
|
||||
if ((tmp = getGame(gameId))) {
|
||||
QMutexLocker locker(tmp->mutex);
|
||||
if ((!tmp->getGameStarted())
|
||||
&& (!tmp->password.compare(password, Qt::CaseSensitive))
|
||||
|
|
@ -124,9 +129,9 @@ bool Server::checkGamePassword(const QString &name, const QString &password)
|
|||
return false;
|
||||
}
|
||||
|
||||
void Server::addClientToGame(const QString name, ServerSocket *client)
|
||||
void Server::addClientToGame(int gameId, ServerSocket *client)
|
||||
{
|
||||
ServerGame *tmp = getGame(name);
|
||||
ServerGame *tmp = getGame(gameId);
|
||||
client->moveToThread(tmp->thread());
|
||||
tmp->addPlayer(client);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@
|
|||
class ServerGame;
|
||||
class ServerSocket;
|
||||
class QSqlDatabase;
|
||||
class QSettings;
|
||||
|
||||
enum AuthenticationResult { PasswordWrong = 0, PasswordRight = 1, UnknownUser = 2 };
|
||||
|
||||
|
|
@ -32,21 +33,23 @@ class Server : public QTcpServer
|
|||
{
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
void addGame(const QString name, const QString description, const QString password, const int maxPlayers, ServerSocket *creator);
|
||||
void addClientToGame(const QString name, ServerSocket *client);
|
||||
void addGame(const QString description, const QString password, const int maxPlayers, ServerSocket *creator);
|
||||
void addClientToGame(int gameId, ServerSocket *client);
|
||||
void gameCreated(ServerGame *_game, ServerSocket *_creator);
|
||||
void gameClosed();
|
||||
public:
|
||||
Server(QObject *parent = 0);
|
||||
~Server();
|
||||
QSettings *settings;
|
||||
bool openDatabase();
|
||||
bool checkGamePassword(const QString &name, const QString &password);
|
||||
bool checkGamePassword(int gameId, const QString &password);
|
||||
AuthenticationResult checkUserPassword(const QString &user, const QString &password);
|
||||
QList<ServerGame *> listOpenGames();
|
||||
ServerGame *getGame(const QString &name);
|
||||
ServerGame *getGame(int gameId);
|
||||
private:
|
||||
void incomingConnection(int SocketId);
|
||||
QList<ServerGame *> games;
|
||||
int nextGameId;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@
|
|||
#include "random.h"
|
||||
#include "serversocket.h"
|
||||
|
||||
ServerGame::ServerGame(QString _name, QString _description, QString _password, int _maxPlayers, QObject *parent)
|
||||
: QObject(parent), gameStarted(false), rnd(0), name(_name), description(_description), password(_password), maxPlayers(_maxPlayers)
|
||||
ServerGame::ServerGame(ServerSocket *_creator, int _gameId, QString _description, QString _password, int _maxPlayers, QObject *parent)
|
||||
: QObject(parent), gameStarted(false), rnd(0), creator(_creator), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers)
|
||||
{
|
||||
mutex = new QMutex(QMutex::Recursive);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,11 +39,12 @@ public slots:
|
|||
public:
|
||||
QMutex *mutex;
|
||||
Random *rnd;
|
||||
QString name;
|
||||
ServerSocket *creator;
|
||||
int gameId;
|
||||
QString description;
|
||||
QString password;
|
||||
int maxPlayers;
|
||||
ServerGame(QString _name, QString _description, QString _password, int _maxPlayers, QObject *parent = 0);
|
||||
ServerGame(ServerSocket *_creator, int _gameId, QString _description, QString _password, int _maxPlayers, QObject *parent = 0);
|
||||
~ServerGame();
|
||||
bool getGameStarted();
|
||||
int getPlayerCount();
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
#include "servergamethread.h"
|
||||
#include "servergame.h"
|
||||
|
||||
ServerGameThread::ServerGameThread(const QString _name, const QString _description, const QString _password, const int _maxPlayers, ServerSocket *_creator, QObject *parent)
|
||||
: QThread(parent), name(_name), description(_description), password(_password), maxPlayers(_maxPlayers), creator(_creator), game(0)
|
||||
ServerGameThread::ServerGameThread(int _gameId, const QString _description, const QString _password, const int _maxPlayers, ServerSocket *_creator, QObject *parent)
|
||||
: QThread(parent), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), creator(_creator), game(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ ServerGameThread::~ServerGameThread()
|
|||
|
||||
void ServerGameThread::run()
|
||||
{
|
||||
game = new ServerGame(name, description, password, maxPlayers);
|
||||
game = new ServerGame(creator, gameId, description, password, maxPlayers);
|
||||
emit gameCreated(game, creator);
|
||||
exec();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,14 +12,14 @@ class ServerGameThread : public QThread {
|
|||
signals:
|
||||
void gameCreated(ServerGame *_game, ServerSocket *creator);
|
||||
private:
|
||||
QString name;
|
||||
int gameId;
|
||||
QString description;
|
||||
QString password;
|
||||
int maxPlayers;
|
||||
ServerSocket *creator;
|
||||
ServerGame *game;
|
||||
public:
|
||||
ServerGameThread(const QString _name, const QString _description, const QString _password, const int _maxPlayers, ServerSocket *_creator, QObject *parent = 0);
|
||||
ServerGameThread(int _gameId, const QString _description, const QString _password, const int _maxPlayers, ServerSocket *_creator, QObject *parent = 0);
|
||||
~ServerGameThread();
|
||||
ServerGame *getGame() { return game; }
|
||||
void run();
|
||||
|
|
|
|||
|
|
@ -186,10 +186,9 @@ const ServerSocket::CommandProperties ServerSocket::commandList[ServerSocket::nu
|
|||
<< QVariant::String, &ServerSocket::cmdLogin},
|
||||
{"list_games", true, false, false, QList<QVariant::Type>(), &ServerSocket::cmdListGames},
|
||||
{"create_game", true, false, false, QList<QVariant::Type>() << QVariant::String
|
||||
<< QVariant::String
|
||||
<< QVariant::String
|
||||
<< QVariant::Int, &ServerSocket::cmdCreateGame},
|
||||
{"join_game", true, false, false, QList<QVariant::Type>() << QVariant::String
|
||||
{"join_game", true, false, false, QList<QVariant::Type>() << QVariant::Int
|
||||
<< QVariant::String, &ServerSocket::cmdJoinGame},
|
||||
{"leave_game", true, true, false, QList<QVariant::Type>(), &ServerSocket::cmdLeaveGame},
|
||||
{"list_players", true, true, false, QList<QVariant::Type>(), &ServerSocket::cmdListPlayers},
|
||||
|
|
@ -250,11 +249,12 @@ ReturnMessage::ReturnCode ServerSocket::cmdListGames(const QList<QVariant> ¶
|
|||
while (gameListIterator.hasNext()) {
|
||||
ServerGame *tmp = gameListIterator.next();
|
||||
tmp->mutex->lock();
|
||||
result << QString("%1|%2|%3|%4|%5").arg(tmp->name)
|
||||
result << QString("%1|%2|%3|%4|%5|%6").arg(tmp->gameId)
|
||||
.arg(tmp->description)
|
||||
.arg(tmp->password == "" ? 0 : 1)
|
||||
.arg(tmp->getPlayerCount())
|
||||
.arg(tmp->maxPlayers);
|
||||
.arg(tmp->maxPlayers)
|
||||
.arg(tmp->creator->PlayerName);
|
||||
tmp->mutex->unlock();
|
||||
}
|
||||
remsg->sendList(result);
|
||||
|
|
@ -263,25 +263,22 @@ ReturnMessage::ReturnCode ServerSocket::cmdListGames(const QList<QVariant> ¶
|
|||
|
||||
ReturnMessage::ReturnCode ServerSocket::cmdCreateGame(const QList<QVariant> ¶ms)
|
||||
{
|
||||
QString name = params[0].toString();
|
||||
QString description = params[1].toString();
|
||||
QString password = params[2].toString();
|
||||
int maxPlayers = params[3].toInt();
|
||||
if (server->getGame(name))
|
||||
return ReturnMessage::ReturnNameNotFound;
|
||||
QString description = params[0].toString();
|
||||
QString password = params[1].toString();
|
||||
int maxPlayers = params[2].toInt();
|
||||
leaveGame();
|
||||
emit createGame(name, description, password, maxPlayers, this);
|
||||
emit createGame(description, password, maxPlayers, this);
|
||||
return ReturnMessage::ReturnOk;
|
||||
}
|
||||
|
||||
ReturnMessage::ReturnCode ServerSocket::cmdJoinGame(const QList<QVariant> ¶ms)
|
||||
{
|
||||
QString name = params[0].toString();
|
||||
int gameId = params[0].toInt();
|
||||
QString password = params[1].toString();
|
||||
if (!server->checkGamePassword(name, password))
|
||||
if (!server->checkGamePassword(gameId, password))
|
||||
return ReturnMessage::ReturnPasswordWrong;
|
||||
leaveGame();
|
||||
emit joinGame(name, this);
|
||||
emit joinGame(gameId, this);
|
||||
return ReturnMessage::ReturnOk;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -40,8 +40,8 @@ private slots:
|
|||
void readClient();
|
||||
void catchSocketError(QAbstractSocket::SocketError socketError);
|
||||
signals:
|
||||
void createGame(const QString name, const QString description, const QString password, const int maxPlayers, ServerSocket *creator);
|
||||
void joinGame(const QString name, ServerSocket *player);
|
||||
void createGame(const QString description, const QString password, const int maxPlayers, ServerSocket *creator);
|
||||
void joinGame(int gameId, ServerSocket *player);
|
||||
void commandReceived(QString cmd, ServerSocket *player);
|
||||
void broadcastEvent(const QString &event, ServerSocket *player);
|
||||
void startGameIfReady();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue