summaryrefslogtreecommitdiffstats
path: root/src/account-server
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-09-09 02:35:25 +0200
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-09-09 02:36:30 +0200
commit6ae2001e91eaaeb8b72031e96f88820711bb1ca0 (patch)
tree8ab01e1d9e59860837560e6cd8bbd7eabaa428de /src/account-server
parent40a31c52aebc19221cc9da8a0f764d21e672937b (diff)
downloadmanaserv-6ae2001e91eaaeb8b72031e96f88820711bb1ca0.tar.gz
manaserv-6ae2001e91eaaeb8b72031e96f88820711bb1ca0.tar.xz
manaserv-6ae2001e91eaaeb8b72031e96f88820711bb1ca0.zip
Add persistent items support based on seeseekey's work.
Also made some random changes where useful, including: - Code formatting fixes, - Design fix about the fact that only the game config option should be checked. - Fixed the size of the values sent and receive to follow the rest of the development. - Fixed variables names to make them show what they are, and not why they are used. Resolves: Mana-Mantis #142.
Diffstat (limited to 'src/account-server')
-rw-r--r--src/account-server/flooritem.h66
-rw-r--r--src/account-server/serverhandler.cpp52
-rw-r--r--src/account-server/storage.cpp87
-rw-r--r--src/account-server/storage.h33
4 files changed, 238 insertions, 0 deletions
diff --git a/src/account-server/flooritem.h b/src/account-server/flooritem.h
new file mode 100644
index 0000000..436dedb
--- /dev/null
+++ b/src/account-server/flooritem.h
@@ -0,0 +1,66 @@
+/*
+ * The Mana Server
+ * Copyright (C) 2011 The Mana Development Team
+ *
+ * This file is part of The Mana Server.
+ *
+ * The Mana Server is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana Server is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef FLOOR_ITEM_H
+#define FLOOR_ITEM_H
+
+class FloorItem
+{
+public:
+ FloorItem():
+ mItemId(0), mItemAmount(0), mPosX(0), mPosY(0)
+ {}
+
+ FloorItem(int itemId, int itemAmount, int posX, int posY):
+ mItemId(itemId), mItemAmount(itemAmount), mPosX(posX), mPosY(posY)
+ {}
+
+ /**
+ * Returns the item id
+ */
+ int getItemId() const
+ { return mItemId; }
+
+ /**
+ * Returns the amount of items
+ */
+ int getItemAmount() const
+ { return mItemAmount; }
+
+ /**
+ * Returns the position x of the item(s)
+ */
+ int getPosX() const
+ { return mPosX; }
+
+ /**
+ * Returns the position x of the item(s)
+ */
+ int getPosY() const
+ { return mPosY; }
+
+private:
+ int mItemId;
+ int mItemAmount;
+ int mPosX;
+ int mPosY;
+};
+
+#endif // FLOOR_ITEM_H
diff --git a/src/account-server/serverhandler.cpp b/src/account-server/serverhandler.cpp
index e5bfdc4..6a41d71 100644
--- a/src/account-server/serverhandler.cpp
+++ b/src/account-server/serverhandler.cpp
@@ -27,6 +27,7 @@
#include "account-server/accountclient.h"
#include "account-server/accounthandler.h"
#include "account-server/character.h"
+#include "account-server/flooritem.h"
#include "account-server/storage.h"
#include "chat-server/chathandler.h"
#include "chat-server/post.h"
@@ -241,9 +242,15 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg)
else
{
MessageOut outMsg(AGMSG_ACTIVE_MAP);
+
+ // Map variables
outMsg.writeInt16(id);
std::map<std::string, std::string> variables;
variables = storage->getAllWorldStateVars(id);
+
+ // Map vars number
+ outMsg.writeInt16(variables.size());
+
for (std::map<std::string, std::string>::iterator i = variables.begin();
i != variables.end();
i++)
@@ -251,6 +258,23 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg)
outMsg.writeString(i->first);
outMsg.writeString(i->second);
}
+
+ // Persistent Floor Items
+ std::list<FloorItem> items;
+ items = storage->getFloorItemsFromMap(id);
+
+ outMsg.writeInt16(items.size()); //number of floor items
+
+ // Send each map item: item_id, amount, pos_x, pos_y
+ for (std::list<FloorItem>::iterator i = items.begin();
+ i != items.end(); ++i)
+ {
+ outMsg.writeInt32(i->getItemId());
+ outMsg.writeInt16(i->getItemAmount());
+ outMsg.writeInt16(i->getPosX());
+ outMsg.writeInt16(i->getPosY());
+ }
+
comp->send(outMsg);
MapStatistics &m = server->maps[id];
m.nbThings = 0;
@@ -549,6 +573,34 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg)
chatHandler->handlePartyInvite(msg);
break;
+ case GAMSG_CREATE_ITEM_ON_MAP:
+ {
+ int mapId = msg.readInt32();
+ int itemId = msg.readInt32();
+ int amount = msg.readInt16();
+ int posX = msg.readInt16();
+ int posY = msg.readInt16();
+
+ LOG_DEBUG("Gameserver create item " << itemId
+ << " on map " << mapId);
+
+ storage->addFloorItem(mapId, itemId, amount, posX, posY);
+ } break;
+
+ case GAMSG_REMOVE_ITEM_ON_MAP:
+ {
+ int mapId = msg.readInt32();
+ int itemId = msg.readInt32();
+ int amount = msg.readInt16();
+ int posX = msg.readInt16();
+ int posY = msg.readInt16();
+
+ LOG_DEBUG("Gameserver removed item " << itemId
+ << " from map " << mapId);
+
+ storage->removeFloorItem(mapId, itemId, amount, posX, posY);
+ } break;
+
default:
LOG_WARN("ServerHandler::processMessage, Invalid message type: "
<< msg.getId());
diff --git a/src/account-server/storage.cpp b/src/account-server/storage.cpp
index 4f22964..a1d1694 100644
--- a/src/account-server/storage.cpp
+++ b/src/account-server/storage.cpp
@@ -24,6 +24,7 @@
#include "account-server/storage.h"
#include "account-server/account.h"
+#include "account-server/flooritem.h"
#include "chat-server/chatchannel.h"
#include "chat-server/guild.h"
#include "chat-server/post.h"
@@ -90,6 +91,7 @@ static const char *AUCTION_TBL_NAME = "mana_auctions";
static const char *AUCTION_BIDS_TBL_NAME = "mana_auction_bids";
static const char *ONLINE_USERS_TBL_NAME = "mana_online_list";
static const char *TRANSACTION_TBL_NAME = "mana_transactions";
+static const char *FLOOR_ITEMS_TBL_NAME = "mana_floor_items";
Storage::Storage()
: mDb(dal::DataProviderFactory::createDataProvider()),
@@ -138,6 +140,15 @@ void Storage::open()
std::ostringstream sql;
sql << "DELETE FROM " << ONLINE_USERS_TBL_NAME;
mDb->execSql(sql.str());
+
+ // In case where the server shouldn't keep floor item in database,
+ // we remove remnants at startup
+ if (Configuration::getValue("game_floorItemDecayTime", 0) > 0)
+ {
+ sql.clear();
+ sql << "DELETE FROM " << FLOOR_ITEMS_TBL_NAME;
+ mDb->execSql(sql.str());
+ }
}
catch (const DbConnectionFailure& e)
{
@@ -1376,6 +1387,82 @@ void Storage::removeGuildMember(int guildId, int memberId)
}
}
+void Storage::addFloorItem(int mapId, int itemId, int amount,
+ int posX, int posY)
+{
+ try
+ {
+ std::ostringstream sql;
+ sql << "INSERT INTO " << FLOOR_ITEMS_TBL_NAME
+ << " (map_id, item_id, amount, pos_x, pos_y)"
+ << " VALUES ("
+ << mapId << ", "
+ << itemId << ", "
+ << amount << ", "
+ << posX << ", "
+ << posY << ");";
+ mDb->execSql(sql.str());
+ }
+ catch (const dal::DbSqlQueryExecFailure& e)
+ {
+ utils::throwError("(DALStorage::addFloorItem) SQL query failure: ", e);
+ }
+}
+
+void Storage::removeFloorItem(int mapId, int itemId, int amount,
+ int posX, int posY)
+{
+ try
+ {
+ std::ostringstream sql;
+ sql << "DELETE FROM " << FLOOR_ITEMS_TBL_NAME
+ << " WHERE map_id = "
+ << mapId << " AND item_id = "
+ << itemId << " AND amount = "
+ << amount << " AND pos_x = "
+ << posX << " AND pos_y = "
+ << posY << ";";
+ mDb->execSql(sql.str());
+ }
+ catch (const dal::DbSqlQueryExecFailure& e)
+ {
+ utils::throwError("(DALStorage::removeFloorItem) SQL query failure: ",
+ e);
+ }
+}
+
+std::list<FloorItem> Storage::getFloorItemsFromMap(int mapId)
+{
+ std::list<FloorItem> floorItems;
+
+ try
+ {
+ std::ostringstream sql;
+ sql << "SELECT * FROM " << FLOOR_ITEMS_TBL_NAME
+ << " WHERE map_id = " << mapId;
+
+ string_to< unsigned > toUint;
+ const dal::RecordSet &itemInfo = mDb->execSql(sql.str());
+ if (!itemInfo.isEmpty())
+ {
+ for (int k = 0, size = itemInfo.rows(); k < size; ++k)
+ {
+ floorItems.push_back(FloorItem(toUint(itemInfo(k, 2)),
+ toUint(itemInfo(k, 3)),
+ toUint(itemInfo(k, 4)),
+ toUint(itemInfo(k, 5))));
+ }
+ }
+ }
+ catch (const dal::DbSqlQueryExecFailure &e)
+ {
+ utils::throwError("DALStorage::getFloorItemsFromMap "
+ "SQL query failure: ", e);
+ }
+
+ return floorItems;
+}
+
void Storage::setMemberRights(int guildId, int memberId, int rights)
{
try
diff --git a/src/account-server/storage.h b/src/account-server/storage.h
index a44156a..3c62992 100644
--- a/src/account-server/storage.h
+++ b/src/account-server/storage.h
@@ -32,6 +32,7 @@
class Account;
class Character;
class ChatChannel;
+class FloorItem;
class Guild;
class Letter;
class Post;
@@ -288,6 +289,38 @@ class Storage
std::list<Guild*> getGuildList();
/**
+ * Add a floor item to map.
+ *
+ * Used to keep the floor item persistently between two server restart.
+ *
+ * @param mapId The map id
+ * @param itemId The item id
+ * @param posX Position X of the item in pixels
+ * @param posY Position Y of the item in pixels
+ */
+ void addFloorItem(int mapId, int itemId, int amount,
+ int posX, int posY);
+
+ /**
+ * Remove item from map persistence
+ *
+ * @param mapId The map id
+ * @param itemId The item id
+ * @param posX Position X of the item in pixels
+ * @param posY Position Y of the item in pixels
+ */
+ void removeFloorItem(int mapId, int itemId, int amount,
+ int posX, int posY);
+
+
+ /**
+ * Get all persistent items from the given map id
+ *
+ * @param mapId The map id
+ */
+ std::list<FloorItem> getFloorItemsFromMap(int mapId);
+
+ /**
* Update an account to the database.
*
* @param Account object to update.