summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Sehmisch <mana@crushnet.org>2011-03-04 18:35:17 +0100
committerPhilipp Sehmisch <mana@crushnet.org>2011-03-04 18:36:17 +0100
commit54d5f7e577db0639e42b18beae4b5c87af6d6843 (patch)
tree2a667f208be4191a54d08131d7c3984cdfd74132
parentbc5a0495ba7fbf992a1733850cbbe1cfb14c7c8d (diff)
downloadmanaserv-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
-rw-r--r--src/account-server/serverhandler.cpp52
-rw-r--r--src/account-server/storage.cpp42
-rw-r--r--src/account-server/storage.h7
-rw-r--r--src/game-server/accountconnection.cpp64
-rw-r--r--src/game-server/accountconnection.h21
-rw-r--r--src/game-server/mapcomposite.cpp26
-rw-r--r--src/game-server/mapcomposite.h21
-rw-r--r--src/game-server/quest.cpp4
-rw-r--r--src/game-server/state.cpp29
-rw-r--r--src/game-server/state.h18
-rw-r--r--src/manaserv_protocol.h16
-rw-r--r--src/scripting/lua.cpp92
12 files changed, 370 insertions, 22 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
diff --git a/src/game-server/accountconnection.cpp b/src/game-server/accountconnection.cpp
index c3bc83bf..32d64a3c 100644
--- a/src/game-server/accountconnection.cpp
+++ b/src/game-server/accountconnection.cpp
@@ -128,6 +128,18 @@ void AccountConnection::processMessage(MessageIn &msg)
stop();
exit(EXIT_BAD_CONFIG_PARAMETER);
}
+
+ // read world state variables
+ while (msg.getUnreadLength())
+ {
+ std::string key = msg.readString();
+ std::string value = msg.readString();
+ if (!key.empty() && !value.empty())
+ {
+ GameState::setVariableFromDbserver(key, value);
+ }
+ }
+
} break;
case AGMSG_PLAYER_ENTER:
@@ -140,7 +152,28 @@ void AccountConnection::processMessage(MessageIn &msg)
case AGMSG_ACTIVE_MAP:
{
int id = msg.readInt16();
- MapManager::raiseActive(id);
+ if (MapManager::raiseActive(id))
+ {
+ // set map variables
+ MapComposite *m = MapManager::getMap(id);
+ while (msg.getUnreadLength())
+ {
+ std::string key = msg.readString();
+ std::string value = msg.readString();
+ if (!key.empty() && !value.empty())
+ {
+ m->setVariableFromDbserver(key, value);
+ }
+ }
+ }
+ } break;
+
+ case AGMSG_SET_VAR_WORLD:
+ {
+ std::string key = msg.readString();
+ std::string value = msg.readString();
+ GameState::setVariableFromDbserver(key, value);
+ LOG_INFO("Global variable \""<<key<<"\" has changed to \""<<value<<"\"");
} break;
case AGMSG_REDIRECT_RESPONSE:
@@ -152,7 +185,7 @@ void AccountConnection::processMessage(MessageIn &msg)
gameHandler->completeServerChange(id, token, address, port);
} break;
- case AGMSG_GET_QUEST_RESPONSE:
+ case AGMSG_GET_VAR_CHR_RESPONSE:
{
int id = msg.readInt32();
std::string name = msg.readString();
@@ -219,24 +252,43 @@ void AccountConnection::playerReconnectAccount(int id,
send(msg);
}
-void AccountConnection::requestQuestVar(Character *ch, const std::string &name)
+void AccountConnection::requestCharacterVar(Character *ch, const std::string &name)
{
- MessageOut msg(GAMSG_GET_QUEST);
+ MessageOut msg(GAMSG_GET_VAR_CHR);
msg.writeInt32(ch->getDatabaseID());
msg.writeString(name);
send(msg);
}
-void AccountConnection::updateQuestVar(Character *ch, const std::string &name,
+void AccountConnection::updateCharacterVar(Character *ch, const std::string &name,
const std::string &value)
{
- MessageOut msg(GAMSG_SET_QUEST);
+ MessageOut msg(GAMSG_SET_VAR_CHR);
msg.writeInt32(ch->getDatabaseID());
msg.writeString(name);
msg.writeString(value);
send(msg);
}
+void AccountConnection::updateMapVar(MapComposite *map, const std::string &name,
+ const std::string &value)
+{
+ MessageOut msg(GAMSG_SET_VAR_MAP);
+ msg.writeInt32(map->getID());
+ msg.writeString(name);
+ msg.writeString(value);
+ send(msg);
+}
+
+void AccountConnection::updateWorldVar(const std::string &name,
+ const std::string &value)
+{
+ MessageOut msg(GAMSG_SET_VAR_WORLD);
+ msg.writeString(name);
+ msg.writeString(value);
+ send(msg);
+}
+
void AccountConnection::banCharacter(Character *ch, int duration)
{
MessageOut msg(GAMSG_BAN_PLAYER);
diff --git a/src/game-server/accountconnection.h b/src/game-server/accountconnection.h
index c0bbc5cd..37033389 100644
--- a/src/game-server/accountconnection.h
+++ b/src/game-server/accountconnection.h
@@ -25,6 +25,7 @@
#include "net/connection.h"
class Character;
+class MapComposite;
/** \fn void AccountConnection::syncChanges(bool force = false)
*
@@ -75,14 +76,26 @@ class AccountConnection : public Connection
void playerReconnectAccount(int id, const std::string &magic_token);
/**
- * Requests the value of a quest variable from the database.
+ * Requests the value of a character-bound variable from the database.
*/
- void requestQuestVar(Character *, const std::string &);
+ void requestCharacterVar(Character *, const std::string &);
/**
- * Pushes a new quest value to the database.
+ * Pushes a new character-bound value to the database.
*/
- void updateQuestVar(Character *, const std::string &name,
+ void updateCharacterVar(Character *, const std::string &name,
+ const std::string &value);
+
+ /**
+ * Pushes a new value of a map variable to the account server.
+ */
+ void updateMapVar(MapComposite *, const std::string &name,
+ const std::string &value);
+
+ /**
+ * Pushes a new value of a world variable to the account server.
+ */
+ void updateWorldVar(const std::string &name,
const std::string &value);
/**
diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp
index ae88a37c..451147f7 100644
--- a/src/game-server/mapcomposite.cpp
+++ b/src/game-server/mapcomposite.cpp
@@ -22,6 +22,7 @@
#include <cassert>
#include "common/configuration.h"
+#include "accountconnection.h"
#include "game-server/map.h"
#include "game-server/mapcomposite.h"
#include "game-server/character.h"
@@ -593,3 +594,28 @@ const std::vector< Thing * > &MapComposite::getEverything() const
{
return mContent->things;
}
+
+
+std::string MapComposite::getVariable(const std::string &key)
+{
+ std::map<std::string, std::string>::iterator iValue = mScriptVariables.find(key);
+ if (iValue != mScriptVariables.end())
+ {
+ return iValue->second;
+ } else {
+ return std::string();
+ }
+}
+
+void MapComposite::setVariable(const std::string &key, const std::string &value)
+{
+ // check if the value actually changed
+ std::map<std::string, std::string>::iterator iOldValue = mScriptVariables.find(key);
+ if (iOldValue == mScriptVariables.end() || iOldValue->second != value)
+ {
+ // changed valu or unknown variable
+ mScriptVariables[key] = value;
+ // update accountserver
+ accountHandler->updateMapVar(this, key, value);
+ }
+}
diff --git a/src/game-server/mapcomposite.h b/src/game-server/mapcomposite.h
index 3de83c77..351a0572 100644
--- a/src/game-server/mapcomposite.h
+++ b/src/game-server/mapcomposite.h
@@ -23,6 +23,7 @@
#include <string>
#include <vector>
+#include <map>
class Actor;
class Being;
@@ -332,6 +333,24 @@ class MapComposite
*/
const std::vector< Thing * > &getEverything() const;
+ /**
+ * Gets the cached value of a map-bound script variable
+ */
+ std::string getVariable(const std::string &key);
+
+ /**
+ * Changes a script variable and notifies the database server
+ * about the change
+ */
+ void setVariable (const std::string &key, const std::string &value);
+
+ /**
+ * Changes a script variable without notifying the database server
+ * about the change
+ */
+ void setVariableFromDbserver (const std::string &key, const std::string &value)
+ { mScriptVariables[key] = value ;}
+
private:
MapComposite(const MapComposite &);
@@ -340,7 +359,7 @@ class MapComposite
Script *mScript; /**< Script associated to this map. */
std::string mName; /**< Name of the map. */
unsigned short mID; /**< ID of the map. */
-
+ std::map< std::string, std::string > mScriptVariables; /** Cached persistent variables */
PvPRules mPvPRules;
};
diff --git a/src/game-server/quest.cpp b/src/game-server/quest.cpp
index 0a7b02f9..e4607c6a 100644
--- a/src/game-server/quest.cpp
+++ b/src/game-server/quest.cpp
@@ -69,7 +69,7 @@ void setQuestVar(Character *ch, const std::string &name,
{
i->second = value;
}
- accountHandler->updateQuestVar(ch, name, value);
+ accountHandler->updateCharacterVar(ch, name, value);
}
/**
@@ -126,7 +126,7 @@ void recoverQuestVar(Character *ch, const std::string &name,
ch->addListener(&questDeathListener);
}
i->second.variables[name].push_back(f);
- accountHandler->requestQuestVar(ch, name);
+ accountHandler->requestCharacterVar(ch, name);
}
void recoveredQuestVar(int id, const std::string &name,
diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp
index abfa2342..642d5dbe 100644
--- a/src/game-server/state.cpp
+++ b/src/game-server/state.cpp
@@ -65,6 +65,12 @@ typedef std::map< Actor *, DelayedEvent > DelayedEvents;
static DelayedEvents delayedEvents;
/**
+ * Cached persistent script variables
+ */
+static std::map< std::string, std::string > mScriptVariables;
+
+
+/**
* Updates object states on the map.
*/
static void updateMap(MapComposite *map)
@@ -809,3 +815,26 @@ void GameState::sayToAll(const std::string &text)
// Sends it to everyone connected to the game server
gameHandler->sendToEveryone(msg);
}
+
+
+std::string GameState::getVariable(const std::string &key)
+{
+ std::map<std::string, std::string>::iterator iValue = mScriptVariables.find(key);
+ if (iValue != mScriptVariables.end())
+ {
+ return iValue->second;
+ } else {
+ return std::string();
+ }
+}
+
+void GameState::setVariable(const std::string &key, const std::string &value)
+{
+ mScriptVariables[key] = value;
+ accountHandler->updateWorldVar(key, value);
+}
+
+void GameState::setVariableFromDbserver(const std::string &key, const std::string &value)
+{
+ mScriptVariables[key] = value ;
+}
diff --git a/src/game-server/state.h b/src/game-server/state.h
index f58ab038..ff945a8a 100644
--- a/src/game-server/state.h
+++ b/src/game-server/state.h
@@ -28,6 +28,7 @@ class Thing;
class Actor;
class Character;
+
namespace GameState
{
/**
@@ -103,6 +104,23 @@ namespace GameState
*/
void sayToAll(const std::string &text);
+ /**
+ * Gets the cached value of a global script variable
+ */
+ std::string getVariable(const std::string &key);
+
+ /**
+ * Changes a global script variable and notifies the database server
+ * about the change
+ */
+ void setVariable (const std::string &key, const std::string &value);
+
+ /**
+ * Changes a global variable without notifying the database server
+ * about the change
+ */
+ void setVariableFromDbserver (const std::string &key, const std::string &value);
+
}
#endif
diff --git a/src/manaserv_protocol.h b/src/manaserv_protocol.h
index 76b5f34d..50908730 100644
--- a/src/manaserv_protocol.h
+++ b/src/manaserv_protocol.h
@@ -225,17 +225,23 @@ enum {
// Inter-server
GAMSG_REGISTER = 0x0500, // S address, W port, S password, D items db revision, { W map id }*
- AGMSG_REGISTER_RESPONSE = 0x0501, // C item version, C password response
- AGMSG_ACTIVE_MAP = 0x0502, // W map id
+ AGMSG_REGISTER_RESPONSE = 0x0501, // C item version, C password response, { S globalvar_key, S globalvar_value }
+ AGMSG_ACTIVE_MAP = 0x0502, // W map id, { S mapvar_key, S mapvar_value }
AGMSG_PLAYER_ENTER = 0x0510, // B*32 token, D id, S name, serialised character data
GAMSG_PLAYER_DATA = 0x0520, // D id, serialised character data
GAMSG_REDIRECT = 0x0530, // D id
AGMSG_REDIRECT_RESPONSE = 0x0531, // D id, B*32 token, S game address, W game port
GAMSG_PLAYER_RECONNECT = 0x0532, // D id, B*32 token
GAMSG_PLAYER_SYNC = 0x0533, // serialised sync data
- GAMSG_SET_QUEST = 0x0540, // D id, S name, S value
- GAMSG_GET_QUEST = 0x0541, // D id, S name
- AGMSG_GET_QUEST_RESPONSE = 0x0542, // D id, S name, S value
+ GAMSG_SET_VAR_CHR = 0x0540, // D id, S name, S value
+ GAMSG_GET_VAR_CHR = 0x0541, // D id, S name
+ AGMSG_GET_VAR_CHR_RESPONSE = 0x0542, // D id, S name, S value
+ //reserved GAMSG_SET_VAR_ACC = 0x0543, // D charid, S name, S value
+ //reserved GAMSG_GET_VAR_ACC = 0x0544, // D charid, S name
+ //reserved AGMSG_GET_VAR_ACC_RESPONSE = 0x0545, // D charid, S name, S value
+ GAMSG_SET_VAR_MAP = 0x0546, // D mapid, S name, S value
+ GAMSG_SET_VAR_WORLD = 0x0547, // S name, S value
+ AGMSG_SET_VAR_WORLD = 0x0548, // S name, S value
GAMSG_BAN_PLAYER = 0x0550, // D id, W duration
GAMSG_CHANGE_PLAYER_LEVEL = 0x0555, // D id, W level
GAMSG_CHANGE_ACCOUNT_LEVEL = 0x0556, // D id, W level
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index 76280242..f55b96d1 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -1080,6 +1080,92 @@ static int chr_get_quest(lua_State *s)
return 0;
}
+
+/**
+ * gets the value of a persistent map variable.
+ * mana.getvar_map(string): string
+ */
+ static int getvar_map(lua_State *s)
+{
+ const char *m = luaL_checkstring(s, 1);
+ if (m[0] == 0)
+ {
+ raiseScriptError(s, "getvar_map called for unnamed variable.");
+ return 0;
+ }
+
+ lua_pushlightuserdata(s, (void *)&registryKey);
+ lua_gettable(s, LUA_REGISTRYINDEX);
+ Script *script = static_cast<Script *>(lua_touserdata(s, -1));
+ std::string value = script->getMap()->getVariable(m);
+
+ lua_pushstring(s, value.c_str());
+ return 1;
+}
+
+/**
+ * sets the value of a persistent map variable.
+ * mana.setvar_map(string, string)
+ */
+ static int setvar_map(lua_State *s)
+{
+ const char *m = luaL_checkstring(s, 1);
+ if (m[0] == 0)
+ {
+ raiseScriptError(s, "getvar_map called for unnamed variable.");
+ return 0;
+ }
+
+ lua_pushlightuserdata(s, (void *)&registryKey);
+ lua_gettable(s, LUA_REGISTRYINDEX);
+ Script *script = static_cast<Script *>(lua_touserdata(s, -1));
+ std::string key = lua_tostring(s, 1);
+ std::string value = lua_tostring(s, 2);
+ script->getMap()->setVariable(key, value);
+
+ return 0;
+}
+
+/**
+ * gets the value of a persistent global variable.
+ * mana.getvar_world(string): string
+ */
+ static int getvar_world(lua_State *s)
+{
+ const char *m = luaL_checkstring(s, 1);
+ if (m[0] == 0)
+ {
+ raiseScriptError(s, "getvar_world called for unnamed variable.");
+ return 0;
+ }
+
+ std::string value = GameState::getVariable(m);
+
+ lua_pushstring(s, value.c_str());
+ return 1;
+}
+
+/**
+ * sets the value of a persistent global variable.
+ * mana.setvar_world(string, string)
+ */
+ static int setvar_world(lua_State *s)
+{
+ const char *m = luaL_checkstring(s, 1);
+ if (m[0] == 0)
+ {
+ raiseScriptError(s, "setvar_world called with unnamed variable.");
+ return 0;
+ }
+
+ std::string key = lua_tostring(s, 1);
+ std::string value = lua_tostring(s, 2);
+ GameState::setVariable(key, value);
+
+ return 0;
+}
+
+
/**
* Callback for setting a quest variable.
* mana.chr_set_chest(character, string, string)
@@ -1697,7 +1783,7 @@ LuaScript::LuaScript():
lua_rawseti(mState, -2, 2);
lua_pop(mState, 2);
- // Put some callback functions in the scripting environment.
+ // Put the callback functions in the scripting environment.
static luaL_Reg const callbacks[] = {
{ "npc_create", &npc_create },
{ "npc_message", &npc_message },
@@ -1711,6 +1797,10 @@ LuaScript::LuaScript():
{ "chr_inv_count", &chr_inv_count },
{ "chr_get_quest", &chr_get_quest },
{ "chr_set_quest", &chr_set_quest },
+ { "getvar_map", &getvar_map },
+ { "setvar_map", &setvar_map },
+ { "getvar_world", &getvar_world },
+ { "setvar_world", &setvar_world },
{ "chr_get_post", &chr_get_post },
{ "chr_get_exp", &chr_get_exp },
{ "chr_give_exp", &chr_give_exp },