mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-11 00:24:47 -07:00
boom
This commit is contained in:
parent
d329376e93
commit
1c2aa15b22
38 changed files with 638 additions and 1651 deletions
119
servatrice/src/servatrice.cpp
Normal file
119
servatrice/src/servatrice.cpp
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/***************************************************************************
|
||||
* Copyright (C) 2008 by Max-Wilhelm Bruker *
|
||||
* brukie@laptop *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
#include <QtSql>
|
||||
#include <QSettings>
|
||||
#include "servatrice.h"
|
||||
#include "server_chatchannel.h"
|
||||
#include "serversocketinterface.h"
|
||||
|
||||
Servatrice::Servatrice(QObject *parent)
|
||||
: Server(parent)
|
||||
{
|
||||
tcpServer = new QTcpServer(this);
|
||||
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
|
||||
tcpServer->listen(QHostAddress::Any, 4747); // XXX make customizable
|
||||
|
||||
settings = new QSettings("servatrice.ini", QSettings::IniFormat, this);
|
||||
|
||||
QString dbType = settings->value("database/type").toString();
|
||||
if (dbType == "mysql")
|
||||
openDatabase();
|
||||
|
||||
int size = settings->beginReadArray("chatchannels");
|
||||
for (int i = 0; i < size; ++i) {
|
||||
settings->setArrayIndex(i);
|
||||
Server_ChatChannel *newChannel = new Server_ChatChannel(
|
||||
settings->value("name").toString(),
|
||||
settings->value("description").toString(),
|
||||
settings->value("autojoin").toBool(),
|
||||
settings->value("joinmessage").toStringList()
|
||||
);
|
||||
addChatChannel(newChannel);
|
||||
}
|
||||
settings->endArray();
|
||||
|
||||
loginMessage = settings->value("messages/login").toStringList();
|
||||
}
|
||||
|
||||
Servatrice::~Servatrice()
|
||||
{
|
||||
}
|
||||
|
||||
bool Servatrice::openDatabase()
|
||||
{
|
||||
if (!QSqlDatabase::connectionNames().isEmpty())
|
||||
QSqlDatabase::removeDatabase(QSqlDatabase::database().connectionNames().at(0));
|
||||
|
||||
settings->beginGroup("database");
|
||||
QSqlDatabase sqldb = QSqlDatabase::addDatabase("QMYSQL");
|
||||
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();
|
||||
|
||||
if (!sqldb.open())
|
||||
return false;
|
||||
|
||||
if (!nextGameId) {
|
||||
QSqlQuery query;
|
||||
if (!query.exec("select max(id) from games"))
|
||||
return false;
|
||||
if (!query.next())
|
||||
return false;
|
||||
nextGameId = query.value(0).toInt() + 1;
|
||||
qDebug(QString("set nextGameId to %1").arg(nextGameId).toLatin1());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Servatrice::newConnection()
|
||||
{
|
||||
QTcpSocket *socket = tcpServer->nextPendingConnection();
|
||||
ServerSocketInterface *ssi = new ServerSocketInterface(this, socket);
|
||||
addClient(ssi);
|
||||
}
|
||||
|
||||
AuthenticationResult Servatrice::checkUserPassword(const QString &user, const QString &password)
|
||||
{
|
||||
const QString method = settings->value("authentication/method").toString();
|
||||
if (method == "none")
|
||||
return UnknownUser;
|
||||
else if (method == "sql") {
|
||||
if (!QSqlDatabase::database().exec("select 1").isActive())
|
||||
openDatabase();
|
||||
|
||||
QSqlQuery query;
|
||||
query.prepare("select password from players where name = :name");
|
||||
query.bindValue(":name", user);
|
||||
if (!query.exec()) {
|
||||
qCritical(QString("Database error: %1").arg(query.lastError().text()).toLatin1());
|
||||
return PasswordWrong;
|
||||
}
|
||||
if (query.next()) {
|
||||
if (query.value(0).toString() == password)
|
||||
return PasswordRight;
|
||||
else
|
||||
return PasswordWrong;
|
||||
} else
|
||||
return UnknownUser;
|
||||
} else
|
||||
return UnknownUser;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue