Merge pull request #11186 from zfox23/commerce_jsonInventory

Commerce: Use new JSON inventory format
This commit is contained in:
Zach Fox 2017-08-14 11:59:01 -07:00 committed by GitHub
commit a5fc47fc4d
5 changed files with 28 additions and 10 deletions

View file

@ -59,7 +59,7 @@ Rectangle {
console.log("Failed to get inventory", failureMessage); console.log("Failed to get inventory", failureMessage);
} else { } else {
inventoryReceived = true; inventoryReceived = true;
if (inventory.indexOf(itemId) !== -1) { if (inventoryContains(inventory.assets, itemId)) {
alreadyOwned = true; alreadyOwned = true;
} else { } else {
alreadyOwned = false; alreadyOwned = false;
@ -434,6 +434,15 @@ Rectangle {
} }
signal sendToScript(var message); signal sendToScript(var message);
function inventoryContains(inventoryJson, id) {
for (var idx = 0; idx < inventoryJson.length; idx++) {
if(inventoryJson[idx].id === id) {
return true;
}
}
return false;
}
// //
// FUNCTION DEFINITIONS END // FUNCTION DEFINITIONS END
// //

View file

@ -40,7 +40,7 @@ Rectangle {
if (failureMessage.length) { if (failureMessage.length) {
console.log("Failed to get inventory", failureMessage); console.log("Failed to get inventory", failureMessage);
} else { } else {
inventoryContentsList.model = inventory; inventoryContentsList.model = inventory.assets;
} }
} }
} }
@ -181,7 +181,7 @@ Rectangle {
size: 20; size: 20;
// Style // Style
color: hifi.colors.blueAccent; color: hifi.colors.blueAccent;
text: modelData; text: modelData.title;
// Alignment // Alignment
horizontalAlignment: Text.AlignHLeft; horizontalAlignment: Text.AlignHLeft;
} }
@ -189,7 +189,7 @@ Rectangle {
anchors.fill: parent; anchors.fill: parent;
hoverEnabled: enabled; hoverEnabled: enabled;
onClicked: { onClicked: {
sendToScript({method: 'inventory_itemClicked', itemId: thisItemId.text}); sendToScript({method: 'inventory_itemClicked', itemId: modelData.id});
} }
onEntered: { onEntered: {
thisItemId.color = hifi.colors.blueHighlight; thisItemId.color = hifi.colors.blueHighlight;

View file

@ -44,7 +44,11 @@ void Ledger::buy(const QString& hfc_key, int cost, const QString& asset_id, cons
return emit buyResult("Insufficient funds."); return emit buyResult("Insufficient funds.");
} }
_balance -= cost; _balance -= cost;
_inventory.push_back(asset_id); QJsonObject inventoryAdditionObject;
inventoryAdditionObject["id"] = asset_id;
inventoryAdditionObject["title"] = "Test Title";
inventoryAdditionObject["preview"] = "https://www.aspca.org/sites/default/files/cat-care_cat-nutrition-tips_overweight_body4_left.jpg";
_inventory.push_back(inventoryAdditionObject);
emit buyResult(""); emit buyResult("");
} }
@ -69,6 +73,9 @@ void Ledger::balance(const QStringList& keys) {
void Ledger::inventory(const QStringList& keys) { void Ledger::inventory(const QStringList& keys) {
// FIXME: talk to server instead // FIXME: talk to server instead
qCInfo(commerce) << "Inventory:" << _inventory; QJsonObject inventoryObject;
emit inventoryResult(_inventory, ""); inventoryObject.insert("success", true);
inventoryObject.insert("assets", _inventory);
qCInfo(commerce) << "Inventory:" << inventoryObject;
emit inventoryResult(inventoryObject, "");
} }

View file

@ -15,6 +15,8 @@
#define hifi_Ledger_h #define hifi_Ledger_h
#include <DependencyManager.h> #include <DependencyManager.h>
#include <qjsonobject.h>
#include <qjsonarray.h>
class Ledger : public QObject, public Dependency { class Ledger : public QObject, public Dependency {
Q_OBJECT Q_OBJECT
@ -30,12 +32,12 @@ signals:
void buyResult(const QString& failureReason); void buyResult(const QString& failureReason);
void receiveAtResult(const QString& failureReason); void receiveAtResult(const QString& failureReason);
void balanceResult(int balance, const QString& failureReason); void balanceResult(int balance, const QString& failureReason);
void inventoryResult(QStringList inventory, const QString& failureReason); void inventoryResult(QJsonObject inventory, const QString& failureReason);
private: private:
// These in-memory caches is temporary, until we start sending things to the server. // These in-memory caches is temporary, until we start sending things to the server.
int _balance{ -1 }; int _balance{ -1 };
QStringList _inventory{}; QJsonArray _inventory{};
int initializedBalance() { if (_balance < 0) _balance = 100; return _balance; } int initializedBalance() { if (_balance < 0) _balance = 100; return _balance; }
}; };

View file

@ -29,7 +29,7 @@ signals:
// Balance and Inventory are NOT properties, because QML can't change them (without risk of failure), and // Balance and Inventory are NOT properties, because QML can't change them (without risk of failure), and
// because we can't scalably know of out-of-band changes (e.g., another machine interacting with the block chain). // because we can't scalably know of out-of-band changes (e.g., another machine interacting with the block chain).
void balanceResult(int balance, const QString& failureMessage); void balanceResult(int balance, const QString& failureMessage);
void inventoryResult(QStringList inventory, const QString& failureMessage); void inventoryResult(QJsonObject inventory, const QString& failureMessage);
protected: protected:
Q_INVOKABLE void buy(const QString& assetId, int cost, const QString& buyerUsername = ""); Q_INVOKABLE void buy(const QString& assetId, int cost, const QString& buyerUsername = "");