2018-04-11 20:27:22 +03:00
|
|
|
#include "backEnd.h"
|
|
|
|
#include <cmath>
|
2018-04-13 22:24:52 +03:00
|
|
|
#include <QDataStream>
|
2019-07-04 18:06:25 +03:00
|
|
|
#include <QDir>
|
2019-11-10 21:46:27 +03:00
|
|
|
#include "gamestate.h"
|
|
|
|
|
|
|
|
constexpr unsigned char currentVersion = 6;
|
2018-01-20 17:44:28 +03:00
|
|
|
|
2018-04-11 20:27:22 +03:00
|
|
|
BackEnd::BackEnd():
|
2018-01-20 17:44:28 +03:00
|
|
|
QObject()
|
|
|
|
{
|
2019-11-10 14:11:50 +03:00
|
|
|
_settings = QuasarAppUtils::Settings::get();
|
2018-07-03 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackEnd::reset(){
|
2019-11-10 14:11:50 +03:00
|
|
|
|
|
|
|
_settings->setValue("isFirstStart", true);
|
|
|
|
_settings->setValue("lvl", 1);
|
|
|
|
_settings->setValue("animation", true);
|
|
|
|
_settings->setValue("randomColor", false);
|
2019-09-17 15:40:48 +03:00
|
|
|
|
2019-11-10 22:14:27 +03:00
|
|
|
for (auto& item : _profileList) {
|
|
|
|
item->deleteLater();
|
|
|
|
}
|
2019-11-10 21:46:27 +03:00
|
|
|
_profileList.clear();
|
|
|
|
emit profileListChanged();
|
|
|
|
|
|
|
|
}
|
2019-10-07 15:20:07 +03:00
|
|
|
|
2019-11-10 21:46:27 +03:00
|
|
|
bool BackEnd::init() {
|
|
|
|
QFile f(MAIN_SETINGS_FILE);
|
|
|
|
if(f.exists() && f.open(QIODevice::ReadOnly)){
|
|
|
|
QDataStream stream(&f);
|
|
|
|
|
|
|
|
unsigned char dataVersion;
|
|
|
|
stream >> dataVersion;
|
|
|
|
if (dataVersion != currentVersion) {
|
|
|
|
stream >> _profileList;
|
|
|
|
stream >> _profile;
|
|
|
|
|
|
|
|
f.close();
|
|
|
|
} else {
|
|
|
|
unsigned short lvl;
|
|
|
|
bool isFirstStart, _animation, _randomColor;
|
|
|
|
stream >> lvl;
|
|
|
|
stream >> isFirstStart;
|
|
|
|
stream >> _animation;
|
|
|
|
stream >> _randomColor;
|
|
|
|
|
|
|
|
if(lvl < 1 || lvl > 99) {
|
|
|
|
lvl = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
setAnimation(_animation);
|
|
|
|
setRandomColor(_randomColor);
|
|
|
|
setRandomColor(isFirstStart);
|
|
|
|
|
2019-11-10 22:14:27 +03:00
|
|
|
auto profile = _profileList.value("User", nullptr);
|
|
|
|
if (!profile) {
|
|
|
|
profile = new ProfileData();
|
|
|
|
_profileList["User"] = profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
static_cast<GameState*>((profile->
|
2019-11-10 21:46:27 +03:00
|
|
|
gameState()))->saveLvl(
|
|
|
|
static_cast<short>(lvl));
|
|
|
|
|
|
|
|
emit firstChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-07 15:20:07 +03:00
|
|
|
|
2019-11-10 21:46:27 +03:00
|
|
|
return false;
|
2018-04-11 20:27:22 +03:00
|
|
|
}
|
|
|
|
|
2019-11-10 21:46:27 +03:00
|
|
|
void BackEnd::saveLocalData() const {
|
|
|
|
QFile f(MAIN_SETINGS_FILE);
|
|
|
|
|
|
|
|
if(f.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
|
|
|
|
QDataStream stream(&f);
|
|
|
|
stream << currentVersion;
|
|
|
|
stream << _profileList;
|
|
|
|
stream << _profile;
|
|
|
|
|
|
|
|
f.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuasarAppUtils::Params::verboseLog("local file data not opened on not created1 " + f.fileName(),
|
|
|
|
QuasarAppUtils::Error);
|
2018-01-20 17:44:28 +03:00
|
|
|
}
|
2018-04-11 20:27:22 +03:00
|
|
|
|
2018-07-03 17:45:02 +03:00
|
|
|
bool BackEnd::randomColor() const {
|
2019-11-10 14:11:50 +03:00
|
|
|
return _settings->getValue("randomColor", false).toBool();
|
2018-07-03 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackEnd::setRandomColor(bool random) {
|
2019-11-10 14:11:50 +03:00
|
|
|
_settings->setValue("randomColor", random);
|
|
|
|
emit randomColorChanged();
|
2018-07-03 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BackEnd::animation() const{
|
2019-11-10 14:11:50 +03:00
|
|
|
return _settings->getValue("animation", true).toBool();
|
2018-07-03 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackEnd::setAnimation(bool name) {
|
2019-11-10 14:11:50 +03:00
|
|
|
_settings->setValue("animation", name);
|
|
|
|
emit animationChanged();
|
2018-07-03 17:45:02 +03:00
|
|
|
}
|
|
|
|
|
2018-04-11 22:13:34 +03:00
|
|
|
unsigned short BackEnd::getMinSteps(const unsigned short lvl) const{
|
2019-07-05 11:17:26 +03:00
|
|
|
return static_cast<unsigned short>(pow(2, lvl)) - 1;
|
2018-04-11 22:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BackEnd::isFirst()const{
|
2019-11-10 14:11:50 +03:00
|
|
|
return _settings->getValue("isFirstStart", true).toBool();
|
2018-04-11 22:13:34 +03:00
|
|
|
}
|
|
|
|
|
2019-11-10 14:11:50 +03:00
|
|
|
void BackEnd::setShowHelp(bool state) {
|
|
|
|
_settings->setValue("isFirstStart", state);
|
2018-07-03 17:45:02 +03:00
|
|
|
emit firstChanged();
|
2019-11-10 14:11:50 +03:00
|
|
|
|
2018-04-11 22:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BackEnd::~BackEnd(){
|
2019-11-10 21:46:27 +03:00
|
|
|
saveLocalData();
|
2018-01-20 17:44:28 +03:00
|
|
|
}
|
2019-09-17 15:40:48 +03:00
|
|
|
|
2019-11-10 14:11:50 +03:00
|
|
|
QString BackEnd::profile() const {
|
2019-11-10 21:46:27 +03:00
|
|
|
return _profile;
|
2019-11-10 14:11:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList BackEnd::profileList() const {
|
2019-11-10 21:46:27 +03:00
|
|
|
return _profileList.keys();
|
2019-11-10 14:11:50 +03:00
|
|
|
}
|
|
|
|
|
2019-10-07 15:20:07 +03:00
|
|
|
QObject* BackEnd::gameState() {
|
2019-11-10 21:46:27 +03:00
|
|
|
if (!_profileList.contains(_profile)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-10 14:11:50 +03:00
|
|
|
|
2019-11-10 22:14:27 +03:00
|
|
|
return _profileList[_profile]->gameState();
|
2019-11-10 14:11:50 +03:00
|
|
|
}
|