Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
itemmarketentrymoney.cpp
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
28
29
#include <QDebug>
30
31
#include "
./itemmarketentrymoney.h
"
32
#include <
pse-db/gamecornerdb.h
>
33
#include <
pse-savefile/expanded/player/playerbasics.h
>
34
35
// Compatibility is Either/Either: these rows are only ever built into the dedicated
36
// Exchange list now, so they always pass the filter; the swap direction comes from
37
// forceDir, not the global buy/sell flag.
38
ItemMarketEntryMoney::ItemMarketEntryMoney
(
int
forceDir
)
39
:
ItemMarketEntry
(
CompatEither
,
CompatEither
),
40
forceDir
(
forceDir
)
41
{
42
finishConstruction
();
43
exclude
=
true
;
44
}
45
46
ItemMarketEntryMoney::~ItemMarketEntryMoney
() {}
47
48
bool
ItemMarketEntryMoney::buying
()
const
49
{
50
if
(
forceDir
!=
DirGlobal
)
51
return
forceDir
==
DirToCoins
;
52
return
*
isBuyMode
;
53
}
54
55
QString
ItemMarketEntryMoney::_name
()
56
{
57
if
(
buying
())
58
return
tr(
"Money => Coins"
);
59
60
return
tr(
"Coins => Money"
);
61
}
62
63
// Always "selling" something, so it always has an in-stock value: how many of the
64
// SOURCE currency you hold (money when buying coins, coins when selling them).
65
int
ItemMarketEntryMoney::_inStockCount
()
66
{
67
if
(
buying
())
68
return
player
->money;
69
70
return
player
->coins;
71
}
72
73
bool
ItemMarketEntryMoney::_canSell
()
74
{
75
return
true
;
76
}
77
78
// The per-coin rate: cost to buy a coin, or money returned for selling one.
79
int
ItemMarketEntryMoney::_itemWorth
()
80
{
81
if
(
buying
())
82
return
GameCornerDB::inst
()->
getBuyPrice
();
83
84
return
GameCornerDB::inst
()->
getSellPrice
();
85
}
86
87
QString
ItemMarketEntryMoney::_whichType
()
88
{
89
return
type
;
90
}
91
92
int
ItemMarketEntryMoney::moneyDelta
()
const
93
{
94
const
int
rate =
buying
() ?
GameCornerDB::inst
()->
getBuyPrice
()
95
:
GameCornerDB::inst
()->
getSellPrice
();
96
// Buying coins spends money; selling coins gains money.
97
return
(
buying
() ? -rate : rate) *
onCart
;
98
}
99
100
int
ItemMarketEntryMoney::coinsDelta
()
const
101
{
102
// Buying gains coins; selling spends them.
103
return
(
buying
() ? 1 : -1) *
onCart
;
104
}
105
106
// How many MORE coins can be added to the cart, bounded by both currencies' caps and
107
// by what the player can afford / actually owns.
108
int
ItemMarketEntryMoney::onCartLeft
()
109
{
110
const
int
rate =
buying
() ?
GameCornerDB::inst
()->
getBuyPrice
()
111
:
GameCornerDB::inst
()->
getSellPrice
();
112
if
(rate <= 0)
113
return
0;
114
115
int
maxCoins = 0;
116
if
(
buying
()) {
117
const
int
coinCapRoom = 9999 -
player
->coins;
// coins fit under 9,999
118
const
int
affordable =
player
->money / rate;
// coins you can pay for
119
maxCoins = qMin(coinCapRoom, affordable);
120
}
else
{
121
const
int
coinsOwned =
player
->coins;
// coins you actually have
122
const
int
moneyCapRoom = (999999 -
player
->money) / rate;
// money fits under 999,999
123
maxCoins = qMin(coinsOwned, moneyCapRoom);
124
}
125
126
return
maxCoins -
onCart
;
127
}
128
129
int
ItemMarketEntryMoney::stackCount
()
130
{
131
// Applies only to buying items
132
return
0;
133
}
134
135
// Exchange-aware affordability: gate on the currency actually being spent.
136
bool
ItemMarketEntryMoney::canCheckout
()
137
{
138
if
(
onCart
<= 0)
139
return
false
;
140
if
(
onCartLeft
() < 0)
141
return
false
;
142
143
const
int
rate =
GameCornerDB::inst
()->
getBuyPrice
();
144
return
buying
() ? (
player
->money >= rate *
onCart
)
// can pay for the coins
145
: (
player
->coins >=
onCart
);
// owns the coins to sell
146
}
147
148
void
ItemMarketEntryMoney::checkout
()
149
{
150
if
(!
canCheckout
())
151
return
;
152
153
player
->money +=
moneyDelta
();
154
player
->coins +=
coinsDelta
();
155
156
player
->moneyChanged();
157
player
->coinsChanged();
158
159
onCart
= 0;
160
}
GameCornerDB::getBuyPrice
int getBuyPrice() const
The buy rate (backs getBuyPrice).
Definition
gamecornerdb.cpp:132
GameCornerDB::inst
static GameCornerDB * inst()
< Number of prize entries.
Definition
gamecornerdb.cpp:108
GameCornerDB::getSellPrice
int getSellPrice() const
The sell rate (half the buy rate).
Definition
gamecornerdb.cpp:137
ItemMarketEntryMoney::coinsDelta
int coinsDelta() const
Signed coins change for this row's onCart (+buying, -selling).
Definition
itemmarketentrymoney.cpp:100
ItemMarketEntryMoney::~ItemMarketEntryMoney
virtual ~ItemMarketEntryMoney()
Definition
itemmarketentrymoney.cpp:46
ItemMarketEntryMoney::ItemMarketEntryMoney
ItemMarketEntryMoney(int forceDir=DirGlobal)
Definition
itemmarketentrymoney.cpp:38
ItemMarketEntryMoney::buying
bool buying() const
This row's effective direction: true = Money=>Coins (spend money), false = Coins=>Money (spend coins)...
Definition
itemmarketentrymoney.cpp:48
ItemMarketEntryMoney::_canSell
virtual bool _canSell() override
Subtype: compute sellability.
Definition
itemmarketentrymoney.cpp:73
ItemMarketEntryMoney::_whichType
virtual QString _whichType() override
Subtype: report the type label.
Definition
itemmarketentrymoney.cpp:87
ItemMarketEntryMoney::DirGlobal
@ DirGlobal
Follow the model's global buy/sell flag (legacy behaviour).
Definition
itemmarketentrymoney.h:32
ItemMarketEntryMoney::DirToCoins
@ DirToCoins
Money => Coins (the "buy" direction).
Definition
itemmarketentrymoney.h:34
ItemMarketEntryMoney::_inStockCount
virtual int _inStockCount() override
Subtype: compute the owned/sellable count.
Definition
itemmarketentrymoney.cpp:65
ItemMarketEntryMoney::type
static constexpr const char * type
This row's type key.
Definition
itemmarketentrymoney.h:66
ItemMarketEntryMoney::onCartLeft
virtual int onCartLeft() override
Subtype: how many more may be added.
Definition
itemmarketentrymoney.cpp:108
ItemMarketEntryMoney::stackCount
virtual int stackCount() override
Subtype: new stack slots needed (see note above).
Definition
itemmarketentrymoney.cpp:129
ItemMarketEntryMoney::canCheckout
virtual bool canCheckout() override
Exchange-aware affordability gate.
Definition
itemmarketentrymoney.cpp:136
ItemMarketEntryMoney::forceDir
int forceDir
Fixed direction, or DirGlobal to follow the model.
Definition
itemmarketentrymoney.h:68
ItemMarketEntryMoney::_name
virtual QString _name() override
Subtype: compute the display name.
Definition
itemmarketentrymoney.cpp:55
ItemMarketEntryMoney::_itemWorth
virtual int _itemWorth() override
Subtype: compute the unit value.
Definition
itemmarketentrymoney.cpp:79
ItemMarketEntryMoney::moneyDelta
int moneyDelta() const
Signed money change for this row's onCart (-cost buying, +gain selling).
Definition
itemmarketentrymoney.cpp:92
ItemMarketEntryMoney::checkout
virtual void checkout() override
Apply the money/coins change.
Definition
itemmarketentrymoney.cpp:148
ItemMarketEntry::ItemMarketEntry
ItemMarketEntry(int compatMoneyCurrency=CompatEither, int compatBuyMode=CompatEither)
Definition
itemmarketentry.cpp:28
ItemMarketEntry::player
static PlayerBasics * player
Shared: player money/coins.
Definition
itemmarketentry.h:193
ItemMarketEntry::finishConstruction
void finishConstruction()
Finalise construction (register the instance).
Definition
itemmarketentry.cpp:58
ItemMarketEntry::onCart
int onCart
Backing cart quantity.
Definition
itemmarketentry.h:169
ItemMarketEntry::exclude
bool exclude
Exclude from aggregate totals (see note).
Definition
itemmarketentry.h:174
ItemMarketEntry::isBuyMode
static bool * isBuyMode
Shared: current buy/sell mode.
Definition
itemmarketentry.h:192
ItemMarketEntry::CompatEither
@ CompatEither
Definition
itemmarketentry.h:76
gamecornerdb.h
itemmarketentrymoney.h
playerbasics.h
projects
app
src
mvc
itemmarket
itemmarketentrymoney.cpp
Generated by
1.17.0