Commit 792a400a authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

minor improvements

parent a429a4a0
......@@ -148,7 +148,7 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if ((event->screenPos() - event->buttonDownScreenPos(Qt::LeftButton)).manhattanLength() < QApplication::startDragDistance())
return;
bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier);
bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier) || facedown;
createDragItem((CardZone *) parentItem(), id, event->pos(), event->scenePos(), faceDown);
dragItem->grabMouse();
......
......@@ -155,7 +155,7 @@ void Client::readLine()
else if (!prefix.compare("welcome")) {
emit welcomeMsgReceived(welcomemsg);
setStatus(StatusConnected);
setName(PlayerName);
login(PlayerName, password);
}
msgbuf.clear();
} else
......@@ -189,9 +189,10 @@ int Client::cmd(const QString &s)
return MsgId;
}
void Client::connectToServer(const QString &hostname, unsigned int port, const QString &playername)
void Client::connectToServer(const QString &hostname, unsigned int port, const QString &playername, const QString &_password)
{
PlayerName = playername;
password = _password;
socket->connectToHost(hostname, port);
setStatus(StatusConnecting);
}
......@@ -229,9 +230,9 @@ int Client::leaveGame()
return cmd("leave_game");
}
int Client::setName(const QString &name)
int Client::login(const QString &name, const QString &pass)
{
return cmd(QString("set_name|%1").arg(name));
return cmd(QString("login|%1|%2").arg(name).arg(pass));
}
int Client::say(const QString &s)
......
......@@ -48,6 +48,7 @@ private:
ProtocolStatus status;
QList<QStringList> msgbuf;
QString PlayerName;
QString password;
unsigned int MsgId;
void msg(const QString &s);
int cmd(const QString &s);
......@@ -58,14 +59,14 @@ public:
ProtocolStatus getStatus() { return status; }
QString peerName() const { return socket->peerName(); }
void connectToServer(const QString &hostname, unsigned int port, const QString &playername);
void connectToServer(const QString &hostname, unsigned int port, const QString &playername, const QString &password);
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 leaveGame();
int setName(const QString &name);
int login(const QString &name, const QString &pass);
int say(const QString &s);
int shuffle();
int rollDice(unsigned int sides);
......
......@@ -16,6 +16,11 @@ DlgConnect::DlgConnect(QWidget *parent)
playernameEdit = new QLineEdit("Player");
playernameLabel->setBuddy(playernameEdit);
passwordLabel = new QLabel(tr("P&assword:"));
passwordEdit = new QLineEdit;
passwordLabel->setBuddy(passwordEdit);
passwordEdit->setEchoMode(QLineEdit::Password);
okButton = new QPushButton(tr("&OK"));
okButton->setDefault(true);
cancelButton = new QPushButton(tr("&Cancel"));
......@@ -27,6 +32,8 @@ DlgConnect::DlgConnect(QWidget *parent)
grid->addWidget(portEdit, 1, 1);
grid->addWidget(playernameLabel, 2, 0);
grid->addWidget(playernameEdit, 2, 1);
grid->addWidget(passwordLabel, 3, 0);
grid->addWidget(passwordEdit, 3, 1);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
......@@ -59,3 +66,8 @@ QString DlgConnect::getPlayerName()
{
return playernameEdit->text();
}
QString DlgConnect::getPassword()
{
return passwordEdit->text();
}
......@@ -14,9 +14,10 @@ public:
QString getHost();
int getPort();
QString getPlayerName();
QString getPassword();
private:
QLabel *hostLabel, *portLabel, *playernameLabel;
QLineEdit *hostEdit, *portEdit, *playernameEdit;
QLabel *hostLabel, *portLabel, *playernameLabel, *passwordLabel;
QLineEdit *hostEdit, *portEdit, *playernameEdit, *passwordEdit;
QPushButton *okButton, *cancelButton;
};
......
......@@ -68,6 +68,8 @@ Game::Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_a
connect(aUntap, SIGNAL(triggered()), this, SLOT(actUntap()));
aDoesntUntap = new QAction(tr("Toggle &normal untapping"), this);
connect(aDoesntUntap, SIGNAL(triggered()), this, SLOT(actDoesntUntap()));
aFlip = new QAction(tr("&Flip"), this);
connect(aFlip, SIGNAL(triggered()), this, SLOT(actFlip()));
aAddCounter = new QAction(tr("&Add counter"), this);
connect(aAddCounter, SIGNAL(triggered()), this, SLOT(actAddCounter()));
aRemoveCounter = new QAction(tr("&Remove counter"), this);
......@@ -81,6 +83,8 @@ Game::Game(CardDatabase *_db, Client *_client, QGraphicsScene *_scene, QMenu *_a
cardMenu->addAction(aUntap);
cardMenu->addAction(aDoesntUntap);
cardMenu->addSeparator();
cardMenu->addAction(aFlip);
cardMenu->addSeparator();
cardMenu->addAction(aAddCounter);
cardMenu->addAction(aRemoveCounter);
cardMenu->addAction(aSetCounters);
......@@ -321,6 +325,16 @@ void Game::actDoesntUntap()
}
}
void Game::actFlip()
{
QListIterator<QGraphicsItem *> i(scene->selectedItems());
while (i.hasNext()) {
CardItem *temp = (CardItem *) i.next();
QString zone = qgraphicsitem_cast<CardZone *>(temp->parentItem())->getName();
client->moveCard(temp->getId(), zone, zone, temp->pos().x(), temp->pos().y(), !temp->getFaceDown());
}
}
void Game::actAddCounter()
{
QListIterator<QGraphicsItem *> i(scene->selectedItems());
......
......@@ -15,7 +15,7 @@ class Game : public QObject {
Q_OBJECT
private:
QMenu *actionsMenu, *cardMenu;
QAction *aTap, *aUntap, *aDoesntUntap, *aAddCounter, *aRemoveCounter, *aSetCounters, *aRearrange,
QAction *aTap, *aUntap, *aDoesntUntap, *aFlip, *aAddCounter, *aRemoveCounter, *aSetCounters, *aRearrange,
*aUntapAll, *aDecLife, *aIncLife, *aSetLife, *aShuffle, *aDraw, *aDrawCards, *aRollDice, *aCreateToken;
DlgStartGame *dlgStartGame;
......@@ -41,6 +41,7 @@ private slots:
void actTap();
void actUntap();
void actDoesntUntap();
void actFlip();
void actAddCounter();
void actRemoveCounter();
void actSetCounters();
......
......@@ -82,7 +82,7 @@ void MainWindow::actConnect()
{
DlgConnect dlg(this);
if (dlg.exec())
client->connectToServer(dlg.getHost(), dlg.getPort(), dlg.getPlayerName());
client->connectToServer(dlg.getHost(), dlg.getPort(), dlg.getPlayerName(), dlg.getPassword());
}
void MainWindow::actDisconnect()
......
......@@ -8,25 +8,25 @@ DEPENDPATH += . src
INCLUDEPATH += . src
CONFIG += qt thread
QT += network
QT += network sql
QT -= gui
# Input
HEADERS += src/testserver.h src/testservergame.h src/testserversocket.h \
HEADERS += src/server.h src/servergame.h src/serversocket.h \
src/playerzone.h \
src/testcard.h \
src/card.h \
src/version.h \
src/counter.h \
src/testrandom.h \
src/testservergamethread.h \
src/random.h \
src/servergamethread.h \
src/returnmessage.h
SOURCES += src/main.cpp \
src/testserver.cpp \
src/testservergame.cpp \
src/testserversocket.cpp \
src/server.cpp \
src/servergame.cpp \
src/serversocket.cpp \
src/playerzone.cpp \
src/testcard.cpp \
src/card.cpp \
src/counter.cpp \
src/testrandom.cpp \
src/testservergamethread.cpp \
src/random.cpp \
src/servergamethread.cpp \
src/returnmessage.cpp
......@@ -17,19 +17,19 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "testcard.h"
#include "card.h"
TestCard::TestCard(QString _name, int _id, int _coord_x, int _coord_y)
Card::Card(QString _name, int _id, int _coord_x, int _coord_y)
: id(_id), coord_x(_coord_x), coord_y(_coord_y), name(_name), counters(0), tapped(false), attacking(false), facedown(false), annotation(QString()), doesntUntap(false)
{
}
TestCard::~TestCard()
Card::~Card()
{
}
void TestCard::resetState()
void Card::resetState()
{
setCoords(0, 0);
setCounters(0);
......@@ -40,7 +40,7 @@ void TestCard::resetState()
setDoesntUntap(false);
}
bool TestCard::setAttribute(const QString &aname, const QString &avalue, bool allCards)
bool Card::setAttribute(const QString &aname, const QString &avalue, bool allCards)
{
if (!aname.compare("counters")) {
bool ok;
......
......@@ -17,12 +17,12 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef TESTCARD_H
#define TESTCARD_H
#ifndef CARD_H
#define CARD_H
#include <QString>
class TestCard {
class Card {
private:
int id;
int coord_x, coord_y;
......@@ -34,8 +34,8 @@ private:
QString annotation;
bool doesntUntap;
public:
TestCard(QString _name, int _id, int _coord_x, int _coord_y);
~TestCard();
Card(QString _name, int _id, int _coord_x, int _coord_y);
~Card();
int getId() { return id; }
int getX() { return coord_x; }
......
......@@ -20,14 +20,18 @@
#include <QCoreApplication>
#include "testserver.h"
#include "server.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
TestServer server;
server.listen(QHostAddress::Any, 4747);
QCoreApplication app(argc, argv);
return app.exec();
Server server;
if (!server.openDatabase()) {
qCritical("Database error");
return -1;
}
server.listen(QHostAddress::Any, 4747);
return app.exec();
}
......@@ -18,6 +18,8 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "playerzone.h"
#include "random.h"
#include "card.h"
PlayerZone::PlayerZone(QString _name, bool _has_coords, bool _is_public, bool _is_private, bool _id_access)
: name(_name), has_coords(_has_coords), is_public(_is_public), is_private(_is_private), id_access(_id_access)
......@@ -30,21 +32,21 @@ PlayerZone::~PlayerZone()
clear();
}
void PlayerZone::shuffle(TestRandom *rnd)
void PlayerZone::shuffle(Random *rnd)
{
QList<TestCard *> temp;
QList<Card *> temp;
for (int i = cards.size(); i; i--)
temp.append(cards.takeAt(rnd->getNumber(0, i - 1)));
cards = temp;
}
TestCard *PlayerZone::getCard(int id, bool remove, int *position)
Card *PlayerZone::getCard(int id, bool remove, int *position)
{
if (hasIdAccess()) {
QListIterator<TestCard *> CardIterator(cards);
QListIterator<Card *> CardIterator(cards);
int i = 0;
while (CardIterator.hasNext()) {
TestCard *tmp = CardIterator.next();
Card *tmp = CardIterator.next();
if (tmp->getId() == id) {
if (remove)
cards.removeAt(i);
......@@ -58,7 +60,7 @@ TestCard *PlayerZone::getCard(int id, bool remove, int *position)
} else {
if (id >= cards.size())
return NULL;
TestCard *tmp = cards[id];
Card *tmp = cards[id];
if (remove)
cards.removeAt(id);
if (position)
......@@ -67,7 +69,7 @@ TestCard *PlayerZone::getCard(int id, bool remove, int *position)
}
}
void PlayerZone::insertCard(TestCard *card, int x, int y)
void PlayerZone::insertCard(Card *card, int x, int y)
{
if (hasCoords()) {
card->setCoords(x, y);
......
......@@ -21,8 +21,10 @@
#define PLAYERZONE_H
#include <QList>
#include "testcard.h"
#include "testrandom.h"
#include <QString>
class Card;
class Random;
class PlayerZone {
private:
......@@ -40,7 +42,7 @@ public:
PlayerZone(QString _name, bool _has_coords, bool _is_public, bool _is_private, bool _id_access);
~PlayerZone();
TestCard *getCard(int id, bool remove, int *position = NULL);
Card *getCard(int id, bool remove, int *position = NULL);
bool isPublic() { return is_public; }
bool isPrivate() { return is_private; }
......@@ -48,9 +50,9 @@ public:
bool hasIdAccess() { return id_access; }
QString getName() { return name; }
QList<TestCard *> cards;
void insertCard(TestCard *card, int x, int y);
void shuffle(TestRandom *rnd);
QList<Card *> cards;
void insertCard(Card *card, int x, int y);
void shuffle(Random *rnd);
void clear();
};
......
#include "testrandom.h"
#include "random.h"
#include <QThread>
void TestRandom::init()
void Random::init()
{
if (initialized)
return;
......@@ -11,7 +11,7 @@ void TestRandom::init()
initialized = true;
}
unsigned int TestRandom::getNumber(unsigned int min, unsigned int max)
unsigned int Random::getNumber(unsigned int min, unsigned int max)
{
int r = qrand();
return min + (unsigned int) (((double) (max + 1 - min)) * r / (RAND_MAX + 1.0));
......
#ifndef TESTRANDOM_H
#define TESTRANDOM_H
#ifndef RANDOM_H
#define RANDOM_H
#include <QObject>
#include <QDateTime>
#include <stdlib.h>
class TestRandom : public QObject {
class Random : public QObject {
Q_OBJECT
private:
bool initialized;
public:
TestRandom(QObject *parent) : QObject(parent), initialized(false) { }
Random(QObject *parent) : QObject(parent), initialized(false) { }
void init();
unsigned int getNumber(unsigned int min, unsigned int max);
};
......
#include "returnmessage.h"
#include "testserversocket.h"
#include "serversocket.h"
void ReturnMessage::setMsgId(unsigned int _msg_id)
{
msg_id = _msg_id;
}
bool ReturnMessage::send(const QString &args, bool success)
bool ReturnMessage::send(ReturnCode code)
{
TestServerSocket *s = qobject_cast<TestServerSocket *>(parent());
ServerSocket *s = qobject_cast<ServerSocket *>(parent());
if (!s)
return false;
bool success = (code == ReturnOk);
QString returnCodeString;
switch (code) {
case ReturnNothing: return true;
case ReturnOk: break;
case ReturnLoginNeeded: returnCodeString = "login_needed"; break;
case ReturnSyntaxError: returnCodeString = "syntax"; break;
case ReturnContextError: returnCodeString = "context"; break;
case ReturnPasswordWrong: returnCodeString = "password"; break;
case ReturnNameNotFound: returnCodeString = "name_not_found"; break;
}
s->msg(QString("resp|%1|%2|%3").arg(msg_id)
.arg(success ? "ok" : "err")
.arg(args));
.arg(returnCodeString));
return success;
}
bool ReturnMessage::sendList(const QStringList &args)
{
TestServerSocket *s = qobject_cast<TestServerSocket *>(parent());
ServerSocket *s = qobject_cast<ServerSocket *>(parent());
if (!s)
return false;
......
#ifndef RETURNMESSAGE_H
#define RETURNMESSAGE_H
#include <QString>
#include <QObject>
#include <QStringList>
class ReturnMessage : public QObject {
Q_OBJECT
......@@ -10,11 +9,12 @@ private:
unsigned int msg_id;
QString cmd;
public:
enum ReturnCode { ReturnNothing, ReturnOk, ReturnLoginNeeded, ReturnSyntaxError, ReturnContextError, ReturnPasswordWrong, ReturnNameNotFound };
ReturnMessage(QObject *parent = 0) : QObject(parent), msg_id(0) { }
unsigned int getMsgId() const { return msg_id; }
void setMsgId(unsigned int _msg_id);
void setCmd(const QString &_cmd) { cmd = _cmd; }
bool send(const QString &args = QString(), bool success = true);
bool send(ReturnCode code);
bool sendList(const QStringList &args);
};
......
......@@ -17,58 +17,91 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "testserver.h"
#include "server.h"
#include "servergamethread.h"
#include "servergame.h"
#include "serversocket.h"
#include "counter.h"
#include <QtSql>
TestServer::TestServer(QObject *parent)
Server::Server(QObject *parent)
: QTcpServer(parent)
{
}
TestServer::~TestServer()
Server::~Server()
{
}
void TestServer::gameCreated(TestServerGame *_game, TestServerSocket *_creator)
bool Server::openDatabase()
{
QSqlDatabase sqldb = QSqlDatabase::addDatabase("QMYSQL");
sqldb.setHostName("localhost");
sqldb.setDatabaseName("cockatrice");
sqldb.setUserName("cockatrice");
sqldb.setPassword("45CdX6rmd");
return sqldb.open();
}
void Server::gameCreated(ServerGame *_game, ServerSocket *_creator)
{
games << _game;
_creator->moveToThread(_game->thread());
_game->addPlayer(_creator);
}
void TestServer::addGame(const QString name, const QString description, const QString password, const int maxPlayers, TestServerSocket *creator)
void Server::addGame(const QString name, const QString description, const QString password, const int maxPlayers, ServerSocket *creator)
{
TestServerGameThread *newThread = new TestServerGameThread(name, description, password, maxPlayers, creator);
connect(newThread, SIGNAL(gameCreated(TestServerGame *, TestServerSocket *)), this, SLOT(gameCreated(TestServerGame *, TestServerSocket *)));
ServerGameThread *newThread = new ServerGameThread(name, description, password, maxPlayers, creator);
connect(newThread, SIGNAL(gameCreated(ServerGame *, ServerSocket *)), this, SLOT(gameCreated(ServerGame *, ServerSocket *)));
connect(newThread, SIGNAL(finished()), this, SLOT(gameClosed()));
newThread->start();
}
void TestServer::incomingConnection(int socketId)
void Server::incomingConnection(int socketId)
{
TestServerSocket *socket = new TestServerSocket(this);
ServerSocket *socket = new ServerSocket(this);
socket->setSocketDescriptor(socketId);
connect(socket, SIGNAL(createGame(const QString, const QString, const QString, const int, TestServerSocket *)), this, SLOT(addGame(const QString, const QString, const QString, const int, TestServerSocket *)));
connect(socket, SIGNAL(joinGame(const QString, TestServerSocket *)), this, SLOT(addClientToGame(const QString, TestServerSocket *)));
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 *)));
socket->initConnection();
}
TestServerGame *TestServer::getGame(const QString &name)
AuthenticationResult Server::checkUserPassword(const QString &user, const QString &password)
{
QSqlQuery query;
query.prepare("select password from users where name = :name");
query.bindValue(":name", user);
if (!query.exec()) {
qCritical(QString("Database error: %1").arg(query.lastError().text()).toLatin1());
exit(-1);
}
if (query.next()) {
if (query.value(0).toString() == password)
return PasswordRight;
else
return PasswordWrong;
} else
return UnknownUser;
}
ServerGame *Server::getGame(const QString &name)
{
QListIterator<TestServerGame *> i(games);
QListIterator<ServerGame *> i(games);
while (i.hasNext()) {
TestServerGame *tmp = i.next();
ServerGame *tmp = i.next();
if ((!tmp->name.compare(name, Qt::CaseSensitive)) && !tmp->getGameStarted())
return tmp;
}
return NULL;
}
QList<TestServerGame *> TestServer::listOpenGames()
QList<ServerGame *> Server::listOpenGames()
{
QList<TestServerGame *> result;
QListIterator<TestServerGame *> i(games);
QList<ServerGame *> result;
QListIterator<ServerGame *> i(games);
while (i.hasNext()) {
TestServerGame *tmp = i.next();
ServerGame *tmp = i.next();
tmp->mutex->lock();
if ((!tmp->getGameStarted())
&& (tmp->getPlayerCount() < tmp->maxPlayers))
......@@ -78,9 +111,9 @@ QList<TestServerGame *> TestServer::listOpenGames()
return result;
}
bool TestServer::checkGamePassword(const QString &name, const QString &password)
bool Server::checkGamePassword(const QString &name, const QString &password)
{
TestServerGame *tmp;
ServerGame *tmp;
if ((tmp = getGame(name))) {
QMutexLocker locker(tmp->mutex);
if ((!tmp->getGameStarted())
......@@ -91,17 +124,17 @@ bool TestServer::checkGamePassword(const QString &name, const QString &password)
return false;
}
void TestServer::addClientToGame(const QString name, TestServerSocket *client)
void Server::addClientToGame(const QString name, ServerSocket *client)
{
TestServerGame *tmp = getGame(name);
ServerGame *tmp = getGame(name);
client->moveToThread(tmp->thread());
tmp->addPlayer(client);
}
void TestServer::gameClosed()
void Server::gameClosed()
{
qDebug("TestServer::gameClosed");
TestServerGameThread *t = qobject_cast<TestServerGameThread *>(sender());
qDebug("Server::gameClosed");
ServerGameThread *t = qobject_cast<ServerGameThread *>(sender());
games.removeAt(games.indexOf(t->getGame()));
delete t;
}
......@@ -17,33 +17,36 @@
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef TESTSERVER_H
#define TESTSERVER_H
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
#include "testservergamethread.h"
#include "testservergame.h"
class TestServerGame;
class TestServerSocket;
class ServerGame;
class ServerSocket;
class QSqlDatabase;
class TestServer : public QTcpServer
enum AuthenticationResult { PasswordWrong = 0, PasswordRight = 1, UnknownUser = 2 };
class Server : public QTcpServer
{
Q_OBJECT
private slots:
void addGame(const QString name, const QString description, const QString password, const int maxPlayers, TestServerSocket *creator);
void addClientToGame(const QString name, TestServerSocket *client);
void gameCreated(TestServerGame *_game, TestServerSocket *_creator);
void addGame(const QString name, const QString description, const QString password, const int maxPlayers, ServerSocket *creator);
void addClientToGame(const QString name, ServerSocket *client);
void gameCreated(ServerGame *_game, ServerSocket *_creator);
void gameClosed();
public:
TestServer(QObject *parent = 0);
~TestServer();
Server(QObject *parent = 0);
~Server();
bool openDatabase();
bool checkGamePassword(const QString &name, const QString &password);
QList<TestServerGame *> listOpenGames();
TestServerGame *getGame(const QString &name);
AuthenticationResult checkUserPassword(const QString &user, const QString &password);
QList<ServerGame *> listOpenGames();
ServerGame *getGame(const QString &name);
private:
void incomingConnection(int SocketId);
QList<TestServerGame *> games;
QList<ServerGame *> games;
};
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment