Skip to content

Commit 389056c

Browse files
author
Gin
committed
refactor home sorter
1 parent 66b6459 commit 389056c

File tree

9 files changed

+176
-131
lines changed

9 files changed

+176
-131
lines changed

SerialPrograms/Source/Pokemon/Options/Pokemon_BoxSortingTable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace PokemonAutomation{
1010
namespace Pokemon{
1111

1212

13-
const EnumDropdownDatabase<SortingRuleType>& BallType_Database(){
13+
const EnumDropdownDatabase<SortingRuleType>& SortingRuleType_Database(){
1414
static const EnumDropdownDatabase<SortingRuleType> database({
15-
{SortingRuleType::NationalDexNo, "dex", "National Dex Number"},
15+
{SortingRuleType::DexNo, "dex", "Dex Number"},
1616
{SortingRuleType::Shiny, "shiny", "Shiny"},
1717
{SortingRuleType::Gigantamax, "gigantamax", "Gigantamax"},
1818
{SortingRuleType::Alpha, "alpha", "Alpha"},
@@ -26,7 +26,7 @@ const EnumDropdownDatabase<SortingRuleType>& BallType_Database(){
2626

2727
BoxSortingRow::BoxSortingRow(EditableTableOption& parent_table)
2828
: EditableTableRow(parent_table)
29-
, sort_type(BallType_Database(), LockMode::LOCK_WHILE_RUNNING, SortingRuleType::NationalDexNo)
29+
, sort_type(SortingRuleType_Database(), LockMode::LOCK_WHILE_RUNNING, SortingRuleType::DexNo)
3030
, reverse(LockMode::LOCK_WHILE_RUNNING, false)
3131
{
3232
PA_ADD_OPTION(sort_type);

SerialPrograms/Source/Pokemon/Options/Pokemon_BoxSortingTable.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Pokemon{
1717

1818
enum class SortingRuleType
1919
{
20-
NationalDexNo,
20+
DexNo,
2121
Shiny,
2222
Gigantamax,
2323
Alpha,
@@ -27,7 +27,7 @@ enum class SortingRuleType
2727

2828
struct SortingRule
2929
{
30-
SortingRuleType sort_type = SortingRuleType::NationalDexNo;
30+
SortingRuleType sort_type = SortingRuleType::DexNo;
3131
bool reverse = false;
3232
};
3333

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Box Cursor
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "Pokemon_BoxCursor.h"
8+
9+
namespace PokemonAutomation{
10+
namespace Pokemon{
11+
12+
13+
const size_t BOX_ROWS = 5;
14+
const size_t BOX_COLS = 6;
15+
16+
17+
std::ostream& operator<<(std::ostream& os, const BoxCursor& cursor){
18+
os << "(" << cursor.box << "/" << cursor.row << "/" << cursor.column << ")";
19+
return os;
20+
}
21+
22+
BoxCursor::BoxCursor(size_t index){
23+
column = index % BOX_COLS;
24+
index = index / BOX_COLS;
25+
row = index % BOX_ROWS;
26+
box = index / BOX_ROWS;
27+
}
28+
29+
size_t to_global_index(size_t box, size_t row, size_t column){
30+
return box * BOX_ROWS * BOX_COLS + row * BOX_COLS + column;
31+
}
32+
33+
34+
}
35+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Box Cursor
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_Pokemon_BoxCursor_H
8+
#define PokemonAutomation_Pokemon_BoxCursor_H
9+
10+
#include <cstddef>
11+
#include <ostream>
12+
13+
namespace PokemonAutomation{
14+
namespace Pokemon{
15+
16+
17+
// Constants for box dimensions
18+
extern const size_t BOX_ROWS;
19+
extern const size_t BOX_COLS;
20+
21+
22+
// Represents a cursor position within Pokemon storage boxes
23+
struct BoxCursor{
24+
// Convert a global index to a BoxCursor position
25+
BoxCursor(size_t index);
26+
BoxCursor(size_t box, size_t row, size_t column) : box(box), row(row), column(column) {}
27+
BoxCursor() {}
28+
29+
size_t box = 0;
30+
size_t row = 0;
31+
size_t column = 0;
32+
};
33+
34+
// Output operator for BoxCursor
35+
std::ostream& operator<<(std::ostream& os, const BoxCursor& cursor);
36+
37+
// Convert a box cursor coordinate to a global index
38+
size_t to_global_index(size_t box, size_t row, size_t column);
39+
40+
41+
}
42+
}
43+
#endif

SerialPrograms/Source/Pokemon/Pokemon_CollectedPokemonInfo.cpp

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
*/
66

77
#include "Common/Cpp/Exceptions.h"
8+
#include "Common/Cpp/Json/JsonValue.h"
9+
#include "Common/Cpp/Json/JsonArray.h"
10+
#include "Common/Cpp/Json/JsonObject.h"
811
#include "Pokemon/Pokemon_Strings.h"
12+
#include "Pokemon/Pokemon_BoxCursor.h"
913
#include "Pokemon/Resources/Pokemon_PokemonNames.h"
1014
#include "Pokemon/Resources/Pokemon_PokemonSlugs.h"
1115
#include "Pokemon_CollectedPokemonInfo.h"
@@ -16,7 +20,8 @@ namespace Pokemon{
1620

1721
bool operator==(const CollectedPokemonInfo& lhs, const CollectedPokemonInfo& rhs){
1822
// NOTE edit when adding new struct members
19-
return lhs.national_dex_number == rhs.national_dex_number &&
23+
return lhs.dex_number == rhs.dex_number &&
24+
lhs.name_slug == rhs.name_slug &&
2025
lhs.shiny == rhs.shiny &&
2126
lhs.gmax == rhs.gmax &&
2227
lhs.alpha == rhs.alpha &&
@@ -25,19 +30,7 @@ bool operator==(const CollectedPokemonInfo& lhs, const CollectedPokemonInfo& rhs
2530
lhs.ot_id == rhs.ot_id;
2631
}
2732

28-
// Sort two pokemon slot. "Smaller" pokemon is placed closer to front.
29-
// If a slot is empty, it is always larger than non-empty slot so all the empty slots are at end after sorting.
30-
// For two pokemon, check each preference rule. If we cannot determine the order based on the first rule, go
31-
// to the second rule, and so on.
32-
// Available rules:
33-
// - National dex number: smaller ID should be at front.
34-
// - Shiny: shiny pokemon should be at front.
35-
// - Gigantamax: Gigantamax pokemon should be at front.
36-
// - Alpha: Alpha pokemon should be at front.
37-
// - Ball slug: pokemon with a ball slug that's smaller in the alphabetical order should be at front.
38-
// - Gender: Male should be at front, then comes female, and genderless is last
39-
// Each rule type also has a "reverse" which if true, reverses above ordering for that rule type.
40-
// If user does not give a preference ruleset, sort by national dex number.
33+
4134
bool operator<(const std::optional<CollectedPokemonInfo>& lhs, const std::optional<CollectedPokemonInfo>& rhs){
4235
if (!lhs.has_value()){ // lhs is empty
4336
return false;
@@ -49,10 +42,10 @@ bool operator<(const std::optional<CollectedPokemonInfo>& lhs, const std::option
4942
for (const SortingRule& preference : *lhs->preferences){
5043
switch(preference.sort_type){
5144
// NOTE edit when adding new struct members
52-
case SortingRuleType::NationalDexNo:
53-
if (lhs->national_dex_number != rhs->national_dex_number){
45+
case SortingRuleType::DexNo:
46+
if (lhs->dex_number != rhs->dex_number){
5447
// we use (boolean != reverse) to apply the reverse effect
55-
return (lhs->national_dex_number < rhs->national_dex_number) != preference.reverse;
48+
return (lhs->dex_number < rhs->dex_number) != preference.reverse;
5649
}
5750
break;
5851
case SortingRuleType::Shiny:
@@ -85,20 +78,20 @@ bool operator<(const std::optional<CollectedPokemonInfo>& lhs, const std::option
8578
} // end switch
8679
} // end for preference
8780

88-
// Default to sort by national dex number
89-
return lhs->national_dex_number < rhs->national_dex_number;
81+
// Default to sort by dex number
82+
return lhs->dex_number < rhs->dex_number;
9083
}
9184

9285
std::ostream& operator<<(std::ostream& os, const std::optional<CollectedPokemonInfo>& pokemon)
9386
{
9487
if (pokemon.has_value()){
9588
// NOTE edit when adding new struct members
9689
os << "(";
97-
os << "national_dex_number:" << pokemon->national_dex_number << " ";
90+
os << "dex_id: " << pokemon->dex_number << " " << pokemon->name_slug << " ";
9891
os << "shiny:" << (pokemon->shiny ? "true" : "false") << " ";
9992
os << "gmax:" << (pokemon->gmax ? "true" : "false") << " ";
10093
os << "alpha:" << (pokemon->alpha ? "true" : "false") << " ";
101-
os << "ball_slug:" << pokemon->ball_slug << " ";
94+
os << "ball:" << pokemon->ball_slug << " ";
10295
os << "gender:" << gender_to_string(pokemon->gender) << " ";
10396
os << "ot_id:" << pokemon->ot_id << " ";
10497
os << ")";
@@ -109,9 +102,10 @@ std::ostream& operator<<(std::ostream& os, const std::optional<CollectedPokemonI
109102
}
110103

111104
std::string create_overlay_info(const CollectedPokemonInfo& pokemon){
112-
const std::string& species_slug = NATIONAL_DEX_SLUGS()[pokemon.national_dex_number-1];
113-
const std::string& display_name = get_pokemon_name(species_slug).display_name();
114-
std::string overlay_log = display_name;
105+
const std::string& display_name = get_pokemon_name(pokemon.name_slug).display_name();
106+
char dex_str[5];
107+
snprintf(dex_str, sizeof(dex_str), "%04d", pokemon.dex_number);
108+
std::string overlay_log = std::string(dex_str) + " " + display_name;
115109
if(pokemon.gender == StatsHuntGenderFilter::Male){
116110
overlay_log += " " + UNICODE_MALE;
117111
} else if (pokemon.gender == StatsHuntGenderFilter::Female){
@@ -130,5 +124,32 @@ std::string create_overlay_info(const CollectedPokemonInfo& pokemon){
130124
}
131125

132126

127+
void save_boxes_data_to_json(const std::vector<std::optional<CollectedPokemonInfo>>& boxes_data, const std::string& json_path){
128+
JsonArray pokemon_data;
129+
for (size_t slot_idx = 0; slot_idx < boxes_data.size(); slot_idx++){
130+
BoxCursor cursor(slot_idx);
131+
JsonObject pokemon;
132+
pokemon["index"] = slot_idx;
133+
pokemon["box"] = cursor.box;
134+
pokemon["row"] = cursor.row;
135+
pokemon["column"] = cursor.column;
136+
const auto& current_pokemon = boxes_data[slot_idx];
137+
if (current_pokemon != std::nullopt){
138+
// NOTE edit when adding new struct members
139+
pokemon["name"] = current_pokemon->name_slug;
140+
pokemon["dex"] = current_pokemon->dex_number;
141+
pokemon["shiny"] = current_pokemon->shiny;
142+
pokemon["gmax"] = current_pokemon->gmax;
143+
pokemon["alpha"] = current_pokemon->alpha;
144+
pokemon["ball"] = current_pokemon->ball_slug;
145+
pokemon["gender"] = gender_to_string(current_pokemon->gender);
146+
pokemon["ot_id"] = current_pokemon->ot_id;
147+
}
148+
pokemon_data.push_back(std::move(pokemon));
149+
}
150+
pokemon_data.dump(json_path);
151+
}
152+
153+
133154
}
134155
}

SerialPrograms/Source/Pokemon/Pokemon_CollectedPokemonInfo.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ namespace Pokemon{
2222
struct CollectedPokemonInfo{
2323
const std::vector<SortingRule>* preferences;
2424

25-
// When adding any new member here, do not forget to modify the operators below (ctrl-f "new struct members")
26-
uint16_t national_dex_number = 0;
25+
// When adding any new member here, do not forget to modify the functions below (ctrl-f "new struct members")
26+
uint16_t dex_number = 0;
27+
std::string name_slug = "";
2728
bool shiny = false;
2829
bool gmax = false;
2930
bool alpha = false;
@@ -39,14 +40,14 @@ bool operator==(const CollectedPokemonInfo& lhs, const CollectedPokemonInfo& rhs
3940
// For two pokemon, check each preference rule. If we cannot determine the order based on the first rule, go
4041
// to the second rule, and so on.
4142
// Available rules:
42-
// - National dex number: smaller ID should be at front.
43+
// - Dex number: smaller ID should be at front.
4344
// - Shiny: shiny pokemon should be at front.
4445
// - Gigantamax: Gigantamax pokemon should be at front.
4546
// - Alpha: Alpha pokemon should be at front.
4647
// - Ball slug: pokemon with a ball slug that's smaller in the alphabetical order should be at front.
4748
// - Gender: Male should be at front, then comes female, and genderless is last
4849
// Each rule type also has a "reverse" which if true, reverses above ordering for that rule type.
49-
// If user does not give a preference ruleset, sort by national dex number.
50+
// If user does not give a preference ruleset, sort by dex number.
5051
bool operator<(const std::optional<CollectedPokemonInfo>& lhs, const std::optional<CollectedPokemonInfo>& rhs);
5152

5253
// Print pokemon info into ostream
@@ -55,6 +56,9 @@ std::ostream& operator<<(std::ostream& os, const std::optional<CollectedPokemonI
5556
// Create short info of a pokemon for overlay log display
5657
std::string create_overlay_info(const CollectedPokemonInfo& pokemon);
5758

59+
// save boxes of pokemon info to a JSON file
60+
void save_boxes_data_to_json(const std::vector<std::optional<CollectedPokemonInfo>>& boxes_data, const std::string& json_path);
61+
5862

5963
}
6064
}

0 commit comments

Comments
 (0)