diff options
author | Philipp Sehmisch <mana@crushnet.org> | 2011-03-04 18:35:17 +0100 |
---|---|---|
committer | Philipp Sehmisch <mana@crushnet.org> | 2011-03-04 18:36:17 +0100 |
commit | 54d5f7e577db0639e42b18beae4b5c87af6d6843 (patch) | |
tree | 2a667f208be4191a54d08131d7c3984cdfd74132 /src/account-server | |
parent | bc5a0495ba7fbf992a1733850cbbe1cfb14c7c8d (diff) | |
download | manaserv-54d5f7e577db0639e42b18beae4b5c87af6d6843.tar.gz manaserv-54d5f7e577db0639e42b18beae4b5c87af6d6843.tar.bz2 manaserv-54d5f7e577db0639e42b18beae4b5c87af6d6843.tar.xz manaserv-54d5f7e577db0639e42b18beae4b5c87af6d6843.zip |
Implemented persistent world and map variables
The gameserver now receive a copy of all world state variables when
they are accepted by the accountserver and receive a copy of all
map state variables of a map when they register it successfully.
Implemented LUA script bindings for getting and setting these variables.
When such a variable is set, the accountserver is notified about this
change. Changes to world state variables are then propagated to all
gameservers by the accountserver.
Be aware that when a gameserver is updating a map, there is no check if
it is actually responsible for said map. But I consider this not a
security flaw, because authenticated game servers are considered to be
trustworthy in a lot of other situations, too.
Also renamed "quest" to "character variable" in the sourcecode.
Reviewed-by: Bertram
Diffstat (limited to 'src/account-server')
-rw-r--r-- | src/account-server/serverhandler.cpp | 52 | ||||
-rw-r--r-- | src/account-server/storage.cpp | 42 | ||||
-rw-r--r-- | src/account-server/storage.h | 7 |
3 files changed, 98 insertions, 3 deletions
diff --git a/src/account-server/serverhandler.cpp b/src/account-server/serverhandler.cpp index 1fd574ca..dd6a82e9 100644 --- a/src/account-server/serverhandler.cpp +++ b/src/account-server/serverhandler.cpp @@ -204,6 +204,17 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg) { outMsg.writeInt16(PASSWORD_OK); comp->send(outMsg); + + // transmit global world state variables + std::map<std::string, std::string> variables; + variables = storage->getAllWorldStateVars(0); + for (std::map<std::string, std::string>::iterator i = variables.begin(); + i != variables.end(); + i++) + { + outMsg.writeString(i->first); + outMsg.writeString(i->second); + } } else { @@ -230,6 +241,15 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg) { MessageOut outMsg(AGMSG_ACTIVE_MAP); outMsg.writeInt16(id); + std::map<std::string, std::string> variables; + variables = storage->getAllWorldStateVars(id); + for (std::map<std::string, std::string>::iterator i = variables.begin(); + i != variables.end(); + i++) + { + outMsg.writeString(i->first); + outMsg.writeString(i->second); + } comp->send(outMsg); MapStatistics &m = server->maps[id]; m.nbThings = 0; @@ -315,18 +335,18 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg) } } break; - case GAMSG_GET_QUEST: + case GAMSG_GET_VAR_CHR: { int id = msg.readInt32(); std::string name = msg.readString(); std::string value = storage->getQuestVar(id, name); - result.writeInt16(AGMSG_GET_QUEST_RESPONSE); + result.writeInt16(AGMSG_GET_VAR_CHR_RESPONSE); result.writeInt32(id); result.writeString(name); result.writeString(value); } break; - case GAMSG_SET_QUEST: + case GAMSG_SET_VAR_CHR: { int id = msg.readInt32(); std::string name = msg.readString(); @@ -334,6 +354,32 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg) storage->setQuestVar(id, name, value); } break; + case GAMSG_SET_VAR_WORLD: + { + std::string name = msg.readString(); + std::string value = msg.readString(); + // save the new value to the database + storage->setWorldStateVar(name, value); + // relay the new value to all gameservers + for (ServerHandler::NetComputers::iterator i = clients.begin(); + i != clients.end(); + i++) + { + MessageOut varUpdateMessage(AGMSG_SET_VAR_WORLD); + varUpdateMessage.writeString(name); + varUpdateMessage.writeString(value); + (*i)->send(varUpdateMessage); + } + } break; + + case GAMSG_SET_VAR_MAP: + { + int mapid = msg.readInt32(); + std::string name = msg.readString(); + std::string value = msg.readString(); + storage->setWorldStateVar(name, mapid, value); + } break; + case GAMSG_BAN_PLAYER: { int id = msg.readInt32(); diff --git a/src/account-server/storage.cpp b/src/account-server/storage.cpp index 9bc67edb..2326dec6 100644 --- a/src/account-server/storage.cpp +++ b/src/account-server/storage.cpp @@ -1505,6 +1505,48 @@ std::string Storage::getWorldStateVar(const std::string &name, int mapId) return std::string(); } +std::map<std::string, std::string> Storage::getAllWorldStateVars(int mapId) +{ + std::map<std::string, std::string> variables; + + try + { + std::ostringstream query; + query << "SELECT `state_name`, `value` " + << "FROM " << WORLD_STATES_TBL_NAME; + + // Add map filter if map_id is given + if (mapId >= 0) + query << " WHERE `map_id` = ?"; + + //query << ";"; <-- No ';' at the end of prepared statements. + + if (mDb->prepareSql(query.str())) + { + if (mapId >= 0) + mDb->bindValue(1, mapId); + const dal::RecordSet &results = mDb->processSql(); + + for (unsigned int i = 0; i < results.rows(); i++) + { + variables[results(i, 0)] = results(i, 1); + } + } + else + { + utils::throwError("(DALStorage:getAllWorldStateVar) " + "SQL query preparation failure."); + } + } + catch (const dal::DbSqlQueryExecFailure &e) + { + utils::throwError("(DALStorage::getWorldStateVar) SQL query failure: ", + e); + } + + return variables; +} + void Storage::setWorldStateVar(const std::string &name, const std::string &value) { diff --git a/src/account-server/storage.h b/src/account-server/storage.h index 8e7f90f3..a44156a4 100644 --- a/src/account-server/storage.h +++ b/src/account-server/storage.h @@ -339,6 +339,13 @@ class Storage const std::string &value); /** + * Gets the value of all world state variable of a specific map. + * + * @param mapId ID of the specific map + */ + std::map<std::string, std::string> getAllWorldStateVars(int mapId); + + /** * Set the level on an account. * * @param id The id of the account |