Skip to content

Commit

Permalink
Create achievement_id alias
Browse files Browse the repository at this point in the history
This has spread far enough to warrant a type alias now.
  • Loading branch information
jbytheway committed May 29, 2020
1 parent ee563dc commit 3efd7ed
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/achievement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ struct achievement_requirement {
abort();
}

void check( const string_id<achievement> &id ) const {
void check( const achievement_id &id ) const {
if( !statistic.is_valid() ) {
debugmsg( "score %s refers to invalid statistic %s", id.str(), statistic.str() );
}
Expand Down Expand Up @@ -190,7 +190,7 @@ void achievement::time_bound::deserialize( JsonIn &jin )
}
}

void achievement::time_bound::check( const string_id<achievement> &id ) const
void achievement::time_bound::check( const achievement_id &id ) const
{
if( comparison_ == achievement_comparison::anything ) {
debugmsg( "Achievement %s has unconstrained \"anything\" time_constraint. "
Expand Down Expand Up @@ -341,7 +341,7 @@ void achievement::load( const JsonObject &jo, const std::string & )

void achievement::check() const
{
for( const string_id<achievement> &a : hidden_by_ ) {
for( const achievement_id &a : hidden_by_ ) {
if( !a.is_valid() ) {
debugmsg( "Achievement %s specifies hidden_by achievement %s, but the latter does not "
"exist.", id.str(), a.str() );
Expand Down Expand Up @@ -650,7 +650,7 @@ void achievements_tracker::report_achievement( const achievement *a, achievement
trackers_.erase( tracker_it );
}

achievement_completion achievements_tracker::is_completed( const string_id<achievement> &id ) const
achievement_completion achievements_tracker::is_completed( const achievement_id &id ) const
{
auto it = achievements_status_.find( id );
if( it == achievements_status_.end() ) {
Expand All @@ -672,7 +672,7 @@ bool achievements_tracker::is_hidden( const achievement *ach ) const
return false;
}

for( const string_id<achievement> &hidden_by : ach->hidden_by() ) {
for( const achievement_id &hidden_by : ach->hidden_by() ) {
if( is_completed( hidden_by ) != end_state ) {
return true;
}
Expand Down
16 changes: 8 additions & 8 deletions src/achievement.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class achievement
static const std::vector<achievement> &get_all();
static void reset();

string_id<achievement> id;
achievement_id id;
bool was_loaded = false;

const translation &name() const {
Expand All @@ -71,7 +71,7 @@ class achievement
return is_conduct_;
}

const std::vector<string_id<achievement>> &hidden_by() const {
const std::vector<achievement_id> &hidden_by() const {
return hidden_by_;
}

Expand All @@ -85,7 +85,7 @@ class achievement
};

void deserialize( JsonIn & );
void check( const string_id<achievement> & ) const;
void check( const achievement_id & ) const;

time_point target() const;
achievement_completion completed() const;
Expand All @@ -108,7 +108,7 @@ class achievement
translation name_;
translation description_;
bool is_conduct_ = false;
std::vector<string_id<achievement>> hidden_by_;
std::vector<achievement_id> hidden_by_;
cata::optional<time_bound> time_constraint_;
std::vector<achievement_requirement> requirements_;
};
Expand Down Expand Up @@ -181,7 +181,7 @@ class achievements_tracker : public event_subscriber

void report_achievement( const achievement *, achievement_completion );

achievement_completion is_completed( const string_id<achievement> & ) const;
achievement_completion is_completed( const achievement_id & ) const;
bool is_hidden( const achievement * ) const;
std::string ui_text_for( const achievement * ) const;
bool is_enabled() const {
Expand All @@ -203,12 +203,12 @@ class achievements_tracker : public event_subscriber
bool enabled_ = true;
std::function<void( const achievement *, bool )> achievement_attained_callback_;
std::function<void( const achievement *, bool )> achievement_failed_callback_;
std::unordered_set<string_id<achievement>> initial_achievements_;
std::unordered_set<achievement_id> initial_achievements_;

// Class invariant: each valid achievement has exactly one of a watcher
// (if it's pending) or a status (if it's completed or failed).
std::unordered_map<string_id<achievement>, achievement_tracker> trackers_;
std::unordered_map<string_id<achievement>, achievement_state> achievements_status_;
std::unordered_map<achievement_id, achievement_tracker> trackers_;
std::unordered_map<achievement_id, achievement_state> achievements_status_;
};

#endif // CATA_SRC_ACHIEVEMENT_H
3 changes: 3 additions & 0 deletions src/type_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "int_id.h"
#include "string_id.h"

class achievement;
using achievement_id = string_id<achievement>;

class activity_type;
using activity_id = string_id<activity_type>;

Expand Down
32 changes: 16 additions & 16 deletions tests/stats_tracker_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ TEST_CASE( "achievments_tracker", "[stats]" )
{
override_option opt( "24_HOUR", "military" );

std::map<string_id<achievement>, const achievement *> achievements_completed;
std::map<string_id<achievement>, const achievement *> achievements_failed;
std::map<achievement_id, const achievement *> achievements_completed;
std::map<achievement_id, const achievement *> achievements_failed;
event_bus b;
stats_tracker s;
b.subscribe( &s );
Expand All @@ -476,7 +476,7 @@ TEST_CASE( "achievments_tracker", "[stats]" )
const cata::event avatar_wakes_up =
cata::event::make<event_type::character_wakes_up>( u_id );

string_id<achievement> a_survive_one_day( "achievement_survive_one_day" );
achievement_id a_survive_one_day( "achievement_survive_one_day" );

b.send<event_type::game_start>( u_id );

Expand All @@ -496,10 +496,10 @@ TEST_CASE( "achievments_tracker", "[stats]" )
const cata::event avatar_zombie_kill =
cata::event::make<event_type::character_kills_monster>( u_id, mon_zombie );

string_id<achievement> a_kill_10( "achievement_kill_10_monsters" );
string_id<achievement> a_kill_100( "achievement_kill_100_monsters" );
string_id<achievement> c_pacifist( "conduct_zero_kills" );
string_id<achievement> c_merciful( "conduct_zero_character_kills" );
achievement_id a_kill_10( "achievement_kill_10_monsters" );
achievement_id a_kill_100( "achievement_kill_100_monsters" );
achievement_id c_pacifist( "conduct_zero_kills" );
achievement_id c_merciful( "conduct_zero_character_kills" );

b.send<event_type::game_start>( u_id );

Expand All @@ -526,10 +526,10 @@ TEST_CASE( "achievments_tracker", "[stats]" )
const cata::event avatar_zombie_kill =
cata::event::make<event_type::character_kills_monster>( u_id, mon_zombie );

string_id<achievement> c_pacifist( "conduct_zero_kills" );
string_id<achievement> c_merciful( "conduct_zero_character_kills" );
string_id<achievement> a_kill_zombie( "achievement_kill_zombie" );
string_id<achievement> a_kill_in_first_minute( "achievement_kill_in_first_minute" );
achievement_id c_pacifist( "conduct_zero_kills" );
achievement_id c_merciful( "conduct_zero_character_kills" );
achievement_id a_kill_zombie( "achievement_kill_zombie" );
achievement_id a_kill_in_first_minute( "achievement_kill_in_first_minute" );

b.send<event_type::game_start>( u_id );

Expand Down Expand Up @@ -657,7 +657,7 @@ TEST_CASE( "achievments_tracker", "[stats]" )
character_movemode::CMM_WALK, false, -OVERMAP_DEPTH );

SECTION( "achievement_marathon" ) {
string_id<achievement> a_marathon( "achievement_marathon" );
achievement_id a_marathon( "achievement_marathon" );

GIVEN( "a new game" ) {
const character_id u_id = g->u.getID();
Expand All @@ -676,7 +676,7 @@ TEST_CASE( "achievments_tracker", "[stats]" )
}

SECTION( "achievement_traverse_sharp_terrain" ) {
string_id<achievement> a_traverse_sharp_terrain( "achievement_traverse_sharp_terrain" );
achievement_id a_traverse_sharp_terrain( "achievement_traverse_sharp_terrain" );

GIVEN( "a new game" ) {
const character_id u_id = g->u.getID();
Expand All @@ -695,7 +695,7 @@ TEST_CASE( "achievments_tracker", "[stats]" )
}

SECTION( "achievement_swim_merit_badge" ) {
string_id<achievement> a_swim_merit_badge( "achievement_swim_merit_badge" );
achievement_id a_swim_merit_badge( "achievement_swim_merit_badge" );

GIVEN( "a new game" ) {
const character_id u_id = g->u.getID();
Expand All @@ -718,7 +718,7 @@ TEST_CASE( "achievments_tracker", "[stats]" )
}

SECTION( "achievement_reach_max_z_level" ) {
string_id<achievement> a_reach_max_z_level( "achievement_reach_max_z_level" );
achievement_id a_reach_max_z_level( "achievement_reach_max_z_level" );

GIVEN( "a new game" ) {
const character_id u_id = g->u.getID();
Expand All @@ -735,7 +735,7 @@ TEST_CASE( "achievments_tracker", "[stats]" )
}

SECTION( "achievement_reach_min_z_level" ) {
string_id<achievement> a_reach_min_z_level( "achievement_reach_min_z_level" );
achievement_id a_reach_min_z_level( "achievement_reach_min_z_level" );

GIVEN( "a new game" ) {
const character_id u_id = g->u.getID();
Expand Down

0 comments on commit 3efd7ed

Please sign in to comment.