removed thread
This commit is contained in:
parent
8680bce5fc
commit
16cc20a603
10 changed files with 25 additions and 95 deletions
|
@ -77,7 +77,7 @@ bool DeckListModel::loadFromFile(const QString &fileName)
|
|||
while (!in.atEnd()) {
|
||||
QString line = in.readLine().simplified();
|
||||
bool isSideboard = false;
|
||||
if (line.startsWith("SB:")) {
|
||||
if (line.startsWith("SB:", Qt::CaseInsensitive)) {
|
||||
line = line.mid(3).trimmed();
|
||||
isSideboard = true;
|
||||
}
|
||||
|
|
|
@ -38,13 +38,15 @@ void myMessageOutput(QtMsgType type, const char *msg)
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// qInstallMsgHandler(myMessageOutput);
|
||||
qInstallMsgHandler(myMessageOutput);
|
||||
QApplication app(argc, argv);
|
||||
app.addLibraryPath("plugins");
|
||||
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
|
||||
|
||||
MainWindow *ui = new MainWindow;
|
||||
qDebug("main(): MainWindow constructor finished");
|
||||
ui->show();
|
||||
qDebug("main(): ui->show() finished");
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
#include "server.h"
|
||||
#include "servergamethread.h"
|
||||
#include "servergame.h"
|
||||
#include "serversocket.h"
|
||||
#include "counter.h"
|
||||
|
@ -48,19 +47,12 @@ bool Server::openDatabase()
|
|||
return sqldb.open();
|
||||
}
|
||||
|
||||
void Server::gameCreated(ServerGame *_game, ServerSocket *_creator)
|
||||
{
|
||||
games << _game;
|
||||
_creator->moveToThread(_game->thread());
|
||||
_game->addPlayer(_creator);
|
||||
}
|
||||
|
||||
void Server::addGame(const QString description, const QString password, const int maxPlayers, ServerSocket *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();
|
||||
ServerGame *newGame = new ServerGame(creator, nextGameId++, description, password, maxPlayers);
|
||||
games << newGame;
|
||||
connect(newGame, SIGNAL(gameClosing()), this, SLOT(gameClosing()));
|
||||
newGame->addPlayer(creator);
|
||||
}
|
||||
|
||||
void Server::incomingConnection(int socketId)
|
||||
|
@ -74,12 +66,18 @@ void Server::incomingConnection(int socketId)
|
|||
|
||||
AuthenticationResult Server::checkUserPassword(const QString &user, const QString &password)
|
||||
{
|
||||
if (!QSqlDatabase::database().isOpen())
|
||||
if (!openDatabase()) {
|
||||
qCritical(QString("Database error: %1").arg(QSqlDatabase::database().lastError().text()).toLatin1());
|
||||
return PasswordWrong;
|
||||
}
|
||||
|
||||
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);
|
||||
return PasswordWrong;
|
||||
}
|
||||
if (query.next()) {
|
||||
if (query.value(0).toString() == password)
|
||||
|
@ -107,11 +105,9 @@ QList<ServerGame *> Server::listOpenGames()
|
|||
QListIterator<ServerGame *> i(games);
|
||||
while (i.hasNext()) {
|
||||
ServerGame *tmp = i.next();
|
||||
tmp->mutex->lock();
|
||||
if ((!tmp->getGameStarted())
|
||||
&& (tmp->getPlayerCount() < tmp->maxPlayers))
|
||||
result.append(tmp);
|
||||
tmp->mutex->unlock();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -120,7 +116,6 @@ bool Server::checkGamePassword(int gameId, const QString &password)
|
|||
{
|
||||
ServerGame *tmp;
|
||||
if ((tmp = getGame(gameId))) {
|
||||
QMutexLocker locker(tmp->mutex);
|
||||
if ((!tmp->getGameStarted())
|
||||
&& (!tmp->password.compare(password, Qt::CaseSensitive))
|
||||
&& (tmp->getPlayerCount() < tmp->maxPlayers))
|
||||
|
@ -132,14 +127,12 @@ bool Server::checkGamePassword(int gameId, const QString &password)
|
|||
void Server::addClientToGame(int gameId, ServerSocket *client)
|
||||
{
|
||||
ServerGame *tmp = getGame(gameId);
|
||||
client->moveToThread(tmp->thread());
|
||||
tmp->addPlayer(client);
|
||||
}
|
||||
|
||||
void Server::gameClosed()
|
||||
void Server::gameClosing()
|
||||
{
|
||||
qDebug("Server::gameClosed");
|
||||
ServerGameThread *t = qobject_cast<ServerGameThread *>(sender());
|
||||
games.removeAt(games.indexOf(t->getGame()));
|
||||
delete t;
|
||||
qDebug("Server::gameClosing");
|
||||
ServerGame *g = qobject_cast<ServerGame *>(sender());
|
||||
games.removeAt(games.indexOf(g));
|
||||
}
|
||||
|
|
|
@ -35,8 +35,7 @@ class Server : public QTcpServer
|
|||
private slots:
|
||||
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();
|
||||
void gameClosing();
|
||||
public:
|
||||
Server(QObject *parent = 0);
|
||||
~Server();
|
||||
|
|
|
@ -24,13 +24,12 @@
|
|||
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);
|
||||
}
|
||||
|
||||
ServerGame::~ServerGame()
|
||||
{
|
||||
emit gameClosing();
|
||||
delete rnd;
|
||||
delete mutex;
|
||||
qDebug("ServerGame destructor");
|
||||
}
|
||||
|
||||
|
@ -41,14 +40,11 @@ bool ServerGame::getGameStarted()
|
|||
|
||||
int ServerGame::getPlayerCount()
|
||||
{
|
||||
QMutexLocker locker(mutex);
|
||||
return players.size();
|
||||
}
|
||||
|
||||
QStringList ServerGame::getPlayerNames()
|
||||
{
|
||||
QMutexLocker locker(mutex);
|
||||
|
||||
QStringList result;
|
||||
QListIterator<ServerSocket *> i(players);
|
||||
while (i.hasNext()) {
|
||||
|
@ -71,8 +67,6 @@ ServerSocket *ServerGame::getPlayer(int player_id)
|
|||
|
||||
void ServerGame::msg(const QString &s)
|
||||
{
|
||||
QMutexLocker locker(mutex);
|
||||
|
||||
QListIterator<ServerSocket *> i(players);
|
||||
while (i.hasNext())
|
||||
i.next()->msg(s);
|
||||
|
@ -88,8 +82,6 @@ void ServerGame::broadcastEvent(const QString &cmd, ServerSocket *player)
|
|||
|
||||
void ServerGame::startGameIfReady()
|
||||
{
|
||||
QMutexLocker locker(mutex);
|
||||
|
||||
if (players.size() < maxPlayers)
|
||||
return;
|
||||
for (int i = 0; i < players.size(); i++)
|
||||
|
@ -112,8 +104,6 @@ void ServerGame::startGameIfReady()
|
|||
|
||||
void ServerGame::addPlayer(ServerSocket *player)
|
||||
{
|
||||
QMutexLocker locker(mutex);
|
||||
|
||||
int max = -1;
|
||||
QListIterator<ServerSocket *> i(players);
|
||||
while (i.hasNext()) {
|
||||
|
@ -134,12 +124,10 @@ void ServerGame::addPlayer(ServerSocket *player)
|
|||
|
||||
void ServerGame::removePlayer(ServerSocket *player)
|
||||
{
|
||||
QMutexLocker locker(mutex);
|
||||
|
||||
players.removeAt(players.indexOf(player));
|
||||
broadcastEvent("leave", player);
|
||||
if (!players.size())
|
||||
thread()->quit();
|
||||
deleteLater();
|
||||
}
|
||||
|
||||
void ServerGame::setActivePlayer(int _activePlayer)
|
||||
|
|
|
@ -20,8 +20,6 @@
|
|||
#ifndef SERVERGAME_H
|
||||
#define SERVERGAME_H
|
||||
|
||||
#include <QThread>
|
||||
#include <QMutex>
|
||||
#include <QStringList>
|
||||
|
||||
class ServerSocket;
|
||||
|
@ -34,10 +32,11 @@ private:
|
|||
bool gameStarted;
|
||||
int activePlayer;
|
||||
int activePhase;
|
||||
signals:
|
||||
void gameClosing();
|
||||
public slots:
|
||||
void broadcastEvent(const QString &event, ServerSocket *player);
|
||||
public:
|
||||
QMutex *mutex;
|
||||
Random *rnd;
|
||||
ServerSocket *creator;
|
||||
int gameId;
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
#include "servergamethread.h"
|
||||
#include "servergame.h"
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ServerGameThread::~ServerGameThread()
|
||||
{
|
||||
if (game)
|
||||
delete game;
|
||||
}
|
||||
|
||||
void ServerGameThread::run()
|
||||
{
|
||||
game = new ServerGame(creator, gameId, description, password, maxPlayers);
|
||||
emit gameCreated(game, creator);
|
||||
exec();
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
#ifndef SERVERGAMETHREAD_H
|
||||
#define SERVERGAMETHREAD_H
|
||||
|
||||
#include <QThread>
|
||||
#include <QMutex>
|
||||
|
||||
class ServerGame;
|
||||
class ServerSocket;
|
||||
|
||||
class ServerGameThread : public QThread {
|
||||
Q_OBJECT
|
||||
signals:
|
||||
void gameCreated(ServerGame *_game, ServerSocket *creator);
|
||||
private:
|
||||
int gameId;
|
||||
QString description;
|
||||
QString password;
|
||||
int maxPlayers;
|
||||
ServerSocket *creator;
|
||||
ServerGame *game;
|
||||
public:
|
||||
ServerGameThread(int _gameId, const QString _description, const QString _password, const int _maxPlayers, ServerSocket *_creator, QObject *parent = 0);
|
||||
~ServerGameThread();
|
||||
ServerGame *getGame() { return game; }
|
||||
void run();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -254,14 +254,12 @@ ReturnMessage::ReturnCode ServerSocket::cmdListGames(const QList<QVariant> ¶
|
|||
QStringList result;
|
||||
while (gameListIterator.hasNext()) {
|
||||
ServerGame *tmp = gameListIterator.next();
|
||||
tmp->mutex->lock();
|
||||
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->creator->PlayerName);
|
||||
tmp->mutex->unlock();
|
||||
}
|
||||
remsg->sendList(result);
|
||||
return ReturnMessage::ReturnOk;
|
||||
|
|
|
@ -18,4 +18,4 @@
|
|||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
const char *VERSION_STRING = "Servatrice 0.20090409";
|
||||
const char *VERSION_STRING = "Servatrice 0.20090416";
|
||||
|
|
Loading…
Reference in a new issue