4
0
mirror of https://github.com/QuasarApp/Hanoi-Towers.git synced 2025-05-14 18:39:33 +00:00

added base offline functions

This commit is contained in:
Andrei Yankovich 2019-11-20 17:55:13 +03:00
parent ed0154ef44
commit b272942672
7 changed files with 97 additions and 17 deletions

@ -1 +1 @@
Subproject commit 6359c798331eb40455e9f7f7513db9ebd8bebc0e
Subproject commit 08824c8e610526b38b00c3b51d1e3f7bd9b115f1

@ -2,6 +2,7 @@
#include <cmath>
#include <QDataStream>
#include <QDir>
#include <qmlnotifyservice.h>
#include "gamestate.h"
constexpr unsigned char currentVersion = 6;
@ -18,8 +19,14 @@ BackEnd::BackEnd():
QObject()
{
_settings = QuasarAppUtils::Settings::get();
_profile = _settings->getStrValue(CURRENT_PROFILE_KEY, DEFAULT_USER);
connect(this, &BackEnd::profileChanged, [this](){
_settings->setValue(CURRENT_PROFILE_KEY, profile());
});
init();
setProfile(_settings->getStrValue(CURRENT_PROFILE_KEY, DEFAULT_USER));
}
void BackEnd::reset(){
@ -33,9 +40,7 @@ void BackEnd::reset(){
item->deleteLater();
}
_profileList.clear();
_profile = addProfile(DEFAULT_USER, false)->name();
emit profileListChanged();
setProfile(addProfile(DEFAULT_USER, false)->name());
}
@ -60,18 +65,18 @@ void BackEnd::init() {
for (int i = 0; i < size; ++i ) {
QString key;
stream >> key;
auto obj = new ProfileData(key);
auto obj = addProfile(key, false);
stream >> *obj;
_profileList[key] = obj;
}
stream >> _profile;
if (_profileList.isEmpty()) {
_profile = addProfile(DEFAULT_USER, false)->name();
setProfile(addProfile(DEFAULT_USER, false)->name());
} else if (_profile.isEmpty()) {
_profile = _profileList.begin().key();
setProfile(_profileList.begin().key());
}
} else {
@ -137,8 +142,6 @@ void BackEnd::saveLocalData() const {
stream << *it.value();
}
stream << _profile;
f.close();
return;
}
@ -147,9 +150,33 @@ void BackEnd::saveLocalData() const {
QuasarAppUtils::Error);
}
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();
}
void BackEnd::handleOnlineRequest() {
// not supported
assert(false);
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Register online error"), tr("not Supported"), "",
QmlNotificationService::NotificationData::Warning);
// assert(false);
}
void BackEnd::handleRemoveRequest() {
// not supported
QmlNotificationService::NotificationService::getService()->setNotify(
tr("Remove online error"), tr("not Supported"), "",
QmlNotificationService::NotificationData::Warning);
// assert(false);
}
bool BackEnd::randomColor() const {
@ -227,9 +254,32 @@ int BackEnd::record(const QString &name) {
}
void BackEnd::removeUser(const QString &name) {
auto profile = _profileList.value(name, nullptr);
if (!profile) {
return;
}
if (profile->isOnline()) {
handleRemoveRequest();
} else {
removeLocalUserData(name);
}
}
void BackEnd::setOnline(const QString &name, bool online) {
auto profile = _profileList.value(name, nullptr);
if (!profile) {
return;
}
profile->setOnline(online);
}
void BackEnd::setProfile(QString profile) {
if (!_profileList.contains(profile) || _profile == profile)
return;
_profile = profile;
emit profileChanged(_profile);
}

@ -21,13 +21,14 @@ class BackEnd: public QObject
Q_PROPERTY(QObject* gameState READ gameState)
Q_PROPERTY(QStringList profileList READ profileList NOTIFY profileListChanged)
Q_PROPERTY(QString profile READ profile NOTIFY profileChanged)
Q_PROPERTY(QString profile READ profile WRITE setProfile NOTIFY profileChanged)
private:
void init();
ProfileData *addProfile(const QString& userName, bool isOnlineuser);
void saveLocalData() const;
void removeLocalUserData(const QString &name);
QuasarAppUtils::Settings *_settings = nullptr;
@ -37,6 +38,7 @@ private:
private slots:
void handleOnlineRequest();
void handleRemoveRequest();
public:
BackEnd();
@ -105,6 +107,8 @@ public slots:
void removeUser(const QString& name);
void setOnline(const QString& name, bool online);
void setProfile(QString profile);
signals:
void firstChanged();
void animationChanged();

@ -119,6 +119,8 @@ Rectangle {
function load () {
backEnd.gameState.load();
tumbler.spin.maximumValue = backEnd.gameState.getMaxValueOfLoadedSaves();
tower1.clear()
tower2.clear()
tower3.clear()

@ -26,7 +26,7 @@ Page {
Base.BaseText {
Layout.alignment: Qt.AlignCenter
text: qsTr("Hanoi Towers");
text: qsTr("Welcom to Hanoi Towers ") + backEnd.profile;
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pixelSize: theme.headerSize;

@ -13,14 +13,29 @@ import QtQuick.Controls.Material 2.13
import "./../base" as Base
Item {
id: row
property string name: ""
property bool online: false
property string record: "0"
property int recordLength: 0
property bool selected: false
height: source.height
signal removedRow();
signal onlineRowChanged(var online);
signal clicked();
Rectangle {
color: (selected)? "#3700f991" : "#0000f991"
anchors.fill: parent;
Behavior on color {
ColorAnimation {
duration: 500
}
}
}
RowLayout {
id : source
@ -50,6 +65,11 @@ Item {
Layout.fillWidth: true
readOnly: true;
text: name
onFocusChanged: {
if (focus)
row.clicked();
}
}
Text {

@ -78,7 +78,7 @@ Item {
Base.BaseButton {
id: button
text: qsTr("Create new user")
text: qsTr("Create the new user")
onClicked: {
if (backEnd) {
@ -102,13 +102,17 @@ Item {
record: backEnd.record(modelData)
width: listView.width
recordLength: button.width - gridLayout.rowSpacing
selected: modelData == backEnd.profile;
onRemovedRow: {
backEnd.removeUser(modelData)
}
onOnlineRowChanged: {
backEnd.removeUser(modelData)
backEnd.setOnline(modelData, online);
}
onClicked: {
backEnd.setProfile(modelData);
}
}
}