minor improvements

This commit is contained in:
Max-Wilhelm Bruker 2009-04-14 19:18:46 +02:00
parent 3388804e8f
commit 99ff7fd15f
23 changed files with 114 additions and 95 deletions

View file

@ -130,7 +130,7 @@ void Client::readLine()
// XXX Parametergültigkeit überprüfen
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"))
playerlist << new ServerPlayer(val[0].toInt(), val[1]);
else if (!prefix.compare("list_counters"))
@ -215,14 +215,14 @@ int Client::listPlayers()
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()

View file

@ -64,8 +64,8 @@ public:
void disconnectFromServer();
int listGames();
int listPlayers();
int createGame(const QString &name, const QString &description, const QString &password, unsigned int maxPlayers);
int joinGame(const QString &name, const QString &password);
int createGame(const QString &description, const QString &password, unsigned int maxPlayers);
int joinGame(int gameId, const QString &password);
int leaveGame();
int login(const QString &name, const QString &pass);
int say(const QString &s);

View file

@ -2,14 +2,8 @@
#include "dlg_creategame.h"
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:"));
descriptionEdit = new QLineEdit;
descriptionLabel->setBuddy(descriptionEdit);
@ -18,19 +12,17 @@ DlgCreateGame::DlgCreateGame(Client *_client, QWidget *parent)
passwordEdit = new QLineEdit;
passwordLabel->setBuddy(passwordEdit);
maxPlayersLabel = new QLabel(tr("&Max Players:"));
maxPlayersLabel = new QLabel(tr("P&layers:"));
maxPlayersEdit = new QLineEdit("2");
maxPlayersLabel->setBuddy(maxPlayersEdit);
QGridLayout *grid = new QGridLayout;
grid->addWidget(nameLabel, 0, 0);
grid->addWidget(nameEdit, 0, 1);
grid->addWidget(descriptionLabel, 1, 0);
grid->addWidget(descriptionEdit, 1, 1);
grid->addWidget(passwordLabel, 2, 0);
grid->addWidget(passwordEdit, 2, 1);
grid->addWidget(maxPlayersLabel, 3, 0);
grid->addWidget(maxPlayersEdit, 3, 1);
grid->addWidget(descriptionLabel, 0, 0);
grid->addWidget(descriptionEdit, 0, 1);
grid->addWidget(passwordLabel, 1, 0);
grid->addWidget(passwordEdit, 1, 1);
grid->addWidget(maxPlayersLabel, 2, 0);
grid->addWidget(maxPlayersEdit, 2, 1);
okButton = new QPushButton(tr("&OK"));
okButton->setDefault(true);
@ -65,8 +57,7 @@ void DlgCreateGame::actOK()
QMessageBox::critical(this, tr("Error"), tr("Invalid number of players."));
return;
}
msgid = client->createGame(nameEdit->text(),
descriptionEdit->text(),
msgid = client->createGame(descriptionEdit->text(),
passwordEdit->text(),
maxPlayers);
}

View file

@ -19,8 +19,8 @@ private:
Client *client;
int msgid;
QLabel *nameLabel, *descriptionLabel, *passwordLabel, *maxPlayersLabel;
QLineEdit *nameEdit, *descriptionEdit, *passwordEdit, *maxPlayersEdit;
QLabel *descriptionLabel, *passwordLabel, *maxPlayersLabel;
QLineEdit *descriptionEdit, *passwordEdit, *maxPlayersEdit;
QPushButton *okButton, *cancelButton;
};

View file

@ -3,10 +3,8 @@
#include "dlg_creategame.h"
DlgGames::DlgGames(Client *_client, QWidget *parent)
: QDialog(parent), client(_client)
: QDialog(parent), client(_client), msgid(0)
{
msgid = 0;
tableView = new QTreeView;
tableModel = new GamesModel(this);
tableView->setModel(tableModel);
@ -28,8 +26,8 @@ DlgGames::DlgGames(Client *_client, QWidget *parent)
setLayout(mainLayout);
setWindowTitle(tr("Games"));
setMinimumWidth(sizeHint().width());
setMinimumWidth(tableView->columnWidth(0) * tableModel->columnCount());
connect(createButton, SIGNAL(clicked()), this, SLOT(actCreate()));
connect(refreshButton, SIGNAL(clicked()), this, SLOT(actRefresh()));
connect(joinButton, SIGNAL(clicked()), this, SLOT(actJoin()));
@ -78,7 +76,7 @@ void DlgGames::actJoin()
}
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)

View file

@ -73,12 +73,11 @@ signals:
void logSetCounter(QString playerName, QString counterName, int value, int oldValue);
void logSetDoesntUntap(QString playerName, QString cardName, bool doesntUntap);
void logDumpZone(QString playerName, QString zoneName, QString zoneOwner, int numberCards);
public slots:
void restartGameDialog();
public:
Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_actionsMenu, QMenu *_cardMenu, int playerId, const QString &playerName);
~Game();
Player *getLocalPlayer() const { return localPlayer; }
void restartGameDialog();
};
#endif

View file

@ -13,14 +13,14 @@ int GamesModel::rowCount(const QModelIndex &parent) const
int GamesModel::columnCount(const QModelIndex &parent) const
{
return 4;
return 5;
}
QVariant GamesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if ((index.row() >= gameList.size()) || (index.column() >= 4))
if ((index.row() >= gameList.size()) || (index.column() >= columnCount()))
return QVariant();
if (role != Qt::DisplayRole)
@ -28,10 +28,11 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
ServerGame *g = gameList.at(index.row());
switch (index.column()) {
case 0: return g->getName();
case 1: return g->getDescription();
case 2: return QString(g->getHasPassword() ? "yes" : "no");
case 3: return QString("%1/%2").arg(g->getPlayerCount()).arg(g->getMaxPlayers());
case 0: return g->getGameId();
case 1: return g->getCreator();
case 2: return g->getDescription();
case 3: return QString(g->getHasPassword() ? "yes" : "no");
case 4: return QString("%1/%2").arg(g->getPlayerCount()).arg(g->getMaxPlayers());
default: return QVariant();
}
}
@ -43,10 +44,11 @@ QVariant GamesModel::headerData(int section, Qt::Orientation orientation, int ro
if (orientation != Qt::Horizontal)
return QVariant();
switch (section) {
case 0: return QString("Name");
case 1: return QString("Description");
case 2: return QString("Password");
case 3: return QString("Players");
case 0: return QString("Game ID");
case 1: return QString("Creator");
case 2: return QString("Description");
case 3: return QString("Password");
case 4: return QString("Players");
default: return QVariant();
}
}

View file

@ -276,7 +276,6 @@ void Player::gameEvent(ServerEventData *event)
int colorValue = data[1].toInt();
int value = data[2].toInt();
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);
break;
}

View file

@ -3,15 +3,17 @@
class ServerGame {
private:
QString name;
int gameId;
QString creator;
QString description;
bool hasPassword;
unsigned char playerCount;
unsigned char maxPlayers;
public:
ServerGame(const QString &_name, const QString &_description, bool _hasPassword, unsigned char _playerCount, unsigned char _maxPlayers)
: name(_name), description(_description), hasPassword(_hasPassword), playerCount(_playerCount), maxPlayers(_maxPlayers) { }
QString getName() { return name; }
ServerGame(int _gameId, const QString &_creator, const QString &_description, bool _hasPassword, unsigned char _playerCount, unsigned char _maxPlayers)
: gameId(_gameId), creator(_creator), description(_description), hasPassword(_hasPassword), playerCount(_playerCount), maxPlayers(_maxPlayers) { }
int getGameId() { return gameId; }
QString getCreator() { return creator; }
QString getDescription() { return description; }
bool getHasPassword() { return hasPassword; }
unsigned char getPlayerCount() { return playerCount; }

View file

@ -96,8 +96,15 @@ void MainWindow::actGames()
dlg.exec();
}
void MainWindow::actRestartGame()
{
zoneLayout->clear();
game->restartGameDialog();
}
void MainWindow::actLeaveGame()
{
zoneLayout->clear();
client->leaveGame();
delete game;
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(playerAdded(Player *)), this, SLOT(playerAdded(Player *)));
connect(game, SIGNAL(playerRemoved(Player *)), this, SLOT(playerRemoved(Player *)));
connect(aRestartGame, SIGNAL(triggered()), game, SLOT(restartGameDialog()));
playerAdded(game->getLocalPlayer());
messageLog->connectToGame(game);
@ -174,6 +180,7 @@ void MainWindow::createActions()
aRestartGame = new QAction(tr("&Restart game..."), this);
aRestartGame->setShortcut(tr("F2"));
aRestartGame->setEnabled(false);
connect(aRestartGame, SIGNAL(triggered()), this, SLOT(actRestartGame()));
aLeaveGame = new QAction(tr("&Leave game"), this);
aLeaveGame->setEnabled(false);
connect(aLeaveGame, SIGNAL(triggered()), this, SLOT(actLeaveGame()));

View file

@ -53,6 +53,7 @@ private slots:
void actConnect();
void actDisconnect();
void actGames();
void actRestartGame();
void actLeaveGame();
void actDeckEditor();
void actExit();

View file

@ -52,3 +52,9 @@ void ZoneViewLayout::closeMostRecentZoneView()
return;
views.at(views.size() - 1)->close();
}
void ZoneViewLayout::clear()
{
for (int i = views.size() - 1; i >= 0; i--)
views.at(i)->close();
}

View file

@ -21,6 +21,7 @@ public slots:
void addItem(Player *player, const QString &zoneName, int numberCards = 0);
void removeItem(ZoneViewWidget *item);
void closeMostRecentZoneView();
void clear();
};
#endif

View file

@ -0,0 +1,5 @@
[database]
hostname=localhost
database=servatrice
user=servatrice
password=foobar

View file

@ -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()) {

View file

@ -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);
}

View file

@ -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

View file

@ -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);
}

View file

@ -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();

View file

@ -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();
}

View file

@ -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();

View file

@ -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> &para
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> &para
ReturnMessage::ReturnCode ServerSocket::cmdCreateGame(const QList<QVariant> &params)
{
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> &params)
{
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;
}

View file

@ -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();