From ca13d58b0b103b1df23cb1835cee3043e01925e5 Mon Sep 17 00:00:00 2001 From: howard-stearns Date: Thu, 10 Aug 2017 12:44:50 -0700 Subject: [PATCH] blance and inventory stubs --- interface/src/commerce/Ledger.cpp | 17 +++++++++++++++++ interface/src/commerce/Ledger.h | 8 ++++++++ interface/src/commerce/QmlCommerce.cpp | 19 +++++++++++++++++-- interface/src/commerce/QmlCommerce.h | 5 +++++ scripts/system/marketplaces/marketplaces.js | 2 +- 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/interface/src/commerce/Ledger.cpp b/interface/src/commerce/Ledger.cpp index 0d9d780743..41dd42ea22 100644 --- a/interface/src/commerce/Ledger.cpp +++ b/interface/src/commerce/Ledger.cpp @@ -33,6 +33,11 @@ bool Ledger::buy(const QString& hfc_key, int cost, const QString& asset_id, cons request["signature"] = signature; qCInfo(commerce) << "Transaction:" << QJsonDocument(request).toJson(QJsonDocument::Compact); + // FIXME: talk to server instead + QStringList keySet{ hfc_key }; + if (initializedBalance() < cost) return false; + _balance -= cost; + _inventory.push_back(asset_id); return true; // FIXME send to server. } @@ -45,4 +50,16 @@ bool Ledger::receiveAt(const QString& hfc_key) { auto username = accountManager->getAccountInfo().getUsername(); qCInfo(commerce) << "Setting default receiving key for" << username; return true; // FIXME send to server. +} + +int Ledger::balance(const QStringList& keys) { + // FIXME: talk to server instead + qCInfo(commerce) << "Balance:" << initializedBalance(); + return _balance; +} + +QStringList Ledger::inventory(const QStringList& keys) { + // FIXME: talk to server instead + qCInfo(commerce) << "Inventory:" << _inventory; + return _inventory; } \ No newline at end of file diff --git a/interface/src/commerce/Ledger.h b/interface/src/commerce/Ledger.h index 0b7e8465f0..092af13d75 100644 --- a/interface/src/commerce/Ledger.h +++ b/interface/src/commerce/Ledger.h @@ -23,6 +23,14 @@ class Ledger : public QObject, public Dependency { public: bool buy(const QString& hfc_key, int cost, const QString& asset_id, const QString& inventory_key, const QString& buyerUsername = ""); bool receiveAt(const QString& hfc_key); + int balance(const QStringList& keys); + QStringList inventory(const QStringList& keys); + +private: + // These in-memory caches is temporary, until we start sending things to the server. + int _balance{ -1 }; + QStringList _inventory{}; + int initializedBalance() { if (_balance < 0) _balance = 100; return _balance; } }; #endif // hifi_Ledger_h diff --git a/interface/src/commerce/QmlCommerce.cpp b/interface/src/commerce/QmlCommerce.cpp index 0350ece15c..92ac2ab787 100644 --- a/interface/src/commerce/QmlCommerce.cpp +++ b/interface/src/commerce/QmlCommerce.cpp @@ -17,7 +17,7 @@ HIFI_QML_DEF(QmlCommerce) -bool QmlCommerce::buy(const QString& assetId, int cost, const QString& buyerUsername) { +void QmlCommerce::buy(const QString& assetId, int cost, const QString& buyerUsername) { auto ledger = DependencyManager::get(); auto wallet = DependencyManager::get(); QStringList keys = wallet->listPublicKeys(); @@ -26,5 +26,20 @@ bool QmlCommerce::buy(const QString& assetId, int cost, const QString& buyerUser } QString key = keys[0]; // For now, we receive at the same key that pays for it. - return ledger->buy(key, cost, assetId, key, buyerUsername); + bool success = ledger->buy(key, cost, assetId, key, buyerUsername); + // FIXME: until we start talking to server, report post-transaction balance and inventory so we can see log for testing. + balance(); + inventory(); + return success; } + +int QmlCommerce::balance() { + auto ledger = DependencyManager::get(); + auto wallet = DependencyManager::get(); + return ledger->balance(wallet->listPublicKeys()); +} +QStringList QmlCommerce::inventory() { + auto ledger = DependencyManager::get(); + auto wallet = DependencyManager::get(); + return ledger->inventory(wallet->listPublicKeys()); + } \ No newline at end of file diff --git a/interface/src/commerce/QmlCommerce.h b/interface/src/commerce/QmlCommerce.h index 3bb72a935c..fd695b2d1c 100644 --- a/interface/src/commerce/QmlCommerce.h +++ b/interface/src/commerce/QmlCommerce.h @@ -21,8 +21,13 @@ class QmlCommerce : public OffscreenQmlDialog { Q_OBJECT HIFI_QML_DECL +signals: + void buyResult(const QString& failureMessage); + protected: Q_INVOKABLE bool buy(const QString& assetId, int cost, const QString& buyerUsername = ""); + Q_INVOKABLE int balance(); + Q_INVOKABLE QStringList inventory(); }; #endif // hifi_QmlCommerce_h diff --git a/scripts/system/marketplaces/marketplaces.js b/scripts/system/marketplaces/marketplaces.js index 4dd7438ac2..97ed271009 100644 --- a/scripts/system/marketplaces/marketplaces.js +++ b/scripts/system/marketplaces/marketplaces.js @@ -131,8 +131,8 @@ } else { var parsedJsonMessage = JSON.parse(message); if (parsedJsonMessage.type === "CHECKOUT") { - tablet.sendToQml({ method: 'updateCheckoutQML', params: parsedJsonMessage }); tablet.pushOntoStack(MARKETPLACE_CHECKOUT_QML_PATH); + tablet.sendToQml({ method: 'updateCheckoutQML', params: parsedJsonMessage }); } else if (parsedJsonMessage.type === "REQUEST_SETTING") { tablet.emitScriptEvent(JSON.stringify({ type: "marketplaces",