Skip to content

Commit 4a08f17

Browse files
committed
Refactor diary change list functions
1 parent 61568d2 commit 4a08f17

File tree

3 files changed

+70
-33
lines changed

3 files changed

+70
-33
lines changed

src/diary.cpp

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ int diary::set_opened_page( int pagenum )
7070
opened_page = - 1;
7171
} else if( pagenum < 0 ) {
7272
opened_page = pages.size() - 1;
73-
} else if( pagenum == 0 && pagenum != opened_page ) {
74-
opened_page = 0;
75-
open_summary_page();
7673
} else {
7774
opened_page = pagenum % pages.size();
7875
}
@@ -89,7 +86,7 @@ diary_page *diary::get_page_ptr( int offset, bool allow_summary )
8986
{
9087
const int page = offset + opened_page;
9188
if( !pages.empty() && page >= 0 && page < static_cast<int>( pages.size() ) ) {
92-
diary_page * const ptr = pages[page].get();
89+
diary_page *const ptr = pages[page].get();
9390
if( allow_summary || !ptr->is_summary() ) {
9491
return ptr;
9592
}
@@ -110,7 +107,7 @@ size_t diary::add_to_change_list( const std::string &entry, const std::string &d
110107
void diary::update_change_list( const size_t index, const std::string &entry,
111108
const std::string &desc )
112109
{
113-
if( index < 0 || index >= change_list.size() ) {
110+
if( index >= change_list.size() ) {
114111
return;
115112
}
116113
if( desc.empty() ) {
@@ -141,6 +138,7 @@ void diary::open_summary_page()
141138

142139
template<typename Container, typename Fn>
143140
void diary::changes( Container diary_page::* member, Fn &&get_entry,
141+
// NOLINTNEXTLINE(cata-use-string_view)
144142
const std::string &heading_first, const std::string &headings_first,
145143
const std::string &heading, const std::string &headings,
146144
diary_page *const currpage, diary_page *const prevpage )
@@ -170,6 +168,7 @@ void diary::changes( Container diary_page::* member, Fn &&get_entry,
170168

171169
template<typename Container, typename Fn>
172170
void diary::changes( Container diary_page::* member, Fn &&get_entry,
171+
// NOLINTNEXTLINE(cata-use-string_view)
173172
const std::string &heading_first, const std::string &headings_first,
174173
const std::string &heading, const std::string &headings )
175174
{
@@ -179,6 +178,7 @@ void diary::changes( Container diary_page::* member, Fn &&get_entry,
179178

180179
template<typename Container, typename Fn>
181180
void diary::changes( Container diary_page::* member, Fn &&get_entry,
181+
// NOLINTNEXTLINE(cata-use-string_view)
182182
const std::string &heading_first, const std::string &headings_first )
183183
{
184184
changes( member, get_entry, heading_first, headings_first, heading_first, headings_first );
@@ -232,12 +232,12 @@ void diary::mission_changes()
232232
_( "Active mission: " ), _( "Active missions: " ),
233233
_( "New mission: " ), _( "New missions: " )
234234
);
235-
changes( &diary_page::mission_active,
235+
changes( &diary_page::mission_completed,
236236
mission_change,
237237
_( "Completed mission: " ), _( "Completed missions: " ),
238238
_( "New completed mission: " ), _( "New completed missions: " )
239239
);
240-
changes( &diary_page::mission_active,
240+
changes( &diary_page::mission_failed,
241241
mission_change,
242242
_( "Failed mission: " ), _( "Failed missions: " ),
243243
_( "New failed mission: " ), _( "New failed missions: " )
@@ -304,12 +304,12 @@ void diary::npc_kill_changes()
304304
changes( &diary_page::npc_kills, npc_kill_change, _( "NPC killed: " ), _( "NPCs killed: " ) );
305305
}
306306

307-
static diary_entry_opt skill_change( const std::pair<const string_id<Skill>, int> &elem,
307+
static diary_entry_opt skill_change( const std::pair<const skill_id, int> &elem,
308308
diary_page *const prevpage )
309309
{
310310
const int prevlvl = prevpage ? prevpage->skillsL[ elem.first ] : 0;
311311
if( elem.second > prevlvl ) {
312-
Skill s = elem.first.obj();
312+
const Skill &s = elem.first.obj();
313313
return { {
314314
prevpage ?
315315
string_format( _( "<color_light_blue>%s: %d -> %d</color>" ), s.name(),
@@ -350,12 +350,12 @@ void diary::trait_changes()
350350
);
351351
}
352352

353-
static const std::array<std::tuple<const char *, const char *, int diary_page::*>, 4> diary_stats
353+
static const std::array<std::tuple<translation, translation, int diary_page::*>, 4> diary_stats
354354
= { {
355-
{ _( "Strength: %d" ), _( "Strength: %d -> %d" ), &diary_page::strength },
356-
{ _( "Dexterity: %d" ), _( "Dexterity: %d -> %d" ), &diary_page::dexterity },
357-
{ _( "Intelligence: %d" ), _( "Intelligence: %d -> %d" ), &diary_page::intelligence },
358-
{ _( "Perception: %d" ), _( "Perception: %d -> %d" ), &diary_page::perception }
355+
{ to_translation( "Strength: %d" ), to_translation( "Strength: %d -> %d" ), &diary_page::strength },
356+
{ to_translation( "Dexterity: %d" ), to_translation( "Dexterity: %d -> %d" ), &diary_page::dexterity },
357+
{ to_translation( "Intelligence: %d" ), to_translation( "Intelligence: %d -> %d" ), &diary_page::intelligence },
358+
{ to_translation( "Perception: %d" ), to_translation( "Perception: %d -> %d" ), &diary_page::perception }
359359
}
360360
};
361361

@@ -408,10 +408,12 @@ void diary::prof_changes()
408408

409409
const std::vector<std::string> &diary::get_change_list()
410410
{
411-
if( !change_list.empty() ) {
411+
if( !change_list.empty() || pages.empty() ) {
412412
return change_list;
413413
}
414-
if( !pages.empty() ) {
414+
if( get_page_ptr()->is_summary() ) {
415+
open_summary_page();
416+
} else {
415417
stat_changes();
416418
skill_changes();
417419
prof_changes();
@@ -446,26 +448,40 @@ std::string diary::get_page_text()
446448
return "";
447449
}
448450

449-
std::string diary::get_head_text()
451+
std::string diary::get_head_text( bool is_summary )
450452
{
451453

452454
if( !pages.empty() ) {
453455
const diary_page *prevpageptr = get_page_ptr( -1, false );
454456
const diary_page *currpageptr = get_page_ptr();
455-
const time_point prev_turn = ( prevpageptr != nullptr ) ? prevpageptr->turn : calendar::turn_zero;
457+
const time_point prev_turn = ( prevpageptr != nullptr ) ? prevpageptr->turn :
458+
calendar::start_of_game;
456459
const time_duration turn_diff = currpageptr->turn - prev_turn;
457460
std::string time_diff_text;
458-
if( opened_page != 0 ) {
461+
if( prevpageptr ) {
459462
time_diff_text = get_diary_time_since_str( turn_diff, currpageptr->time_acc );
460463
}
461-
//~ Head text of a diary page
462-
//~ %1$d is the current page number, %2$d is the number of pages in total
463-
//~ %3$s is the time point when the current page was created
464-
//~ %4$s is time relative to the previous page
465-
return string_format( _( "Entry: %1$d/%2$d, %3$s, %4$s" ),
466-
opened_page + 1, pages.size(),
467-
get_diary_time_str( currpageptr->turn, currpageptr->time_acc ),
468-
time_diff_text );
464+
if( is_summary ) {
465+
//~ Head text of diary summary page
466+
//~ %1$d is the number of entries in total
467+
return string_format( _( "Summary: %1$d entries" ), pages.size() - 1 );
468+
} else if( time_diff_text.empty() ) {
469+
//~ Head text of first diary entry
470+
//~ %1$d is the current entry number, %2$d is the number of entries in total
471+
//~ %3$s is the time point when the current entry was created
472+
return string_format( _( "Entry: %1$d/%2$d, %3$s" ),
473+
opened_page, pages.size() - 1,
474+
get_diary_time_str( currpageptr->turn, currpageptr->time_acc ) );
475+
} else {
476+
//~ Head text of second and later diary entries
477+
//~ %1$d is the current entry number, %2$d is the number of entries in total
478+
//~ %3$s is the time point when the current entry was created
479+
//~ %4$s is time relative to the previous entry
480+
return string_format( _( "Entry: %1$d/%2$d, %3$s, %4$s" ),
481+
opened_page, pages.size() - 1,
482+
get_diary_time_str( currpageptr->turn, currpageptr->time_acc ),
483+
time_diff_text );
484+
}
469485
}
470486
return "";
471487
}
@@ -523,6 +539,7 @@ void diary::new_page()
523539
page->intelligence = u->get_int_base();
524540
page->perception = u->get_per_base();
525541
page->traits = u->get_mutations_variants( false );
542+
std::sort( page->traits.begin(), page->traits.end(), trait_var_display_sort );
526543
const auto spells = u->magic->get_spells();
527544
for( const spell *spell : spells ) {
528545
const spell_id &id = spell->id();
@@ -531,12 +548,13 @@ void diary::new_page()
531548
}
532549
page-> bionics = u->get_bionics();
533550
for( Skill &elem : Skill::skills ) {
534-
535551
int level = u->get_skill_level_object( elem.ident() ).level();
536552
page->skillsL.insert( { elem.ident(), level } );
537553
}
538554
page -> known_profs = u->_proficiencies->known_profs();
555+
std::sort( page->known_profs.begin(), page->known_profs.end() );
539556
page -> learning_profs = u->_proficiencies->learning_profs();
557+
std::sort( page->learning_profs.begin(), page->learning_profs.end() );
540558
page->max_power_level = u->get_max_power_level();
541559
diary::pages.push_back( std::move( page ) );
542560
}
@@ -560,7 +578,7 @@ void diary::export_to_txt( bool lastexport )
560578
for( int i = 0; i < static_cast<int>( pages.size() ); i++ ) {
561579
set_opened_page( i );
562580
const diary_page page = *get_page_ptr();
563-
myfile << get_head_text() + "\n\n";
581+
myfile << get_head_text( page.is_summary() ) + "\n\n";
564582
for( const std::string &str : this->get_change_list() ) {
565583
myfile << remove_color_tags( str ) + "\n";
566584
}

src/diary.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,31 @@
1717

1818
class JsonOut;
1919
class JsonValue;
20+
class Skill;
2021

2122
namespace catacurses
2223
{
2324
class window;
2425
} // namespace catacurses
2526

27+
///Compares two string_id by translated name
28+
template<typename T>
29+
class id_translated_name_comp
30+
{
31+
public:
32+
bool operator()( const string_id<T> &a, const string_id<T> &b ) const {
33+
const std::string &a_name = a->name();
34+
const std::string &b_name = b->name();
35+
if( a_name == b_name ) {
36+
// just in case there's a collision in translated names
37+
// NOLINTNEXTLINE(cata-use-localized-sorting)
38+
return a.str() < b.str();
39+
}
40+
// NOLINTNEXTLINE(cata-use-localized-sorting)
41+
return a->name() < b->name();
42+
}
43+
};
44+
2645
/// <summary>
2746
/// diary page, to save current character progression
2847
/// </summary>
@@ -60,7 +79,7 @@ struct diary_page {
6079
/*bionics id`s the character has*/
6180
std::vector<bionic_id> bionics;
6281
/*skill id's with level the character has*/
63-
std::map<skill_id, int> skillsL;
82+
std::map<skill_id, int, id_translated_name_comp<Skill>> skillsL;
6483
/*known and learning profession id of the character*/
6584
std::vector<proficiency_id> known_profs;
6685
std::vector<proficiency_id> learning_profs;
@@ -148,7 +167,7 @@ class diary
148167
/*returns the text of opened page*/
149168
std::string get_page_text();
150169
/*returns text for head of page*/
151-
std::string get_head_text();
170+
std::string get_head_text( bool is_summary = false );
152171
/*helper functions for *_changes below, adds to change_list with headers, pluralization, etc*/
153172
template<typename Container, typename Fn>
154173
void changes( Container diary_page::* member, Fn &&get_entry,

src/diary_ui.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void diary::show_diary_ui( diary *c_diary )
230230
currwin == window_mode::TEXT_WIN, false, report_color_error::no );
231231

232232
trim_and_print( w_head, point::south_east, getmaxx( w_head ) - 2, c_white,
233-
c_diary->get_head_text() );
233+
c_diary->get_head_text( c_diary->get_page_ptr()->is_summary() ) );
234234

235235
wnoutrefresh( w_border );
236236
wnoutrefresh( w_head );
@@ -256,7 +256,7 @@ void diary::show_diary_ui( diary *c_diary )
256256
print_list_scrollable( &w_pages, c_diary->get_pages_list(), &selected[window_mode::PAGE_WIN],
257257
currwin == window_mode::PAGE_WIN, true, report_color_error::yes );
258258
center_print( w_pages, 0, c_light_gray, string_format( _( "pages: %d" ),
259-
c_diary->get_pages_list().size() - 1 ) );
259+
c_diary->get_pages_list().size() ) );
260260

261261
wnoutrefresh( w_pages );
262262
} );

0 commit comments

Comments
 (0)