Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
itemmarketentry.h
Go to the documentation of this file.
1/*
2 * Copyright 2020 Twilight
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15*/
16#pragma once
17#include <QObject>
18#include <QHash>
19#include <QVector>
20#include <QVariant>
21
22class PlayerBasics;
23
41class ItemMarketEntry : public QObject
42{
43 Q_OBJECT
44
45 // These change
46 Q_PROPERTY(int onCart READ getCartCount WRITE setCartCount NOTIFY onCartChanged)
47 Q_PROPERTY(bool canCheckout READ canCheckout NOTIFY onCartChanged)
48 Q_PROPERTY(int cartWorth READ cartWorth NOTIFY onCartChanged)
49 Q_PROPERTY(int stackCount READ stackCount NOTIFY onCartChanged)
50 Q_PROPERTY(int onCartLeft READ onCartLeft NOTIFY onCartChanged)
51 Q_PROPERTY(int totalStackCount READ totalStackCount NOTIFY onCartChanged)
52
53 // These do not change, they depend on the mode but if the mode changes they
54 // will be re-created anyways. during the lifetime of the class, they will not
55 // change.
56 // However a signal is in place to force a re-update should it be needed
57 Q_PROPERTY(QString name READ name NOTIFY doReUpdateConstants)
58 Q_PROPERTY(int inStockCount READ inStockCount NOTIFY doReUpdateConstants)
59 Q_PROPERTY(bool canSell READ canSell NOTIFY doReUpdateConstants)
60 Q_PROPERTY(int itemWorth READ itemWorth NOTIFY doReUpdateConstants)
61 Q_PROPERTY(QString whichType READ whichType NOTIFY doReUpdateConstants)
62
63signals:
64 // Notified when cart is changed
65 void onCartChanged();
66
67 // Emitted externally when data that is normally not re-read should be re-read
69
70public:
72 enum {
73 // Don't change these or re-assign different values or ordering
77 };
78
80 enum {
86 };
87
91
92 virtual ~ItemMarketEntry();
93
94 // Called only for the first instance
95 virtual void initOnce();
96
97 void finishConstruction();
98
99 // Virtual Table for child classes
100
101 // Usually never change
102
103 // Name of item
104 QString name();
105 virtual QString _name() = 0;
106
107 // Amount of item to sell (When in sell mode)
108 int inStockCount();
109 virtual int _inStockCount() = 0;
110
111 // If the item can be sold (When in sell mode)
112 bool canSell();
113 virtual bool _canSell() = 0;
114
115 // Value of item individually to buy or sell
116 int itemWorth();
117 virtual int _itemWorth() = 0;
118
119 // Type of this item
120 QString whichType();
121 virtual QString _whichType() = 0;
122
123 // Detailed-tooltip text for this row (what it is / how it's obtained). Empty by
124 // default (messages, money); item rows override to return their DB entry's info.
125 virtual QString infoText() { return QString(); }
126
127 // Stack count of this item
128 // This goes for all types, anything that takes up more than one stack
129 // Pokemon are a stack of 1, Items are a stack of 99, Money and others don't
130 // take up any stack (they are a stack of infinity)
131 // This number is for counting new stack space that will be filled, not
132 // consuming existing stack space.
133 virtual int stackCount() = 0;
134
135 // Left that can be placed on the cart
136 virtual int onCartLeft() = 0;
137
138 // Tools for the child class to leverage
139 bool requestFilter();
140
141 // Members that wrap around properties
142 int getCartCount();
143
144 // Value of item total on cart to buy or sell
145 int cartWorth();
146
147 // Returns total stack count for all instances in this type
148 int totalStackCount();
149 int totalWorth();
150
151 // Calculate how much money is leftover
152 int moneyLeftover();
153
154 // Can checkout notifies if the checkout can be completed.
155 virtual bool canCheckout();
156 bool canAnyCheckout();
157
158public slots:
159 virtual void checkout() = 0;
160 void setCartCount(int val);
161 void reUpdateConstants();
162
163public:
164 // Compatibility of this item, set by child classes
167
168 // Properties
169 int onCart = 0;
170
171 // Excude from total calculations
172 // Will not be included in most. if not all, calculations such as total items
173 // on cart and total worth amongst others.
174 bool exclude = false;
175
176 // Which left-list view this row belongs to in the unified buy+sell list:
177 // ViewBuy / ViewSell (see ItemMarketModel), or -1 for "not view-filtered"
178 // (e.g. the exchange list, which is shown wholesale). Set during the build.
179 int viewTag = -1;
180
181 // Sign this row contributes to the cart's single-currency net: +1 for a sell
182 // (money in), -1 for a buy (money out). A PLAIN member (not a virtual) on
183 // purpose: totalWorth() sweeps the static `instancesCombined` registry, which can
184 // briefly hold torn-down entries across model rebuilds -- a virtual call there
185 // would deref a freed vtable and crash (a plain member read does not). Buys set
186 // this to -1 in their constructor. Money/exchange rows are excluded from totals.
187 int cartSignVal = 1;
188
189 // References to the model class, these are static because the model class
190 // is a singleton
191 static bool* isMoneyCurrency;
192 static bool* isBuyMode;
194
195 // Holds global instances categorized by type
196 static QHash<QString, QVector<ItemMarketEntry*>*> instances;
197
198 // Holds all instances combined regardless of type
199 static QVector<ItemMarketEntry*> instancesCombined;
200
201 // The CURRENT model's row list (its itemListCache), set by ItemMarketModel on every
202 // build. Aggregates (totalWorth / canAnyCheckout / totalStackCount) iterate THIS, not
203 // the global registries above -- those accumulate rows across every model/app and, on
204 // a per-test app teardown, can hand back a freed entry (use-after-free). Restricting
205 // the sweep to the live current list keeps the totals correct AND memory-safe.
206 static QVector<ItemMarketEntry*>* activeList;
207
208 // Cached data
209 // Many data are very expensive to re-create, we cache them here and re-create
210 // them only when needed
211 QHash<int, QVariant> cache;
212};
virtual QString _name()=0
Subtype: compute the display name.
ItemMarketEntry(int compatMoneyCurrency=CompatEither, int compatBuyMode=CompatEither)
int totalStackCount()
Stacks across all rows of this type.
int cartSignVal
Net contribution sign (-1 buy / +1 sell).
int moneyLeftover()
Money remaining if this checks out.
static QVector< ItemMarketEntry * > * activeList
Current model's live rows.
void reUpdateConstants()
Clear the cached mode-stable values.
bool requestFilter()
Helper: does this row pass the current mode filter?
QString name()
Cached display name.
virtual void checkout()=0
Subtype: apply this row's transaction.
virtual QString _whichType()=0
Subtype: report the type label.
static QHash< QString, QVector< ItemMarketEntry * > * > instances
All rows, grouped by type.
int compatBuyMode
Buy/sell compatibility.
QString whichType()
Cached type label.
static bool * isMoneyCurrency
Shared: current currency mode.
int itemWorth()
Cached unit value.
virtual int stackCount()=0
Subtype: new stack slots needed (see note above).
static PlayerBasics * player
Shared: player money/coins.
QHash< int, QVariant > cache
Memoised mode-stable values (see HashKey* enum).
void setCartCount(int val)
Set the cart quantity (backs onCart).
int totalWorth()
Signed worth across ALL rows (sell +, buy -).
int viewTag
Left-list view filter tag.
void finishConstruction()
Finalise construction (register the instance).
int inStockCount()
Cached owned/sellable count.
virtual int _itemWorth()=0
Subtype: compute the unit value.
virtual int _inStockCount()=0
Subtype: compute the owned/sellable count.
static QVector< ItemMarketEntry * > instancesCombined
All rows, flat.
virtual ~ItemMarketEntry()
virtual bool _canSell()=0
Subtype: compute sellability.
void doReUpdateConstants()
Force a refresh of the "mode-stable" values.
int onCart
Backing cart quantity.
int compatMoneyCurrency
Money/coins compatibility.
virtual void initOnce()
One-time setup for the first instance of a type.
bool canAnyCheckout()
Can any row check out?
bool exclude
Exclude from aggregate totals (see note).
virtual QString infoText()
Detailed-tooltip body (default none).
int cartWorth()
Value of the cart quantity.
static bool * isBuyMode
Shared: current buy/sell mode.
bool canSell()
Cached sellable flag.
int getCartCount()
Current cart quantity (backs onCart).
virtual int onCartLeft()=0
Subtype: how many more may be added.
virtual bool canCheckout()
Can this row alone check out?
The trainer's headline values: name, ID, money, coins, badges, starter.