converted some Player commands to command lists; added db type checks in server
This commit is contained in:
parent
0c9a2b061c
commit
4634787b00
4 changed files with 127 additions and 75 deletions
|
@ -15,7 +15,6 @@ unix:!macx {
|
|||
} else {
|
||||
QT += multimedia
|
||||
}
|
||||
QT += multimedia
|
||||
|
||||
HEADERS += src/abstractcounter.h \
|
||||
src/counter_general.h \
|
||||
|
|
|
@ -1592,15 +1592,19 @@ void Player::cardMenuAction(QAction *a)
|
|||
void Player::actIncPT(int deltaP, int deltaT)
|
||||
{
|
||||
QString ptString = "+" + QString::number(deltaP) + "/+" + QString::number(deltaT);
|
||||
|
||||
QList< const ::google::protobuf::Message * > commandList;
|
||||
QListIterator<QGraphicsItem *> j(scene()->selectedItems());
|
||||
while (j.hasNext()) {
|
||||
CardItem *card = static_cast<CardItem *>(j.next());
|
||||
Command_SetCardAttr cmd;
|
||||
cmd.set_zone(card->getZone()->getName().toStdString());
|
||||
cmd.set_card_id(card->getId());
|
||||
cmd.set_attr_name("pt");
|
||||
cmd.set_attr_value(ptString.toStdString());
|
||||
Command_SetCardAttr *cmd = new Command_SetCardAttr;
|
||||
cmd->set_zone(card->getZone()->getName().toStdString());
|
||||
cmd->set_card_id(card->getId());
|
||||
cmd->set_attr_name("pt");
|
||||
cmd->set_attr_value(ptString.toStdString());
|
||||
commandList.append(cmd);
|
||||
}
|
||||
sendGameCommand(prepareGameCommand(commandList));
|
||||
}
|
||||
|
||||
void Player::actSetPT(QAction * /*a*/)
|
||||
|
@ -1621,15 +1625,18 @@ void Player::actSetPT(QAction * /*a*/)
|
|||
if (!ok)
|
||||
return;
|
||||
|
||||
QList< const ::google::protobuf::Message * > commandList;
|
||||
QListIterator<QGraphicsItem *> j(scene()->selectedItems());
|
||||
while (j.hasNext()) {
|
||||
CardItem *card = static_cast<CardItem *>(j.next());
|
||||
Command_SetCardAttr cmd;
|
||||
cmd.set_zone(card->getZone()->getName().toStdString());
|
||||
cmd.set_card_id(card->getId());
|
||||
cmd.set_attr_name("pt");
|
||||
cmd.set_attr_value(pt.toStdString());
|
||||
Command_SetCardAttr *cmd = new Command_SetCardAttr;
|
||||
cmd->set_zone(card->getZone()->getName().toStdString());
|
||||
cmd->set_card_id(card->getId());
|
||||
cmd->set_attr_name("pt");
|
||||
cmd->set_attr_value(pt.toStdString());
|
||||
commandList.append(cmd);
|
||||
}
|
||||
sendGameCommand(prepareGameCommand(commandList));
|
||||
}
|
||||
|
||||
void Player::actSetAnnotation(QAction * /*a*/)
|
||||
|
@ -1651,15 +1658,18 @@ void Player::actSetAnnotation(QAction * /*a*/)
|
|||
if (!ok)
|
||||
return;
|
||||
|
||||
QList< const ::google::protobuf::Message * > commandList;
|
||||
i.toFront();
|
||||
while (i.hasNext()) {
|
||||
CardItem *card = static_cast<CardItem *>(i.next());
|
||||
Command_SetCardAttr cmd;
|
||||
cmd.set_zone(card->getZone()->getName().toStdString());
|
||||
cmd.set_card_id(card->getId());
|
||||
cmd.set_attr_name("annotation");
|
||||
cmd.set_attr_value(annotation.toStdString());
|
||||
Command_SetCardAttr *cmd = new Command_SetCardAttr;
|
||||
cmd->set_zone(card->getZone()->getName().toStdString());
|
||||
cmd->set_card_id(card->getId());
|
||||
cmd->set_attr_name("annotation");
|
||||
cmd->set_attr_value(annotation.toStdString());
|
||||
commandList.append(cmd);
|
||||
}
|
||||
sendGameCommand(prepareGameCommand(commandList));
|
||||
}
|
||||
|
||||
void Player::actAttach(QAction *a)
|
||||
|
@ -1683,18 +1693,19 @@ void Player::actCardCounterTrigger(QAction *a)
|
|||
{
|
||||
int counterId = a->data().toInt() / 1000;
|
||||
int action = a->data().toInt() % 1000;
|
||||
QList< const ::google::protobuf::Message * > commandList;
|
||||
switch (action) {
|
||||
case 9: {
|
||||
QListIterator<QGraphicsItem *> i(scene()->selectedItems());
|
||||
while (i.hasNext()) {
|
||||
CardItem *card = static_cast<CardItem *>(i.next());
|
||||
if (card->getCounters().value(counterId, 0) < MAX_COUNTERS_ON_CARD) {
|
||||
Command_SetCardCounter cmd;
|
||||
cmd.set_zone(card->getZone()->getName().toStdString());
|
||||
cmd.set_card_id(card->getId());
|
||||
cmd.set_counter_id(counterId);
|
||||
cmd.set_counter_value(card->getCounters().value(counterId, 0) + 1);
|
||||
sendGameCommand(cmd);
|
||||
Command_SetCardCounter *cmd = new Command_SetCardCounter;
|
||||
cmd->set_zone(card->getZone()->getName().toStdString());
|
||||
cmd->set_card_id(card->getId());
|
||||
cmd->set_counter_id(counterId);
|
||||
cmd->set_counter_value(card->getCounters().value(counterId, 0) + 1);
|
||||
commandList.append(cmd);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -1704,12 +1715,12 @@ void Player::actCardCounterTrigger(QAction *a)
|
|||
while (i.hasNext()) {
|
||||
CardItem *card = static_cast<CardItem *>(i.next());
|
||||
if (card->getCounters().value(counterId, 0)) {
|
||||
Command_SetCardCounter cmd;
|
||||
cmd.set_zone(card->getZone()->getName().toStdString());
|
||||
cmd.set_card_id(card->getId());
|
||||
cmd.set_counter_id(counterId);
|
||||
cmd.set_counter_value(card->getCounters().value(counterId, 0) - 1);
|
||||
sendGameCommand(cmd);
|
||||
Command_SetCardCounter *cmd = new Command_SetCardCounter;
|
||||
cmd->set_zone(card->getZone()->getName().toStdString());
|
||||
cmd->set_card_id(card->getId());
|
||||
cmd->set_counter_id(counterId);
|
||||
cmd->set_counter_value(card->getCounters().value(counterId, 0) - 1);
|
||||
commandList.append(cmd);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -1727,17 +1738,18 @@ void Player::actCardCounterTrigger(QAction *a)
|
|||
QListIterator<QGraphicsItem *> i(scene()->selectedItems());
|
||||
while (i.hasNext()) {
|
||||
CardItem *card = static_cast<CardItem *>(i.next());
|
||||
Command_SetCardCounter cmd;
|
||||
cmd.set_zone(card->getZone()->getName().toStdString());
|
||||
cmd.set_card_id(card->getId());
|
||||
cmd.set_counter_id(counterId);
|
||||
cmd.set_counter_value(number);
|
||||
sendGameCommand(cmd);
|
||||
Command_SetCardCounter *cmd = new Command_SetCardCounter;
|
||||
cmd->set_zone(card->getZone()->getName().toStdString());
|
||||
cmd->set_card_id(card->getId());
|
||||
cmd->set_counter_id(counterId);
|
||||
cmd->set_counter_value(number);
|
||||
commandList.append(cmd);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: ;
|
||||
}
|
||||
sendGameCommand(prepareGameCommand(commandList));
|
||||
}
|
||||
|
||||
void Player::setCardMenu(QMenu *menu)
|
||||
|
|
|
@ -71,9 +71,19 @@ Servatrice::Servatrice(QSettings *_settings, QObject *parent)
|
|||
else
|
||||
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();
|
||||
if (dbType == "mysql")
|
||||
if (databaseType != DatabaseNone)
|
||||
openDatabase();
|
||||
|
||||
int size = settings->beginReadArray("rooms");
|
||||
|
@ -151,11 +161,15 @@ bool Servatrice::openDatabase()
|
|||
return true;
|
||||
}
|
||||
|
||||
void Servatrice::checkSql()
|
||||
bool Servatrice::checkSql()
|
||||
{
|
||||
if (databaseType == DatabaseNone)
|
||||
return false;
|
||||
|
||||
QMutexLocker locker(&dbMutex);
|
||||
if (!QSqlDatabase::database().exec("select 1").isActive())
|
||||
openDatabase();
|
||||
return openDatabase();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Servatrice::execSqlQuery(QSqlQuery &query)
|
||||
|
@ -170,10 +184,11 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
|
|||
{
|
||||
QMutexLocker locker(&dbMutex);
|
||||
const QString method = settings->value("authentication/method").toString();
|
||||
if (method == "none")
|
||||
return UnknownUser;
|
||||
else if (method == "sql") {
|
||||
checkSql();
|
||||
switch (authenticationMethod) {
|
||||
case AuthenticationNone: return UnknownUser;
|
||||
case AuthenticationSql: {
|
||||
if (!checkSql())
|
||||
return UnknownUser;
|
||||
|
||||
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");
|
||||
|
@ -228,15 +243,15 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
|
|||
qDebug("Login accepted: unknown user");
|
||||
return UnknownUser;
|
||||
}
|
||||
} else
|
||||
return UnknownUser;
|
||||
}
|
||||
}
|
||||
return UnknownUser;
|
||||
}
|
||||
|
||||
bool Servatrice::userExists(const QString &user)
|
||||
{
|
||||
QMutexLocker locker(&dbMutex);
|
||||
const QString method = settings->value("authentication/method").toString();
|
||||
if (method == "sql") {
|
||||
if (authenticationMethod == AuthenticationSql) {
|
||||
QMutexLocker locker(&dbMutex);
|
||||
checkSql();
|
||||
|
||||
QSqlQuery query;
|
||||
|
@ -245,26 +260,34 @@ bool Servatrice::userExists(const QString &user)
|
|||
if (!execSqlQuery(query))
|
||||
return false;
|
||||
return query.next();
|
||||
} else return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int Servatrice::getUserIdInDB(const QString &name)
|
||||
{
|
||||
QMutexLocker locker(&dbMutex);
|
||||
QSqlQuery query;
|
||||
query.prepare("select id from " + dbPrefix + "_users where name = :name and active = 1");
|
||||
query.bindValue(":name", name);
|
||||
if (!execSqlQuery(query))
|
||||
return -1;
|
||||
if (!query.next())
|
||||
return -1;
|
||||
return query.value(0).toInt();
|
||||
if (authenticationMethod == AuthenticationSql) {
|
||||
QMutexLocker locker(&dbMutex);
|
||||
QSqlQuery query;
|
||||
query.prepare("select id from " + dbPrefix + "_users where name = :name and active = 1");
|
||||
query.bindValue(":name", name);
|
||||
if (!execSqlQuery(query))
|
||||
return -1;
|
||||
if (!query.next())
|
||||
return -1;
|
||||
return query.value(0).toInt();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool Servatrice::isInBuddyList(const QString &whoseList, const QString &who)
|
||||
{
|
||||
if (authenticationMethod == AuthenticationNone)
|
||||
return false;
|
||||
|
||||
QMutexLocker locker(&dbMutex);
|
||||
checkSql();
|
||||
if (!checkSql())
|
||||
return false;
|
||||
|
||||
int id1 = getUserIdInDB(whoseList);
|
||||
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)
|
||||
{
|
||||
if (authenticationMethod == AuthenticationNone)
|
||||
return false;
|
||||
|
||||
QMutexLocker locker(&dbMutex);
|
||||
checkSql();
|
||||
if (!checkSql())
|
||||
return false;
|
||||
|
||||
int id1 = getUserIdInDB(whoseList);
|
||||
int id2 = getUserIdInDB(who);
|
||||
|
@ -334,14 +361,15 @@ ServerInfo_User Servatrice::evalUserQueryResult(const QSqlQuery &query, bool com
|
|||
|
||||
ServerInfo_User Servatrice::getUserData(const QString &name)
|
||||
{
|
||||
QMutexLocker locker(&dbMutex);
|
||||
const QString method = settings->value("authentication/method").toString();
|
||||
ServerInfo_User result;
|
||||
result.set_name(name.toStdString());
|
||||
result.set_user_level(ServerInfo_User::IsUser);
|
||||
if (method == "sql") {
|
||||
checkSql();
|
||||
|
||||
|
||||
if (authenticationMethod == AuthenticationSql) {
|
||||
QMutexLocker locker(&dbMutex);
|
||||
if (!checkSql())
|
||||
return result;
|
||||
|
||||
QSqlQuery query;
|
||||
query.prepare("select name, admin, realname, gender, country, avatar_bmp from " + dbPrefix + "_users where name = :name and active = 1");
|
||||
query.bindValue(":name", name);
|
||||
|
@ -368,8 +396,12 @@ int Servatrice::getUsersWithAddress(const QHostAddress &address) const
|
|||
|
||||
int Servatrice::startSession(const QString &userName, const QString &address)
|
||||
{
|
||||
if (authenticationMethod == AuthenticationNone)
|
||||
return -1;
|
||||
|
||||
QMutexLocker locker(&dbMutex);
|
||||
checkSql();
|
||||
if (!checkSql())
|
||||
return -1;
|
||||
|
||||
QSqlQuery query;
|
||||
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)
|
||||
{
|
||||
if (authenticationMethod == AuthenticationNone)
|
||||
return;
|
||||
|
||||
QMutexLocker locker(&dbMutex);
|
||||
checkSql();
|
||||
if (!checkSql())
|
||||
return;
|
||||
|
||||
QSqlQuery query;
|
||||
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)
|
||||
{
|
||||
QMutexLocker locker(&dbMutex);
|
||||
QMap<QString, ServerInfo_User> result;
|
||||
|
||||
const QString method = settings->value("authentication/method").toString();
|
||||
if (method == "sql") {
|
||||
if (authenticationMethod == AuthenticationSql) {
|
||||
QMutexLocker locker(&dbMutex);
|
||||
checkSql();
|
||||
|
||||
QSqlQuery query;
|
||||
|
@ -416,11 +451,10 @@ QMap<QString, ServerInfo_User> Servatrice::getBuddyList(const QString &name)
|
|||
|
||||
QMap<QString, ServerInfo_User> Servatrice::getIgnoreList(const QString &name)
|
||||
{
|
||||
QMutexLocker locker(&dbMutex);
|
||||
QMap<QString, ServerInfo_User> result;
|
||||
|
||||
const QString method = settings->value("authentication/method").toString();
|
||||
if (method == "sql") {
|
||||
if (authenticationMethod == AuthenticationSql) {
|
||||
QMutexLocker locker(&dbMutex);
|
||||
checkSql();
|
||||
|
||||
QSqlQuery query;
|
||||
|
@ -440,7 +474,9 @@ QMap<QString, ServerInfo_User> Servatrice::getIgnoreList(const QString &name)
|
|||
void Servatrice::updateLoginMessage()
|
||||
{
|
||||
QMutexLocker locker(&dbMutex);
|
||||
checkSql();
|
||||
if (!checkSql())
|
||||
return;
|
||||
|
||||
QSqlQuery query;
|
||||
query.prepare("select message from " + dbPrefix + "_servermessages where id_server = :id_server order by timest desc limit 1");
|
||||
query.bindValue(":id_server", serverId);
|
||||
|
@ -475,7 +511,8 @@ void Servatrice::statusUpdate()
|
|||
rxBytesMutex.unlock();
|
||||
|
||||
QMutexLocker locker(&dbMutex);
|
||||
checkSql();
|
||||
if (!checkSql())
|
||||
return;
|
||||
|
||||
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)");
|
||||
|
|
|
@ -56,7 +56,7 @@ public:
|
|||
Servatrice(QSettings *_settings, QObject *parent = 0);
|
||||
~Servatrice();
|
||||
bool openDatabase();
|
||||
void checkSql();
|
||||
bool checkSql();
|
||||
bool execSqlQuery(QSqlQuery &query);
|
||||
QString getServerName() const { return serverName; }
|
||||
QString getLoginMessage() const { return loginMessage; }
|
||||
|
@ -87,6 +87,10 @@ protected:
|
|||
bool userExists(const QString &user);
|
||||
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr);
|
||||
private:
|
||||
enum AuthenticationMethod { AuthenticationNone, AuthenticationSql };
|
||||
enum DatabaseType { DatabaseNone, DatabaseMySql };
|
||||
AuthenticationMethod authenticationMethod;
|
||||
DatabaseType databaseType;
|
||||
QTimer *pingClock, *statusUpdateClock;
|
||||
QTcpServer *tcpServer;
|
||||
QString serverName;
|
||||
|
|
Loading…
Reference in a new issue