|
From: Lasse Kärkkäi. <tr...@us...> - 2009-11-14 18:43:48
|
Module: performous
Branch: dance
Commit: 96e393748946aabe6b875ca0108cd0e1ce65e8de
Author: Markus Raab <un...@ma...>
Date: Wed Nov 11 16:29:07 2009 +0100
reached Hiscore
checker if a score is in a new hiscore implemented
warning! contains bugs...
---
game/database.cc | 8 ++++++++
game/database.hh | 1 +
game/hiscore.cc | 19 +++++++++++++++++++
game/hiscore.hh | 12 ++++++++++++
4 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/game/database.cc b/game/database.cc
index 305aeda..5b01f36 100644
--- a/game/database.cc
+++ b/game/database.cc
@@ -76,6 +76,12 @@ void Database::addHiscore (boost::shared_ptr<Song> s) {
m_hiscores.addHiscore(score, playerid, songid);
}
+bool Database::reachedHiscore (boost::shared_ptr<Song> s) {
+ int score = m_players.scores.front();
+ int songid = m_songs.lookup(s);
+ return m_hiscores.reachedHiscore(score, songid);
+}
+
int test() {
Database d("database.xml");
d.addPlayer("Markus", "m.jpg");
@@ -88,6 +94,8 @@ int test() {
d.m_players.m_filtered.push_back(pi);
d.m_players.scores.push_back(5000);
+ if (d.reachedHiscore(s)) std::cout << "Reached a new Hiscore" << std::endl;
+
d.addHiscore(s);
return 0;
diff --git a/game/database.hh b/game/database.hh
index 714234a..2018464 100644
--- a/game/database.hh
+++ b/game/database.hh
@@ -37,6 +37,7 @@ class Database
void addPlayer (std::string const& name, std::string const& picture = "", int id = -1);
void addSong (boost::shared_ptr<Song> s);
void addHiscore (boost::shared_ptr<Song> s);
+ bool reachedHiscore (boost::shared_ptr<Song> s);
friend int test();
diff --git a/game/hiscore.cc b/game/hiscore.cc
index ff28b25..b38e771 100644
--- a/game/hiscore.cc
+++ b/game/hiscore.cc
@@ -10,6 +10,25 @@
Hiscore::Hiscore()
{}
+bool Hiscore::reachedHiscore(int score, int songid, std::string const& track)
+{
+ if (score < 0) throw HiscoreException("Score negativ overflow");
+ if (score > 10000) throw HiscoreException("Score positive overflow");
+
+ if (score < 500) return false; // come on, did you even try to sing?
+
+ int counter = 0;
+ for (hiscore_t::const_reverse_iterator it = m_hiscore.rbegin(); it != m_hiscore.rend(); ++it)
+ {
+ if (it->songid != songid) continue;
+ if (it->track != track) continue;
+ if (score > it->score) return true; // seems like you are in top 3!
+ else ++counter;
+ if (counter == 3) return false; // not in top 3 -> leave
+ }
+ return true; // nothing found for that song -> true
+}
+
void Hiscore::addHiscore(int score, int playerid, int songid, std::string const& track)
{
HiscoreItem hi;
diff --git a/game/hiscore.hh b/game/hiscore.hh
index 00f33c2..030b567 100644
--- a/game/hiscore.hh
+++ b/game/hiscore.hh
@@ -39,6 +39,18 @@ public:
void load(xmlpp::NodeSet const& n);
void save(xmlpp::Element *players);
+ /**Check if you reached a new highscore.
+
+ You must be in TOP 3 of a specific song to enter the highscore list.
+ This is because it will take forever to fill more.
+ And people refuse to enter their names if they are not close to the top.
+
+ @param score is a value between 0 and 10000
+ values below 500 will lead to returning false
+ @return true if the score make it into the top.
+ @return false if addNewHiscore does not make sense
+ for that score.*/
+ bool reachedHiscore(int score, int songid, std::string const& track = "VOCALS");
void addHiscore(int score, int playerid, int songid, std::string const& track = "VOCALS");
private:
typedef std::multiset<HiscoreItem>hiscore_t;
|