summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2006-08-04 07:20:52 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2006-08-04 07:20:52 +0000
commit1ca7028c99319e5dc04cb34c1839b04e3de045f2 (patch)
tree89347a05dd31ede185210b0a48893431250a1a29
parent0c0966322b3018fa5fe3be3f52eef867da6ec779 (diff)
downloadmanaserv-1ca7028c99319e5dc04cb34c1839b04e3de045f2.tar.gz
manaserv-1ca7028c99319e5dc04cb34c1839b04e3de045f2.tar.bz2
manaserv-1ca7028c99319e5dc04cb34c1839b04e3de045f2.tar.xz
manaserv-1ca7028c99319e5dc04cb34c1839b04e3de045f2.zip
Added unique ID to Player class.
-rw-r--r--ChangeLog7
-rw-r--r--src/being.h8
-rw-r--r--src/dalstorage.cpp24
-rw-r--r--src/object.cpp7
-rw-r--r--src/object.h22
5 files changed, 48 insertions, 20 deletions
diff --git a/ChangeLog b/ChangeLog
index 8f412218..b4504c1b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2006-08-04 Guillaume Melquiond <guillaume.melquiond@gmail.com>
+
+ * src/object.h, src/object.cpp, src/being.h, src/dalstorage.cpp: Added
+ the database primary key to Player.
+
2006-08-03 Guillaume Melquiond <guillaume.melquiond@gmail.com>
* src/storage.h, src/dalstorage.h, src/dalstorage.cpp: Removed
@@ -68,10 +73,8 @@
src/utils/singleton.h, src/utils/cipher.cpp, src/utils/logger.cpp,
src/utils/cipher.h, src/utils/stringfilter.h, src/utils/logger.h:
Removed tmwserv namespace.
-
* src/utils/countedptr.h: Removed tmwserv namespace. Added conversion
constructor.
-
* src/accounthandler.cpp, src/account.cpp, src/object.cpp,
src/storage.h, src/dalstorage.cpp, src/account.h, src/object.h,
src/gamehandler.h, src/state.cpp, src/being.cpp, src/gamehandler.cpp,
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<unsigned int> 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 <cassert>
#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)
{}
@@ -59,6 +60,20 @@ class Object
{ 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.
*
* @param x the new 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)
{}
};