Use a parametrizable function instead of multiple functions ; track more values

This commit is contained in:
Alayan 2018-10-03 01:13:47 +02:00 committed by auriamg
parent 19d594b465
commit edc683f720
4 changed files with 80 additions and 77 deletions

View File

@ -59,7 +59,8 @@ AchievementsStatus::AchievementsStatus()
TrackStats new_track;
new_track.ident = curr->getIdent();
new_track.race_started = new_track.race_finished = new_track.race_won = 0;
new_track.race_finished_reverse = 0;
new_track.race_finished_reverse = new_track.race_finished_alone = 0;
new_track.egg_hunt_started = new_track.egg_hunt_finished = 0;
m_track_stats.push_back(new_track);
} // for n<track_amount
@ -138,6 +139,9 @@ void AchievementsStatus::load(const XMLNode * input)
xml_achievement_tracks[i]->get("finished",&m_track_stats[j].race_finished);
xml_achievement_tracks[i]->get("won",&m_track_stats[j].race_won);
xml_achievement_tracks[i]->get("finished_reverse",&m_track_stats[j].race_finished_reverse);
xml_achievement_tracks[i]->get("finished_alone",&m_track_stats[j].race_finished_alone);
xml_achievement_tracks[i]->get("egg_hunt_started",&m_track_stats[j].egg_hunt_started);
xml_achievement_tracks[i]->get("egg_hunt_finished",&m_track_stats[j].egg_hunt_finished);
track_found = true;
break;
}
@ -151,6 +155,9 @@ void AchievementsStatus::load(const XMLNode * input)
xml_achievement_tracks[i]->get("finished",&new_track.race_finished);
xml_achievement_tracks[i]->get("won",&new_track.race_won);
xml_achievement_tracks[i]->get("finished_reverse",&new_track.race_finished_reverse);
xml_achievement_tracks[i]->get("finished_alone",&new_track.race_finished_alone);
xml_achievement_tracks[i]->get("egg_hunt_started",&new_track.egg_hunt_started);
xml_achievement_tracks[i]->get("egg_hunt_finished",&new_track.egg_hunt_finished);
m_track_stats.push_back(new_track);
}
@ -192,7 +199,11 @@ void AchievementsStatus::save(UTFWriter &out)
out << " started=\"" << m_track_stats[n].race_started << "\"";
out << " finished=\"" << m_track_stats[n].race_finished << "\"";
out << " won=\"" << m_track_stats[n].race_won << "\"";
out << " finished_reverse=\"" << m_track_stats[n].race_finished_reverse << "\"/>\n";
out << " finished_reverse=\"" << m_track_stats[n].race_finished_reverse << "\"";
out << " finished_alone=\"" << m_track_stats[n].race_finished_alone << "\"";
out << " egg_hunt_started=\"" << m_track_stats[n].egg_hunt_started << "\"";
out << " egg_hunt_finished=\"" << m_track_stats[n].egg_hunt_finished << "\"";
out << "/>\n";
} // for n<m_track_stats.size()
out << " </achievements>\n";
} // save
@ -391,53 +402,32 @@ void AchievementsStatus::onLapEnd()
} // onLapEnd
// ----------------------------------------------------------------------------
void AchievementsStatus::raceStarted(std::string track_ident)
/** Use the event type to increment the correct track event counter
* \param track_ident - the internal name of the track
* \param event - the type of counter to increment */
void AchievementsStatus::trackEvent(std::string track_ident, AchievementsStatus::TrackData event)
{
int track_id = -1;
for (unsigned int i=0;i<m_track_stats.size();i++)
{
if (m_track_stats[i].ident == track_ident)
{
m_track_stats[i].race_started++;
track_id = i;
break;
}
}
} // raceStarted
// ----------------------------------------------------------------------------
void AchievementsStatus::raceFinished(std::string track_ident)
{
for (unsigned int i=0;i<m_track_stats.size();i++)
{
if (m_track_stats[i].ident == track_ident)
{
m_track_stats[i].race_finished++;
break;
}
}
} // raceFinished
// ----------------------------------------------------------------------------
void AchievementsStatus::raceWon(std::string track_ident)
{
for (unsigned int i=0;i<m_track_stats.size();i++)
{
if (m_track_stats[i].ident == track_ident)
{
m_track_stats[i].race_won++;
break;
}
}
} // raceWon
// ----------------------------------------------------------------------------
void AchievementsStatus::raceFinishedReverse(std::string track_ident)
{
for (unsigned int i=0;i<m_track_stats.size();i++)
{
if (m_track_stats[i].ident == track_ident)
{
m_track_stats[i].race_finished_reverse++;
break;
}
}
} // raceFinishedReverse
if (event==TR_STARTED)
m_track_stats[track_id].race_started++;
else if (event==TR_FINISHED)
m_track_stats[track_id].race_finished++;
else if (event==TR_WON)
m_track_stats[track_id].race_won++;
else if (event==TR_FINISHED_REVERSE)
m_track_stats[track_id].race_finished_reverse++;
else if (event==TR_FINISHED_ALONE)
m_track_stats[track_id].race_finished_alone++;
else if (event==TR_EGG_HUNT_STARTED)
m_track_stats[track_id].egg_hunt_started++;
else if (event==TR_EGG_HUNT_FINISHED)
m_track_stats[track_id].egg_hunt_finished++;
} // trackEvent

View File

@ -96,13 +96,32 @@ private:
// counters for standard, TT & FTL races
int race_started;
int race_finished;
int race_won;
int race_won; // doesn't count race without any other AI/player
int race_finished_reverse;
// TODO : Add data to keep track of egg hunts, battle, soccer
int race_finished_alone; // races against replays are counted, too
// counters for egg hunts
int egg_hunt_started;
int egg_hunt_finished;
};
// Switching a few times from public to private
// helps here to keep related things next to each other
public:
enum TrackData {
TR_STARTED = 0,
TR_FINISHED = 1,
TR_WON = 2,
TR_FINISHED_REVERSE = 3,
TR_FINISHED_ALONE = 4,
TR_EGG_HUNT_STARTED = 5,
TR_EGG_HUNT_FINISHED = 6
};
private:
std::vector<TrackStats> m_track_stats;
// TODO : keep track of battle/soccer arenas
bool m_online;
bool m_valid;
@ -125,10 +144,7 @@ public :
void resetDataVar(unsigned int achieve_data_id);
void onRaceEnd(bool aborted=false);
void onLapEnd();
void raceStarted(std::string track_ident);
void raceFinished(std::string track_ident);
void raceWon(std::string track_ident);
void raceFinishedReverse(std::string track_ident);
void trackEvent(std::string track_ident, AchievementsStatus::TrackData event);
// ------------------------------------------------------------------------
const std::map<uint32_t, Achievement *>& getAllAchievements()
{

View File

@ -200,28 +200,13 @@ public:
// ----------------------------------------------------------------------------
static void raceStarted(std::string track_ident)
/** Transmit an incrementation request of one of the track event counters
* \param track_ident - the internal name of the track
* \param event - the type of counter to increment */
static void trackEvent(std::string track_ident, AchievementsStatus::TrackData event)
{
getCurrentAchievementsStatus()->raceStarted(track_ident);
} // raceStarted
// ----------------------------------------------------------------------------
static void raceFinished(std::string track_ident)
{
getCurrentAchievementsStatus()->raceFinished(track_ident);
} // raceFinished
// ----------------------------------------------------------------------------
static void raceWon(std::string track_ident)
{
getCurrentAchievementsStatus()->raceWon(track_ident);
} // raceWon
// ----------------------------------------------------------------------------
static void raceFinishedReverse(std::string track_ident)
{
getCurrentAchievementsStatus()->raceFinishedReverse(track_ident);
} // raceFinishedReverse
getCurrentAchievementsStatus()->trackEvent(track_ident, event);
} // trackEvent
// ------------------------------------------------------------------------
}; // PlayerManager

View File

@ -294,8 +294,10 @@ void World::reset(bool restart)
(*i)->reset();
if ((*i)->getController()->canGetAchievements())
{
printf("Track name is %s\n", race_manager->getTrackName().c_str());
PlayerManager::raceStarted(race_manager->getTrackName());
if (race_manager->isLinearRaceMode())
PlayerManager::trackEvent(race_manager->getTrackName(), AchievementsStatus::TR_STARTED);
else if (race_manager->isEggHuntMode())
PlayerManager::trackEvent(race_manager->getTrackName(), AchievementsStatus::TR_EGG_HUNT_STARTED);
if (restart)
PlayerManager::onRaceEnd(true /* previous race aborted */);
}
@ -629,7 +631,8 @@ void World::terminateRace()
"laps", race_manager->getNumLaps());
}
// Increment won races counts
//TODO : move this stuff to a sub-function
// Increment won races counts and track finished counts
if (race_manager->isLinearRaceMode())
{
for(unsigned int i = 0; i < kart_amount; i++)
@ -638,15 +641,18 @@ void World::terminateRace()
// Retrieve the current player
if (m_karts[i]->getController()->canGetAchievements())
{
PlayerManager::raceFinished(race_manager->getTrackName());
PlayerManager::trackEvent(race_manager->getTrackName(), AchievementsStatus::TR_FINISHED);
if (race_manager->getReverseTrack())
PlayerManager::raceFinishedReverse(race_manager->getTrackName());
PlayerManager::trackEvent(race_manager->getTrackName(), AchievementsStatus::TR_FINISHED_REVERSE);
int winner_position = 1;
if (race_manager->isFollowMode()) winner_position = 2;//TODO : check this always work
// Check if the player has won
if (m_karts[i]->getPosition() == winner_position)
{
PlayerManager::raceWon(race_manager->getTrackName());
if (race_manager->getNumNonGhostKarts() >= 2)
PlayerManager::trackEvent(race_manager->getTrackName(), AchievementsStatus::TR_WON);
else
PlayerManager::trackEvent(race_manager->getTrackName(), AchievementsStatus::TR_FINISHED_ALONE);
if (race_manager->getNumberOfAIKarts() >= 3)
{
PlayerManager::increaseAchievement(AchievementsStatus::ACHIEVE_WON_RACES,1);
@ -673,6 +679,12 @@ void World::terminateRace()
} // for i<kart_amount
} // if isLinearRaceMode
// Increment egg hunt finished count
if (race_manager->isEggHuntMode())
{
PlayerManager::trackEvent(race_manager->getTrackName(), AchievementsStatus::TR_EGG_HUNT_FINISHED);
}
PlayerManager::getCurrentPlayer()->raceFinished();
if (m_race_gui) m_race_gui->clearAllMessages();