From 23242ca2ee2c22f4701da54dbcced8ef81f34b14 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sun, 8 Aug 2021 16:52:58 +0300 Subject: [PATCH] added base implementation of the store class --- src/Core/Crawl/iitem.h | 6 +++++ src/Core/private/store.cpp | 46 +++++++++++++++++++++++++++++++++ src/Core/private/store.h | 52 ++++++++++++++++++++++++++++++++++++++ src/Core/private/user.cpp | 4 ++- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/Core/private/store.cpp create mode 100644 src/Core/private/store.h diff --git a/src/Core/Crawl/iitem.h b/src/Core/Crawl/iitem.h index 6bfaf27..c5473a3 100644 --- a/src/Core/Crawl/iitem.h +++ b/src/Core/Crawl/iitem.h @@ -55,6 +55,12 @@ public: */ virtual int cost() const = 0; + /** + * @brief requiredTier This method return requried level to unlock. + * @return requried level to unlock. + */ + virtual int requiredTier() const = 0; + /** * @brief itemId This method return hash of the IItem::itemTextId. * @return hash of the IItem::itemTextId. diff --git a/src/Core/private/store.cpp b/src/Core/private/store.cpp new file mode 100644 index 0000000..75ebb4f --- /dev/null +++ b/src/Core/private/store.cpp @@ -0,0 +1,46 @@ +//# +//# Copyright (C) 2021-2021 QuasarApp. +//# Distributed under the GPLv3 software license, see the accompanying +//# Everyone is permitted to copy and distribute verbatim copies +//# of this license document, but changing it is not allowed. +//# + + +#include "store.h" + +#include +namespace CRAWL { + +Store::Store() +{ + +} + +bool Store::buy(User &buyer, int itemId) { + + auto item = getItemById(itemId); + + if (buyer.getTier() < item->requiredTier()) { + return false; + } + + if (buyer.getMoney() < item->cost()) { + return false; + } + + buyer.setMoney(buyer.getMoney() - item->cost()); + buyer.unclokItem(itemId); + + return true; +} + +bool Store::init(const QHash &availabelItems) { + _store = availabelItems; + + return true; +} + +const IItem *Store::getItemById(int id) const { + return _store.value(id, nullptr); +} +} diff --git a/src/Core/private/store.h b/src/Core/private/store.h new file mode 100644 index 0000000..087cad2 --- /dev/null +++ b/src/Core/private/store.h @@ -0,0 +1,52 @@ +//# +//# Copyright (C) 2021-2021 QuasarApp. +//# Distributed under the GPLv3 software license, see the accompanying +//# Everyone is permitted to copy and distribute verbatim copies +//# of this license document, but changing it is not allowed. +//# + + +#ifndef STORE_H +#define STORE_H + +#include "user.h" + +namespace CRAWL { + +class IItem; + +/** + * @brief The Store class contains method for control all game items beetwin users. + */ +class Store +{ +public: + Store(); + + /** + * @brief buy This method unlock the item with @a itemId for @a buyer. The + * @param buyer This is user that will be buy item with @a itemId id. + * @param itemId This is id of the item that @a buyer will be buy + * @return true if the deal gas been completed successfully + */ + bool buy(User& buyer, int itemId); + + /** + * @brief init This method initialise store of the game. + * @param availabelItems This is hash of the available item. + * @return true if the items inited successfuly else false. + */ + bool init(const QHash& availabelItems); + + /** + * @brief getItemById This method return item by id. + * @param id This is id of the required item. + * @return pointer to item. if The item with @a id not found then return nullptr. + */ + const IItem* getItemById(int id) const; + +private: + QHash _store; +}; +} +#endif // STORE_H diff --git a/src/Core/private/user.cpp b/src/Core/private/user.cpp index 54f4f32..2c939ed 100644 --- a/src/Core/private/user.cpp +++ b/src/Core/private/user.cpp @@ -11,19 +11,21 @@ namespace CRAWL { constexpr float tierMul = 1.2; constexpr int firstTierCount = 10; -// Tiers table +/// @private template struct Tiers { enum { value = static_cast(tierMul * Tiers::value)}; }; +/// @private template <> struct Tiers<0> { enum { value = 0 }; }; +/// @private template <> struct Tiers<1> {