|
From: Lasse Kärkkäi. <tr...@us...> - 2009-11-14 18:43:18
|
Module: performous
Branch: dance
Commit: c14d2035ec6bdb1ba52571502561b738ba00fef6
Author: Markus Raab <un...@ma...>
Date: Wed Nov 11 09:01:07 2009 +0100
players now have a unique id
use set for players to ensure unique id and for ordering
assignment of new ids implemented
empty set
---
game/player.hh | 9 +++++++++
game/players.cc | 27 +++++++++++++++++++--------
game/players.hh | 9 +++++++--
3 files changed, 35 insertions(+), 10 deletions(-)
diff --git a/game/player.hh b/game/player.hh
index 6056cc2..ae808ea 100644
--- a/game/player.hh
+++ b/game/player.hh
@@ -75,6 +75,15 @@ struct PlayerItem {
std::map<std::string, int> scores; /// map between a Song and the highest score the Player achieved
*/
+ /**For insertion in set.
+ Provides ordering and ensures id is unique.*/
+ bool operator< (PlayerItem const& pi) const
+ {
+ return id < pi.id;
+ }
+
+ /**Checks if a player has the same name.
+ Used to find a PlayerItem with the same name.*/
bool operator== (PlayerItem const& pi) const
{
return name == pi.name;
diff --git a/game/players.cc b/game/players.cc
index 0097c84..fab2c76 100644
--- a/game/players.cc
+++ b/game/players.cc
@@ -69,6 +69,9 @@ void Players::addPlayer (std::string const& name, std::string const& picture, in
pi.picture = picture;
pi.path = "";
+
+ if (pi.id == -1) pi.id = assign_id_internal();
+
if (pi.picture != "") // no picture, so don't search path
{
/* TODO: add again check for pictures
@@ -87,12 +90,13 @@ void Players::addPlayer (std::string const& name, std::string const& picture, in
*/
}
-
- players_t::const_iterator it = std::find(m_players.begin(), m_players.end(), pi);
- if (it != m_players.end()) return; // dont do anything, player exists
-
m_dirty = true;
- m_players.push_back(pi);
+ std::pair<players_t::iterator, bool> ret = m_players.insert(pi);
+ if (!ret.second)
+ {
+ pi.id = assign_id_internal();
+ m_players.insert(pi); // now do the insert with the fresh id
+ }
}
void Players::setFilter(std::string const& val) {
@@ -101,25 +105,32 @@ void Players::setFilter(std::string const& val) {
filter_internal();
}
+int Players::assign_id_internal()
+{
+ players_t::const_reverse_iterator it = m_players.rbegin();
+ if (it != m_players.rend()) return it->id+1;
+ else return 1; // empty set
+}
+
void Players::filter_internal() {
m_dirty = false;
PlayerItem selection = current();
try {
- players_t filtered;
+ fplayers_t filtered;
for (players_t::const_iterator it = m_players.begin(); it != m_players.end(); ++it) {
if (regex_search(it->name, boost::regex(m_filter, boost::regex_constants::icase))) filtered.push_back(*it);
}
m_filtered.swap(filtered);
} catch (...) {
- players_t(m_players.begin(), m_players.end()).swap(m_filtered); // Invalid regex => copy everything
+ fplayers_t(m_players.begin(), m_players.end()).swap(m_filtered); // Invalid regex => copy everything
}
math_cover.reset();
// Restore old selection
int pos = 0;
if (selection.name != "") {
- players_t::iterator it = std::find(m_filtered.begin(), m_filtered.end(), selection);
+ fplayers_t::iterator it = std::find(m_filtered.begin(), m_filtered.end(), selection);
math_cover.setTarget(0, 0);
if (it != m_filtered.end()) pos = it - m_filtered.begin();
}
diff --git a/game/players.hh b/game/players.hh
index 1139603..5350b6f 100644
--- a/game/players.hh
+++ b/game/players.hh
@@ -1,5 +1,7 @@
#pragma once
+#include <set>
+#include <list>
#include <vector>
#include <string>
@@ -18,13 +20,14 @@ namespace xmlpp { class Node; class Element; typedef std::vector<Node*>NodeSet;
be retrieved with Engine::getPlayers().*/
class Players: boost::noncopyable {
private:
- typedef std::vector<PlayerItem> players_t;
+ typedef std::set<PlayerItem> players_t;
+ typedef std::vector<PlayerItem> fplayers_t;
typedef std::list<Player> cur_players_t;
typedef std::list<int> cur_scores_t;
private:
players_t m_players;
- players_t m_filtered;
+ fplayers_t m_filtered;
std::string m_filter;
AnimAcceleration math_cover;
@@ -44,6 +47,7 @@ class Players: boost::noncopyable {
void update();
+
/// add a player with a displayed name and an optional picture; if no id is given one will be assigned
void addPlayer (std::string const& name, std::string const& picture = "", int id = -1);
@@ -80,5 +84,6 @@ class Players: boost::noncopyable {
/// filters playerlist by regular expression
void setFilter(std::string const& regex);
private:
+ int assign_id_internal(); /// returns the next available id
void filter_internal();
};
|