From 1ca7028c99319e5dc04cb34c1839b04e3de045f2 Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Fri, 4 Aug 2006 07:20:52 +0000 Subject: Added unique ID to Player class. --- src/being.h | 8 ++++---- src/dalstorage.cpp | 24 +++++++++++++----------- src/object.cpp | 7 +++++++ src/object.h | 22 +++++++++++++++++++--- 4 files changed, 43 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/being.h b/src/being.h index f19df9c0..052d82de 100644 --- a/src/being.h +++ b/src/being.h @@ -82,8 +82,8 @@ class Being: public MovingObject /** * Proxy constructor. */ - Being(int type) - : MovingObject(type) + Being(int type, int id) + : MovingObject(type, id) {} /** @@ -115,8 +115,8 @@ class Player: public Being { public: - Player(std::string const &name) - : Being(OBJECT_PLAYER), + Player(std::string const &name, int id = -1) + : Being(OBJECT_PLAYER, id), mName(name) {} diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index 2b3d8b5e..6950f3d6 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -254,7 +254,7 @@ DALStorage::getAccount(const std::string& userName) unsigned int charRows = charInfo.rows(); for (unsigned int k = 0; k < charRows; ++k) { - PlayerPtr player(new Player(strCharInfo[k][2])); + PlayerPtr player(new Player(strCharInfo[k][2], toUint(strCharInfo[k][0]))); player->setGender((Genders)toUshort(strCharInfo[k][3])); player->setHairStyle(toUshort(strCharInfo[k][4])); player->setHairColor(toUshort(strCharInfo[k][5])); @@ -652,15 +652,10 @@ void DALStorage::flush(AccountPtr const &account) // insert or update the characters. for (Players::const_iterator it = characters.begin(), it_end = characters.end(); it != it_end; ++it) { - // check if the character already exists in the database - // (reminder: the character names are unique in the database). - std::ostringstream sql2; - sql2 << "select id from " << CHARACTERS_TBL_NAME - << " where name = \"" << (*it)->getName() << "\";"; - const RecordSet& charInfo = mDb->execSql(sql2.str()); std::ostringstream sql3; - if (charInfo.rows() == 0) { + if ((*it)->getID() < 0) { + // insert the character sql3 << "insert into " << CHARACTERS_TBL_NAME << " (user_id, name, gender, hair_style, hair_color, level, money," " x, y, map_id, str, agi, vit, int, dex, luck) values (" @@ -680,8 +675,15 @@ void DALStorage::flush(AccountPtr const &account) << (*it)->getRawStat(STAT_INT) << ", " << (*it)->getRawStat(STAT_DEX) << ", " << (*it)->getRawStat(STAT_LUK) << ");"; - } - else { + + // get the character id + std::ostringstream sql2; + sql2 << "select id from " << CHARACTERS_TBL_NAME + << " where name = \"" << (*it)->getName() << "\";"; + RecordSet const &charInfo = mDb->execSql(sql2.str()); + string_to toUint; + (*it)->setID(toUint(charInfo(0, 0))); + } else { sql3 << "update " << CHARACTERS_TBL_NAME << " set name = \"" << (*it)->getName() << "\", " << " gender = " << (*it)->getGender() << ", " @@ -702,7 +704,7 @@ void DALStorage::flush(AccountPtr const &account) #endif << " dex = " << (*it)->getRawStat(STAT_DEX) << ", " << " luck = " << (*it)->getRawStat(STAT_LUK) - << " where id = " << charInfo(0, 0) << ";"; + << " where id = " << (*it)->getID() << ";"; } mDb->execSql(sql3.str()); diff --git a/src/object.cpp b/src/object.cpp index cab2622e..5c03981a 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -20,5 +20,12 @@ * $Id$ */ +#include #include "object.h" + +void Object::setID(int id) +{ + assert(mID < 0); + mID = id; +} diff --git a/src/object.h b/src/object.h index 77705801..7c64719f 100644 --- a/src/object.h +++ b/src/object.h @@ -40,8 +40,9 @@ class Object /** * Constructor. */ - Object(int type) + Object(int type, int id) : mType(type), + mID(id), mNeedUpdate(false) {} @@ -58,6 +59,20 @@ class Object unsigned int getType() const { return mType; } + /** + * Get object ID. + * + * @return the unique ID, a negative number if none yet. + */ + int getID() const + { return mID; } + + /** + * Set object ID. + * The account shall not have any ID yet. + */ + void setID(int id); + /** * Set the x coordinate. * @@ -129,6 +144,7 @@ class Object private: int mType; /**< Object type */ + int mID; /** Object unique ID (wrt its type and its map at least) */ unsigned int mX; /**< x coordinate */ unsigned int mY; /**< y coordinate */ unsigned int mMapId; /**< id of the map being is on */ @@ -146,8 +162,8 @@ class MovingObject: public Object /** * Proxy constructor. */ - MovingObject(int type) - : Object(type) + MovingObject(int type, int id) + : Object(type, id) {} }; -- cgit v1.2.3-70-g09d2