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-11-08 23:13:05 +03:00
|
|
|
#include "hanoiimageprovider.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>
|
2020-11-08 23:13:05 +03:00
|
|
|
#include <QQmlContext>
|
2021-01-15 19:09:07 +03:00
|
|
|
#include "localuser.h"
|
2019-11-10 21:46:27 +03:00
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
#define DEFAULT_USER_ID "DefaultUser"
|
2020-11-02 22:00:29 +03:00
|
|
|
#define DEFAULT_USER_NAME "User"
|
|
|
|
|
2019-11-11 23:36:18 +03:00
|
|
|
#define FIRST_RUN_KEY "isFirstStart"
|
|
|
|
#define ANIMATION_KEY "animation"
|
|
|
|
#define RANDOM_COLOR_KEY "randomColor"
|
|
|
|
#define CURRENT_PROFILE_KEY "currentProfile"
|
2020-11-03 21:22:13 +03:00
|
|
|
#define FOG "fog"
|
|
|
|
#define FOG_ANIMATION "fogAnimation"
|
2019-11-11 23:36:18 +03:00
|
|
|
|
2020-05-24 02:24:49 +03:00
|
|
|
BackEnd::BackEnd(QQmlApplicationEngine *engine):
|
2021-01-15 19:09:07 +03:00
|
|
|
QObject(),
|
|
|
|
_profile()
|
2018-01-20 17:44:28 +03:00
|
|
|
{
|
2019-11-10 14:11:50 +03:00
|
|
|
_settings = QuasarAppUtils::Settings::get();
|
2019-11-20 17:55:13 +03:00
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
_client = new HanoiClient();
|
|
|
|
_loginModel = new LoginView::LVMainModel("userLogin", this);
|
|
|
|
_recordsTable = new RecordListModel(this);
|
|
|
|
_imageProvider = new HanoiImageProvider(_client);
|
|
|
|
|
2020-05-27 01:02:05 +03:00
|
|
|
_loginModel->setComponents(LoginView::Nickname);
|
2020-05-24 02:24:49 +03:00
|
|
|
_loginModel->init(engine);
|
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
_recordsTable->setSource(_client->localUsersPreview());
|
2020-10-31 19:07:28 +03:00
|
|
|
|
2020-11-08 23:13:05 +03:00
|
|
|
engine->addImageProvider("HanoiImages", _imageProvider);
|
|
|
|
|
2020-05-27 01:02:05 +03:00
|
|
|
connect(_loginModel , &LoginView::LVMainModel::sigLoginRequest,
|
|
|
|
this, &BackEnd::handleOnlineRequest);
|
|
|
|
|
|
|
|
connect(_loginModel , &LoginView::LVMainModel::sigRegisterRequest,
|
|
|
|
this, &BackEnd::handleOnlineRequest);
|
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
connect(_client , &HanoiClient::requestError,
|
2020-10-29 20:25:28 +03:00
|
|
|
this, &BackEnd::handleOnlineRequestError);
|
2020-05-27 01:02:05 +03:00
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
connect(_client , &HanoiClient::profileChanged,
|
|
|
|
this, &BackEnd::handleProfileChanged);
|
2020-11-02 22:00:29 +03:00
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
connect(&_profile, &LocalUser::onlineRequest,
|
2020-11-02 22:00:29 +03:00
|
|
|
this, &BackEnd::handleOnlineRequestfromProfile);
|
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
connect(&_profile, &LocalUser::nameChanged,
|
2020-11-08 23:13:05 +03:00
|
|
|
this, &BackEnd::handleChangeName);
|
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
setProfile(_settings->getStrValue(CURRENT_PROFILE_KEY, DEFAULT_USER_ID));
|
|
|
|
init();
|
2020-11-02 22:00:29 +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(ANIMATION_KEY, true);
|
|
|
|
_settings->setValue(RANDOM_COLOR_KEY, false);
|
2020-11-03 21:22:13 +03:00
|
|
|
_settings->setValue(FOG, true);
|
|
|
|
_settings->setValue(FOG_ANIMATION, true);
|
2019-09-17 15:40:48 +03:00
|
|
|
|
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);
|
|
|
|
|
2020-11-01 23:59:37 +03:00
|
|
|
unsigned short lvl;
|
|
|
|
bool isFirstStart, _animation, _randomColor;
|
|
|
|
stream >> lvl;
|
|
|
|
stream >> isFirstStart;
|
|
|
|
stream >> _animation;
|
|
|
|
stream >> _randomColor;
|
2019-11-10 21:46:27 +03:00
|
|
|
|
2020-11-01 23:59:37 +03:00
|
|
|
if(lvl < 1 || lvl > 99) {
|
|
|
|
lvl = 1;
|
2019-11-10 21:46:27 +03:00
|
|
|
}
|
2020-11-01 23:59:37 +03:00
|
|
|
|
|
|
|
setAnimation(_animation);
|
|
|
|
setRandomColor(_randomColor);
|
|
|
|
setShowHelp(isFirstStart);
|
|
|
|
|
|
|
|
|
2019-11-12 20:13:09 +03:00
|
|
|
f.close();
|
2020-11-02 22:00:29 +03:00
|
|
|
f.remove();
|
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
|
|
|
}
|
|
|
|
|
2020-05-27 01:02:05 +03:00
|
|
|
void BackEnd::handleOnlineRequestfromProfile(const QString &name) {
|
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
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
void BackEnd::handleChangeName(const QString & name) {
|
|
|
|
_client->updateProfile(_profile);
|
|
|
|
emit profileChanged(name);
|
2020-11-08 23:13:05 +03:00
|
|
|
}
|
|
|
|
|
2020-05-27 01:02:05 +03:00
|
|
|
void BackEnd::handleOnlineRequest(const LoginView::UserData & user) {
|
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
if (!_client->login(user.nickname().toLatin1(), user.rawPassword().toLatin1())) {
|
2020-10-30 20:45:31 +03:00
|
|
|
QmlNotificationService::NotificationService::getService()->setNotify(
|
2020-11-01 23:59:37 +03:00
|
|
|
tr("Register online error"),
|
|
|
|
tr("Failed to register this account, if this account was created by you, try to restore it."), "",
|
2020-10-30 20:45:31 +03:00
|
|
|
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 &) {
|
2021-01-15 19:09:07 +03:00
|
|
|
handleOnlineRequestfromProfile("");
|
2020-05-24 02:24:49 +03:00
|
|
|
}
|
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
void BackEnd::handleProfileChanged(QSharedPointer<LocalUser> profileId) {
|
2020-05-24 02:24:49 +03:00
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
_profile.copyFrom(profileId.data());
|
|
|
|
_settings->setValue(CURRENT_PROFILE_KEY, _profile.getId());
|
|
|
|
|
|
|
|
emit profileChanged(_profile.getId().toString());
|
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) {
|
2020-11-03 12:19:01 +03:00
|
|
|
|
|
|
|
if (_settingsData.randomColor != random) {
|
|
|
|
|
|
|
|
_settings->setValue(RANDOM_COLOR_KEY, random);
|
|
|
|
_settingsData.randomColor = random;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-03 12:19:01 +03:00
|
|
|
void BackEnd::setAnimation(bool value) {
|
|
|
|
if (_settingsData.animation != value) {
|
|
|
|
|
|
|
|
_settings->setValue(ANIMATION_KEY, value);
|
|
|
|
_settingsData.animation = value;
|
|
|
|
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-04-11 22:13:34 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 23:13:05 +03:00
|
|
|
void BackEnd::setNewAvatar(const QString &pathToAvatar) {
|
|
|
|
QImage image;
|
|
|
|
if (pathToAvatar.contains("file://")) {
|
|
|
|
image = QImage(pathToAvatar.right(pathToAvatar.size() - 7));
|
|
|
|
} else {
|
|
|
|
image = QImage(pathToAvatar);
|
|
|
|
}
|
2021-01-15 19:09:07 +03:00
|
|
|
_client->setNewAvatar(_profile.getId().toString(), image);
|
2020-11-08 23:13:05 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 21:22:13 +03:00
|
|
|
bool BackEnd::fog() const {
|
|
|
|
return _settings->getValue(FOG, true).toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BackEnd::fogAnimation() const {
|
|
|
|
return _settings->getValue(FOG_ANIMATION, true).toBool();
|
|
|
|
}
|
|
|
|
|
2020-11-01 14:28:13 +03:00
|
|
|
BackEnd::~BackEnd() {
|
2021-01-15 19:09:07 +03:00
|
|
|
_client->updateProfile(_profile);
|
|
|
|
_imageProvider->stop();
|
|
|
|
_client->softDelete();
|
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 {
|
2021-01-15 19:09:07 +03:00
|
|
|
return _profile.name();
|
2019-11-10 14:11:50 +03:00
|
|
|
}
|
|
|
|
|
2020-10-31 19:07:28 +03:00
|
|
|
QObject* BackEnd::profileList() {
|
2020-11-01 14:28:13 +03:00
|
|
|
return _recordsTable;
|
2019-11-10 14:11:50 +03:00
|
|
|
}
|
|
|
|
|
2020-11-01 23:59:37 +03:00
|
|
|
bool BackEnd::createProfile(const QString& userId, const QString &userName) {
|
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
if (!(_client->registerOflineUser(userId, userName))) {
|
2020-11-01 23:59:37 +03:00
|
|
|
return false;
|
2021-01-15 19:09:07 +03:00
|
|
|
}
|
2020-11-01 23:59:37 +03:00
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
return true;
|
2019-11-11 23:36:18 +03:00
|
|
|
}
|
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
QObject *BackEnd::profileObject() {
|
|
|
|
return &_profile;
|
2020-05-24 02:24:49 +03:00
|
|
|
}
|
|
|
|
|
2021-01-17 12:12:39 +03:00
|
|
|
GameState *BackEnd::gameState() {
|
2021-01-15 19:09:07 +03:00
|
|
|
if (auto obj = dynamic_cast<LocalUser*>(profileObject())) {
|
2020-05-24 02:24:49 +03:00
|
|
|
return obj->gameState();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-29 20:25:28 +03:00
|
|
|
QObject *BackEnd::client() {
|
2021-01-15 19:09:07 +03:00
|
|
|
return _client;
|
2019-11-10 14:11:50 +03:00
|
|
|
}
|
2019-11-11 23:36:18 +03:00
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
void BackEnd::removeUser(const QString &userId) {
|
2019-11-19 18:09:30 +03:00
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
if (!_client->removeUser(userId)) {
|
2020-11-01 14:28:13 +03:00
|
|
|
QmlNotificationService::NotificationService::getService()->setNotify(
|
|
|
|
tr("Remove online error"), tr("current profile not online!"), "",
|
|
|
|
QmlNotificationService::NotificationData::Warning);
|
2019-11-20 17:55:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 23:59:37 +03:00
|
|
|
void BackEnd::setProfile(QString userId) {
|
2021-01-15 19:09:07 +03:00
|
|
|
if (!_client)
|
|
|
|
return;
|
2020-05-27 01:02:05 +03:00
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
if ( _client->login(userId)) {
|
2020-11-01 23:59:37 +03:00
|
|
|
emit profileChanged(userId);
|
2021-01-15 19:09:07 +03:00
|
|
|
} else if (userId == DEFAULT_USER_ID) {
|
|
|
|
createProfile(DEFAULT_USER_ID, DEFAULT_USER_NAME);
|
2020-10-29 20:25:28 +03:00
|
|
|
}
|
2019-11-19 18:09:30 +03:00
|
|
|
}
|
2019-11-20 18:12:18 +03:00
|
|
|
|
|
|
|
void BackEnd::setReward(int revard) {
|
|
|
|
|
2021-01-15 19:09:07 +03:00
|
|
|
if (_profile.record() < revard) {
|
|
|
|
_profile.setRecord(revard);
|
|
|
|
_client->updateProfile(_profile);
|
2019-11-20 18:12:18 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-03 21:22:13 +03:00
|
|
|
|
|
|
|
void BackEnd::setFog(bool fog) {
|
|
|
|
if (_settingsData.fog == fog)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_settingsData.fog = fog;
|
|
|
|
_settings->setValue(FOG, fog);
|
|
|
|
|
|
|
|
if (!_settingsData.fog)
|
|
|
|
setFogAnimation(_settingsData.fog);
|
|
|
|
|
|
|
|
emit fogChanged(_settingsData.fog);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackEnd::setFogAnimation(bool fogAnimation) {
|
|
|
|
if (_settingsData.fogAnimation == fogAnimation ||
|
|
|
|
!_settingsData.fog)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_settingsData.fogAnimation = fogAnimation;
|
|
|
|
_settings->setValue(FOG_ANIMATION, fogAnimation);
|
|
|
|
|
|
|
|
emit fogAnimationChanged(_settingsData.fogAnimation);
|
|
|
|
}
|
2021-01-17 12:12:39 +03:00
|
|
|
|
|
|
|
void BackEnd::setGameState(GameState *gameState) {
|
|
|
|
if (_profile.gameState() == gameState)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_profile.setGameState(*gameState);
|
|
|
|
emit gameStateChanged(gameState);
|
|
|
|
}
|