summaryrefslogtreecommitdiff
path: root/src/game-server
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 /src/game-server
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
Diffstat (limited to 'src/game-server')
-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
7 files changed, 170 insertions, 13 deletions
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