Skip to content

Commit

Permalink
Fix garbage highscore pointers and ranking reloading
Browse files Browse the repository at this point in the history
Replaced the vector with highscore pointers to 2 vectors(one with pointers to highscore vectors, and another with unsigned ints which are the indexes of the vectors to which the pointers in the first one point) to make the ranking still work after new highscores are added instead of returning garbage.
  • Loading branch information
nico-abram authored Jan 17, 2017
1 parent e9f6ab6 commit 3f1b927
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 49 deletions.
136 changes: 88 additions & 48 deletions src/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2232,12 +2232,28 @@ SongID Profile::GetTopSSRSongID(unsigned int rank, int skillset) {
HighScore* Profile::GetTopSSRHighScore(unsigned int rank, int skillset) {
if (rank <= 0)
rank = 1;
if (rank > static_cast<unsigned int>(topSSRHighScores[skillset].size()))
if (rank > static_cast<unsigned int>(topSSRHighScoreLists[skillset].size()))
if (CalcAllTopSSRs(rank) == false)
return NULL;

if(skillset >= 0 && skillset < NUM_Skillset)
return topSSRHighScores[skillset][rank - 1];
if (skillset >= 0 && skillset < NUM_Skillset) {
if (topSSRHighScoreIndexs[skillset][rank - 1] > 0)
return &((*topSSRHighScoreLists[skillset][rank - 1])[topSSRHighScoreIndexs[skillset][rank - 1] - 1]);
else
return NULL;
/*
if (topSSRHighScores[skillset][rank - 1] != NULL)
if (!topSSRHighScores[skillset][rank - 1]->IsEmpty())
return topSSRHighScores[skillset][rank - 1];
else
if (CalcAllTopSSRs(rank) == false)
return NULL;
else
return topSSRHighScores[skillset][rank - 1];
else
return NULL;
*/
}
//Undefined skillset returns an empty pointer(NULL)

return NULL;
Expand All @@ -2256,8 +2272,10 @@ bool Profile::CalcTopSSRs(unsigned int qty, int skillset) {

vector<float> topSSRs; //Auxiliary vector to sort faster

//Pointers to the skillset's vectors
vector<HighScore*> *topSSRHighScoresPtr = &topSSRHighScores[skillset];
//Pointers to the skillset's vectors
//vector<HighScore*> *topSSRHighScoresPtr = &topSSRHighScores[skillset];
vector<vector<HighScore>*> *topSSRHighScoreListsPtr = &topSSRHighScoreLists[skillset];
vector<unsigned int> *topSSRHighScoreIndexsPtr = &topSSRHighScoreIndexs[skillset];
vector<StepsID> *topSSRStepIdsPtr = &topSSRStepIds[skillset];
vector<SongID> *topSSRSongIdsPtr = &topSSRSongIds[skillset];

Expand All @@ -2270,9 +2288,12 @@ bool Profile::CalcTopSSRs(unsigned int qty, int skillset) {
StepsID emptySteps;
SongID emptySong;
HighScore* emptyHighScorePtr = NULL;
vector<HighScore>* emptyHighScoreListsPtr = NULL;
(*topSSRStepIdsPtr).clear();
(*topSSRSongIdsPtr).clear();
(*topSSRHighScoresPtr).clear();
//(*topSSRHighScoresPtr).clear();
(*topSSRHighScoreListsPtr).clear();
(*topSSRHighScoreIndexsPtr).clear();

//Empty the vectors if qty=0
if (qty == 0)
Expand All @@ -2282,14 +2303,21 @@ bool Profile::CalcTopSSRs(unsigned int qty, int skillset) {
topSSRs.push_back(0);
(*topSSRStepIdsPtr).emplace_back(emptySteps);
(*topSSRSongIdsPtr).emplace_back(emptySong);
(*topSSRHighScoresPtr).emplace_back(emptyHighScorePtr);
//(*topSSRHighScoresPtr).emplace_back(emptyHighScorePtr);
(*topSSRHighScoreListsPtr).emplace_back(emptyHighScoreListsPtr);
(*topSSRHighScoreIndexsPtr).emplace_back(0);
}

struct info {
float ssr;
unsigned int pos;
};
info temp[40];
for (int i = 0;i < 40;i++) {
temp[i].ssr = 0;
temp[i].pos = 0;
}

bool replaced = false;

//Build the top
Expand Down Expand Up @@ -2325,13 +2353,18 @@ bool Profile::CalcTopSSRs(unsigned int qty, int skillset) {

if ((temp[rate - 1]).ssr != 0)
replaced = true;
else
replaced = false;


if (replaced) {
(*topSSRStepIdsPtr).erase((*topSSRStepIdsPtr).begin() + temp[rate - 1].pos);
topSSRs.erase(topSSRs.begin() + temp[rate - 1].pos);
(*topSSRSongIdsPtr).erase((*topSSRSongIdsPtr).begin() + temp[rate - 1].pos);
(*topSSRHighScoresPtr).erase((*topSSRHighScoresPtr).begin() + temp[rate - 1].pos);
qty--;
//(*topSSRHighScoresPtr).erase((*topSSRHighScoresPtr).begin() + temp[rate - 1].pos);
(*topSSRHighScoreListsPtr).erase((*topSSRHighScoreListsPtr).begin() + temp[rate - 1].pos);
(*topSSRHighScoreIndexsPtr).erase((*topSSRHighScoreIndexsPtr).begin() + temp[rate - 1].pos);
//qty--;
}


Expand All @@ -2347,7 +2380,9 @@ bool Profile::CalcTopSSRs(unsigned int qty, int skillset) {
(*topSSRStepIdsPtr).insert((*topSSRStepIdsPtr).begin() + poscounter, stepsid);
topSSRs.insert(topSSRs.begin() + poscounter, ssr);
(*topSSRSongIdsPtr).insert((*topSSRSongIdsPtr).begin() + poscounter, id);
(*topSSRHighScoresPtr).insert((*topSSRHighScoresPtr).begin() + poscounter, &(hsv[i]));
//(*topSSRHighScoresPtr).insert((*topSSRHighScoresPtr).begin() + poscounter, &(hsv[i]));
(*topSSRHighScoreListsPtr).insert((*topSSRHighScoreListsPtr).begin() + poscounter, &hsv);
(*topSSRHighScoreIndexsPtr).insert((*topSSRHighScoreIndexsPtr).begin() + poscounter, i+1);


//erase last element to keep the same amount of elements(qty)
Expand All @@ -2356,10 +2391,12 @@ bool Profile::CalcTopSSRs(unsigned int qty, int skillset) {
topSSRs.pop_back();
(*topSSRStepIdsPtr).pop_back();
(*topSSRSongIdsPtr).pop_back();
(*topSSRHighScoresPtr).pop_back();
//(*topSSRHighScoresPtr).pop_back();
(*topSSRHighScoreListsPtr).pop_back();
(*topSSRHighScoreIndexsPtr).pop_back();
}
else
qty++;
//else
//qty++;
}
}
}
Expand All @@ -2371,76 +2408,71 @@ bool Profile::CalcTopSSRs(unsigned int qty, int skillset) {
return false;
}

//Todo: Make this not recalc everything -Nick12
//What's below the return currently crashes
void Profile::TopSSRsAddNewScore(HighScore *hs, StepsID stepsid, SongID songid) {
for (int skillset = 0; skillset < NUM_Skillset; skillset++) {//undefined skillset
if (skillset < 0 || skillset >= NUM_Skillset)
continue;

if (!songid.IsValid())
continue;
void Profile::TopSSRsAddNewScore(HighScore *hs, StepsID stepsid, SongID songid) {
if (!songid.IsValid())
return;

if (!stepsid.IsValid() || stepsid.GetStepsType() != StepsType_dance_single)
continue;
if (!stepsid.IsValid() || stepsid.GetStepsType() != StepsType_dance_single)
return;

Steps* psteps = stepsid.ToSteps(songid.ToSong(), true);
if (!psteps)
continue;
Steps* psteps = stepsid.ToSteps(songid.ToSong(), true);
if (!psteps)
return;

for (int skillset = 0; skillset < NUM_Skillset; skillset++) {

vector<float> topSSRs; //Auxiliary vector to sort faster

//Pointers to the skillset's vectors
vector<HighScore*> *topSSRHighScoresPtr = &topSSRHighScores[skillset];
vector<unsigned int> *topSSRHighScoreIndexsPtr = &topSSRHighScoreIndexs[skillset];
vector<vector<HighScore>*> *topSSRHighScoreListsPtr = &topSSRHighScoreLists[skillset];
vector<StepsID> *topSSRStepIdsPtr = &topSSRStepIds[skillset];
vector<SongID> *topSSRSongIdsPtr = &topSSRSongIds[skillset];

unsigned int poscounter;

int qty = (*topSSRHighScoresPtr).size();
unsigned int qty = (*topSSRSongIdsPtr).size();
if (qty == 0)
continue;

float ssr = hs->GetSkillsetSSR(static_cast<Skillset>(skillset));
if (ssr == 0)
return;

for (unsigned int i = 0; i < qty; i++) {
if ((*topSSRHighScoresPtr)[i] != NULL)
topSSRs.push_back((*topSSRHighScoresPtr)[i]->GetSkillsetSSR(static_cast<Skillset>(skillset)));
if ((*topSSRHighScoreIndexsPtr)[i] != 0)
topSSRs.push_back((*topSSRHighScoreLists[skillset][i])[topSSRHighScoreIndexs[skillset][i] - 1].GetSkillsetSSR(static_cast<Skillset>(skillset)));
else
topSSRs.push_back(0);
}

float ssr = hs->GetSkillsetSSR(static_cast<Skillset>(skillset));

//Compare with the smallest value(last one) to see if we need to change the values
if (topSSRs[qty - 1] < ssr) {

//This isnt finished so we just recalc it all for the moment when we find it needs to
//
for (unsigned int i = 0; i < qty; i++) {
if ((*topSSRSongIdsPtr)[i] == songid && (*topSSRStepIdsPtr)[i] == stepsid &&
(*topSSRHighScoresPtr)[i]->GetMusicRate() == hs->GetMusicRate() &&
(*topSSRHighScoresPtr)[i] != NULL) {
if (topSSRs[i] >= ssr)
return;
}
}

//Screw it lets just try always recalculating to see if this works at the very least
//Todo:Make this not recalc all the time -Nick12
CalcAllTopSSRs(qty);
return;
//


//Check for duplicates
bool replace = false;
bool matches = false;
for (unsigned int i = 0; i < qty; i++) {
if ((*topSSRSongIdsPtr)[i] == songid && (*topSSRStepIdsPtr)[i] == stepsid &&
(*topSSRHighScoresPtr)[i]->GetMusicRate() == hs->GetMusicRate() &&
(*topSSRHighScoresPtr)[i] != NULL) {
(*topSSRHighScoreListsPtr)[i] != 0 &&
(*topSSRHighScoreLists[skillset][i])[topSSRHighScoreIndexs[skillset][i] - 1].GetMusicRate() == hs->GetMusicRate() ) {
matches = true;
if (topSSRs[i] < ssr) {
(*topSSRStepIdsPtr).erase((*topSSRStepIdsPtr).begin() + i);
topSSRs.erase(topSSRs.begin() + i);
(*topSSRSongIdsPtr).erase((*topSSRSongIdsPtr).begin() + i);
(*topSSRHighScoresPtr).erase((*topSSRHighScoresPtr).begin() + i);
//(*topSSRHighScoresPtr).erase((*topSSRHighScoresPtr).begin() + i);
(*topSSRHighScoreListsPtr).erase((*topSSRHighScoreListsPtr).begin() + i);
(*topSSRHighScoreIndexsPtr).erase((*topSSRHighScoreIndexsPtr).begin() + i);
qty--;
replace = true;
}
Expand All @@ -2462,15 +2494,18 @@ void Profile::TopSSRsAddNewScore(HighScore *hs, StepsID stepsid, SongID songid)
(*topSSRStepIdsPtr).insert((*topSSRStepIdsPtr).begin() + poscounter, stepsid);
topSSRs.insert(topSSRs.begin() + poscounter, ssr);
(*topSSRSongIdsPtr).insert((*topSSRSongIdsPtr).begin() + poscounter, songid);
(*topSSRHighScoresPtr).insert((*topSSRHighScoresPtr).begin() + poscounter, hs);
//(*topSSRHighScoresPtr).insert((*topSSRHighScoresPtr).begin() + poscounter, hs);
//I have no idea how to get the HighscoreForASteps and the index from the hs i got

//erase last element to keep the same amount of elements(qty)
if (!replace) {
topSSRs.pop_back();
(*topSSRStepIdsPtr).pop_back();
(*topSSRSongIdsPtr).pop_back();
(*topSSRHighScoresPtr).pop_back();
(*topSSRHighScoreListsPtr).pop_back();
(*topSSRHighScoreIndexsPtr).pop_back();
}

}

}
Expand Down Expand Up @@ -3256,6 +3291,10 @@ class LunaProfile : public Luna<Profile>
id.ToSong()->PushSelf(L);
return 1;
}
static int RecalcTopSSR(T* p, lua_State *L) {
p->CalcAllTopSSRs(p->topSSRHighScoreLists[0].size());
return 1;
}

DEFINE_METHOD( GetGUID, m_sGuid );

Expand Down Expand Up @@ -3335,7 +3374,8 @@ class LunaProfile : public Luna<Profile>
ADD_METHOD( GetTopSSRValue );
ADD_METHOD( GetTopSSRSongName );
ADD_METHOD( GetSongFromSSR );
ADD_METHOD( GetTopSSRHighScore );
ADD_METHOD(GetTopSSRHighScore);
ADD_METHOD(RecalcTopSSR);
}
};

Expand Down
4 changes: 3 additions & 1 deletion src/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ class Profile
RString GetTopSSRSongName(unsigned int rank, int skillset);
//Vector array instead of a bunch of vectors
vector<SongID> topSSRSongIds[NUM_Skillset];
vector<HighScore*> topSSRHighScores[NUM_Skillset];
//vector<HighScore*> topSSRHighScores[NUM_Skillset];
vector<vector<HighScore>*> topSSRHighScoreLists[NUM_Skillset];
vector<unsigned int> topSSRHighScoreIndexs[NUM_Skillset];//we work with index+1 so 0 is undefined
vector<StepsID> topSSRStepIds[NUM_Skillset];

private:
Expand Down

0 comments on commit 3f1b927

Please sign in to comment.