Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Toggle main menu visibility
Loading...
Searching...
No Matches
trades.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2019 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
21
22
#include <QJsonArray>
23
#include <QQmlEngine>
24
#include <
pse-common/utility.h
>
25
26
#ifdef QT_DEBUG
27
#include <QtDebug>
28
#endif
29
30
#include "
./trades.h
"
31
#include "
./util/gamedata.h
"
32
#include "
./pokemon.h
"
33
34
TradeDBEntry::TradeDBEntry
() {}
35
TradeDBEntry::TradeDBEntry
(QJsonValue& data)
36
{
37
give
= data[
"give"
].toString();
38
get
= data[
"get"
].toString();
39
textId
=
static_cast<
var8
>
(data[
"textId"
].toDouble());
40
nickname
= data[
"nickname"
].toString();
41
if
(data[
"unused"
].isBool())
unused
= data[
"unused"
].toBool();
42
}
43
44
void
TradeDBEntry::deepLink
()
45
{
46
toGive
=
PokemonDB::inst
()->
getIndAt
(
give
);
47
toGet
=
PokemonDB::inst
()->
getIndAt
(
get
);
48
#ifdef QT_DEBUG
49
if
(!
toGive
) qCritical() <<
"Trade give:"
<<
give
<<
"could not be deep linked."
;
50
if
(!
toGet
) qCritical() <<
"Trade get:"
<<
get
<<
"could not be deep linked."
;
51
#endif
52
if
(
toGive
)
toGive
->toTrades.append(
this
);
53
if
(
toGet
)
toGet
->toTrades.append(
this
);
54
}
55
56
TradesDB*
TradesDB::inst
()
57
{
58
static
TradesDB* _inst =
new
TradesDB;
59
return
_inst;
60
}
61
62
const
QVector<TradeDBEntry*>
TradesDB::getStore
()
const
{
return
store; }
63
int
TradesDB::getStoreSize
()
const
{
return
store.size(); }
64
65
TradeDBEntry
*
TradesDB::getStoreAt
(
int
idx)
const
66
{
67
if
(idx < 0 || idx >= store.size())
return
nullptr
;
68
return
store.at(idx);
69
}
70
71
void
TradesDB::load
()
72
{
73
static
bool
once =
false
;
74
if
(once)
return
;
75
auto
jsonData =
GameData::inst
()->
json
(
"trades"
);
76
for
(QJsonValue entry : jsonData.array())
77
store.append(
new
TradeDBEntry
(entry));
78
once =
true
;
79
}
80
81
void
TradesDB::deepLink
()
82
{
83
static
bool
once =
false
;
84
if
(once)
return
;
85
for
(
auto
* entry : store)
86
entry->deepLink();
87
once =
true
;
88
}
89
90
void
TradesDB::qmlProtect
(
const
QQmlEngine*
const
engine)
const
91
{
92
Utility::qmlProtectUtil
(
this
, engine);
93
}
94
95
void
TradesDB::qmlRegister()
const
96
{
97
static
bool
once =
false
;
98
if
(once)
return
;
99
qmlRegisterUncreatableType<TradesDB>(
"PSE.DB.TradesDB"
, 1, 0,
"TradesDB"
,
"Can't instantiate in QML"
);
100
once =
true
;
101
}
102
103
TradesDB::TradesDB()
104
{
105
qmlRegister();
106
}
GameData::json
const QJsonDocument json(const QString filename) const
Parsed document for filename.
Definition
gamedata.cpp:45
GameData::inst
static GameData * inst()
The process-wide GameData singleton.
Definition
gamedata.cpp:71
PokemonDB::inst
static PokemonDB * inst()
< Number of species.
Definition
pokemon.cpp:183
PokemonDB::getIndAt
PokemonDBEntry * getIndAt(const QString &key) const
Species by name key (for QML).
Definition
pokemon.cpp:199
TradesDB::deepLink
void deepLink()
Resolve each trade's species links.
Definition
trades.cpp:81
TradesDB::inst
static TradesDB * inst()
< Number of trades.
Definition
trades.cpp:56
TradesDB::getStoreSize
int getStoreSize() const
Trade count.
Definition
trades.cpp:63
TradesDB::qmlProtect
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
Definition
trades.cpp:90
TradesDB::load
void load()
Load trades from JSON.
Definition
trades.cpp:71
TradesDB::getStoreAt
TradeDBEntry * getStoreAt(int idx) const
Trade by store index (for QML).
Definition
trades.cpp:65
TradesDB::getStore
const QVector< TradeDBEntry * > getStore() const
All trades.
Definition
trades.cpp:62
Utility::qmlProtectUtil
static void qmlProtectUtil(const QObject *const obj, const QQmlEngine *const engine)
Pin obj to C++ ownership so the QML engine never garbage-collects it.
Definition
utility.cpp:63
var8
var8e var8
Everyday 8-bit alias. Exact (not "fastest") to dodge the pointer-width bug noted above.
Definition
types.h:124
gamedata.h
pokemon.h
TradeDBEntry
One in-game (NPC) trade definition: what you give and get.
Definition
trades.h:37
TradeDBEntry::toGet
PokemonDBEntry * toGet
Resolved species you get (deepLink).
Definition
trades.h:49
TradeDBEntry::deepLink
void deepLink()
Resolve the give/get species links.
Definition
trades.cpp:44
TradeDBEntry::textId
var8 textId
Trade dialogue text id.
Definition
trades.h:44
TradeDBEntry::TradeDBEntry
TradeDBEntry()
Empty entry.
Definition
trades.cpp:34
TradeDBEntry::unused
bool unused
Whether this trade slot is unused.
Definition
trades.h:46
TradeDBEntry::nickname
QString nickname
Nickname the received mon comes with.
Definition
trades.h:45
TradeDBEntry::give
QString give
Species name you give (resolved to toGive).
Definition
trades.h:42
TradeDBEntry::get
QString get
Species name you get (resolved to toGet).
Definition
trades.h:43
TradeDBEntry::toGive
PokemonDBEntry * toGive
Resolved species you give (deepLink).
Definition
trades.h:48
trades.h
utility.h
projects
db
src
pse-db
trades.cpp
Generated by
1.17.0