197 lines
4.6 KiB
C++
Raw Normal View History

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
2019-11-11 23:36:18 +03:00
#define DEFAULT_USER "User"
#define CURRENT_PROFILE_KEY "currentProfile"
#define FIRST_RUN_KEY "isFirstStart"
#define LVL_KEY "lvl"
#define ANIMATION_KEY "animation"
#define RANDOM_COLOR_KEY "randomColor"
#define CURRENT_PROFILE_KEY "currentProfile"
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();
2019-11-11 23:36:18 +03:00
_profile = _settings->getStrValue(CURRENT_PROFILE_KEY, DEFAULT_USER);
2018-07-03 17:45:02 +03:00
}
void BackEnd::reset(){
2019-11-10 14:11:50 +03:00
2019-11-11 23:36:18 +03:00
_settings->setValue(FIRST_RUN_KEY, true);
_settings->setValue(LVL_KEY, 1);
_settings->setValue(ANIMATION_KEY, true);
_settings->setValue(RANDOM_COLOR_KEY, 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) {
2019-11-11 23:36:18 +03:00
// TO-DO - find solution of input data from pointers list
2019-11-10 21:46:27 +03:00
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-11 23:36:18 +03:00
auto profile = addProfile(DEFAULT_USER, false);
2019-11-10 22:14:27 +03:00
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-11 23:36:18 +03:00
ProfileData* BackEnd::addProfile(const QString &userName, bool isOnlineuser) {
auto profile = _profileList.value(userName, nullptr);
if (profile) {
return profile;
}
profile = new ProfileData();
connect(profile, &ProfileData::onlineRequest,
this, &BackEnd::handleOnlineRequest);
profile->setOnline(isOnlineuser);
_profileList[userName] = profile;
emit profileListChanged();
return profile;
}
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
2019-11-11 23:36:18 +03:00
void BackEnd::handleOnlineRequest() {
// not supported
assert(false);
}
2018-07-03 17:45:02 +03:00
bool BackEnd::randomColor() const {
2019-11-11 23:36:18 +03:00
return _settings->getValue(RANDOM_COLOR_KEY, false).toBool();
2018-07-03 17:45:02 +03:00
}
void BackEnd::setRandomColor(bool random) {
2019-11-11 23:36:18 +03:00
_settings->setValue(RANDOM_COLOR_KEY, random);
2019-11-10 14:11:50 +03:00
emit randomColorChanged();
2018-07-03 17:45:02 +03:00
}
bool BackEnd::animation() const{
2019-11-11 23:36:18 +03:00
return _settings->getValue(ANIMATION_KEY, true).toBool();
2018-07-03 17:45:02 +03:00
}
void BackEnd::setAnimation(bool name) {
2019-11-11 23:36:18 +03:00
_settings->setValue(ANIMATION_KEY, name);
2019-11-10 14:11:50 +03:00
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-11 23:36:18 +03:00
return _settings->getValue(FIRST_RUN_KEY, true).toBool();
2018-04-11 22:13:34 +03:00
}
2019-11-10 14:11:50 +03:00
void BackEnd::setShowHelp(bool state) {
2019-11-11 23:36:18 +03:00
_settings->setValue(FIRST_RUN_KEY, 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
}
2019-11-11 23:36:18 +03:00
QStringList BackEnd::profileList() {
2019-11-10 21:46:27 +03:00
return _profileList.keys();
2019-11-10 14:11:50 +03:00
}
2019-11-11 23:36:18 +03:00
void BackEnd::createProfile(const QString &userName, bool isOnlineuser) {
addProfile(userName, isOnlineuser);
}
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
}
2019-11-11 23:36:18 +03:00
bool BackEnd::isOnline(const QString &name) {
auto profile = _profileList.value(name, nullptr);
if (!profile) {
return false;
}
return profile->isOnline();
}
int BackEnd::record(const QString &name) {
auto profile = _profileList.value(name, nullptr);
if (!profile) {
return 0;
}
return profile->record();
}