|
From: Markus R. <god...@us...> - 2009-11-12 12:49:27
|
Module: performous
Branch: master
Commit: 8620976dcd614eb3dcec6d59508de7cf092e23de
Author: Markus Raab <un...@ma...>
Date: Wed Nov 11 08:39:09 2009 +0100
preserve id
---
game/player.hh | 4 +++-
game/players.cc | 18 +++++++++++++-----
game/players.hh | 3 ++-
3 files changed, 18 insertions(+), 7 deletions(-)
diff --git a/game/player.hh b/game/player.hh
index 25f0775..6056cc2 100644
--- a/game/player.hh
+++ b/game/player.hh
@@ -65,7 +65,9 @@ struct Player {
Used for Players Management.
*/
struct PlayerItem {
- std::string name; /// unique name, link to highscore
+ int id; /// unique identifier for this PlayerItem, Link to hiscore
+
+ std::string name; /// name displayed and used for searching the player
std::string path; /// a path to a picture shown
std::string picture; /// + the filename for it
/* Future ideas
diff --git a/game/players.cc b/game/players.cc
index 5935dc2..0097c84 100644
--- a/game/players.cc
+++ b/game/players.cc
@@ -3,10 +3,13 @@
#include "fs.hh"
#include "configuration.hh"
+#include <set>
#include <fstream>
#include <iostream>
-#include <set>
+
#include <boost/regex.hpp>
+#include <boost/lexical_cast.hpp>
+
#include <libxml++/libxml++.h>
Players::Players():
@@ -25,7 +28,10 @@ void Players::load(xmlpp::NodeSet n) {
for (xmlpp::NodeSet::const_iterator it = n.begin(); it != n.end(); ++it)
{
xmlpp::Element& element = dynamic_cast<xmlpp::Element&>(**it);
- xmlpp::Attribute* a = element.get_attribute("name");
+ xmlpp::Attribute* a_name = element.get_attribute("name");
+ xmlpp::Attribute* a_id = element.get_attribute("id");
+ int id = -1;
+ try {id = boost::lexical_cast<int>(a_id->get_value());} catch (boost::bad_lexical_cast const&) { }
xmlpp::NodeSet n2 = element.find("picture");
std::string picture;
if (!n2.empty()) // optional picture element
@@ -34,7 +40,7 @@ void Players::load(xmlpp::NodeSet n) {
xmlpp::TextNode* tn = element2.get_child_text();
picture = tn->get_content();
}
- addPlayer(a->get_value(), picture);
+ addPlayer(a_name->get_value(), picture, id);
}
}
@@ -43,6 +49,7 @@ void Players::save(xmlpp::Element *players) {
{
xmlpp::Element* player = players->add_child("player");
player->set_attribute("name", it->name);
+ player->set_attribute("id", boost::lexical_cast<std::string>(it->id));
if (it->picture != "")
{
xmlpp::Element* picture = player->add_child("picture");
@@ -55,15 +62,16 @@ void Players::update() {
if (m_dirty) filter_internal();
}
-void Players::addPlayer (std::string const& name, std::string const& picture) {
+void Players::addPlayer (std::string const& name, std::string const& picture, int id) {
PlayerItem pi;
+ pi.id = id;
pi.name = name;
pi.picture = picture;
pi.path = "";
if (pi.picture != "") // no picture, so don't search path
{
- /* TODO: add again
+ /* TODO: add again check for pictures
ConfigItem::StringList const& sl = config["system/path_pictures"].sl();
typedef std::set<fs::path> dirs;
dirs d;
diff --git a/game/players.hh b/game/players.hh
index 4c226f0..1139603 100644
--- a/game/players.hh
+++ b/game/players.hh
@@ -44,7 +44,8 @@ class Players: boost::noncopyable {
void update();
- void addPlayer (std::string const& name, std::string const& picture = "");
+ /// 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);
/// const array access
PlayerItem operator[](std::size_t pos) const {
|