Converted the server's managers to singletons

This commit is contained in:
Kayne Ruse
2014-08-17 10:06:29 +10:00
parent ce97245131
commit e7ba097e6a
10 changed files with 40 additions and 87 deletions
+7 -4
View File
@@ -23,16 +23,14 @@
#define ACCOUNTMANAGER_HPP_ #define ACCOUNTMANAGER_HPP_
#include "account_data.hpp" #include "account_data.hpp"
#include "singleton.hpp"
#include "sqlite3/sqlite3.h" #include "sqlite3/sqlite3.h"
#include <map> #include <map>
class AccountManager { class AccountManager : public Singleton<AccountManager> {
public: public:
AccountManager() = default;
~AccountManager() { UnloadAll(); };
//public access methods //public access methods
int CreateAccount(std::string username, int clientIndex); int CreateAccount(std::string username, int clientIndex);
int LoadAccount(std::string username, int clientIndex); int LoadAccount(std::string username, int clientIndex);
@@ -50,6 +48,11 @@ public:
sqlite3* GetDatabase(); sqlite3* GetDatabase();
private: private:
friend Singleton<AccountManager>;
AccountManager() = default;
~AccountManager() { UnloadAll(); };
std::map<int, AccountData> accountMap; std::map<int, AccountData> accountMap;
sqlite3* database = nullptr; sqlite3* database = nullptr;
}; };
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. INCLUDES+=. ../../common/utilities
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+7 -4
View File
@@ -23,17 +23,15 @@
#define CHARACTERMANAGER_HPP_ #define CHARACTERMANAGER_HPP_
#include "character_data.hpp" #include "character_data.hpp"
#include "singleton.hpp"
#include "sqlite3/sqlite3.h" #include "sqlite3/sqlite3.h"
#include <map> #include <map>
#include <functional> #include <functional>
class CharacterManager { class CharacterManager : public Singleton<CharacterManager> {
public: public:
CharacterManager() = default;
~CharacterManager() { UnloadAll(); };
//public access methods //public access methods
int CreateCharacter(int owner, std::string handle, std::string avatar); int CreateCharacter(int owner, std::string handle, std::string avatar);
int LoadCharacter(int owner, std::string handle, std::string avatar); int LoadCharacter(int owner, std::string handle, std::string avatar);
@@ -53,6 +51,11 @@ public:
sqlite3* GetDatabase(); sqlite3* GetDatabase();
private: private:
friend Singleton<CharacterManager>;
CharacterManager() = default;
~CharacterManager() { UnloadAll(); };
std::map<int, CharacterData> characterMap; std::map<int, CharacterData> characterMap;
sqlite3* database = nullptr; sqlite3* database = nullptr;
}; };
+6
View File
@@ -33,7 +33,10 @@ using namespace std;
int main(int argc, char** argv) { int main(int argc, char** argv) {
try { try {
//create the singletons //create the singletons
AccountManager::Create();
CharacterManager::Create();
ConfigUtility::Create(); ConfigUtility::Create();
RoomManager::Create();
UDPNetworkUtility::Create(); UDPNetworkUtility::Create();
//call the server's routines //call the server's routines
@@ -47,7 +50,10 @@ int main(int argc, char** argv) {
ServerApplication::Delete(); ServerApplication::Delete();
//delete the singletons //delete the singletons
AccountManager::Delete();
CharacterManager::Delete();
ConfigUtility::Delete(); ConfigUtility::Delete();
RoomManager::Delete();
UDPNetworkUtility::Delete(); UDPNetworkUtility::Delete();
} }
catch(exception& e) { catch(exception& e) {
+1 -1
View File
@@ -1,5 +1,5 @@
#config #config
INCLUDES+=. ../mapgen ../mapgen/generators ../../common/map INCLUDES+=. ../../common/map ../../common/utilities
LIBS+= LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+7 -6
View File
@@ -23,18 +23,14 @@
#define ROOMMANAGER_HPP_ #define ROOMMANAGER_HPP_
#include "room_data.hpp" #include "room_data.hpp"
#include "singleton.hpp"
#include "lua/lua.hpp" #include "lua/lua.hpp"
#include <map> #include <map>
#define ROOM_MANAGER_PSEUDOINDEX "RoomManager" class RoomManager : public Singleton<RoomManager> {
class RoomManager {
public: public:
RoomManager() = default;
~RoomManager() = default;
//public access methods //public access methods
int CreateRoom(); int CreateRoom();
void UnloadRoom(int uid); void UnloadRoom(int uid);
@@ -52,6 +48,11 @@ public:
lua_State* GetLuaState() { return luaState; } lua_State* GetLuaState() { return luaState; }
private: private:
friend Singleton<RoomManager>;
RoomManager() = default;
~RoomManager() = default;
std::map<int, RoomData*> roomMap; std::map<int, RoomData*> roomMap;
lua_State* luaState = nullptr; lua_State* luaState = nullptr;
int counter = 0; int counter = 0;
+7 -28
View File
@@ -26,45 +26,24 @@
#include <string> #include <string>
static int getRoom(lua_State* L) { static int getRoom(lua_State* L) {
//get the room manager //find, push and return the room
lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); RoomData* room = RoomManager::GetSingleton().GetRoom(lua_tointeger(L, -2));
lua_gettable(L, LUA_REGISTRYINDEX); lua_pushlightuserdata(L, reinterpret_cast<void*>(room));
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
//push the room and return it
lua_pushlightuserdata(L, reinterpret_cast<void*>( roomMgr->GetRoom(lua_tointeger(L, -2)) ));
return 1; return 1;
} }
static int createRoom(lua_State* L) { static int createRoom(lua_State* L) {
//TODO: check parameter count for the glue functions //TODO: check parameter count for the glue functions
//get the room manager //create, find and return the room
lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX); int uid = RoomManager::GetSingleton().CreateRoom();
lua_gettable(L, LUA_REGISTRYINDEX); lua_pushlightuserdata(L, RoomManager::GetSingleton().FindRoom(uid));
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
//create the room
int uid = roomMgr->CreateRoom();
//TODO: any room parameters
//return the new room
lua_pushlightuserdata(L, roomMgr->FindRoom(uid));
return 1; return 1;
} }
static int unloadRoom(lua_State* L) { static int unloadRoom(lua_State* L) {
//get the room manager
lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX);
lua_gettable(L, LUA_REGISTRYINDEX);
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
//TODO: any room parameters
//unload the specified room //unload the specified room
roomMgr->UnloadRoom(lua_tointeger(L, -2)); RoomManager::GetSingleton().UnloadRoom(lua_tointeger(L, -2));
return 0; return 0;
} }
+1 -8
View File
@@ -85,14 +85,7 @@ void ServerApplication::Init(int argc, char** argv) {
roomMgr.SetLuaState(luaState); roomMgr.SetLuaState(luaState);
std::cout << "Internal managers set" << std::endl; std::cout << "Internal managers initialized" << std::endl;
//register the "globals"
lua_pushstring(luaState, ROOM_MANAGER_PSEUDOINDEX);
lua_pushlightuserdata(luaState, &roomMgr);
lua_settable(luaState, LUA_REGISTRYINDEX);
std::cout << "Internal managers registered with lua" << std::endl;
//------------------------- //-------------------------
//Run the startup scripts //Run the startup scripts
+3 -3
View File
@@ -92,9 +92,9 @@ private:
std::map<int, ClientData> clientMap; std::map<int, ClientData> clientMap;
//managers //managers
AccountManager accountMgr; AccountManager& accountMgr = AccountManager::GetSingleton();
CharacterManager characterMgr; CharacterManager& characterMgr = CharacterManager::GetSingleton();
RoomManager roomMgr; RoomManager& roomMgr = RoomManager::GetSingleton();
//misc //misc
bool running = true; bool running = true;
-32
View File
@@ -1,5 +1,4 @@
TODO: encapsulate the data structures TODO: encapsulate the data structures
TODO: The server's managers should be singletons too
TODO: Get the rooms working TODO: Get the rooms working
TODO: A proper logging system TODO: A proper logging system
TODO: Move the statistics into their own SQL table, instead of duplicating the structure a dozen times TODO: Move the statistics into their own SQL table, instead of duplicating the structure a dozen times
@@ -10,34 +9,3 @@ TODO: server is slaved to the client
TODO: Time delay for requesting region packets TODO: Time delay for requesting region packets
TODO: command line parameters overriding config.cfg settings TODO: command line parameters overriding config.cfg settings
--Requirements--
The enemies need AI scripts
The scripts need to be able to generate other enemies (frog king).
The characters need a flag to show if they're in a combat instance or not, to signify of they should be unloaded client-side
On each game loop, the server should envoke each combat instance's update function
Each combat instance invokes each enemy's and character's update functions
These update functions increase the ATB guagues
if an ATB guage is full
than the stored command is executed
the players issue their commands during the build up
if there isn't a command ready, then the player is still choosing
for the enemies, the stored commands are driven by scripts, so when the enemies need to attack, their attached scripts are called.
after the commands are called, the ATB is reset to 0.
etc...
--Enemy API--
enemyTables -- The global store of enemy tables. Only accessed by C++ code (unless you want to break something).
enemy.new(parameters) -- return a new enemy object
table.logic: the AI logic. If null, do nothing
table.ref: reference to the enemy itself, for use by API functions, set by constructor?
combat -- the combat API
combat.new(mapIndex, x, y) -- return combat instance's index
combat.pushenemy(c, enemy) -- return the enemy's position
combat.popenemy(c, position) --