converted some Player commands to command lists; added db type checks in server

This commit is contained in:
Max-Wilhelm Bruker 2012-01-02 19:41:37 +01:00
parent 0c9a2b061c
commit 4634787b00
4 changed files with 127 additions and 75 deletions

View file

@ -15,7 +15,6 @@ unix:!macx {
} else { } else {
QT += multimedia QT += multimedia
} }
QT += multimedia
HEADERS += src/abstractcounter.h \ HEADERS += src/abstractcounter.h \
src/counter_general.h \ src/counter_general.h \

View file

@ -1592,15 +1592,19 @@ void Player::cardMenuAction(QAction *a)
void Player::actIncPT(int deltaP, int deltaT) void Player::actIncPT(int deltaP, int deltaT)
{ {
QString ptString = "+" + QString::number(deltaP) + "/+" + QString::number(deltaT); QString ptString = "+" + QString::number(deltaP) + "/+" + QString::number(deltaT);
QList< const ::google::protobuf::Message * > commandList;
QListIterator<QGraphicsItem *> j(scene()->selectedItems()); QListIterator<QGraphicsItem *> j(scene()->selectedItems());
while (j.hasNext()) { while (j.hasNext()) {
CardItem *card = static_cast<CardItem *>(j.next()); CardItem *card = static_cast<CardItem *>(j.next());
Command_SetCardAttr cmd; Command_SetCardAttr *cmd = new Command_SetCardAttr;
cmd.set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd.set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd.set_attr_name("pt"); cmd->set_attr_name("pt");
cmd.set_attr_value(ptString.toStdString()); cmd->set_attr_value(ptString.toStdString());
commandList.append(cmd);
} }
sendGameCommand(prepareGameCommand(commandList));
} }
void Player::actSetPT(QAction * /*a*/) void Player::actSetPT(QAction * /*a*/)
@ -1621,15 +1625,18 @@ void Player::actSetPT(QAction * /*a*/)
if (!ok) if (!ok)
return; return;
QList< const ::google::protobuf::Message * > commandList;
QListIterator<QGraphicsItem *> j(scene()->selectedItems()); QListIterator<QGraphicsItem *> j(scene()->selectedItems());
while (j.hasNext()) { while (j.hasNext()) {
CardItem *card = static_cast<CardItem *>(j.next()); CardItem *card = static_cast<CardItem *>(j.next());
Command_SetCardAttr cmd; Command_SetCardAttr *cmd = new Command_SetCardAttr;
cmd.set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd.set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd.set_attr_name("pt"); cmd->set_attr_name("pt");
cmd.set_attr_value(pt.toStdString()); cmd->set_attr_value(pt.toStdString());
commandList.append(cmd);
} }
sendGameCommand(prepareGameCommand(commandList));
} }
void Player::actSetAnnotation(QAction * /*a*/) void Player::actSetAnnotation(QAction * /*a*/)
@ -1651,15 +1658,18 @@ void Player::actSetAnnotation(QAction * /*a*/)
if (!ok) if (!ok)
return; return;
QList< const ::google::protobuf::Message * > commandList;
i.toFront(); i.toFront();
while (i.hasNext()) { while (i.hasNext()) {
CardItem *card = static_cast<CardItem *>(i.next()); CardItem *card = static_cast<CardItem *>(i.next());
Command_SetCardAttr cmd; Command_SetCardAttr *cmd = new Command_SetCardAttr;
cmd.set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd.set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd.set_attr_name("annotation"); cmd->set_attr_name("annotation");
cmd.set_attr_value(annotation.toStdString()); cmd->set_attr_value(annotation.toStdString());
commandList.append(cmd);
} }
sendGameCommand(prepareGameCommand(commandList));
} }
void Player::actAttach(QAction *a) void Player::actAttach(QAction *a)
@ -1683,18 +1693,19 @@ void Player::actCardCounterTrigger(QAction *a)
{ {
int counterId = a->data().toInt() / 1000; int counterId = a->data().toInt() / 1000;
int action = a->data().toInt() % 1000; int action = a->data().toInt() % 1000;
QList< const ::google::protobuf::Message * > commandList;
switch (action) { switch (action) {
case 9: { case 9: {
QListIterator<QGraphicsItem *> i(scene()->selectedItems()); QListIterator<QGraphicsItem *> i(scene()->selectedItems());
while (i.hasNext()) { while (i.hasNext()) {
CardItem *card = static_cast<CardItem *>(i.next()); CardItem *card = static_cast<CardItem *>(i.next());
if (card->getCounters().value(counterId, 0) < MAX_COUNTERS_ON_CARD) { if (card->getCounters().value(counterId, 0) < MAX_COUNTERS_ON_CARD) {
Command_SetCardCounter cmd; Command_SetCardCounter *cmd = new Command_SetCardCounter;
cmd.set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd.set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd.set_counter_id(counterId); cmd->set_counter_id(counterId);
cmd.set_counter_value(card->getCounters().value(counterId, 0) + 1); cmd->set_counter_value(card->getCounters().value(counterId, 0) + 1);
sendGameCommand(cmd); commandList.append(cmd);
} }
} }
break; break;
@ -1704,12 +1715,12 @@ void Player::actCardCounterTrigger(QAction *a)
while (i.hasNext()) { while (i.hasNext()) {
CardItem *card = static_cast<CardItem *>(i.next()); CardItem *card = static_cast<CardItem *>(i.next());
if (card->getCounters().value(counterId, 0)) { if (card->getCounters().value(counterId, 0)) {
Command_SetCardCounter cmd; Command_SetCardCounter *cmd = new Command_SetCardCounter;
cmd.set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd.set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd.set_counter_id(counterId); cmd->set_counter_id(counterId);
cmd.set_counter_value(card->getCounters().value(counterId, 0) - 1); cmd->set_counter_value(card->getCounters().value(counterId, 0) - 1);
sendGameCommand(cmd); commandList.append(cmd);
} }
} }
break; break;
@ -1727,17 +1738,18 @@ void Player::actCardCounterTrigger(QAction *a)
QListIterator<QGraphicsItem *> i(scene()->selectedItems()); QListIterator<QGraphicsItem *> i(scene()->selectedItems());
while (i.hasNext()) { while (i.hasNext()) {
CardItem *card = static_cast<CardItem *>(i.next()); CardItem *card = static_cast<CardItem *>(i.next());
Command_SetCardCounter cmd; Command_SetCardCounter *cmd = new Command_SetCardCounter;
cmd.set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd.set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd.set_counter_id(counterId); cmd->set_counter_id(counterId);
cmd.set_counter_value(number); cmd->set_counter_value(number);
sendGameCommand(cmd); commandList.append(cmd);
} }
break; break;
} }
default: ; default: ;
} }
sendGameCommand(prepareGameCommand(commandList));
} }
void Player::setCardMenu(QMenu *menu) void Player::setCardMenu(QMenu *menu)

View file

@ -71,9 +71,19 @@ Servatrice::Servatrice(QSettings *_settings, QObject *parent)
else else
qDebug() << "tcpServer->listen(): Error."; qDebug() << "tcpServer->listen(): Error.";
QString dbType = settings->value("database/type").toString(); const QString authenticationMethodStr = settings->value("authentication/method").toString();
if (authenticationMethodStr == "sql")
authenticationMethod = AuthenticationSql;
else
authenticationMethod = AuthenticationNone;
QString dbTypeStr = settings->value("database/type").toString();
if (dbTypeStr == "mysql")
databaseType = DatabaseMySql;
else
databaseType = DatabaseNone;
dbPrefix = settings->value("database/prefix").toString(); dbPrefix = settings->value("database/prefix").toString();
if (dbType == "mysql") if (databaseType != DatabaseNone)
openDatabase(); openDatabase();
int size = settings->beginReadArray("rooms"); int size = settings->beginReadArray("rooms");
@ -151,11 +161,15 @@ bool Servatrice::openDatabase()
return true; return true;
} }
void Servatrice::checkSql() bool Servatrice::checkSql()
{ {
if (databaseType == DatabaseNone)
return false;
QMutexLocker locker(&dbMutex); QMutexLocker locker(&dbMutex);
if (!QSqlDatabase::database().exec("select 1").isActive()) if (!QSqlDatabase::database().exec("select 1").isActive())
openDatabase(); return openDatabase();
return true;
} }
bool Servatrice::execSqlQuery(QSqlQuery &query) bool Servatrice::execSqlQuery(QSqlQuery &query)
@ -170,10 +184,11 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
{ {
QMutexLocker locker(&dbMutex); QMutexLocker locker(&dbMutex);
const QString method = settings->value("authentication/method").toString(); const QString method = settings->value("authentication/method").toString();
if (method == "none") switch (authenticationMethod) {
return UnknownUser; case AuthenticationNone: return UnknownUser;
else if (method == "sql") { case AuthenticationSql: {
checkSql(); if (!checkSql())
return UnknownUser;
QSqlQuery ipBanQuery; QSqlQuery ipBanQuery;
ipBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))) < 0, b.minutes <=> 0, b.visible_reason from " + dbPrefix + "_bans b where b.time_from = (select max(c.time_from) from " + dbPrefix + "_bans c where c.ip_address = :address) and b.ip_address = :address2"); ipBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))) < 0, b.minutes <=> 0, b.visible_reason from " + dbPrefix + "_bans b where b.time_from = (select max(c.time_from) from " + dbPrefix + "_bans c where c.ip_address = :address) and b.ip_address = :address2");
@ -228,15 +243,15 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
qDebug("Login accepted: unknown user"); qDebug("Login accepted: unknown user");
return UnknownUser; return UnknownUser;
} }
} else }
return UnknownUser; }
return UnknownUser;
} }
bool Servatrice::userExists(const QString &user) bool Servatrice::userExists(const QString &user)
{ {
QMutexLocker locker(&dbMutex); if (authenticationMethod == AuthenticationSql) {
const QString method = settings->value("authentication/method").toString(); QMutexLocker locker(&dbMutex);
if (method == "sql") {
checkSql(); checkSql();
QSqlQuery query; QSqlQuery query;
@ -245,26 +260,34 @@ bool Servatrice::userExists(const QString &user)
if (!execSqlQuery(query)) if (!execSqlQuery(query))
return false; return false;
return query.next(); return query.next();
} else return false; }
return false;
} }
int Servatrice::getUserIdInDB(const QString &name) int Servatrice::getUserIdInDB(const QString &name)
{ {
QMutexLocker locker(&dbMutex); if (authenticationMethod == AuthenticationSql) {
QSqlQuery query; QMutexLocker locker(&dbMutex);
query.prepare("select id from " + dbPrefix + "_users where name = :name and active = 1"); QSqlQuery query;
query.bindValue(":name", name); query.prepare("select id from " + dbPrefix + "_users where name = :name and active = 1");
if (!execSqlQuery(query)) query.bindValue(":name", name);
return -1; if (!execSqlQuery(query))
if (!query.next()) return -1;
return -1; if (!query.next())
return query.value(0).toInt(); return -1;
return query.value(0).toInt();
}
return -1;
} }
bool Servatrice::isInBuddyList(const QString &whoseList, const QString &who) bool Servatrice::isInBuddyList(const QString &whoseList, const QString &who)
{ {
if (authenticationMethod == AuthenticationNone)
return false;
QMutexLocker locker(&dbMutex); QMutexLocker locker(&dbMutex);
checkSql(); if (!checkSql())
return false;
int id1 = getUserIdInDB(whoseList); int id1 = getUserIdInDB(whoseList);
int id2 = getUserIdInDB(who); int id2 = getUserIdInDB(who);
@ -280,8 +303,12 @@ bool Servatrice::isInBuddyList(const QString &whoseList, const QString &who)
bool Servatrice::isInIgnoreList(const QString &whoseList, const QString &who) bool Servatrice::isInIgnoreList(const QString &whoseList, const QString &who)
{ {
if (authenticationMethod == AuthenticationNone)
return false;
QMutexLocker locker(&dbMutex); QMutexLocker locker(&dbMutex);
checkSql(); if (!checkSql())
return false;
int id1 = getUserIdInDB(whoseList); int id1 = getUserIdInDB(whoseList);
int id2 = getUserIdInDB(who); int id2 = getUserIdInDB(who);
@ -334,14 +361,15 @@ ServerInfo_User Servatrice::evalUserQueryResult(const QSqlQuery &query, bool com
ServerInfo_User Servatrice::getUserData(const QString &name) ServerInfo_User Servatrice::getUserData(const QString &name)
{ {
QMutexLocker locker(&dbMutex);
const QString method = settings->value("authentication/method").toString();
ServerInfo_User result; ServerInfo_User result;
result.set_name(name.toStdString()); result.set_name(name.toStdString());
result.set_user_level(ServerInfo_User::IsUser); result.set_user_level(ServerInfo_User::IsUser);
if (method == "sql") {
checkSql(); if (authenticationMethod == AuthenticationSql) {
QMutexLocker locker(&dbMutex);
if (!checkSql())
return result;
QSqlQuery query; QSqlQuery query;
query.prepare("select name, admin, realname, gender, country, avatar_bmp from " + dbPrefix + "_users where name = :name and active = 1"); query.prepare("select name, admin, realname, gender, country, avatar_bmp from " + dbPrefix + "_users where name = :name and active = 1");
query.bindValue(":name", name); query.bindValue(":name", name);
@ -368,8 +396,12 @@ int Servatrice::getUsersWithAddress(const QHostAddress &address) const
int Servatrice::startSession(const QString &userName, const QString &address) int Servatrice::startSession(const QString &userName, const QString &address)
{ {
if (authenticationMethod == AuthenticationNone)
return -1;
QMutexLocker locker(&dbMutex); QMutexLocker locker(&dbMutex);
checkSql(); if (!checkSql())
return -1;
QSqlQuery query; QSqlQuery query;
query.prepare("insert into " + dbPrefix + "_sessions (user_name, ip_address, start_time) values(:user_name, :ip_address, NOW())"); query.prepare("insert into " + dbPrefix + "_sessions (user_name, ip_address, start_time) values(:user_name, :ip_address, NOW())");
@ -382,8 +414,12 @@ int Servatrice::startSession(const QString &userName, const QString &address)
void Servatrice::endSession(int sessionId) void Servatrice::endSession(int sessionId)
{ {
if (authenticationMethod == AuthenticationNone)
return;
QMutexLocker locker(&dbMutex); QMutexLocker locker(&dbMutex);
checkSql(); if (!checkSql())
return;
QSqlQuery query; QSqlQuery query;
query.prepare("update " + dbPrefix + "_sessions set end_time=NOW() where id = :id_session"); query.prepare("update " + dbPrefix + "_sessions set end_time=NOW() where id = :id_session");
@ -393,11 +429,10 @@ void Servatrice::endSession(int sessionId)
QMap<QString, ServerInfo_User> Servatrice::getBuddyList(const QString &name) QMap<QString, ServerInfo_User> Servatrice::getBuddyList(const QString &name)
{ {
QMutexLocker locker(&dbMutex);
QMap<QString, ServerInfo_User> result; QMap<QString, ServerInfo_User> result;
const QString method = settings->value("authentication/method").toString(); if (authenticationMethod == AuthenticationSql) {
if (method == "sql") { QMutexLocker locker(&dbMutex);
checkSql(); checkSql();
QSqlQuery query; QSqlQuery query;
@ -416,11 +451,10 @@ QMap<QString, ServerInfo_User> Servatrice::getBuddyList(const QString &name)
QMap<QString, ServerInfo_User> Servatrice::getIgnoreList(const QString &name) QMap<QString, ServerInfo_User> Servatrice::getIgnoreList(const QString &name)
{ {
QMutexLocker locker(&dbMutex);
QMap<QString, ServerInfo_User> result; QMap<QString, ServerInfo_User> result;
const QString method = settings->value("authentication/method").toString(); if (authenticationMethod == AuthenticationSql) {
if (method == "sql") { QMutexLocker locker(&dbMutex);
checkSql(); checkSql();
QSqlQuery query; QSqlQuery query;
@ -440,7 +474,9 @@ QMap<QString, ServerInfo_User> Servatrice::getIgnoreList(const QString &name)
void Servatrice::updateLoginMessage() void Servatrice::updateLoginMessage()
{ {
QMutexLocker locker(&dbMutex); QMutexLocker locker(&dbMutex);
checkSql(); if (!checkSql())
return;
QSqlQuery query; QSqlQuery query;
query.prepare("select message from " + dbPrefix + "_servermessages where id_server = :id_server order by timest desc limit 1"); query.prepare("select message from " + dbPrefix + "_servermessages where id_server = :id_server order by timest desc limit 1");
query.bindValue(":id_server", serverId); query.bindValue(":id_server", serverId);
@ -475,7 +511,8 @@ void Servatrice::statusUpdate()
rxBytesMutex.unlock(); rxBytesMutex.unlock();
QMutexLocker locker(&dbMutex); QMutexLocker locker(&dbMutex);
checkSql(); if (!checkSql())
return;
QSqlQuery query; QSqlQuery query;
query.prepare("insert into " + dbPrefix + "_uptime (id_server, timest, uptime, users_count, games_count, tx_bytes, rx_bytes) values(:id, NOW(), :uptime, :users_count, :games_count, :tx, :rx)"); query.prepare("insert into " + dbPrefix + "_uptime (id_server, timest, uptime, users_count, games_count, tx_bytes, rx_bytes) values(:id, NOW(), :uptime, :users_count, :games_count, :tx, :rx)");

View file

@ -56,7 +56,7 @@ public:
Servatrice(QSettings *_settings, QObject *parent = 0); Servatrice(QSettings *_settings, QObject *parent = 0);
~Servatrice(); ~Servatrice();
bool openDatabase(); bool openDatabase();
void checkSql(); bool checkSql();
bool execSqlQuery(QSqlQuery &query); bool execSqlQuery(QSqlQuery &query);
QString getServerName() const { return serverName; } QString getServerName() const { return serverName; }
QString getLoginMessage() const { return loginMessage; } QString getLoginMessage() const { return loginMessage; }
@ -87,6 +87,10 @@ protected:
bool userExists(const QString &user); bool userExists(const QString &user);
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr); AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr);
private: private:
enum AuthenticationMethod { AuthenticationNone, AuthenticationSql };
enum DatabaseType { DatabaseNone, DatabaseMySql };
AuthenticationMethod authenticationMethod;
DatabaseType databaseType;
QTimer *pingClock, *statusUpdateClock; QTimer *pingClock, *statusUpdateClock;
QTcpServer *tcpServer; QTcpServer *tcpServer;
QString serverName; QString serverName;