summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog13
-rw-r--r--src/account-server/accounthandler.cpp61
-rw-r--r--src/account-server/accounthandler.hpp4
-rw-r--r--src/account-server/characterdata.cpp2
-rw-r--r--src/account-server/characterdata.hpp9
-rw-r--r--src/account-server/dalstorage.cpp8
-rw-r--r--src/common/inventorydata.hpp6
-rw-r--r--src/game-server/buysell.cpp8
-rw-r--r--src/game-server/character.cpp2
-rw-r--r--src/game-server/character.hpp11
-rw-r--r--src/game-server/inventory.cpp23
-rw-r--r--src/game-server/inventory.hpp6
-rw-r--r--src/serialize/characterdata.hpp4
13 files changed, 82 insertions, 75 deletions
diff --git a/ChangeLog b/ChangeLog
index 2f7c65da..11d36560 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2007-08-14 Guillaume Melquiond <guillaume.melquiond@gmail.com>
+
+ * src/serialize/characterdata.hpp, src/common/inventorydata.hpp,
+ src/account-server/accounthandler.hpp, src/game-server/character.cpp,
+ src/account-server/dalstorage.cpp, src/game-server/character.hpp,
+ src/account-server/characterdata.cpp,
+ src/account-server/accounthandler.cpp,
+ src/account-server/characterdata.hpp: Handled money as part of the
+ inventory.
+ * src/game-server/inventory.hpp, src/game-server/inventory.cpp,
+ src/game-server/buysell.cpp: Added money accessor to the inventory
+ manager.
+
2007-08-13 Eugenio Favalli <elvenprogrammer@gmail.com>
* accountserver.cbp, gameserver.cbp: Updated project files and added
diff --git a/src/account-server/accounthandler.cpp b/src/account-server/accounthandler.cpp
index 3c1d61a6..5c553afa 100644
--- a/src/account-server/accounthandler.cpp
+++ b/src/account-server/accounthandler.cpp
@@ -149,6 +149,25 @@ AccountHandler::processMessage(NetComputer *comp, MessageIn &message)
}
}
+void AccountHandler::sendCharacterData(AccountClient &computer, int slot, CharacterData const &ch)
+{
+ MessageOut charInfo(APMSG_CHAR_INFO);
+ charInfo.writeByte(slot);
+ charInfo.writeString(ch.getName());
+ charInfo.writeByte((int)ch.getGender());
+ charInfo.writeByte(ch.getHairStyle());
+ charInfo.writeByte(ch.getHairColor());
+ charInfo.writeByte(ch.getLevel());
+ charInfo.writeLong(ch.getPossessions().money);
+
+ for (int j = 0; j < NB_BASE_ATTRIBUTES; ++j)
+ {
+ charInfo.writeShort(ch.getBaseAttribute(j));
+ }
+
+ computer.send(charInfo);
+}
+
void
AccountHandler::handleLoginMessage(AccountClient &computer, MessageIn &msg)
{
@@ -211,19 +230,7 @@ AccountHandler::handleLoginMessage(AccountClient &computer, MessageIn &msg)
// Send characters list
for (unsigned int i = 0; i < chars.size(); i++)
{
- MessageOut charInfo(APMSG_CHAR_INFO);
- charInfo.writeByte(i); // Slot
- charInfo.writeString(chars[i]->getName());
- charInfo.writeByte((unsigned char) chars[i]->getGender());
- charInfo.writeByte(chars[i]->getHairStyle());
- charInfo.writeByte(chars[i]->getHairColor());
- charInfo.writeByte(chars[i]->getLevel());
- charInfo.writeLong(chars[i]->getMoney());
-
- for (int j = 0; j < NB_BASE_ATTRIBUTES; ++j)
- charInfo.writeShort(chars[i]->getBaseAttribute(j));
-
- computer.send(charInfo);
+ sendCharacterData(computer, i, *chars[i]);
}
return;
}
@@ -582,7 +589,6 @@ AccountHandler::handleCharacterCreateMessage(AccountClient &computer,
CharacterPtr newCharacter(new CharacterData(name));
for (int i = 0; i < NB_BASE_ATTRIBUTES; ++i)
newCharacter->setBaseAttribute(i, attributes[i]);
- newCharacter->setMoney(0);
newCharacter->setLevel(1);
newCharacter->setGender(gender);
newCharacter->setHairStyle(hairStyle);
@@ -601,18 +607,8 @@ AccountHandler::handleCharacterCreateMessage(AccountClient &computer,
computer.send(reply);
// Send new characters infos back to client
- MessageOut charInfo(APMSG_CHAR_INFO);
int slot = chars.size() - 1;
- charInfo.writeByte(slot);
- charInfo.writeString(chars[slot]->getName());
- charInfo.writeByte((unsigned char) chars[slot]->getGender());
- charInfo.writeByte(chars[slot]->getHairStyle());
- charInfo.writeByte(chars[slot]->getHairColor());
- charInfo.writeByte(chars[slot]->getLevel());
- charInfo.writeShort(chars[slot]->getMoney());
- for (int j = 0; j < NB_BASE_ATTRIBUTES; ++j)
- charInfo.writeShort(chars[slot]->getBaseAttribute(j));
- computer.send(charInfo);
+ sendCharacterData(computer, slot, *chars[slot]);
return;
}
}
@@ -745,20 +741,7 @@ AccountHandler::tokenMatched(AccountClient *computer, int accountID)
// Send characters list
for (unsigned int i = 0; i < chars.size(); i++)
{
- MessageOut charInfo(APMSG_CHAR_INFO);
- charInfo.writeByte(i); // Slot
- charInfo.writeString(chars[i]->getName());
- charInfo.writeByte((unsigned char) chars[i]->getGender());
- charInfo.writeByte(chars[i]->getHairStyle());
- charInfo.writeByte(chars[i]->getHairColor());
- charInfo.writeByte(chars[i]->getLevel());
- charInfo.writeShort(chars[i]->getMoney());
-
- for (int j = 0; j < NB_BASE_ATTRIBUTES; ++j)
- {
- charInfo.writeShort(chars[i]->getBaseAttribute(j));
- }
- computer->send(charInfo);
+ sendCharacterData(*computer, i, *chars[i]);
}
}
diff --git a/src/account-server/accounthandler.hpp b/src/account-server/accounthandler.hpp
index cb4c266b..61e81103 100644
--- a/src/account-server/accounthandler.hpp
+++ b/src/account-server/accounthandler.hpp
@@ -93,7 +93,9 @@ class AccountHandler : public ConnectionHandler
void
computerDisconnected(NetComputer *comp);
- // --- message handling ---
+ private:
+
+ void sendCharacterData(AccountClient &, int, CharacterData const &);
void
handleLoginMessage(AccountClient &computer, MessageIn &msg);
diff --git a/src/account-server/characterdata.cpp b/src/account-server/characterdata.cpp
index 79fcce34..0bf79ff0 100644
--- a/src/account-server/characterdata.cpp
+++ b/src/account-server/characterdata.cpp
@@ -24,7 +24,7 @@
CharacterData::CharacterData(std::string const &name, int id):
mDatabaseID(id), mAccountID(-1), mName(name), mGender(0), mHairStyle(0),
- mHairColor(0), mLevel(0), mMoney(0), mMapId(0), mPos(0,0)
+ mHairColor(0), mLevel(0), mMapId(0), mPos(0,0)
{
for (int i = 0; i < NB_BASE_ATTRIBUTES; ++i)
{
diff --git a/src/account-server/characterdata.hpp b/src/account-server/characterdata.hpp
index 0dc53ba3..1655082d 100644
--- a/src/account-server/characterdata.hpp
+++ b/src/account-server/characterdata.hpp
@@ -99,14 +99,6 @@ class CharacterData
void
setLevel(int level) { mLevel = level; }
- /** Gets the amount of money the character has. */
- int
- getMoney() const { return mMoney; }
-
- /** Sets the amount of money the character has. */
- void
- setMoney(int amount) { mMoney = amount; }
-
/** Gets the value of a base attribute of the character. */
unsigned short
getBaseAttribute(int attributeNumber) const
@@ -165,7 +157,6 @@ class CharacterData
unsigned char mHairStyle; //!< Hair Style of the being.
unsigned char mHairColor; //!< Hair Color of the being.
unsigned char mLevel; //!< Level of the being.
- unsigned int mMoney; //!< Wealth of the being.
unsigned short mBaseAttributes[NB_BASE_ATTRIBUTES]; //!< The attributes of the
//!< character.
unsigned short mMapId; //!< Map the being is on.
diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp
index 7f46f7bd..17bfa386 100644
--- a/src/account-server/dalstorage.cpp
+++ b/src/account-server/dalstorage.cpp
@@ -354,7 +354,7 @@ CharacterPtr DALStorage::getCharacterBySQL(std::string const &query)
character->setHairStyle(toUshort(charInfo(0, 4)));
character->setHairColor(toUshort(charInfo(0, 5)));
character->setLevel(toUshort(charInfo(0, 6)));
- character->setMoney(toUint(charInfo(0, 7)));
+ character->getPossessions().money = toUint(charInfo(0, 7));
Point pos(toUshort(charInfo(0, 8)), toUshort(charInfo(0, 9)));
character->setPosition(pos);
for (int i = 0; i < NB_BASE_ATTRIBUTES; ++i)
@@ -581,7 +581,7 @@ DALStorage::updateCharacter(CharacterPtr character)
<< "', "
<< "level = '" << character->getLevel()
<< "', "
- << "money = '" << character->getMoney()
+ << "money = '" << character->getPossessions().money
<< "', "
<< "x = '" << character->getPosition().x
<< "', "
@@ -920,7 +920,7 @@ void DALStorage::flush(AccountPtr const &account)
<< (int)(*it)->getHairStyle() << ", "
<< (int)(*it)->getHairColor() << ", "
<< (int)(*it)->getLevel() << ", "
- << (*it)->getMoney() << ", "
+ << (*it)->getPossessions().money << ", "
<< (*it)->getPosition().x << ", "
<< (*it)->getPosition().y << ", "
<< (*it)->getMapId() << ", "
@@ -942,7 +942,7 @@ void DALStorage::flush(AccountPtr const &account)
<< " hair_style = " << (int)(*it)->getHairStyle() << ", "
<< " hair_color = " << (int)(*it)->getHairColor() << ", "
<< " level = " << (int)(*it)->getLevel() << ", "
- << " money = " << (*it)->getMoney() << ", "
+ << " money = " << (*it)->getPossessions().money << ", "
<< " x = " << (*it)->getPosition().x << ", "
<< " y = " << (*it)->getPosition().y << ", "
<< " map_id = " << (*it)->getMapId() << ", "
diff --git a/src/common/inventorydata.hpp b/src/common/inventorydata.hpp
index 88bb7086..11c15a3d 100644
--- a/src/common/inventorydata.hpp
+++ b/src/common/inventorydata.hpp
@@ -51,9 +51,11 @@ struct InventoryItem
*/
struct Possessions
{
- unsigned short equipment[EQUIPMENT_SLOTS];
std::vector< InventoryItem > inventory;
- Possessions() { for (int i = 0; i < EQUIPMENT_SLOTS; ++i) equipment[i] = 0; }
+ int money;
+ unsigned short equipment[EQUIPMENT_SLOTS];
+ Possessions(): money(0)
+ { for (int i = 0; i < EQUIPMENT_SLOTS; ++i) equipment[i] = 0; }
};
#endif
diff --git a/src/game-server/buysell.cpp b/src/game-server/buysell.cpp
index e73141df..f249d9c1 100644
--- a/src/game-server/buysell.cpp
+++ b/src/game-server/buysell.cpp
@@ -83,7 +83,6 @@ void BuySell::start(MovingObject *obj)
void BuySell::perform(int id, int amount)
{
Inventory inv(mChar);
- int money = mChar->getMoney();
for (TradedItems::iterator i = mItems.begin(),
i_end = mItems.end(); i != i_end; ++i)
{
@@ -92,13 +91,13 @@ void BuySell::perform(int id, int amount)
if (mSell)
{
amount -= inv.remove(id, amount);
- money += amount * i->cost;
+ inv.changeMoney(amount * i->cost);
}
else
{
- amount = std::min(amount, money / i->cost);
+ amount = std::min(amount, mChar->getPossessions().money / i->cost);
amount -= inv.insert(id, amount);
- money -= amount * i->cost;
+ inv.changeMoney(-amount * i->cost);
}
if (i->amount)
{
@@ -108,7 +107,6 @@ void BuySell::perform(int id, int amount)
mItems.erase(i);
}
}
- mChar->setMoney(money);
return;
}
}
diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp
index 7a9372fb..a1670389 100644
--- a/src/game-server/character.cpp
+++ b/src/game-server/character.cpp
@@ -36,7 +36,7 @@
Character::Character(MessageIn & msg):
Being(OBJECT_CHARACTER, 65535),
mClient(NULL), mTransactionHandler(NULL), mDatabaseID(-1),
- mMoney(0), mGender(0), mHairStyle(0), mHairColor(0), mLevel(0),
+ mGender(0), mHairStyle(0), mHairColor(0), mLevel(0),
mTransaction(TRANS_NONE), mAttributesChanged(true)
{
// prepare attributes vector
diff --git a/src/game-server/character.hpp b/src/game-server/character.hpp
index ff59c8a9..6d6e3376 100644
--- a/src/game-server/character.hpp
+++ b/src/game-server/character.hpp
@@ -176,16 +176,6 @@ class Character : public Being
setLevel(int level)
{ mLevel = level; }
- /** Gets the amount of money the character has. */
- int
- getMoney() const
- { return mMoney; }
-
- /** Sets the amount of money the character has. */
- void
- setMoney(int amount)
- { mMoney = amount; }
-
/**
* Gets the value of an attribute of the character.
*/
@@ -247,7 +237,6 @@ class Character : public Being
std::string mName; /**< Name of the character. */
int mDatabaseID; /**< Character's database ID. */
- unsigned short mMoney; /**< Wealth of the character. */
unsigned char mGender; /**< Gender of the character. */
unsigned char mHairStyle; /**< Hair Style of the character. */
unsigned char mHairColor; /**< Hair Color of the character. */
diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp
index af780117..767ed72e 100644
--- a/src/game-server/inventory.cpp
+++ b/src/game-server/inventory.cpp
@@ -136,6 +136,9 @@ void Inventory::sendFull() const
}
}
+ m.writeByte(255);
+ m.writeLong(mPoss->money);
+
gameHandler->sendTo(mClient, m);
}
@@ -303,6 +306,26 @@ int Inventory::count(int itemId) const
return nb;
}
+bool Inventory::changeMoney(int amount)
+{
+ if (amount == 0)
+ {
+ return true;
+ }
+
+ int money = mPoss->money + amount;
+ if (money < 0)
+ {
+ return false;
+ }
+
+ prepare();
+
+ mPoss->money = money;
+ msg.writeByte(255);
+ msg.writeLong(money);
+}
+
void Inventory::freeIndex(int i)
{
InventoryItem &it = mPoss->inventory[i];
diff --git a/src/game-server/inventory.hpp b/src/game-server/inventory.hpp
index 1f2611eb..ff01087e 100644
--- a/src/game-server/inventory.hpp
+++ b/src/game-server/inventory.hpp
@@ -144,6 +144,12 @@ class Inventory
*/
int getItem(int slot) const;
+ /**
+ * Changes amount of money.
+ * @return false if not enough money.
+ */
+ bool changeMoney(int);
+
private:
/**
diff --git a/src/serialize/characterdata.hpp b/src/serialize/characterdata.hpp
index a946a3aa..59edf642 100644
--- a/src/serialize/characterdata.hpp
+++ b/src/serialize/characterdata.hpp
@@ -36,7 +36,6 @@ void serializeCharacterData(T const &data, MessageOut &msg)
msg.writeByte(data.getHairStyle());
msg.writeByte(data.getHairColor());
msg.writeByte(data.getLevel());
- msg.writeShort(data.getMoney());
for (int i = 0; i < NB_BASE_ATTRIBUTES; ++i)
{
@@ -49,6 +48,7 @@ void serializeCharacterData(T const &data, MessageOut &msg)
msg.writeShort(pos.y);
Possessions const &poss = data.getPossessions();
+ msg.writeLong(poss.money);
for (int j = 0; j < EQUIPMENT_SLOTS; ++j)
{
msg.writeShort(poss.equipment[j]);
@@ -68,7 +68,6 @@ void deserializeCharacterData(T &data, MessageIn &msg)
data.setHairStyle(msg.readByte());
data.setHairColor(msg.readByte());
data.setLevel(msg.readByte());
- data.setMoney(msg.readShort());
for (int i = 0; i < NB_BASE_ATTRIBUTES; ++i)
{
@@ -83,6 +82,7 @@ void deserializeCharacterData(T &data, MessageIn &msg)
data.setPosition(temporaryPoint);
Possessions &poss = data.getPossessions();
+ poss.money = msg.readLong();
for (int j = 0; j < EQUIPMENT_SLOTS; ++j)
{
poss.equipment[j] = msg.readShort();