mirror of
https://github.com/QuasarApp/Snake.git
synced 2025-05-08 07:29:45 +00:00
refactor dbWriter
This commit is contained in:
parent
81a10f0b53
commit
eb6738369f
SnakeServer
@ -51,9 +51,8 @@ private:
|
||||
protected:
|
||||
qint8 _class = -1;
|
||||
|
||||
BaseNetworkObject();
|
||||
|
||||
public:
|
||||
BaseNetworkObject();
|
||||
virtual BaseNetworkObject* create() const;
|
||||
virtual ~BaseNetworkObject();
|
||||
|
||||
@ -63,7 +62,6 @@ public:
|
||||
virtual bool isValid() const;
|
||||
void toBytes(QByteArray& array) const;
|
||||
void fromBytes(const QByteArray& array);
|
||||
auto cast();
|
||||
int id() const;
|
||||
void setId(int id);
|
||||
qint8 getClass() const;
|
||||
|
@ -28,13 +28,13 @@ enum class Command: quint8 {
|
||||
GetItem = 0x06,
|
||||
Player = 0x07,
|
||||
Snake = 0x08,
|
||||
Map = 0x08
|
||||
Map = 0x09
|
||||
};
|
||||
|
||||
bool isValidSize(quint8 type, unsigned int size);
|
||||
|
||||
bool initClientProtockol();
|
||||
|
||||
auto cast(const BaseNetworkObject *obj);
|
||||
/**
|
||||
* @brief The Header struct 4 byte
|
||||
*/
|
||||
@ -98,7 +98,7 @@ struct CLIENTPROTOCOLSHARED_EXPORT Package {
|
||||
* @brief isValid
|
||||
* @return true if package is valid
|
||||
*/
|
||||
bool isValid() const;
|
||||
virtual bool isValid() const;
|
||||
|
||||
/**
|
||||
* @brief parse
|
||||
@ -132,6 +132,8 @@ struct CLIENTPROTOCOLSHARED_EXPORT Package {
|
||||
*/
|
||||
void reset();
|
||||
|
||||
virtual ~Package() = default;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += \
|
||||
item.cpp \
|
||||
mainserver.cpp \
|
||||
playerdbdata.cpp \
|
||||
sqldbcache.cpp \
|
||||
@ -39,6 +40,7 @@ CONFIG(release, debug|release): {
|
||||
}
|
||||
|
||||
HEADERS += \
|
||||
item.h \
|
||||
mainserver.h \
|
||||
playerdbdata.h \
|
||||
server_global.h \ \
|
||||
|
40
SnakeServer/Server/item.cpp
Normal file
40
SnakeServer/Server/item.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include "item.h"
|
||||
#include "factorynetobjects.h"
|
||||
|
||||
|
||||
int Item::getId() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
void Item::setId(int value) {
|
||||
id = value;
|
||||
}
|
||||
|
||||
Item::Item(const ClientProtocol::Package &other) {
|
||||
hdr = other.hdr;
|
||||
data = other.data;
|
||||
ClientProtocol::BaseNetworkObject base;
|
||||
base.fromBytes(data);
|
||||
id = base.id();
|
||||
}
|
||||
|
||||
Item::Item(const ClientProtocol::BaseNetworkObject *obj) {
|
||||
if (!create(obj, ClientProtocol::Type::Stream)) {
|
||||
throw "Error create Item from BaseNetworkObject!";
|
||||
}
|
||||
|
||||
id = obj->id();
|
||||
}
|
||||
|
||||
ClientProtocol::Command Item::cmd() const {
|
||||
return static_cast<ClientProtocol::Command>(hdr.command);
|
||||
}
|
||||
|
||||
const QByteArray &Item::dataArray() const {
|
||||
return data;
|
||||
}
|
||||
|
||||
bool Item::isValid() const {
|
||||
return ClientProtocol::FactoryNetObjects::isRegisteredType(hdr.type)
|
||||
&& Package::isValid();
|
||||
}
|
23
SnakeServer/Server/item.h
Normal file
23
SnakeServer/Server/item.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef ITEM_H
|
||||
#define ITEM_H
|
||||
#include <clientprotocol.h>
|
||||
#include <clientprotocol_global.h>
|
||||
|
||||
class Item : protected ClientProtocol::Package
|
||||
{
|
||||
private:
|
||||
|
||||
int id = -1;
|
||||
|
||||
public:
|
||||
Item(const ClientProtocol::Package& other);
|
||||
Item(const ClientProtocol::BaseNetworkObject* obj);
|
||||
~Item() override;
|
||||
ClientProtocol::Command cmd() const;
|
||||
const QByteArray& dataArray() const;
|
||||
bool isValid() const override;
|
||||
int getId() const;
|
||||
void setId(int value);
|
||||
};
|
||||
|
||||
#endif // ITEM_H
|
@ -31,7 +31,7 @@ bool SqlDBCache::checkPlayer(int id) {
|
||||
if (SqlDBWriter::checkPlayer(id)) {
|
||||
|
||||
PlayerDBData *player = getPlayer(id);
|
||||
if (savePlayer(player) < 0) {
|
||||
if (savePlayer(*player) < 0) {
|
||||
QuasarAppUtils::Params::verboseLog("not saved data into cache "
|
||||
" SqlDBCashe::checkPlayer");
|
||||
}
|
||||
@ -183,22 +183,22 @@ ClientProtocol::BaseNetworkObject * SqlDBCache::getItem(int id) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int SqlDBCache::saveItem(ClientProtocol::BaseNetworkObject *res) {
|
||||
int SqlDBCache::saveItem(const Item &item) {
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int id = res->id();
|
||||
int id = item->id();
|
||||
|
||||
if (id < 0) {
|
||||
id = generateIdForItem();
|
||||
res->setId(id);
|
||||
item->setId(id);
|
||||
}
|
||||
|
||||
if (!res->isValid()) {
|
||||
if (!item->isValid()) {
|
||||
return -1;
|
||||
}
|
||||
items.insert(id, res);
|
||||
items.insert(id, item);
|
||||
|
||||
globalUpdateDataBase(SqlDBCasheWriteMode::On_New_Thread);
|
||||
|
||||
@ -224,7 +224,7 @@ PlayerDBData* SqlDBCache::getPlayer(int id) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int SqlDBCache::savePlayer(PlayerDBData *player) {
|
||||
int SqlDBCache::savePlayer(const PlayerDBData &player) {
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <QSet>
|
||||
#include <QVariantMap>
|
||||
#include <player.h>
|
||||
#include <clientprotocol.h>
|
||||
|
||||
enum class SqlDBCasheWriteMode: int {
|
||||
Default = 0x0,
|
||||
@ -24,7 +25,7 @@ private:
|
||||
qint64 lastUpdateTime = 0;
|
||||
qint64 updateInterval = DEFAULT_UPDATE_INTERVAL;
|
||||
|
||||
QMap <int, ClientProtocol::BaseNetworkObject*> items;
|
||||
QMap <int, ClientProtocol::Package> items;
|
||||
QMap <int, PlayerDBData*> players;
|
||||
QHash <int, QSet<int>> owners;
|
||||
|
||||
@ -47,9 +48,9 @@ public:
|
||||
const QString &path = DEFAULT_DB_PATH) override;
|
||||
|
||||
ClientProtocol::BaseNetworkObject * getItem(int id) override;
|
||||
int saveItem(ClientProtocol::BaseNetworkObject *res) override;
|
||||
int saveItem(const Item& item) override;
|
||||
PlayerDBData* getPlayer(int id) override;
|
||||
int savePlayer(PlayerDBData *player) override;
|
||||
int savePlayer(const PlayerDBData &player) override;
|
||||
|
||||
bool giveAwayItem(int player, int item);
|
||||
bool getItem(int player, int item, bool check = true);
|
||||
|
@ -183,19 +183,19 @@ bool SqlDBWriter::checkItem(int idItem, int idOwner) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int SqlDBWriter::savePlayer(PlayerDBData *player) {
|
||||
int SqlDBWriter::savePlayer(const PlayerDBData &player) {
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!player->isValid()) {
|
||||
if (!player.isValid()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
QString request;
|
||||
int id = player->id();
|
||||
int id = player.id();
|
||||
|
||||
int curSnake = player->getCureentSnake();
|
||||
int curSnake = player.getCureentSnake();
|
||||
|
||||
if (curSnake >= 0 && !checkItem(curSnake, id)) {
|
||||
return -1;
|
||||
@ -206,13 +206,13 @@ int SqlDBWriter::savePlayer(PlayerDBData *player) {
|
||||
request = QString("UPDATE players SET name='%0', gmail='%1', money='%2',"
|
||||
" avgrecord='%3', record='%4', lastOnline='%5',"
|
||||
" onlinetime='%6', currentsnake='%7' WHERE id='%8' ").arg(
|
||||
player->getName()).arg(
|
||||
player->getGmail()).arg(
|
||||
player->getMany()).arg(
|
||||
player->getAvgRecord()).arg(
|
||||
player->getRecord()).arg(
|
||||
player->getLastOnline()).arg(
|
||||
player->getOnlineTime()).arg(
|
||||
player.getName()).arg(
|
||||
player.getGmail()).arg(
|
||||
player.getMany()).arg(
|
||||
player.getAvgRecord()).arg(
|
||||
player.getRecord()).arg(
|
||||
player.getLastOnline()).arg(
|
||||
player.getOnlineTime()).arg(
|
||||
(curSnake >= 0)? QString::number(curSnake) : "NULL").arg(
|
||||
id);
|
||||
|
||||
@ -221,13 +221,13 @@ int SqlDBWriter::savePlayer(PlayerDBData *player) {
|
||||
" lastOnline, onlinetime, currentsnake) VALUES "
|
||||
"('%0', '%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8')").arg(
|
||||
id).arg(
|
||||
player->getName()).arg(
|
||||
player->getGmail()).arg(
|
||||
player->getMany()).arg(
|
||||
player->getAvgRecord()).arg(
|
||||
player->getRecord()).arg(
|
||||
player->getLastOnline()).arg(
|
||||
player->getOnlineTime()).arg(
|
||||
player.getName()).arg(
|
||||
player.getGmail()).arg(
|
||||
player.getMany()).arg(
|
||||
player.getAvgRecord()).arg(
|
||||
player.getRecord()).arg(
|
||||
player.getLastOnline()).arg(
|
||||
player.getOnlineTime()).arg(
|
||||
(curSnake >= 0)? QString::number(curSnake) : "NULL");
|
||||
|
||||
}
|
||||
@ -250,24 +250,21 @@ int SqlDBWriter::savePlayer(PlayerDBData *player) {
|
||||
return id;
|
||||
}
|
||||
|
||||
int SqlDBWriter::saveItem(ClientProtocol::BaseNetworkObject *item) {
|
||||
int SqlDBWriter::saveItem(const Item &item) {
|
||||
if (!isValid()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!item->isValid()) {
|
||||
if (!item.isValid()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto type = item->getClass();
|
||||
auto type = item.cmd();
|
||||
int id = item.getId();
|
||||
|
||||
int id = item->id();
|
||||
|
||||
QByteArray bytes;
|
||||
QByteArray bytes = item.dataArray();
|
||||
QString request;
|
||||
|
||||
item->toBytes(bytes);
|
||||
|
||||
if (checkItem(id)) {
|
||||
request = QString("UPDATE items SET type='%1', data = :bytes where id = %0").
|
||||
arg(id).
|
||||
|
@ -1,5 +1,7 @@
|
||||
#ifndef SQLDBWRITER_H
|
||||
#define SQLDBWRITER_H
|
||||
#include "item.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QSqlDatabase>
|
||||
#include <QDir>
|
||||
@ -35,8 +37,8 @@ protected:
|
||||
virtual bool checkPlayer(int id);
|
||||
virtual bool checkItem(int idItem, int idOwner = -1);
|
||||
|
||||
virtual int savePlayer(PlayerDBData *player);
|
||||
virtual int saveItem(ClientProtocol::BaseNetworkObject *item);
|
||||
virtual int savePlayer(const PlayerDBData& player);
|
||||
virtual int saveItem(const Item &item);
|
||||
virtual bool saveowners(int player, const QSet<int>);
|
||||
|
||||
virtual bool getAllItemsOfPalyer(int player, QSet<int>& items);
|
||||
|
Loading…
x
Reference in New Issue
Block a user