minor improvements
This commit is contained in:
parent
3388804e8f
commit
99ff7fd15f
23 changed files with 114 additions and 95 deletions
|
@ -130,7 +130,7 @@ void Client::readLine()
|
||||||
|
|
||||||
// XXX Parametergültigkeit überprüfen
|
// XXX Parametergültigkeit überprüfen
|
||||||
if (!prefix.compare("list_games"))
|
if (!prefix.compare("list_games"))
|
||||||
gamelist << new ServerGame(val[0], val[1], val[2].toInt(), val[3].toInt(), val[4].toInt());
|
gamelist << new ServerGame(val[0].toInt(), val[5], val[1], val[2].toInt(), val[3].toInt(), val[4].toInt());
|
||||||
else if (!prefix.compare("list_players"))
|
else if (!prefix.compare("list_players"))
|
||||||
playerlist << new ServerPlayer(val[0].toInt(), val[1]);
|
playerlist << new ServerPlayer(val[0].toInt(), val[1]);
|
||||||
else if (!prefix.compare("list_counters"))
|
else if (!prefix.compare("list_counters"))
|
||||||
|
@ -215,14 +215,14 @@ int Client::listPlayers()
|
||||||
return cmd("list_players");
|
return cmd("list_players");
|
||||||
}
|
}
|
||||||
|
|
||||||
int Client::createGame(const QString &name, const QString &description, const QString &password, unsigned int maxPlayers)
|
int Client::createGame(const QString &description, const QString &password, unsigned int maxPlayers)
|
||||||
{
|
{
|
||||||
return cmd(QString("create_game|%1|%2|%3|%4").arg(name).arg(description).arg(password).arg(maxPlayers));
|
return cmd(QString("create_game|%1|%2|%3").arg(description).arg(password).arg(maxPlayers));
|
||||||
}
|
}
|
||||||
|
|
||||||
int Client::joinGame(const QString &name, const QString &password)
|
int Client::joinGame(int gameId, const QString &password)
|
||||||
{
|
{
|
||||||
return cmd(QString("join_game|%1|%2").arg(name).arg(password));
|
return cmd(QString("join_game|%1|%2").arg(gameId).arg(password));
|
||||||
}
|
}
|
||||||
|
|
||||||
int Client::leaveGame()
|
int Client::leaveGame()
|
||||||
|
|
|
@ -64,8 +64,8 @@ public:
|
||||||
void disconnectFromServer();
|
void disconnectFromServer();
|
||||||
int listGames();
|
int listGames();
|
||||||
int listPlayers();
|
int listPlayers();
|
||||||
int createGame(const QString &name, const QString &description, const QString &password, unsigned int maxPlayers);
|
int createGame(const QString &description, const QString &password, unsigned int maxPlayers);
|
||||||
int joinGame(const QString &name, const QString &password);
|
int joinGame(int gameId, const QString &password);
|
||||||
int leaveGame();
|
int leaveGame();
|
||||||
int login(const QString &name, const QString &pass);
|
int login(const QString &name, const QString &pass);
|
||||||
int say(const QString &s);
|
int say(const QString &s);
|
||||||
|
|
|
@ -2,14 +2,8 @@
|
||||||
#include "dlg_creategame.h"
|
#include "dlg_creategame.h"
|
||||||
|
|
||||||
DlgCreateGame::DlgCreateGame(Client *_client, QWidget *parent)
|
DlgCreateGame::DlgCreateGame(Client *_client, QWidget *parent)
|
||||||
: QDialog(parent), client(_client)
|
: QDialog(parent), client(_client), msgid(0)
|
||||||
{
|
{
|
||||||
msgid = 0;
|
|
||||||
|
|
||||||
nameLabel = new QLabel(tr("&Name:"));
|
|
||||||
nameEdit = new QLineEdit;
|
|
||||||
nameLabel->setBuddy(nameEdit);
|
|
||||||
|
|
||||||
descriptionLabel = new QLabel(tr("&Description:"));
|
descriptionLabel = new QLabel(tr("&Description:"));
|
||||||
descriptionEdit = new QLineEdit;
|
descriptionEdit = new QLineEdit;
|
||||||
descriptionLabel->setBuddy(descriptionEdit);
|
descriptionLabel->setBuddy(descriptionEdit);
|
||||||
|
@ -18,19 +12,17 @@ DlgCreateGame::DlgCreateGame(Client *_client, QWidget *parent)
|
||||||
passwordEdit = new QLineEdit;
|
passwordEdit = new QLineEdit;
|
||||||
passwordLabel->setBuddy(passwordEdit);
|
passwordLabel->setBuddy(passwordEdit);
|
||||||
|
|
||||||
maxPlayersLabel = new QLabel(tr("&Max Players:"));
|
maxPlayersLabel = new QLabel(tr("P&layers:"));
|
||||||
maxPlayersEdit = new QLineEdit("2");
|
maxPlayersEdit = new QLineEdit("2");
|
||||||
maxPlayersLabel->setBuddy(maxPlayersEdit);
|
maxPlayersLabel->setBuddy(maxPlayersEdit);
|
||||||
|
|
||||||
QGridLayout *grid = new QGridLayout;
|
QGridLayout *grid = new QGridLayout;
|
||||||
grid->addWidget(nameLabel, 0, 0);
|
grid->addWidget(descriptionLabel, 0, 0);
|
||||||
grid->addWidget(nameEdit, 0, 1);
|
grid->addWidget(descriptionEdit, 0, 1);
|
||||||
grid->addWidget(descriptionLabel, 1, 0);
|
grid->addWidget(passwordLabel, 1, 0);
|
||||||
grid->addWidget(descriptionEdit, 1, 1);
|
grid->addWidget(passwordEdit, 1, 1);
|
||||||
grid->addWidget(passwordLabel, 2, 0);
|
grid->addWidget(maxPlayersLabel, 2, 0);
|
||||||
grid->addWidget(passwordEdit, 2, 1);
|
grid->addWidget(maxPlayersEdit, 2, 1);
|
||||||
grid->addWidget(maxPlayersLabel, 3, 0);
|
|
||||||
grid->addWidget(maxPlayersEdit, 3, 1);
|
|
||||||
|
|
||||||
okButton = new QPushButton(tr("&OK"));
|
okButton = new QPushButton(tr("&OK"));
|
||||||
okButton->setDefault(true);
|
okButton->setDefault(true);
|
||||||
|
@ -65,8 +57,7 @@ void DlgCreateGame::actOK()
|
||||||
QMessageBox::critical(this, tr("Error"), tr("Invalid number of players."));
|
QMessageBox::critical(this, tr("Error"), tr("Invalid number of players."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
msgid = client->createGame(nameEdit->text(),
|
msgid = client->createGame(descriptionEdit->text(),
|
||||||
descriptionEdit->text(),
|
|
||||||
passwordEdit->text(),
|
passwordEdit->text(),
|
||||||
maxPlayers);
|
maxPlayers);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,8 @@ private:
|
||||||
Client *client;
|
Client *client;
|
||||||
int msgid;
|
int msgid;
|
||||||
|
|
||||||
QLabel *nameLabel, *descriptionLabel, *passwordLabel, *maxPlayersLabel;
|
QLabel *descriptionLabel, *passwordLabel, *maxPlayersLabel;
|
||||||
QLineEdit *nameEdit, *descriptionEdit, *passwordEdit, *maxPlayersEdit;
|
QLineEdit *descriptionEdit, *passwordEdit, *maxPlayersEdit;
|
||||||
QPushButton *okButton, *cancelButton;
|
QPushButton *okButton, *cancelButton;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,8 @@
|
||||||
#include "dlg_creategame.h"
|
#include "dlg_creategame.h"
|
||||||
|
|
||||||
DlgGames::DlgGames(Client *_client, QWidget *parent)
|
DlgGames::DlgGames(Client *_client, QWidget *parent)
|
||||||
: QDialog(parent), client(_client)
|
: QDialog(parent), client(_client), msgid(0)
|
||||||
{
|
{
|
||||||
msgid = 0;
|
|
||||||
|
|
||||||
tableView = new QTreeView;
|
tableView = new QTreeView;
|
||||||
tableModel = new GamesModel(this);
|
tableModel = new GamesModel(this);
|
||||||
tableView->setModel(tableModel);
|
tableView->setModel(tableModel);
|
||||||
|
@ -28,8 +26,8 @@ DlgGames::DlgGames(Client *_client, QWidget *parent)
|
||||||
setLayout(mainLayout);
|
setLayout(mainLayout);
|
||||||
|
|
||||||
setWindowTitle(tr("Games"));
|
setWindowTitle(tr("Games"));
|
||||||
setMinimumWidth(sizeHint().width());
|
|
||||||
|
|
||||||
|
setMinimumWidth(tableView->columnWidth(0) * tableModel->columnCount());
|
||||||
connect(createButton, SIGNAL(clicked()), this, SLOT(actCreate()));
|
connect(createButton, SIGNAL(clicked()), this, SLOT(actCreate()));
|
||||||
connect(refreshButton, SIGNAL(clicked()), this, SLOT(actRefresh()));
|
connect(refreshButton, SIGNAL(clicked()), this, SLOT(actRefresh()));
|
||||||
connect(joinButton, SIGNAL(clicked()), this, SLOT(actJoin()));
|
connect(joinButton, SIGNAL(clicked()), this, SLOT(actJoin()));
|
||||||
|
@ -78,7 +76,7 @@ void DlgGames::actJoin()
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(client, SIGNAL(responseReceived(ServerResponse *)), this, SLOT(checkResponse(ServerResponse *)));
|
connect(client, SIGNAL(responseReceived(ServerResponse *)), this, SLOT(checkResponse(ServerResponse *)));
|
||||||
msgid = client->joinGame(game->getName(), password);
|
msgid = client->joinGame(game->getGameId(), password);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DlgGames::gameListReceived(QList<ServerGame *> _gameList)
|
void DlgGames::gameListReceived(QList<ServerGame *> _gameList)
|
||||||
|
|
|
@ -73,12 +73,11 @@ signals:
|
||||||
void logSetCounter(QString playerName, QString counterName, int value, int oldValue);
|
void logSetCounter(QString playerName, QString counterName, int value, int oldValue);
|
||||||
void logSetDoesntUntap(QString playerName, QString cardName, bool doesntUntap);
|
void logSetDoesntUntap(QString playerName, QString cardName, bool doesntUntap);
|
||||||
void logDumpZone(QString playerName, QString zoneName, QString zoneOwner, int numberCards);
|
void logDumpZone(QString playerName, QString zoneName, QString zoneOwner, int numberCards);
|
||||||
public slots:
|
|
||||||
void restartGameDialog();
|
|
||||||
public:
|
public:
|
||||||
Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_actionsMenu, QMenu *_cardMenu, int playerId, const QString &playerName);
|
Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_actionsMenu, QMenu *_cardMenu, int playerId, const QString &playerName);
|
||||||
~Game();
|
~Game();
|
||||||
Player *getLocalPlayer() const { return localPlayer; }
|
Player *getLocalPlayer() const { return localPlayer; }
|
||||||
|
void restartGameDialog();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -13,14 +13,14 @@ int GamesModel::rowCount(const QModelIndex &parent) const
|
||||||
|
|
||||||
int GamesModel::columnCount(const QModelIndex &parent) const
|
int GamesModel::columnCount(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
return 4;
|
return 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant GamesModel::data(const QModelIndex &index, int role) const
|
QVariant GamesModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
if (!index.isValid())
|
if (!index.isValid())
|
||||||
return QVariant();
|
return QVariant();
|
||||||
if ((index.row() >= gameList.size()) || (index.column() >= 4))
|
if ((index.row() >= gameList.size()) || (index.column() >= columnCount()))
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
if (role != Qt::DisplayRole)
|
if (role != Qt::DisplayRole)
|
||||||
|
@ -28,10 +28,11 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
|
||||||
|
|
||||||
ServerGame *g = gameList.at(index.row());
|
ServerGame *g = gameList.at(index.row());
|
||||||
switch (index.column()) {
|
switch (index.column()) {
|
||||||
case 0: return g->getName();
|
case 0: return g->getGameId();
|
||||||
case 1: return g->getDescription();
|
case 1: return g->getCreator();
|
||||||
case 2: return QString(g->getHasPassword() ? "yes" : "no");
|
case 2: return g->getDescription();
|
||||||
case 3: return QString("%1/%2").arg(g->getPlayerCount()).arg(g->getMaxPlayers());
|
case 3: return QString(g->getHasPassword() ? "yes" : "no");
|
||||||
|
case 4: return QString("%1/%2").arg(g->getPlayerCount()).arg(g->getMaxPlayers());
|
||||||
default: return QVariant();
|
default: return QVariant();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,10 +44,11 @@ QVariant GamesModel::headerData(int section, Qt::Orientation orientation, int ro
|
||||||
if (orientation != Qt::Horizontal)
|
if (orientation != Qt::Horizontal)
|
||||||
return QVariant();
|
return QVariant();
|
||||||
switch (section) {
|
switch (section) {
|
||||||
case 0: return QString("Name");
|
case 0: return QString("Game ID");
|
||||||
case 1: return QString("Description");
|
case 1: return QString("Creator");
|
||||||
case 2: return QString("Password");
|
case 2: return QString("Description");
|
||||||
case 3: return QString("Players");
|
case 3: return QString("Password");
|
||||||
|
case 4: return QString("Players");
|
||||||
default: return QVariant();
|
default: return QVariant();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -276,7 +276,6 @@ void Player::gameEvent(ServerEventData *event)
|
||||||
int colorValue = data[1].toInt();
|
int colorValue = data[1].toInt();
|
||||||
int value = data[2].toInt();
|
int value = data[2].toInt();
|
||||||
QColor color(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256);
|
QColor color(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256);
|
||||||
qDebug(QString("%1 / %2 / %3").arg(color.red()).arg(color.green()).arg(color.blue()).toLatin1());
|
|
||||||
area->addCounter(counterName, color, value);
|
area->addCounter(counterName, color, value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,15 +3,17 @@
|
||||||
|
|
||||||
class ServerGame {
|
class ServerGame {
|
||||||
private:
|
private:
|
||||||
QString name;
|
int gameId;
|
||||||
|
QString creator;
|
||||||
QString description;
|
QString description;
|
||||||
bool hasPassword;
|
bool hasPassword;
|
||||||
unsigned char playerCount;
|
unsigned char playerCount;
|
||||||
unsigned char maxPlayers;
|
unsigned char maxPlayers;
|
||||||
public:
|
public:
|
||||||
ServerGame(const QString &_name, const QString &_description, bool _hasPassword, unsigned char _playerCount, unsigned char _maxPlayers)
|
ServerGame(int _gameId, const QString &_creator, const QString &_description, bool _hasPassword, unsigned char _playerCount, unsigned char _maxPlayers)
|
||||||
: name(_name), description(_description), hasPassword(_hasPassword), playerCount(_playerCount), maxPlayers(_maxPlayers) { }
|
: gameId(_gameId), creator(_creator), description(_description), hasPassword(_hasPassword), playerCount(_playerCount), maxPlayers(_maxPlayers) { }
|
||||||
QString getName() { return name; }
|
int getGameId() { return gameId; }
|
||||||
|
QString getCreator() { return creator; }
|
||||||
QString getDescription() { return description; }
|
QString getDescription() { return description; }
|
||||||
bool getHasPassword() { return hasPassword; }
|
bool getHasPassword() { return hasPassword; }
|
||||||
unsigned char getPlayerCount() { return playerCount; }
|
unsigned char getPlayerCount() { return playerCount; }
|
||||||
|
|
|
@ -96,8 +96,15 @@ void MainWindow::actGames()
|
||||||
dlg.exec();
|
dlg.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::actRestartGame()
|
||||||
|
{
|
||||||
|
zoneLayout->clear();
|
||||||
|
game->restartGameDialog();
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::actLeaveGame()
|
void MainWindow::actLeaveGame()
|
||||||
{
|
{
|
||||||
|
zoneLayout->clear();
|
||||||
client->leaveGame();
|
client->leaveGame();
|
||||||
delete game;
|
delete game;
|
||||||
game = 0;
|
game = 0;
|
||||||
|
@ -145,7 +152,6 @@ void MainWindow::playerIdReceived(int id, QString name)
|
||||||
connect(game, SIGNAL(hoverCard(QString)), this, SLOT(hoverCard(QString)));
|
connect(game, SIGNAL(hoverCard(QString)), this, SLOT(hoverCard(QString)));
|
||||||
connect(game, SIGNAL(playerAdded(Player *)), this, SLOT(playerAdded(Player *)));
|
connect(game, SIGNAL(playerAdded(Player *)), this, SLOT(playerAdded(Player *)));
|
||||||
connect(game, SIGNAL(playerRemoved(Player *)), this, SLOT(playerRemoved(Player *)));
|
connect(game, SIGNAL(playerRemoved(Player *)), this, SLOT(playerRemoved(Player *)));
|
||||||
connect(aRestartGame, SIGNAL(triggered()), game, SLOT(restartGameDialog()));
|
|
||||||
playerAdded(game->getLocalPlayer());
|
playerAdded(game->getLocalPlayer());
|
||||||
|
|
||||||
messageLog->connectToGame(game);
|
messageLog->connectToGame(game);
|
||||||
|
@ -174,6 +180,7 @@ void MainWindow::createActions()
|
||||||
aRestartGame = new QAction(tr("&Restart game..."), this);
|
aRestartGame = new QAction(tr("&Restart game..."), this);
|
||||||
aRestartGame->setShortcut(tr("F2"));
|
aRestartGame->setShortcut(tr("F2"));
|
||||||
aRestartGame->setEnabled(false);
|
aRestartGame->setEnabled(false);
|
||||||
|
connect(aRestartGame, SIGNAL(triggered()), this, SLOT(actRestartGame()));
|
||||||
aLeaveGame = new QAction(tr("&Leave game"), this);
|
aLeaveGame = new QAction(tr("&Leave game"), this);
|
||||||
aLeaveGame->setEnabled(false);
|
aLeaveGame->setEnabled(false);
|
||||||
connect(aLeaveGame, SIGNAL(triggered()), this, SLOT(actLeaveGame()));
|
connect(aLeaveGame, SIGNAL(triggered()), this, SLOT(actLeaveGame()));
|
||||||
|
|
|
@ -53,6 +53,7 @@ private slots:
|
||||||
void actConnect();
|
void actConnect();
|
||||||
void actDisconnect();
|
void actDisconnect();
|
||||||
void actGames();
|
void actGames();
|
||||||
|
void actRestartGame();
|
||||||
void actLeaveGame();
|
void actLeaveGame();
|
||||||
void actDeckEditor();
|
void actDeckEditor();
|
||||||
void actExit();
|
void actExit();
|
||||||
|
|
|
@ -52,3 +52,9 @@ void ZoneViewLayout::closeMostRecentZoneView()
|
||||||
return;
|
return;
|
||||||
views.at(views.size() - 1)->close();
|
views.at(views.size() - 1)->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ZoneViewLayout::clear()
|
||||||
|
{
|
||||||
|
for (int i = views.size() - 1; i >= 0; i--)
|
||||||
|
views.at(i)->close();
|
||||||
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ public slots:
|
||||||
void addItem(Player *player, const QString &zoneName, int numberCards = 0);
|
void addItem(Player *player, const QString &zoneName, int numberCards = 0);
|
||||||
void removeItem(ZoneViewWidget *item);
|
void removeItem(ZoneViewWidget *item);
|
||||||
void closeMostRecentZoneView();
|
void closeMostRecentZoneView();
|
||||||
|
void clear();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
5
servatrice/servatrice.ini.example
Normal file
5
servatrice/servatrice.ini.example
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
[database]
|
||||||
|
hostname=localhost
|
||||||
|
database=servatrice
|
||||||
|
user=servatrice
|
||||||
|
password=foobar
|
|
@ -25,6 +25,8 @@
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QCoreApplication app(argc, argv);
|
QCoreApplication app(argc, argv);
|
||||||
|
app.setOrganizationName("Cockatrice");
|
||||||
|
app.setApplicationName("Servatrice");
|
||||||
|
|
||||||
Server server;
|
Server server;
|
||||||
if (!server.openDatabase()) {
|
if (!server.openDatabase()) {
|
||||||
|
|
|
@ -23,10 +23,12 @@
|
||||||
#include "serversocket.h"
|
#include "serversocket.h"
|
||||||
#include "counter.h"
|
#include "counter.h"
|
||||||
#include <QtSql>
|
#include <QtSql>
|
||||||
|
#include <QSettings>
|
||||||
|
|
||||||
Server::Server(QObject *parent)
|
Server::Server(QObject *parent)
|
||||||
: QTcpServer(parent)
|
: QTcpServer(parent), nextGameId(1)
|
||||||
{
|
{
|
||||||
|
settings = new QSettings("servatrice.ini", QSettings::IniFormat, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Server::~Server()
|
Server::~Server()
|
||||||
|
@ -35,11 +37,14 @@ Server::~Server()
|
||||||
|
|
||||||
bool Server::openDatabase()
|
bool Server::openDatabase()
|
||||||
{
|
{
|
||||||
|
settings->beginGroup("database");
|
||||||
QSqlDatabase sqldb = QSqlDatabase::addDatabase("QMYSQL");
|
QSqlDatabase sqldb = QSqlDatabase::addDatabase("QMYSQL");
|
||||||
sqldb.setHostName("localhost");
|
sqldb.setHostName(settings->value("hostname").toString());
|
||||||
sqldb.setDatabaseName("cockatrice");
|
sqldb.setDatabaseName(settings->value("database").toString());
|
||||||
sqldb.setUserName("cockatrice");
|
sqldb.setUserName(settings->value("user").toString());
|
||||||
sqldb.setPassword("45CdX6rmd");
|
sqldb.setPassword(settings->value("password").toString());
|
||||||
|
settings->endGroup();
|
||||||
|
|
||||||
return sqldb.open();
|
return sqldb.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,9 +55,9 @@ void Server::gameCreated(ServerGame *_game, ServerSocket *_creator)
|
||||||
_game->addPlayer(_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(gameCreated(ServerGame *, ServerSocket *)), this, SLOT(gameCreated(ServerGame *, ServerSocket *)));
|
||||||
connect(newThread, SIGNAL(finished()), this, SLOT(gameClosed()));
|
connect(newThread, SIGNAL(finished()), this, SLOT(gameClosed()));
|
||||||
newThread->start();
|
newThread->start();
|
||||||
|
@ -62,8 +67,8 @@ void Server::incomingConnection(int socketId)
|
||||||
{
|
{
|
||||||
ServerSocket *socket = new ServerSocket(this);
|
ServerSocket *socket = new ServerSocket(this);
|
||||||
socket->setSocketDescriptor(socketId);
|
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(createGame(const QString, const QString, const int, ServerSocket *)), this, SLOT(addGame(const QString, const QString, const int, ServerSocket *)));
|
||||||
connect(socket, SIGNAL(joinGame(const QString, ServerSocket *)), this, SLOT(addClientToGame(const QString, ServerSocket *)));
|
connect(socket, SIGNAL(joinGame(int, ServerSocket *)), this, SLOT(addClientToGame(int, ServerSocket *)));
|
||||||
socket->initConnection();
|
socket->initConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,12 +90,12 @@ AuthenticationResult Server::checkUserPassword(const QString &user, const QStrin
|
||||||
return UnknownUser;
|
return UnknownUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerGame *Server::getGame(const QString &name)
|
ServerGame *Server::getGame(int gameId)
|
||||||
{
|
{
|
||||||
QListIterator<ServerGame *> i(games);
|
QListIterator<ServerGame *> i(games);
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
ServerGame *tmp = i.next();
|
ServerGame *tmp = i.next();
|
||||||
if ((!tmp->name.compare(name, Qt::CaseSensitive)) && !tmp->getGameStarted())
|
if ((tmp->gameId == gameId) && !tmp->getGameStarted())
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -111,10 +116,10 @@ QList<ServerGame *> Server::listOpenGames()
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Server::checkGamePassword(const QString &name, const QString &password)
|
bool Server::checkGamePassword(int gameId, const QString &password)
|
||||||
{
|
{
|
||||||
ServerGame *tmp;
|
ServerGame *tmp;
|
||||||
if ((tmp = getGame(name))) {
|
if ((tmp = getGame(gameId))) {
|
||||||
QMutexLocker locker(tmp->mutex);
|
QMutexLocker locker(tmp->mutex);
|
||||||
if ((!tmp->getGameStarted())
|
if ((!tmp->getGameStarted())
|
||||||
&& (!tmp->password.compare(password, Qt::CaseSensitive))
|
&& (!tmp->password.compare(password, Qt::CaseSensitive))
|
||||||
|
@ -124,9 +129,9 @@ bool Server::checkGamePassword(const QString &name, const QString &password)
|
||||||
return false;
|
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());
|
client->moveToThread(tmp->thread());
|
||||||
tmp->addPlayer(client);
|
tmp->addPlayer(client);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
class ServerGame;
|
class ServerGame;
|
||||||
class ServerSocket;
|
class ServerSocket;
|
||||||
class QSqlDatabase;
|
class QSqlDatabase;
|
||||||
|
class QSettings;
|
||||||
|
|
||||||
enum AuthenticationResult { PasswordWrong = 0, PasswordRight = 1, UnknownUser = 2 };
|
enum AuthenticationResult { PasswordWrong = 0, PasswordRight = 1, UnknownUser = 2 };
|
||||||
|
|
||||||
|
@ -32,21 +33,23 @@ class Server : public QTcpServer
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private slots:
|
private slots:
|
||||||
void addGame(const QString name, const QString description, const QString password, const int maxPlayers, ServerSocket *creator);
|
void addGame(const QString description, const QString password, const int maxPlayers, ServerSocket *creator);
|
||||||
void addClientToGame(const QString name, ServerSocket *client);
|
void addClientToGame(int gameId, ServerSocket *client);
|
||||||
void gameCreated(ServerGame *_game, ServerSocket *_creator);
|
void gameCreated(ServerGame *_game, ServerSocket *_creator);
|
||||||
void gameClosed();
|
void gameClosed();
|
||||||
public:
|
public:
|
||||||
Server(QObject *parent = 0);
|
Server(QObject *parent = 0);
|
||||||
~Server();
|
~Server();
|
||||||
|
QSettings *settings;
|
||||||
bool openDatabase();
|
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);
|
AuthenticationResult checkUserPassword(const QString &user, const QString &password);
|
||||||
QList<ServerGame *> listOpenGames();
|
QList<ServerGame *> listOpenGames();
|
||||||
ServerGame *getGame(const QString &name);
|
ServerGame *getGame(int gameId);
|
||||||
private:
|
private:
|
||||||
void incomingConnection(int SocketId);
|
void incomingConnection(int SocketId);
|
||||||
QList<ServerGame *> games;
|
QList<ServerGame *> games;
|
||||||
|
int nextGameId;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -21,8 +21,8 @@
|
||||||
#include "random.h"
|
#include "random.h"
|
||||||
#include "serversocket.h"
|
#include "serversocket.h"
|
||||||
|
|
||||||
ServerGame::ServerGame(QString _name, QString _description, QString _password, int _maxPlayers, QObject *parent)
|
ServerGame::ServerGame(ServerSocket *_creator, int _gameId, QString _description, QString _password, int _maxPlayers, QObject *parent)
|
||||||
: QObject(parent), gameStarted(false), rnd(0), name(_name), description(_description), password(_password), maxPlayers(_maxPlayers)
|
: QObject(parent), gameStarted(false), rnd(0), creator(_creator), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers)
|
||||||
{
|
{
|
||||||
mutex = new QMutex(QMutex::Recursive);
|
mutex = new QMutex(QMutex::Recursive);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,11 +39,12 @@ public slots:
|
||||||
public:
|
public:
|
||||||
QMutex *mutex;
|
QMutex *mutex;
|
||||||
Random *rnd;
|
Random *rnd;
|
||||||
QString name;
|
ServerSocket *creator;
|
||||||
|
int gameId;
|
||||||
QString description;
|
QString description;
|
||||||
QString password;
|
QString password;
|
||||||
int maxPlayers;
|
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();
|
~ServerGame();
|
||||||
bool getGameStarted();
|
bool getGameStarted();
|
||||||
int getPlayerCount();
|
int getPlayerCount();
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#include "servergamethread.h"
|
#include "servergamethread.h"
|
||||||
#include "servergame.h"
|
#include "servergame.h"
|
||||||
|
|
||||||
ServerGameThread::ServerGameThread(const QString _name, const QString _description, const QString _password, const int _maxPlayers, ServerSocket *_creator, QObject *parent)
|
ServerGameThread::ServerGameThread(int _gameId, 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)
|
: QThread(parent), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), creator(_creator), game(0)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ ServerGameThread::~ServerGameThread()
|
||||||
|
|
||||||
void ServerGameThread::run()
|
void ServerGameThread::run()
|
||||||
{
|
{
|
||||||
game = new ServerGame(name, description, password, maxPlayers);
|
game = new ServerGame(creator, gameId, description, password, maxPlayers);
|
||||||
emit gameCreated(game, creator);
|
emit gameCreated(game, creator);
|
||||||
exec();
|
exec();
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,14 +12,14 @@ class ServerGameThread : public QThread {
|
||||||
signals:
|
signals:
|
||||||
void gameCreated(ServerGame *_game, ServerSocket *creator);
|
void gameCreated(ServerGame *_game, ServerSocket *creator);
|
||||||
private:
|
private:
|
||||||
QString name;
|
int gameId;
|
||||||
QString description;
|
QString description;
|
||||||
QString password;
|
QString password;
|
||||||
int maxPlayers;
|
int maxPlayers;
|
||||||
ServerSocket *creator;
|
ServerSocket *creator;
|
||||||
ServerGame *game;
|
ServerGame *game;
|
||||||
public:
|
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();
|
~ServerGameThread();
|
||||||
ServerGame *getGame() { return game; }
|
ServerGame *getGame() { return game; }
|
||||||
void run();
|
void run();
|
||||||
|
|
|
@ -186,10 +186,9 @@ const ServerSocket::CommandProperties ServerSocket::commandList[ServerSocket::nu
|
||||||
<< QVariant::String, &ServerSocket::cmdLogin},
|
<< QVariant::String, &ServerSocket::cmdLogin},
|
||||||
{"list_games", true, false, false, QList<QVariant::Type>(), &ServerSocket::cmdListGames},
|
{"list_games", true, false, false, QList<QVariant::Type>(), &ServerSocket::cmdListGames},
|
||||||
{"create_game", true, false, false, QList<QVariant::Type>() << QVariant::String
|
{"create_game", true, false, false, QList<QVariant::Type>() << QVariant::String
|
||||||
<< QVariant::String
|
|
||||||
<< QVariant::String
|
<< QVariant::String
|
||||||
<< QVariant::Int, &ServerSocket::cmdCreateGame},
|
<< 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},
|
<< QVariant::String, &ServerSocket::cmdJoinGame},
|
||||||
{"leave_game", true, true, false, QList<QVariant::Type>(), &ServerSocket::cmdLeaveGame},
|
{"leave_game", true, true, false, QList<QVariant::Type>(), &ServerSocket::cmdLeaveGame},
|
||||||
{"list_players", true, true, false, QList<QVariant::Type>(), &ServerSocket::cmdListPlayers},
|
{"list_players", true, true, false, QList<QVariant::Type>(), &ServerSocket::cmdListPlayers},
|
||||||
|
@ -250,11 +249,12 @@ ReturnMessage::ReturnCode ServerSocket::cmdListGames(const QList<QVariant> ¶
|
||||||
while (gameListIterator.hasNext()) {
|
while (gameListIterator.hasNext()) {
|
||||||
ServerGame *tmp = gameListIterator.next();
|
ServerGame *tmp = gameListIterator.next();
|
||||||
tmp->mutex->lock();
|
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->description)
|
||||||
.arg(tmp->password == "" ? 0 : 1)
|
.arg(tmp->password == "" ? 0 : 1)
|
||||||
.arg(tmp->getPlayerCount())
|
.arg(tmp->getPlayerCount())
|
||||||
.arg(tmp->maxPlayers);
|
.arg(tmp->maxPlayers)
|
||||||
|
.arg(tmp->creator->PlayerName);
|
||||||
tmp->mutex->unlock();
|
tmp->mutex->unlock();
|
||||||
}
|
}
|
||||||
remsg->sendList(result);
|
remsg->sendList(result);
|
||||||
|
@ -263,25 +263,22 @@ ReturnMessage::ReturnCode ServerSocket::cmdListGames(const QList<QVariant> ¶
|
||||||
|
|
||||||
ReturnMessage::ReturnCode ServerSocket::cmdCreateGame(const QList<QVariant> ¶ms)
|
ReturnMessage::ReturnCode ServerSocket::cmdCreateGame(const QList<QVariant> ¶ms)
|
||||||
{
|
{
|
||||||
QString name = params[0].toString();
|
QString description = params[0].toString();
|
||||||
QString description = params[1].toString();
|
QString password = params[1].toString();
|
||||||
QString password = params[2].toString();
|
int maxPlayers = params[2].toInt();
|
||||||
int maxPlayers = params[3].toInt();
|
|
||||||
if (server->getGame(name))
|
|
||||||
return ReturnMessage::ReturnNameNotFound;
|
|
||||||
leaveGame();
|
leaveGame();
|
||||||
emit createGame(name, description, password, maxPlayers, this);
|
emit createGame(description, password, maxPlayers, this);
|
||||||
return ReturnMessage::ReturnOk;
|
return ReturnMessage::ReturnOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnMessage::ReturnCode ServerSocket::cmdJoinGame(const QList<QVariant> ¶ms)
|
ReturnMessage::ReturnCode ServerSocket::cmdJoinGame(const QList<QVariant> ¶ms)
|
||||||
{
|
{
|
||||||
QString name = params[0].toString();
|
int gameId = params[0].toInt();
|
||||||
QString password = params[1].toString();
|
QString password = params[1].toString();
|
||||||
if (!server->checkGamePassword(name, password))
|
if (!server->checkGamePassword(gameId, password))
|
||||||
return ReturnMessage::ReturnPasswordWrong;
|
return ReturnMessage::ReturnPasswordWrong;
|
||||||
leaveGame();
|
leaveGame();
|
||||||
emit joinGame(name, this);
|
emit joinGame(gameId, this);
|
||||||
return ReturnMessage::ReturnOk;
|
return ReturnMessage::ReturnOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,8 +40,8 @@ private slots:
|
||||||
void readClient();
|
void readClient();
|
||||||
void catchSocketError(QAbstractSocket::SocketError socketError);
|
void catchSocketError(QAbstractSocket::SocketError socketError);
|
||||||
signals:
|
signals:
|
||||||
void createGame(const QString name, const QString description, const QString password, const int maxPlayers, ServerSocket *creator);
|
void createGame(const QString description, const QString password, const int maxPlayers, ServerSocket *creator);
|
||||||
void joinGame(const QString name, ServerSocket *player);
|
void joinGame(int gameId, ServerSocket *player);
|
||||||
void commandReceived(QString cmd, ServerSocket *player);
|
void commandReceived(QString cmd, ServerSocket *player);
|
||||||
void broadcastEvent(const QString &event, ServerSocket *player);
|
void broadcastEvent(const QString &event, ServerSocket *player);
|
||||||
void startGameIfReady();
|
void startGameIfReady();
|
||||||
|
|
Loading…
Reference in a new issue