Skip to content

Commit 8fbcdc0

Browse files
committed
Fix multiple profiles
Store profile directory in profile as m_sProfileID and use that as key for maps in scoreman
1 parent 23a92e1 commit 8fbcdc0

File tree

7 files changed

+117
-75
lines changed

7 files changed

+117
-75
lines changed

src/Profile.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ void Profile::LoadTypeFromDir(const RString &dir)
871871

872872
void Profile::CalculateStatsFromScores(LoadingWindow* ld) {
873873
LOG->Trace("Calculating stats from scores");
874-
vector<HighScore*> all = SCOREMAN->GetAllScores();
874+
const vector<HighScore*>& all = SCOREMAN->GetAllProfileScores(m_sProfileID);
875875
float TotalGameplaySeconds = 0.f;
876876
m_iTotalTapsAndHolds = 0;
877877
m_iTotalHolds = 0;
@@ -893,14 +893,14 @@ void Profile::CalculateStatsFromScores(LoadingWindow* ld) {
893893
m_iTotalDancePoints = m_iTotalTapsAndHolds * 2;
894894
m_iTotalGameplaySeconds = static_cast<int>(TotalGameplaySeconds);
895895

896-
SCOREMAN->RecalculateSSRs(ld);
897-
SCOREMAN->CalcPlayerRating(m_fPlayerRating, m_fPlayerSkillsets);
896+
SCOREMAN->RecalculateSSRs(ld, m_sProfileID);
897+
SCOREMAN->CalcPlayerRating(m_fPlayerRating, m_fPlayerSkillsets, m_sProfileID);
898898
//SCOREMAN->RatingOverTime();
899899
}
900900

901901
void Profile::CalculateStatsFromScores() {
902902
LOG->Trace("Calculating stats from scores");
903-
vector<HighScore*> all = SCOREMAN->GetAllScores();
903+
const vector<HighScore*> all = SCOREMAN->GetAllProfileScores(m_sProfileID);
904904
float TotalGameplaySeconds = 0.f;
905905
m_iTotalTapsAndHolds = 0;
906906
m_iTotalHolds = 0;
@@ -922,8 +922,8 @@ void Profile::CalculateStatsFromScores() {
922922
m_iTotalDancePoints = m_iTotalTapsAndHolds * 2;
923923
m_iTotalGameplaySeconds = static_cast<int>(TotalGameplaySeconds);
924924

925-
SCOREMAN->RecalculateSSRs(NULL);
926-
SCOREMAN->CalcPlayerRating(m_fPlayerRating, m_fPlayerSkillsets);
925+
SCOREMAN->RecalculateSSRs(NULL, m_sProfileID);
926+
SCOREMAN->CalcPlayerRating(m_fPlayerRating, m_fPlayerSkillsets, m_sProfileID);
927927
}
928928

929929
bool Profile::SaveAllToDir( const RString &sDir, bool bSignData ) const
@@ -1076,7 +1076,7 @@ void Profile::ImportScoresToEtterna() {
10761076
HighScore hs = hsv[i];
10771077
// ignore historic key and just load from here since the hashing function was changed anyway
10781078
hs.SetChartKey(ck);
1079-
SCOREMAN->ImportScore(hs);
1079+
SCOREMAN->ImportScore(hs, m_sProfileID);
10801080
++loaded;
10811081
}
10821082
continue;
@@ -1150,7 +1150,7 @@ void Profile::ImportScoresToEtterna() {
11501150
if (matched) {
11511151
ck = steps->GetChartKey();
11521152
loaded++;
1153-
SCOREMAN->ImportScore(tmp);
1153+
SCOREMAN->ImportScore(tmp, m_sProfileID);
11541154
}
11551155
}
11561156
}
@@ -1176,6 +1176,7 @@ void Profile::ImportScoresToEtterna() {
11761176
}
11771177

11781178

1179+
11791180
// more future goalman stuff
11801181
void Profile::CreateGoal(const string& ck) {
11811182
ScoreGoal goal;

src/Profile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ class Profile
190190
// Editable data
191191
RString m_sDisplayName;
192192
RString m_sCharacterID;
193+
//Dont edit this. Should be unique (Is it?)
194+
RString m_sProfileID;
193195
/**
194196
* @brief The last used name for high scoring purposes.
195197
*

src/ProfileManager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ void ProfileManager::RefreshLocalProfilesFromDisk(LoadingWindow* ld)
331331
{
332332
DirAndProfile derp;
333333
derp.sDir= *id + "/";
334+
derp.profile.m_sProfileID = derp.sDir;
334335
derp.profile.LoadTypeFromDir(derp.sDir);
335336
map<ProfileType, vector<DirAndProfile> >::iterator category=
336337
categorized_profiles.find(derp.profile.m_Type);
@@ -431,6 +432,7 @@ bool ProfileManager::CreateLocalProfile( const RString &sName, RString &sProfile
431432
Profile *pProfile = new Profile;
432433
pProfile->m_sDisplayName = sName;
433434
pProfile->m_sCharacterID = CHARMAN->GetRandomCharacter()->m_sCharacterID;
435+
pProfile->m_sProfileID = profile_id;
434436

435437
// Save it to disk.
436438
RString sProfileDir = LocalProfileIDToDir(profile_id);

src/ScoreManager.cpp

Lines changed: 72 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ScoresAtRate::ScoresAtRate() {
4141
noccPBptr = nullptr;
4242
}
4343

44-
void ScoresAtRate::AddScore(HighScore& hs) {
44+
HighScore* ScoresAtRate::AddScore(HighScore& hs) {
4545
const string& key = hs.GetScoreKey();
4646
bestGrade = min(hs.GetWifeGrade(), bestGrade);
4747
scores.emplace(key, hs);
@@ -56,6 +56,7 @@ void ScoresAtRate::AddScore(HighScore& hs) {
5656

5757
SCOREMAN->RegisterScore(&scores.find(key)->second);
5858
SCOREMAN->AddToKeyedIndex(&scores.find(key)->second);
59+
return &(scores.find(key)->second);
5960
}
6061

6162
vector<string> ScoresAtRate::GetSortedKeys() {
@@ -92,34 +93,55 @@ void ScoreManager::PurgeScores() {
9293
pscores.clear();
9394
}
9495

95-
void ScoreManager::RatingOverTime() {
96+
void ScoreManager::PurgeProfileScores(const string& profileID) {
97+
TopSSRs.clear();
98+
TopSSRs.shrink_to_fit();
99+
for (auto& score : AllProfileScores[profileID]) {
100+
{
101+
auto& it = find(AllScores.begin(), AllScores.end(), score);
102+
if (it != AllScores.end())
103+
AllScores.erase(it);
104+
}
105+
auto& it = ScoresByKey.find(score->GetChartKey());
106+
if (it != ScoresByKey.end())
107+
ScoresByKey.erase(it);
108+
}
109+
AllScores.shrink_to_fit();
110+
AllProfileScores[profileID].clear();
111+
AllProfileScores[profileID].shrink_to_fit();
112+
113+
pscores[profileID].clear();
114+
}
115+
116+
void ScoreManager::RatingOverTime(const string& profileID) {
96117
auto compdate = [](HighScore* a, HighScore* b) { return (a->GetDateTime() < b->GetDateTime()); };
97118

119+
auto& scores = AllProfileScores[profileID];
98120

99121
vector<bool> wasvalid;
100-
sort(AllScores.begin(), AllScores.end(), compdate);
122+
sort(scores.begin(), scores.end(), compdate);
101123

102-
for (auto& n : AllScores) {
124+
for (auto& n : scores) {
103125
wasvalid.push_back(n->GetEtternaValid());
104126
n->SetEtternaValid(false);
105127
}
106128

107129
float doot = 10.f;
108130
float doot2[8];
109131
LOG->Warn("wer");
110-
if (AllScores.empty())
132+
if (scores.empty())
111133
return;
112134

113-
DateTime lastvalidday = AllScores.front()->GetDateTime();
135+
DateTime lastvalidday = AllProfileScores[profileID].front()->GetDateTime();
114136
lastvalidday.StripTime();
115137

116-
CalcPlayerRating(doot, doot2);
138+
CalcPlayerRating(doot, doot2, profileID);
117139
LOG->Warn(lastvalidday.GetString());
118140

119-
DateTime finalvalidday = AllScores.back()->GetDateTime();
141+
DateTime finalvalidday = scores.back()->GetDateTime();
120142
finalvalidday.StripTime();
121143
while (lastvalidday != finalvalidday) {
122-
for (auto& n : AllScores) {
144+
for (auto& n : scores) {
123145
DateTime date = n->GetDateTime();
124146
date.StripTime();
125147

@@ -130,7 +152,7 @@ void ScoreManager::RatingOverTime() {
130152

131153
n->SetEtternaValid(true);
132154
}
133-
CalcPlayerRating(doot, doot2);
155+
CalcPlayerRating(doot, doot2, profileID);
134156
LOG->Trace("%f", doot);
135157
}
136158
}
@@ -156,15 +178,16 @@ HighScore* ScoresForChart::GetPBUpTo(float& rate) {
156178
return NULL;
157179
}
158180

159-
void ScoresForChart::AddScore(HighScore& hs) {
181+
HighScore* ScoresForChart::AddScore(HighScore& hs) {
160182
bestGrade = min(hs.GetWifeGrade(), bestGrade);
161183

162184
float rate = hs.GetMusicRate();
163185
int key = RateToKey(rate);
164-
ScoresByRate[key].AddScore(hs);
186+
auto hsPtr = ScoresByRate[key].AddScore(hs);
165187
// ok let's try this --lurker
166188
SetTopScores();
167-
hs.SetTopScore(ScoresByRate[key].scores[hs.GetScoreKey()].GetTopScore());
189+
hs.SetTopScore(hsPtr->GetTopScore());
190+
return hsPtr;
168191
}
169192

170193
vector<float> ScoresForChart::GetPlayedRates() {
@@ -235,30 +258,32 @@ vector<HighScore*> ScoresForChart::GetAllPBPtrs() {
235258
return o;
236259
}
237260

238-
vector<vector<HighScore*>> ScoreManager::GetAllPBPtrs() {
261+
vector<vector<HighScore*>> ScoreManager::GetAllPBPtrs(const string& profileID ) {
239262
vector<vector<HighScore*>> vec;
240-
FOREACHUM(string, ScoresForChart, pscores, i) {
263+
FOREACHUM(string, ScoresForChart, pscores[profileID], i) {
241264
if (!SONGMAN->IsChartLoaded(i->first))
242265
continue;
243266
vec.emplace_back(i->second.GetAllPBPtrs());
244267
}
245268
return vec;
246269
}
247270

248-
HighScore* ScoreManager::GetChartPBAt(const string& ck, float& rate) {
271+
272+
HighScore* ScoreManager::GetChartPBAt(const string& ck, float& rate, const string& profileID) {
249273
if (pscores.count(ck))
250-
return pscores.at(ck).GetPBAt(rate);
274+
return pscores[profileID].at(ck).GetPBAt(rate);
251275
return NULL;
252276
}
253277

254-
HighScore* ScoreManager::GetChartPBUpTo(const string& ck, float& rate) {
278+
HighScore* ScoreManager::GetChartPBUpTo(const string& ck, float& rate, const string& profileID) {
255279
if (pscores.count(ck))
256-
return pscores.at(ck).GetPBUpTo(rate);
280+
return pscores[profileID].at(ck).GetPBUpTo(rate);
257281
return NULL;
258282
}
259283

260-
void ScoreManager::SetAllTopScores() {
261-
FOREACHUM(string, ScoresForChart, pscores, i) {
284+
285+
void ScoreManager::SetAllTopScores(const string& profileID) {
286+
FOREACHUM(string, ScoresForChart, pscores[profileID], i) {
262287
if (!SONGMAN->IsChartLoaded(i->first))
263288
continue;
264289
i->second.SetTopScores();
@@ -285,24 +310,25 @@ bool ScoresAtRate::HandleNoCCPB(HighScore& hs) {
285310
return false;
286311
}
287312
static const float ld_update = 0.02f;
288-
void ScoreManager::RecalculateSSRs(LoadingWindow *ld) {
313+
void ScoreManager::RecalculateSSRs(LoadingWindow *ld, const string& profileID) {
289314
RageTimer ld_timer;
315+
auto& scores = AllProfileScores[profileID];
290316
if (ld) {
291317
ld_timer.Touch();
292318
ld->SetIndeterminate(false);
293-
ld->SetTotalWork(AllScores.size());
319+
ld->SetTotalWork(scores.size());
294320
ld->SetText("Updating SSR Calculations for Scores...");
295321
}
296322

297323
int scoreindex = 0;
298-
for(size_t i = 0; i < AllScores.size(); ++i) {
324+
for(size_t i = 0; i < scores.size(); ++i) {
299325
if (ld && ld_timer.Ago() > ld_update) {
300326
ld_timer.Touch();
301327
ld->SetProgress(scoreindex);
302328
}
303329
++scoreindex;
304330

305-
HighScore* hs = AllScores[i];
331+
HighScore* hs = scores[i];
306332
if (hs->GetSSRCalcVersion() == GetCalcVersion())
307333
continue;
308334

@@ -371,16 +397,16 @@ void ScoreManager::EnableAllScores() {
371397
return;
372398
}
373399

374-
void ScoreManager::CalcPlayerRating(float& prating, float* pskillsets) {
375-
SetAllTopScores();
400+
void ScoreManager::CalcPlayerRating(float& prating, float* pskillsets, const string& profileID) {
401+
SetAllTopScores(profileID);
376402

377403
vector<float> skillz;
378404
FOREACH_ENUM(Skillset, ss) {
379405
// actually skip overall, and jack stamina for now
380406
if (ss == Skill_Overall)
381407
continue;
382408

383-
SortTopSSRPtrs(ss);
409+
SortTopSSRPtrs(ss, profileID);
384410
pskillsets[ss] = AggregateSSRs(ss, 0.f, 10.24f, 1) * 1.04f;
385411
CLAMP(pskillsets[ss], 0.f, 100.f);
386412
skillz.push_back (pskillsets[ss]);
@@ -411,9 +437,9 @@ float ScoreManager::AggregateSSRs(Skillset ss, float rating, float res, int iter
411437
return AggregateSSRs(ss, rating - res, res / 2.f, iter + 1);
412438
}
413439

414-
void ScoreManager::SortTopSSRPtrs(Skillset ss) {
440+
void ScoreManager::SortTopSSRPtrs(Skillset ss, const string& profileID) {
415441
TopSSRs.clear();
416-
FOREACHUM(string, ScoresForChart, pscores, i) {
442+
FOREACHUM(string, ScoresForChart, pscores[profileID], i) {
417443
if (!SONGMAN->IsChartLoaded(i->first))
418444
continue;
419445
vector<HighScore*> pbs = i->second.GetAllPBPtrs();
@@ -436,18 +462,20 @@ HighScore* ScoreManager::GetTopSSRHighScore(unsigned int rank, int ss) {
436462
return NULL;
437463
}
438464

439-
void ScoreManager::ImportScore(const HighScore& hs_) {
465+
void ScoreManager::ImportScore(const HighScore& hs_, const string& profileID) {
440466
HighScore hs = hs_;
441467

442468
// don't import duplicated scores
443469
// this may have strange ramifications - mina
444470
// actually i'll just disable this for the time being and give myself time to test it later
445471
//if(!ScoresByKey.count(hs.GetScoreKey()))
446472

447-
pscores[hs.GetChartKey()].AddScore(hs);
473+
RegisterScoreInProfile(pscores[profileID][hs.GetChartKey()].AddScore(hs), profileID);
448474
}
449475

450-
476+
void ScoreManager::RegisterScoreInProfile(HighScore* hs_, const string& profileID) {
477+
AllProfileScores[profileID].emplace_back(hs_);
478+
}
451479

452480

453481

@@ -497,9 +525,9 @@ XNode * ScoresForChart::CreateNode(const string& ck) const {
497525
return o;
498526
}
499527

500-
XNode * ScoreManager::CreateNode() const {
528+
XNode * ScoreManager::CreateNode(const string& profileID) const {
501529
XNode* o = new XNode("PlayerScores");
502-
FOREACHUM_CONST(string, ScoresForChart, pscores, ch) {
530+
FOREACHUM_CONST(string, ScoresForChart, pscores.find(profileID)->second, ch) {
503531
auto node = ch->second.CreateNode(ch->first);
504532
if (!node->ChildrenEmpty())
505533
o->AppendChild(node);
@@ -510,7 +538,7 @@ XNode * ScoreManager::CreateNode() const {
510538

511539

512540
// Read scores from xml
513-
void ScoresAtRate::LoadFromNode(const XNode* node, const string& ck, const float& rate) {
541+
void ScoresAtRate::LoadFromNode(const XNode* node, const string& ck, const float& rate, const string& profileID) {
514542
RString sk;
515543
FOREACH_CONST_Child(node, p) {
516544
p->GetAttrValue("Key", sk);
@@ -537,10 +565,11 @@ void ScoresAtRate::LoadFromNode(const XNode* node, const string& ck, const float
537565
// Very awkward, need to figure this out better so there isn't unnecessary redundancy between loading and adding
538566
SCOREMAN->RegisterScore(&scores.find(sk)->second);
539567
SCOREMAN->AddToKeyedIndex(&scores.find(sk)->second);
568+
SCOREMAN->RegisterScoreInProfile(&scores.find(sk)->second, profileID);
540569
}
541570
}
542571

543-
void ScoresForChart::LoadFromNode(const XNode* node, const string& ck) {
572+
void ScoresForChart::LoadFromNode(const XNode* node, const string& ck, const string& profileID) {
544573
RString rs = "";
545574
int rate;
546575

@@ -558,19 +587,19 @@ void ScoresForChart::LoadFromNode(const XNode* node, const string& ck) {
558587
ASSERT(p->GetName() == "ScoresAt");
559588
p->GetAttrValue("Rate", rs);
560589
rate = 10 * StringToInt(rs.substr(0, 1) + rs.substr(2, 4));
561-
ScoresByRate[rate].LoadFromNode(p, ck, KeyToRate(rate));
590+
ScoresByRate[rate].LoadFromNode(p, ck, KeyToRate(rate), profileID);
562591
bestGrade = min(ScoresByRate[rate].bestGrade, bestGrade);
563592
}
564593
}
565594

566-
void ScoreManager::LoadFromNode(const XNode * node) {
595+
void ScoreManager::LoadFromNode(const XNode * node, const string& profileID) {
567596
FOREACH_CONST_Child(node, p) {
568597
//ASSERT(p->GetName() == "Chart");
569598
RString tmp;
570599
p->GetAttrValue("Key", tmp);
571600
string doot = SONGMAN->ReconcileBustedKeys(tmp);
572601
const string ck = doot;
573-
pscores[ck].LoadFromNode(p, ck);
602+
pscores[profileID][ck].LoadFromNode(p, ck, profileID);
574603
}
575604
}
576605

@@ -583,9 +612,9 @@ ScoresAtRate* ScoresForChart::GetScoresAtRate(const int& rate) {
583612
return NULL;
584613
}
585614

586-
ScoresForChart* ScoreManager::GetScoresForChart(const string& ck) {
587-
auto it = pscores.find(ck);
588-
if (it != pscores.end())
615+
ScoresForChart* ScoreManager::GetScoresForChart(const string& ck, const string& profileID) {
616+
auto it = (pscores[profileID]).find(ck);
617+
if (it != (pscores[profileID]).end())
589618
return &it->second;
590619
return NULL;
591620
}

0 commit comments

Comments
 (0)