381 lines
9.5 KiB
C++
Raw Normal View History

2020-05-23 02:31:12 +03:00
/*
* Copyright (C) 2018-2020 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
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-20 17:55:13 +03:00
#include <qmlnotifyservice.h>
2019-11-10 21:46:27 +03:00
#include "gamestate.h"
2020-05-24 02:24:49 +03:00
#include <QQmlApplicationEngine>
#include <lvmainmodel.h>
2020-10-31 19:07:28 +03:00
#include <recordlistmodel.h>
2019-11-10 21:46:27 +03:00
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 FIRST_RUN_KEY "isFirstStart"
#define LVL_KEY "lvl"
#define ANIMATION_KEY "animation"
#define RANDOM_COLOR_KEY "randomColor"
#define CURRENT_PROFILE_KEY "currentProfile"
2020-05-24 02:24:49 +03:00
BackEnd::BackEnd(QQmlApplicationEngine *engine):
2018-01-20 17:44:28 +03:00
QObject()
{
2019-11-10 14:11:50 +03:00
_settings = QuasarAppUtils::Settings::get();
2019-11-20 17:55:13 +03:00
connect(this, &BackEnd::profileChanged, [this](){
_settings->setValue(CURRENT_PROFILE_KEY, profile());
});
2019-11-12 20:13:09 +03:00
init();
2019-11-20 17:55:13 +03:00
setProfile(_settings->getStrValue(CURRENT_PROFILE_KEY, DEFAULT_USER));
2020-10-29 20:25:28 +03:00
connect(&_client, &HanoiClient::statusChanged,
this, &BackEnd::handleLogined);
2020-05-24 02:24:49 +03:00
2020-10-31 19:07:28 +03:00
_loginModel = new LoginView::LVMainModel("userLogin", this);
2020-05-27 01:02:05 +03:00
_loginModel->setComponents(LoginView::Nickname);
2020-05-24 02:24:49 +03:00
_loginModel->init(engine);
2020-10-31 19:07:28 +03:00
_recordsTable = new RecordListModel(this);
2020-05-27 01:02:05 +03:00
connect(_loginModel , &LoginView::LVMainModel::sigLoginRequest,
this, &BackEnd::handleOnlineRequest);
connect(_loginModel , &LoginView::LVMainModel::sigRegisterRequest,
this, &BackEnd::handleOnlineRequest);
2020-10-29 20:25:28 +03:00
connect(&_client , &HanoiClient::requestError,
this, &BackEnd::handleOnlineRequestError);
2020-05-27 01:02:05 +03:00
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();
2019-11-20 17:55:13 +03:00
setProfile(addProfile(DEFAULT_USER, false)->name());
2019-11-10 21:46:27 +03:00
}
2019-10-07 15:20:07 +03:00
2019-11-12 20:13:09 +03:00
void BackEnd::init() {
2019-11-10 21:46:27 +03:00
QFile f(MAIN_SETINGS_FILE);
2019-11-12 20:13:09 +03:00
if(f.open(QIODevice::ReadOnly)){
2019-11-10 21:46:27 +03:00
QDataStream stream(&f);
unsigned char dataVersion;
stream >> dataVersion;
2019-11-18 17:13:18 +03:00
if (dataVersion == currentVersion) {
2019-11-11 23:36:18 +03:00
// TO-DO - find solution of input data from pointers list
2019-11-18 17:13:18 +03:00
int size;
stream >> size;
if (size * 10 > f.size()) {
reset();
return;
}
for (int i = 0; i < size; ++i ) {
QString key;
stream >> key;
2019-11-20 17:55:13 +03:00
auto obj = addProfile(key, false);
2019-11-18 17:13:18 +03:00
stream >> *obj;
_profileList[key] = obj;
}
2019-11-10 21:46:27 +03:00
2019-11-12 20:13:09 +03:00
if (_profileList.isEmpty()) {
2019-11-20 17:55:13 +03:00
setProfile(addProfile(DEFAULT_USER, false)->name());
2019-11-18 17:13:18 +03:00
} else if (_profile.isEmpty()) {
2019-11-20 17:55:13 +03:00
setProfile(_profileList.begin().key());
2019-11-12 20:13:09 +03:00
}
2019-11-10 21:46:27 +03:00
} 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);
2019-11-12 20:13:09 +03:00
setShowHelp(isFirstStart);
2019-11-10 21:46:27 +03:00
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));
}
2019-11-12 20:13:09 +03:00
f.close();
2019-11-10 21:46:27 +03:00
2019-11-12 20:13:09 +03:00
} else {
reset();
2019-11-10 21:46:27 +03:00
}
2019-10-07 15:20:07 +03:00
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;
}
2019-11-12 20:13:09 +03:00
profile = new ProfileData(userName);
2019-11-11 23:36:18 +03:00
connect(profile, &ProfileData::onlineRequest,
2020-05-27 01:02:05 +03:00
this, &BackEnd::handleOnlineRequestfromProfile);
2019-11-11 23:36:18 +03:00
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);
2020-05-24 02:24:49 +03:00
QDir().mkpath(MAIN_FOLDER);
2019-11-10 21:46:27 +03:00
if(f.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
QDataStream stream(&f);
stream << currentVersion;
2019-11-18 17:13:18 +03:00
stream << static_cast<int>(_profileList.size());
for (auto it = _profileList.begin(); it != _profileList.end(); ++it ) {
stream << it.key();
stream << *it.value();
}
2019-11-10 21:46:27 +03:00
f.close();
return;
}
2020-04-06 00:26:38 +03:00
QuasarAppUtils::Params::log("local file data not opened on not created1 " + f.fileName(),
2019-11-10 21:46:27 +03:00
QuasarAppUtils::Error);
2018-01-20 17:44:28 +03:00
}
2018-04-11 20:27:22 +03:00
2019-11-20 17:55:13 +03:00
void BackEnd::removeLocalUserData(const QString& name) {
_profileList.remove(name);
if (name == _profile && _profileList.size()) {
setProfile(_profileList.begin().key());
} else if (_profileList.isEmpty()) {
reset();
}
emit profileListChanged();
}
2020-05-27 01:02:05 +03:00
void BackEnd::handleOnlineRequestfromProfile(const QString &name) {
if (name != _profile) {
return;
}
2019-11-29 18:02:31 +03:00
2020-05-27 01:02:05 +03:00
LoginView::UserData data;
data.setNickname(name);
_loginModel->setData(data);
2019-11-29 18:02:31 +03:00
2020-05-27 01:02:05 +03:00
emit showOnlinePage();
}
2019-11-29 18:02:31 +03:00
2020-05-27 01:02:05 +03:00
void BackEnd::handleOnlineRequest(const LoginView::UserData & user) {
2020-10-30 20:45:31 +03:00
if (!_client.login(user.nickname(), user.rawPassword().toLatin1())) {
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Register online error"), tr("Failed to register this account, if this account was created by you, try to restore it."), "",
QmlNotificationService::NotificationData::Error);
}
2019-11-29 18:02:31 +03:00
2019-11-20 17:55:13 +03:00
}
2020-05-27 01:02:05 +03:00
void BackEnd::handleOnlineRequestError(const QString &) {
emit handleOnlineRequestfromProfile(_profile);
2020-05-24 02:24:49 +03:00
}
2020-10-30 20:45:31 +03:00
void BackEnd::handleLogined(unsigned char state) {
2020-05-24 02:24:49 +03:00
2020-10-30 20:45:31 +03:00
if (static_cast<Status>(state) == Status::Logined) {
2020-10-29 20:25:28 +03:00
auto logineduser = _client.currentProfile();
2020-10-30 20:45:31 +03:00
*_profileList[logineduser.name()] = logineduser;
2020-10-29 20:25:28 +03:00
}
2019-11-11 23:36:18 +03:00
}
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
}
2020-10-31 19:07:28 +03:00
QObject* 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);
}
2020-05-24 02:24:49 +03:00
//QObject *BackEnd::usersListModel() const {
// return _usersList;
//}
QObject *BackEnd::profileObject() const {
2019-11-10 21:46:27 +03:00
if (!_profileList.contains(_profile)) {
return nullptr;
}
2019-11-10 14:11:50 +03:00
2020-05-24 02:24:49 +03:00
return _profileList[_profile];
}
QObject* BackEnd::gameState() {
if (auto obj = dynamic_cast<ProfileData*>(profileObject())) {
return obj->gameState();
}
return nullptr;
}
2020-10-29 20:25:28 +03:00
QObject *BackEnd::client() {
return &_client;
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();
}
void BackEnd::removeUser(const QString &name) {
2019-11-20 17:55:13 +03:00
auto profile = _profileList.value(name, nullptr);
if (!profile) {
return;
}
2020-05-27 01:02:05 +03:00
removeLocalUserData(name);
}
void BackEnd::setOnline(const QString &name, bool online) {
2019-11-20 17:55:13 +03:00
auto profile = _profileList.value(name, nullptr);
if (!profile) {
return;
}
profile->setOnline(online);
}
void BackEnd::setProfile(QString profile) {
if (!_profileList.contains(profile) || _profile == profile)
return;
2019-11-20 17:55:13 +03:00
_profile = profile;
2020-05-27 01:02:05 +03:00
auto profileData = dynamic_cast<ProfileData*>(profileObject());
2020-10-29 20:25:28 +03:00
if (profileData->isOnline()) {
2020-10-30 20:45:31 +03:00
if (!_client.login(_profile)) {
2020-10-29 20:25:28 +03:00
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Login failed"),
tr("Failed to login %0, if this account was created by you, try to restore it.").arg(_profile),
"",
QmlNotificationService::NotificationData::Warning);
}
}
2020-05-27 01:02:05 +03:00
2019-11-20 17:55:13 +03:00
emit profileChanged(_profile);
}
2019-11-20 18:12:18 +03:00
void BackEnd::setReward(int revard) {
auto profile = _profileList.value(_profile, nullptr);
if (!profile) {
return;
}
if (profile->record() < revard) {
profile->setRecord(revard);
}
}
2020-05-27 01:02:05 +03:00
2020-10-30 20:45:31 +03:00
void BackEnd::removeOnlineProfile(const QString &user) {
2020-05-27 01:02:05 +03:00
// not supported
2020-10-30 20:45:31 +03:00
if (!_client.removeUser(user)) {
2020-05-27 01:02:05 +03:00
2020-10-30 20:45:31 +03:00
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Remove online error"), tr("current profile not online!"), "",
QmlNotificationService::NotificationData::Warning);
}
2020-05-27 01:02:05 +03:00
}