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>
|
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-10-07 15:20:07 +03:00
|
|
|
if (_gameState) {
|
|
|
|
_gameState->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
_gameState = new GameState();
|
|
|
|
|
2018-04-11 20:27:22 +03:00
|
|
|
}
|
|
|
|
|
2019-07-04 18:06:25 +03:00
|
|
|
void BackEnd::writeConfig() const {
|
2019-11-10 14:11:50 +03:00
|
|
|
_settings->sync();
|
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
|
|
|
void BackEnd::readCnfig() {
|
2019-07-04 18:06:25 +03:00
|
|
|
QFile f(MAIN_SETINGS_FILE);
|
2018-04-13 22:24:52 +03:00
|
|
|
if(f.exists() && f.open(QIODevice::ReadOnly)){
|
|
|
|
QDataStream stream(&f);
|
2019-11-10 14:11:50 +03:00
|
|
|
unsigned short lvl;
|
|
|
|
bool isFirstStart, _animation, _randomColor;
|
2018-04-13 22:24:52 +03:00
|
|
|
stream >> lvl;
|
|
|
|
stream >> isFirstStart;
|
2018-07-03 17:45:02 +03:00
|
|
|
stream >> _animation;
|
|
|
|
stream >> _randomColor;
|
|
|
|
|
2019-11-10 14:11:50 +03:00
|
|
|
stream >> _gameState;
|
2019-09-17 15:40:48 +03:00
|
|
|
|
2018-01-20 17:44:28 +03:00
|
|
|
f.close();
|
2018-04-12 23:18:39 +03:00
|
|
|
|
|
|
|
if(lvl < 1 || lvl > 99) {
|
|
|
|
lvl = 1;
|
|
|
|
}
|
|
|
|
|
2019-11-10 14:11:50 +03:00
|
|
|
setAnimation(_animation);
|
|
|
|
setRandomColor(_randomColor);
|
|
|
|
setRandomColor(isFirstStart);
|
|
|
|
|
2018-07-03 17:45:02 +03:00
|
|
|
emit firstChanged();
|
2018-01-20 17:44:28 +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
|
|
|
}
|
|
|
|
|
2019-07-05 11:17:26 +03:00
|
|
|
void BackEnd::save(short lvl){
|
2019-11-10 14:11:50 +03:00
|
|
|
_settings->setValue("lvl", static_cast<unsigned short>(lvl));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
short BackEnd::read()const{
|
2019-11-10 14:11:50 +03:00
|
|
|
return static_cast<short>(_settings->getValue("lvl", 1).toInt());
|
2018-04-11 22:13:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BackEnd::~BackEnd(){
|
|
|
|
writeConfig();
|
2019-10-07 15:20:07 +03:00
|
|
|
|
|
|
|
if (_gameState) {
|
|
|
|
delete _gameState;
|
|
|
|
}
|
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 {
|
|
|
|
return _localProfilesList.value(_profileIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList BackEnd::profileList() const {
|
|
|
|
return _localProfilesList;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BackEnd::profileIndex() const {
|
|
|
|
return _profileIndex;
|
|
|
|
}
|
|
|
|
|
2019-10-07 15:20:07 +03:00
|
|
|
QObject* BackEnd::gameState() {
|
2019-10-07 09:47:51 +03:00
|
|
|
return _gameState;
|
2019-09-17 15:40:48 +03:00
|
|
|
}
|
2019-11-10 14:11:50 +03:00
|
|
|
|
|
|
|
void BackEnd::setProfileList(QStringList profileList) {
|
|
|
|
if (_localProfilesList == profileList)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_localProfilesList = profileList;
|
|
|
|
emit profileListChanged(_localProfilesList);
|
|
|
|
}
|