4
1
mirror of https://github.com/QuasarApp/Snake.git synced 2025-05-04 13:39:43 +00:00

added base implementation of the store class

This commit is contained in:
Andrei Yankovich 2021-08-08 16:52:58 +03:00
parent 4a7fe20989
commit 23242ca2ee
4 changed files with 107 additions and 1 deletions

@ -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.

@ -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 <Crawl/iitem.h>
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<int, const IItem *> &availabelItems) {
_store = availabelItems;
return true;
}
const IItem *Store::getItemById(int id) const {
return _store.value(id, nullptr);
}
}

52
src/Core/private/store.h Normal file

@ -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<int, const IItem*>& 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<int, const IItem*> _store;
};
}
#endif // STORE_H

@ -11,19 +11,21 @@ namespace CRAWL {
constexpr float tierMul = 1.2;
constexpr int firstTierCount = 10;
// Tiers table
/// @private
template <int N>
struct Tiers
{
enum { value = static_cast<const int>(tierMul * Tiers<N - 1>::value)};
};
/// @private
template <>
struct Tiers<0>
{
enum { value = 0 };
};
/// @private
template <>
struct Tiers<1>
{