11#include " SearchHistory.hpp"
2+ #include < Geode/binding/GJSearchObject.hpp>
3+ #include < Geode/loader/Mod.hpp>
4+ #include < Geode/utils/ranges.hpp>
25
36using namespace geode ::prelude;
47
58void SearchHistory::add (GJSearchObject* search, time_t time, int type) {
69 auto history = Mod::get ()->getSavedValue <std::vector<SearchHistoryObject>>(" search-history" );
710
8- auto difficultyStrings = search-> m_difficulty != " - " ? string::split (search-> m_difficulty , " , " ) : std::vector<std::string>();
9- std::vector<int > difficulties;
10- for ( auto const & str : difficultyStrings ) {
11- difficulties .push_back (std::stoi (str ));
12- }
11+ auto difficulties = ranges::reduce< std::vector<int >>(
12+ search-> m_difficulty != " - " ? string::split (search-> m_difficulty , " , " ) : std::vector<std::string>(),
13+ [](std::vector< int >& vec, const std::string& str ) {
14+ if ( auto num = numFromString< int >(str)) vec .push_back (num. unwrap ( ));
15+ });
1316
14- auto lengthStrings = search-> m_length != " - " ? string::split (search-> m_length , " , " ) : std::vector<std::string>();
15- std::vector<int > lengths;
16- for ( auto const & str : lengthStrings ) {
17- lengths .push_back (std::stoi (str ));
18- }
17+ auto lengths = ranges::reduce< std::vector<int >>(
18+ search-> m_length != " - " ? string::split (search-> m_length , " , " ) : std::vector<std::string>(),
19+ [](std::vector< int >& vec, const std::string& str ) {
20+ if ( auto num = numFromString< int >(str)) vec .push_back (num. unwrap ( ));
21+ });
1922
2023 SearchHistoryObject obj = {
2124 .time = time,
@@ -40,9 +43,7 @@ void SearchHistory::add(GJSearchObject* search, time_t time, int type) {
4043 .star = search->m_starFilter
4144 };
4245
43- auto found = std::find_if (history.begin (), history.end (), [&obj](SearchHistoryObject const & o) {
44- return obj == o;
45- });
46+ auto found = std::ranges::find_if (history, [&obj](const SearchHistoryObject& o) { return obj == o; });
4647
4748 if (found != history.end ()) history.erase (found);
4849
@@ -64,3 +65,69 @@ void SearchHistory::remove(int index) {
6465 history.erase (history.begin () + index);
6566 Mod::get ()->setSavedValue (" search-history" , history);
6667}
68+
69+ Result<std::vector<SearchHistoryObject>> matjson::Serialize<std::vector<SearchHistoryObject>>::fromJson(const matjson::Value& value) {
70+ if (!value.isArray ()) return Err (" Expected array" );
71+
72+ return Ok (ranges::reduce<std::vector<SearchHistoryObject>>(value.asArray ().unwrap (),
73+ [](std::vector<SearchHistoryObject>& vec, const matjson::Value& elem) {
74+ SearchHistoryObject obj = {
75+ .time = (int64_t )elem[" time" ].asInt ().unwrapOr (0 ),
76+ .type = (int )elem[" type" ].asInt ().unwrapOr (0 ),
77+ .query = elem[" query" ].asString ().unwrapOr (" " ),
78+ .difficulties = ranges::map<std::vector<int >>(
79+ elem[" difficulties" ].isArray () ? elem[" difficulties" ].asArray ().unwrap () : std::vector<matjson::Value>(),
80+ [](const matjson::Value& e) { return e.asInt ().unwrapOr (0 ); }),
81+ .lengths = ranges::map<std::vector<int >>(
82+ elem[" lengths" ].isArray () ? elem[" lengths" ].asArray ().unwrap () : std::vector<matjson::Value>(),
83+ [](const matjson::Value& e) { return e.asInt ().unwrapOr (0 ); }),
84+ .uncompleted = elem[" uncompleted" ].asBool ().unwrapOr (false ),
85+ .completed = elem[" completed" ].asBool ().unwrapOr (false ),
86+ .featured = elem[" featured" ].asBool ().unwrapOr (false ),
87+ .original = elem[" original" ].asBool ().unwrapOr (false ),
88+ .twoPlayer = elem[" two-player" ].asBool ().unwrapOr (false ),
89+ .coins = elem[" coins" ].asBool ().unwrapOr (false ),
90+ .epic = elem[" epic" ].asBool ().unwrapOr (false ),
91+ .legendary = elem[" legendary" ].asBool ().unwrapOr (false ),
92+ .mythic = elem[" mythic" ].asBool ().unwrapOr (false ),
93+ .song = elem[" song" ].asBool ().unwrapOr (false ),
94+ .customSong = elem[" custom-song" ].asBool ().unwrapOr (false ),
95+ .songID = (int )elem[" song-id" ].asInt ().unwrapOr (0 ),
96+ .demonFilter = (int )elem[" demon-filter" ].asInt ().unwrapOr (0 ),
97+ .noStar = elem[" no-star" ].asBool ().unwrapOr (false ),
98+ .star = elem[" star" ].asBool ().unwrapOr (false )
99+ };
100+
101+ if (!std::ranges::any_of (vec, [&obj](const SearchHistoryObject& o) { return obj == o; })) vec.push_back (obj);
102+ }));
103+ }
104+
105+ matjson::Value matjson::Serialize<std::vector<SearchHistoryObject>>::toJson(const std::vector<SearchHistoryObject>& vec) {
106+ return ranges::map<std::vector<matjson::Value>>(vec, [](const SearchHistoryObject& obj) {
107+ matjson::Value historyObject;
108+ historyObject[" time" ] = obj.time ;
109+ historyObject[" type" ] = obj.type ;
110+ if (!obj.query .empty ()) historyObject[" query" ] = obj.query ;
111+ if (!obj.difficulties .empty ()) historyObject[" difficulties" ] = obj.difficulties ;
112+ if (!obj.lengths .empty ()) historyObject[" lengths" ] = obj.lengths ;
113+ if (obj.uncompleted ) historyObject[" uncompleted" ] = obj.uncompleted ;
114+ if (obj.completed ) historyObject[" completed" ] = obj.completed ;
115+ if (obj.featured ) historyObject[" featured" ] = obj.featured ;
116+ if (obj.original ) historyObject[" original" ] = obj.original ;
117+ if (obj.twoPlayer ) historyObject[" two-player" ] = obj.twoPlayer ;
118+ if (obj.coins ) historyObject[" coins" ] = obj.coins ;
119+ if (obj.epic ) historyObject[" epic" ] = obj.epic ;
120+ if (obj.legendary ) historyObject[" legendary" ] = obj.legendary ;
121+ if (obj.mythic ) historyObject[" mythic" ] = obj.mythic ;
122+ if (obj.song ) {
123+ historyObject[" song" ] = obj.song ;
124+ if (obj.customSong ) historyObject[" custom-song" ] = obj.customSong ;
125+ historyObject[" song-id" ] = obj.songID ;
126+ }
127+ if (obj.demonFilter != 0 ) historyObject[" demon-filter" ] = obj.demonFilter ;
128+ if (obj.noStar ) historyObject[" no-star" ] = obj.noStar ;
129+ if (obj.star ) historyObject[" star" ] = obj.star ;
130+
131+ return historyObject;
132+ });
133+ }
0 commit comments