Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
gamecornerdb.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
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 "./gamecornerdb.h"
31#include "./util/gamedata.h"
32#include "./pokemon.h"
33#include "./itemsdb.h"
35
37{
38 static bool once = false;
39 if(once)
40 return;
41
42 // Grab Item Data
43 auto jsonData = GameData::inst()->json("gameCorner");
44
45 // Go through each item
46 for(QJsonValue jsonEntry : jsonData.array())
47 {
48 if(jsonEntry["options"].isArray()) {
49 for(QJsonValue option : jsonEntry["options"].toArray()) {
50
51 // Create a new item entry
52 auto entry = new GameCornerDBEntry(jsonEntry);
53 entry->level = option["level"].toDouble();
54 entry->price = option["price"].toDouble();
55
56 // Add to array
57 store.append(entry);
58 }
59 }
60 else {
61 auto entry = new GameCornerDBEntry(jsonEntry);
62
63 // Add to array
64 store.append(entry);
65 }
66 }
67
68 once = true;
69}
70
72{
73 static bool once = false;
74 if(once)
75 return;
76
77 for(auto entry : store)
78 {
79 entry->deepLink();
80 }
81
82 once = true;
83}
84
85void GameCornerDB::qmlProtect(const QQmlEngine* const engine) const
86{
87 Utility::qmlProtectUtil(this, engine);
88 for(auto el : store)
89 el->qmlProtect(engine);
90}
91
92void GameCornerDB::qmlRegister() const
93{
94 static bool once = false;
95 if(once)
96 return;
97
98 qmlRegisterUncreatableType<GameCornerDB>(
99 "PSE.DB.GameCornerDB", 1, 0, "GameCornerDB", "Can't instantiate in QML");
100 once = true;
101}
102
103GameCornerDB::GameCornerDB()
104{
105 qmlRegister();
106}
107
108GameCornerDB* GameCornerDB::inst()
109{
110 static GameCornerDB* _inst = new GameCornerDB;
111 return _inst;
112}
113
114const QVector<GameCornerDBEntry*> GameCornerDB::getStore() const
115{
116 return store;
117}
118
120{
121 return store.size();
122}
123
125{
126 if(ind < 0 || ind >= store.size())
127 return nullptr;
128
129 return store.at(ind);
130}
131
133{
134 return buyPrice;
135}
136
138{
139 // Follow the global Poke-World sell-back mechanic: half the buy price back.
140 return buyPrice / 2;
141}
142
void deepLink()
Resolve each prize's item/Pokemon links.
void qmlProtect(const QQmlEngine *const engine) const
Pin to C++ ownership.
GameCornerDBEntry * getStoreAt(const int ind) const
Prize by store index (for QML).
int getStoreSize() const
Prize count.
friend class GameCornerDBEntry
Lets entries populate the store/price during load.
const QVector< GameCornerDBEntry * > getStore() const
All prize entries.
void load()
Load prizes from JSON.
int getBuyPrice() const
The buy rate (backs getBuyPrice).
static GameCornerDB * inst()
< Number of prize entries.
int getSellPrice() const
The sell rate (half the buy rate).
const QJsonDocument json(const QString filename) const
Parsed document for filename.
Definition gamedata.cpp:45
static GameData * inst()
The process-wide GameData singleton.
Definition gamedata.cpp:71
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