Hanoi-Towers/HanoiTowers/client/hanoitowers.cpp

627 lines
18 KiB
C++
Raw Normal View History

2020-05-23 02:31:12 +03:00
/*
* Copyright (C) 2018-2021 QuasarApp.
2020-05-23 02:31:12 +03:00
* 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.
*/
2021-05-04 12:36:43 +03:00
#include "hanoitowers.h"
2018-04-11 20:27:22 +03:00
#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"
#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>
#include <QQmlContext>
2021-01-21 21:38:37 +03:00
#include <QBuffer>
2021-02-19 18:59:21 +03:00
#include <QCoreApplication>
2021-03-02 19:34:53 +03:00
#include <worldupdate.h>
2021-01-18 23:20:37 +03:00
#include "dataconverter.h"
2021-01-15 19:09:07 +03:00
#include "localuser.h"
2021-03-15 18:04:09 +03:00
#include "recordsproxymodel.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"
#define FOG "fog"
#define FOG_ANIMATION "fogAnimation"
2021-05-19 19:10:33 +03:00
#define SHOW_CREDITS "showCredits"
2021-06-01 20:30:43 +03:00
#define LAUNCH_COUNT "launchCount"
2019-11-11 23:36:18 +03:00
2021-05-04 12:36:43 +03:00
HanoiTowers::HanoiTowers(QQmlApplicationEngine *engine):
2021-01-15 19:09:07 +03:00
QObject(),
_profile()
2018-01-20 17:44:28 +03:00
{
2021-04-26 23:57:04 +03:00
_settings = QuasarAppUtils::Settings::instance();
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);
2021-01-17 14:11:10 +03:00
_createNewOfflineUser = new LoginView::LVMainModel("createUser", this);
2021-03-02 19:34:53 +03:00
_recordsTable = new RecordListModel(this);
2021-05-05 11:00:44 +03:00
_recordsTableProxy = new RecordsProxyModel(this);
2021-03-02 19:34:53 +03:00
_world = new RecordListModel(this);
2021-03-15 18:04:09 +03:00
_worldProxy = new RecordsProxyModel(this);
_worldProxy->setSourceModel(_world);
_worldProxy->setDynamicSortFilter(true);
_worldProxy->setSortRole(RecordListModel::RecordListModelRoles::Record);
_worldProxy->sort(0, Qt::SortOrder::DescendingOrder);
2021-03-02 19:34:53 +03:00
2021-05-05 11:00:44 +03:00
_recordsTableProxy->setSourceModel(_recordsTable);
_recordsTableProxy->setDynamicSortFilter(true);
_recordsTableProxy->setSortRole(RecordListModel::RecordListModelRoles::Record);
_recordsTableProxy->sort(0, Qt::SortOrder::DescendingOrder);
2021-03-14 11:51:21 +03:00
_imageProvider = new HanoiImageProvider(_client->getUsersCache());
2021-01-18 23:20:37 +03:00
_dataConverter = new DataConverter;
2021-01-15 19:09:07 +03:00
2021-01-17 20:32:35 +03:00
_loginModel->setComponents(LoginView::Nickname |
LoginView::Title |
LoginView::Password |
2021-02-16 19:16:34 +03:00
LoginView::SigupPage |
2021-01-17 20:32:35 +03:00
LoginView::LoginPage |
LoginView::TermOfUse);
2021-02-16 19:16:34 +03:00
_loginModel->setCurrentPage(LoginView::ViewComponents::LoginPage);
2020-05-24 02:24:49 +03:00
_loginModel->init(engine);
2021-01-17 14:11:10 +03:00
2021-01-17 20:32:35 +03:00
_createNewOfflineUser->setComponents(LoginView::Nickname |
2021-02-16 19:16:34 +03:00
LoginView::SigupPage |
2021-01-17 20:32:35 +03:00
LoginView::Title);
2021-05-05 13:04:55 +03:00
_createNewOfflineUser->setAcceptButtonText(tr("Create"));
2021-01-17 14:11:10 +03:00
_createNewOfflineUser->init(engine);
2020-05-24 02:24:49 +03:00
2021-01-15 19:09:07 +03:00
_recordsTable->setSource(_client->localUsersPreview());
2020-10-31 19:07:28 +03:00
engine->addImageProvider("HanoiImages", _imageProvider);
2021-01-18 23:20:37 +03:00
2021-03-11 17:47:44 +03:00
connect(_loginModel, &LoginView::LVMainModel::sigLoginRequest,
2021-05-04 12:36:43 +03:00
this, &HanoiTowers::handleOnlineRequest);
2020-05-27 01:02:05 +03:00
2021-03-11 17:47:44 +03:00
connect(_loginModel, &LoginView::LVMainModel::sigRegisterRequest,
2021-05-04 12:36:43 +03:00
this, &HanoiTowers::handleOnlineRegisterRequest);
2020-05-27 01:02:05 +03:00
2021-01-17 14:11:10 +03:00
connect(_createNewOfflineUser , &LoginView::LVMainModel::sigRegisterRequest,
2021-05-04 12:36:43 +03:00
this, &HanoiTowers::handleCreateNewProfile);
2021-01-17 14:11:10 +03:00
2021-03-11 17:47:44 +03:00
connect(_client, &HanoiClient::requestError,
2021-05-04 12:36:43 +03:00
this, &HanoiTowers::handleOnlineRequestError);
2020-05-27 01:02:05 +03:00
2021-03-11 17:47:44 +03:00
connect(_client, &HanoiClient::userDataChanged,
2021-05-04 12:36:43 +03:00
this, &HanoiTowers::handleAcceptUserData);
2020-11-02 22:00:29 +03:00
2021-03-11 17:47:44 +03:00
connect(_client, &HanoiClient::statusChanged,
2021-05-04 12:36:43 +03:00
this, &HanoiTowers::setOnlineStatus);
2021-02-12 18:02:18 +03:00
2021-03-02 19:34:53 +03:00
connect(_client, &HanoiClient::worldChanged,
2021-05-04 12:36:43 +03:00
this, &HanoiTowers::handleWorldChanged);
2021-03-02 19:34:53 +03:00
connect(_client, &HanoiClient::worldInited,
2021-05-04 12:36:43 +03:00
this, &HanoiTowers::handleWorldInited);
2021-03-02 19:34:53 +03:00
2021-03-11 12:48:04 +03:00
connect(_client, &HanoiClient::sigBestuserIdChanged,
2021-05-04 12:36:43 +03:00
this, &HanoiTowers::handleBestUserIdChanged);
2021-03-11 12:48:04 +03:00
2021-01-15 19:09:07 +03:00
connect(&_profile, &LocalUser::nameChanged,
2021-05-04 12:36:43 +03:00
this, &HanoiTowers::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
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::init() {
2021-05-13 10:37:51 +03:00
_settingsData.animation = _settings->getValue(ANIMATION_KEY, true).toBool();
_settingsData.randomColor = _settings->getValue(RANDOM_COLOR_KEY, false).toBool();
_settingsData.fog = _settings->getValue(FOG, true).toBool();
_settingsData.fogAnimation = _settings->getValue(FOG_ANIMATION, true).toBool();
2021-05-19 19:10:33 +03:00
_settingsData.showCredits = _settings->getValue(SHOW_CREDITS, true).toBool();
2021-06-01 20:30:43 +03:00
_settingsData._launchCount = _settings->getValue(LAUNCH_COUNT, 0).toUInt();
setLaunchCount(_settingsData._launchCount + 1);
2021-05-13 10:37:51 +03:00
}
void HanoiTowers::loadOldSaves() {
2019-11-10 21:46:27 +03:00
QFile f(MAIN_SETINGS_FILE);
2021-05-15 19:13:35 +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
setAnimation(_animation);
setRandomColor(_randomColor);
setShowHelp(isFirstStart);
2021-05-04 14:11:52 +03:00
if (lvl < 99)
gameState()->saveLvl(lvl);
2020-11-01 23:59:37 +03:00
2019-11-12 20:13:09 +03:00
f.close();
2021-05-18 11:40:24 +03:00
QFile::remove(MAIN_SETINGS_FILE);
2021-05-15 19:13:35 +03:00
updateProfile();
2019-11-10 21:46:27 +03:00
}
2018-04-11 20:27:22 +03:00
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::onlineRequest(const QString &userId) {
2021-01-22 17:23:19 +03:00
2021-03-26 17:20:41 +03:00
if (_client->isLogined()) {
return;
}
2021-01-22 17:23:19 +03:00
if (_profile.token().isValid() && _client->login(userId)) {
return;
}
2019-11-29 18:02:31 +03:00
2020-05-27 01:02:05 +03:00
LoginView::UserData data;
2021-01-22 17:23:19 +03:00
data.setNickname(userId);
2020-05-27 01:02:05 +03:00
_loginModel->setData(data);
2019-11-29 18:02:31 +03:00
2021-02-16 19:16:34 +03:00
if (!_client->isConnected() && !_client->connectToServer()) {
2021-02-09 21:21:30 +03:00
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Connect error"),
tr("Failed to connect to server"
" please check network connection befor login"), "",
QmlNotificationService::NotificationData::Error);
return;
}
2020-05-27 01:02:05 +03:00
emit showOnlinePage();
}
2019-11-29 18:02:31 +03:00
2021-05-04 12:36:43 +03:00
void HanoiTowers::updateProfile() {
if (!_profile.isValid())
return;
2021-03-26 17:20:41 +03:00
if (!_client->updateProfile(_profile)) {
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Update Profile error"),
tr("Failed to update yuo user data"
" please check network connection befor update prifile"), "",
QmlNotificationService::NotificationData::Error);
} else {
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Update Profile"),
tr("User data updated successful"), "",
QmlNotificationService::NotificationData::Normal);
}
}
2021-05-04 12:36:43 +03:00
int HanoiTowers::onlineStatus() const {
return static_cast<int>(_onlineStatus);
}
2021-05-05 11:30:40 +03:00
QString HanoiTowers::onlineStatusColor() const {
switch (_onlineStatus) {
case OnlineStatus::Connected:
case OnlineStatus::Connecting: {
return "#ffaa7f";
}
case OnlineStatus::Loginning:
case OnlineStatus::Logined: {
return "#00ff7f";
}
default: return "#787878";
}
}
2021-05-04 12:36:43 +03:00
QObject *HanoiTowers::bestUser() {
2021-03-07 20:38:38 +03:00
return &_bestUser;
}
2021-05-04 12:36:43 +03:00
QObject *HanoiTowers::selectedUser() {
2021-03-07 20:38:38 +03:00
return &_selectedUser;
2021-03-03 19:20:13 +03:00
}
2021-05-07 22:36:47 +03:00
bool HanoiTowers::isAndroid() const{
#ifdef Q_OS_ANDROID
return true;
#else
return false;
#endif
}
2021-05-11 11:30:39 +03:00
bool HanoiTowers::isAdMod() const {
#ifdef HANOI_ADMOD
return true;
#else
return false;
#endif
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::handleChangeName(const QString &) {
2021-03-15 16:58:03 +03:00
emit profileChanged();
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::handleBestUserIdChanged(const QString & userId) {
2021-03-11 12:48:04 +03:00
_bestUser.setId(userId);
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::handleCreateNewProfile(const LoginView::UserData & data) {
2021-01-21 21:38:37 +03:00
createProfile(data.nickname(), data.nickname());
2021-01-17 14:11:10 +03:00
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::handleOnlineRequest(const LoginView::UserData & user) {
2020-05-27 01:02:05 +03:00
2021-01-22 17:23:19 +03:00
if (!_client->login(user.nickname(), user.rawPassword())) {
QmlNotificationService::NotificationService::getService()->setNotify(
tr("login error"),
tr("Failed to login into online account,"
" please check your password and username"), "",
QmlNotificationService::NotificationData::Error);
}
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::handleOnlineRegisterRequest(const LoginView::UserData &user) {
2021-02-07 20:42:56 +03:00
if (!_client->signup(user.nickname(), user.rawPassword())) {
2020-10-30 20:45:31 +03:00
QmlNotificationService::NotificationService::getService()->setNotify(
2020-11-01 23:59:37 +03:00
tr("Register online error"),
2021-01-22 17:23:19 +03:00
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-20 17:55:13 +03:00
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::handleOnlineRequestError(QH::ErrorCodes::Code code, const QString & err) {
2021-02-16 19:16:34 +03:00
auto errMessage = [](const QString &err) {
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Server error"),
err, "",
QmlNotificationService::NotificationData::Error);
};
if (code == QH::ErrorCodes::UserNotExits) {
_loginModel->setCurrentPage(LoginView::ViewComponents::SigupPage);
errMessage(tr("User with this id is not registered. If it you then use please the sigup form."));
emit showOnlinePage();
return;
}
if (code == QH::ErrorCodes::UserExits) {
_loginModel->setCurrentPage(LoginView::ViewComponents::LoginPage);
errMessage(tr("User with this id already registered. If it you then use please the login form."));
emit showOnlinePage();
return;
}
2021-01-18 23:20:37 +03:00
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Server error"),
err, "",
QmlNotificationService::NotificationData::Error);
2020-05-24 02:24:49 +03:00
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::handleAcceptUserData(QSharedPointer<LocalUser> data) {
2021-03-07 20:38:38 +03:00
if (_profile.getId() == data->getId()) {
_profile.copyFrom(data.data());
_settings->setValue(CURRENT_PROFILE_KEY, _profile.getId());
2021-05-13 10:37:51 +03:00
loadOldSaves();
2021-03-15 16:58:03 +03:00
emit profileChanged();
2021-03-07 20:38:38 +03:00
2021-03-11 12:48:04 +03:00
}
if (_bestUser.getId() == data->getId()) {
2021-03-07 20:38:38 +03:00
_bestUser.copyFrom(data.data());
emit bestUserChanged(&_bestUser);
2021-01-15 19:09:07 +03:00
2021-03-11 12:48:04 +03:00
}
if (_selectedUser.getId() == data->getId()) {
2021-03-07 20:38:38 +03:00
_selectedUser.copyFrom(data.data());
emit selectedUserChanged(&_selectedUser);
}
2019-11-11 23:36:18 +03:00
}
2021-05-04 12:36:43 +03:00
bool HanoiTowers::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
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::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
}
2021-05-04 12:36:43 +03:00
bool HanoiTowers::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
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::setAnimation(bool value) {
2020-11-03 12:19:01 +03:00
if (_settingsData.animation != value) {
_settings->setValue(ANIMATION_KEY, value);
_settingsData.animation = value;
emit animationChanged();
}
2018-07-03 17:45:02 +03:00
}
2021-05-04 12:36:43 +03:00
unsigned short HanoiTowers::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
}
2021-05-04 12:36:43 +03:00
bool HanoiTowers::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
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::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
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::setNewAvatar(QString pathToAvatar) {
if (pathToAvatar.contains("file://")) {
pathToAvatar = pathToAvatar.right(pathToAvatar.size() - 7);
}
2021-01-21 21:38:37 +03:00
QImage img(pathToAvatar);
int maxSize = std::max(img.size().width(), img.size().height());
bool widthIsLarge = maxSize == img.size().width();
2021-02-19 18:59:21 +03:00
if (maxSize > 200) {
2021-01-21 21:38:37 +03:00
if (widthIsLarge) {
2021-02-19 18:59:21 +03:00
img = img.scaledToWidth(200,
2021-01-21 21:38:37 +03:00
Qt::TransformationMode::SmoothTransformation);
} else {
2021-02-19 18:59:21 +03:00
img = img.scaledToHeight(200,
2021-01-21 21:38:37 +03:00
Qt::TransformationMode::SmoothTransformation);
}
}
2021-01-21 21:38:37 +03:00
QByteArray arr;
QBuffer buf(&arr);
buf.open(QIODevice::WriteOnly);
img.save(&buf, "PNG");
buf.close();
2021-03-14 11:51:21 +03:00
_client->setNewAvatar(_profile.getId().toString(), arr);
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::selectUserFromWorldTable(const QString &userId) {
2021-03-07 20:38:38 +03:00
_selectedUser.setId(userId);
_client->getUserData(userId);
}
2021-05-04 12:36:43 +03:00
bool HanoiTowers::fog() const {
return _settings->getValue(FOG, true).toBool();
}
2021-05-04 12:36:43 +03:00
bool HanoiTowers::fogAnimation() const {
return _settings->getValue(FOG_ANIMATION, true).toBool();
}
2021-05-04 12:36:43 +03:00
HanoiTowers::~HanoiTowers() {
2021-05-04 14:11:52 +03:00
updateProfile();
2021-02-19 18:59:21 +03:00
QCoreApplication::processEvents();
2021-01-15 19:09:07 +03:00
_imageProvider->stop();
_client->softDelete();
2021-01-18 23:20:37 +03:00
2021-05-04 14:11:52 +03:00
2021-01-18 23:20:37 +03:00
delete _dataConverter;
2018-01-20 17:44:28 +03:00
}
2019-09-17 15:40:48 +03:00
2021-05-04 12:36:43 +03:00
QString HanoiTowers::profile() const {
2021-01-15 19:09:07 +03:00
return _profile.name();
2019-11-10 14:11:50 +03:00
}
2021-05-04 12:36:43 +03:00
QObject* HanoiTowers::profileList() {
2021-05-05 11:00:44 +03:00
return _recordsTableProxy;
2019-11-10 14:11:50 +03:00
}
2021-05-04 12:36:43 +03:00
QObject *HanoiTowers::worldList() {
2021-03-15 18:04:09 +03:00
return _worldProxy;
2021-03-02 19:34:53 +03:00
}
2021-05-04 12:36:43 +03:00
bool HanoiTowers::createProfile(const QString& userId, const QString &userName) {
2020-11-01 23:59:37 +03:00
2021-01-21 21:38:37 +03:00
LocalUser user;
user.setName(userName);
user.setUserId(userId);
if (!_client->addProfile(user)) {
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Create user error"),
tr("Failed to create a new user, The name %0 alredy used.").arg(userId), "",
QmlNotificationService::NotificationData::Error);
return false;
}
2021-03-02 19:34:53 +03:00
_recordsTable->updateAddSourceItem(_dataConverter->toUserPreview(user));
2021-01-21 21:38:37 +03:00
2021-01-15 19:09:07 +03:00
return true;
2019-11-11 23:36:18 +03:00
}
2021-05-04 12:36:43 +03:00
QObject *HanoiTowers::profileObject() {
2021-01-15 19:09:07 +03:00
return &_profile;
2020-05-24 02:24:49 +03:00
}
2021-05-04 12:36:43 +03:00
GameState *HanoiTowers::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;
}
2021-05-04 12:36:43 +03:00
QObject *HanoiTowers::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-05-04 12:36:43 +03:00
void HanoiTowers::removeUser(const QString &userId) {
2021-05-05 11:00:44 +03:00
if (_client->isLogined() && !_client->removeUser()) {
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
}
2021-01-18 23:20:37 +03:00
2021-05-05 11:00:44 +03:00
if (!_client->removeLocalUser(userId)) {
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Remove Local user"), tr("Failed to remove the local user data"), "",
QmlNotificationService::NotificationData::Warning);
return;
}
2021-01-18 23:20:37 +03:00
_recordsTable->removeSourceItem(userId);
2021-05-05 11:00:44 +03:00
auto userID = _recordsTable->data(_recordsTable->index(0,0), RecordListModel::UserId).toString();
2021-01-21 21:38:37 +03:00
if (userID.isEmpty()) {
userID = DEFAULT_USER_ID;
}
setProfile(userID);
2019-11-20 17:55:13 +03:00
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::setProfile(QString userId) {
2021-01-15 19:09:07 +03:00
if (!_client)
return;
2020-05-27 01:02:05 +03:00
2021-03-26 17:20:41 +03:00
updateProfile();
2021-01-18 23:20:37 +03:00
2021-03-07 20:38:38 +03:00
_profile.setId(userId);
2021-02-08 21:41:44 +03:00
if (_client->setProfile(userId)) {
2021-03-15 16:58:03 +03:00
emit profileChanged();
2021-02-08 21:41:44 +03:00
2021-02-16 19:16:34 +03:00
} else {
createProfile(userId, userId);
2021-02-08 21:41:44 +03:00
_client->setProfile(userId);
2020-10-29 20:25:28 +03:00
}
}
2019-11-20 18:12:18 +03:00
2021-05-04 12:36:43 +03:00
void HanoiTowers::setReward(int revard) {
2019-11-20 18:12:18 +03:00
2021-01-15 19:09:07 +03:00
if (_profile.record() < revard) {
_profile.setRecord(revard);
2021-03-26 17:20:41 +03:00
updateProfile();
2021-03-02 19:34:53 +03:00
_recordsTable->updateAddSourceItem(_dataConverter->toUserPreview(_profile));
2021-01-18 23:20:37 +03:00
2019-11-20 18:12:18 +03:00
}
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::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);
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::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
2021-05-04 12:36:43 +03:00
void HanoiTowers::setGameState(GameState *gameState) {
2021-01-17 12:12:39 +03:00
if (_profile.gameState() == gameState)
return;
_profile.setGameState(*gameState);
2021-03-15 16:58:03 +03:00
emit profileChanged();
2021-01-17 12:12:39 +03:00
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::setOnlineStatus(QH::ClientStatus onlineStatus) {
if (_onlineStatus == static_cast<OnlineStatus>(onlineStatus))
return;
2021-02-25 18:13:37 +03:00
if (_profile.isOnline() && onlineStatus == QH::ClientStatus::Connected) {
2021-03-01 17:56:22 +03:00
if (!_client->login(DataConverter::toUserMember(_profile))) {
2021-05-04 12:36:43 +03:00
QuasarAppUtils::Params::log("Failed to login.", QuasarAppUtils::Error);
2021-03-01 17:56:22 +03:00
}
}
if (_profile.isOnline() && onlineStatus == QH::ClientStatus::Logined) {
2021-05-04 12:36:43 +03:00
if (!_client->getUserData(_profile.getId().toString())) {
QuasarAppUtils::Params::log("Failed to send a user data request.", QuasarAppUtils::Error);
}
2021-03-01 17:56:22 +03:00
if (!_client->subscribeToWorld()) {
2021-05-04 12:36:43 +03:00
QuasarAppUtils::Params::log("Failed to subscribe to world.", QuasarAppUtils::Error);
2021-03-01 17:56:22 +03:00
}
2021-02-25 18:13:37 +03:00
}
_onlineStatus = static_cast<OnlineStatus>(onlineStatus);
emit onlineStatusChanged(static_cast<int>(_onlineStatus));
}
2021-03-02 19:34:53 +03:00
2021-05-04 12:36:43 +03:00
void HanoiTowers::handleWorldChanged(QSharedPointer<WorldUpdate> delta) {
2021-03-02 19:34:53 +03:00
for (const auto &val: qAsConst(delta->getDataAddUpdate())) {
_world->updateAddSourceItem(val);
}
for (const auto &val: qAsConst(delta->getDataRemove())) {
2021-03-11 17:47:44 +03:00
_world->removeSourceItem(val);
2021-03-02 19:34:53 +03:00
}
}
2021-05-04 12:36:43 +03:00
void HanoiTowers::handleWorldInited(QHash<QString, UserPreview> initWorldList) {
2021-03-02 19:34:53 +03:00
_world->setSource({initWorldList.begin(), initWorldList.end()});
}
2021-05-19 19:10:33 +03:00
bool HanoiTowers::showCredits() const {
return _settingsData.showCredits;
}
void HanoiTowers::setShowCredits(bool newShowCredits) {
if (_settingsData.showCredits != newShowCredits) {
_settings->setValue(SHOW_CREDITS, newShowCredits);
_settingsData.showCredits = newShowCredits;
emit showCreditsChanged();
}
}
2021-06-01 20:30:43 +03:00
unsigned int HanoiTowers::launchCount() const {
return _settingsData._launchCount;
}
void HanoiTowers::setLaunchCount(unsigned int newLaunchCount) {
if (_settingsData._launchCount == newLaunchCount)
return;
_settingsData._launchCount = newLaunchCount;
_settings->setValue(LAUNCH_COUNT, newLaunchCount);
emit launchCountChanged();
}