From f17fa0f3459bb91df46bab369f651f2d41d391c4 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 5 Mar 2014 00:27:21 +1100 Subject: [PATCH] Added lua, added db connection to PlayerManager I've also revised the startup process a bit. --- rsc/scripts/setup_room.lua | 0 rsc/scripts/setup_server.lua | 1 + server/player_manager.cpp | 6 +++++ server/player_manager.hpp | 22 +++++++++++---- server/server_application.cpp | 50 ++++++++++++++++++++++++++--------- server/server_application.hpp | 4 ++- 6 files changed, 65 insertions(+), 18 deletions(-) delete mode 100644 rsc/scripts/setup_room.lua diff --git a/rsc/scripts/setup_room.lua b/rsc/scripts/setup_room.lua deleted file mode 100644 index e69de29..0000000 diff --git a/rsc/scripts/setup_server.lua b/rsc/scripts/setup_server.lua index e69de29..a2cf14a 100644 --- a/rsc/scripts/setup_server.lua +++ b/rsc/scripts/setup_server.lua @@ -0,0 +1 @@ +print("Lua script check OK") \ No newline at end of file diff --git a/server/player_manager.cpp b/server/player_manager.cpp index a708874..d5bfebe 100644 --- a/server/player_manager.cpp +++ b/server/player_manager.cpp @@ -37,6 +37,12 @@ int PlayerManager::HandlePlayerUnload(int uniqueID) { // } +void PlayerManager::ForEach(Lambda fn) { + for(Iterator it = playerMap.begin(); it != playerMap.end(); it++) { + fn(it); + } +} + PlayerEntry* PlayerManager::GetPlayer(int uniqueID) { for (auto& it : playerMap) { if (it.first == uniqueID) { diff --git a/server/player_manager.hpp b/server/player_manager.hpp index 50ba849..d4051bd 100644 --- a/server/player_manager.hpp +++ b/server/player_manager.hpp @@ -24,6 +24,9 @@ #include "vector2.hpp" +#include "sqlite3/sqlite3.h" + +#include #include #include @@ -36,24 +39,33 @@ struct PlayerEntry { class PlayerManager { public: + //clarity typedefs + typedef std::map Container; + typedef Container::iterator Iterator; + typedef std::function Lambda; + //These functions interact with the database //*Deletion, *Load and *Unload returns: 0 success, -1 failure //*Creation returns the uniqueID, but doesn't load // that object; call *Load directly afterward - int HandlePlayerCreation (std::string name, std::string avatar); int HandlePlayerDeletion (int uniqueID); int HandlePlayerLoad (int uniqueID, int clientIndex); int HandlePlayerUnload (int uniqueID); - //basic accessor - PlayerEntry* GetPlayer (int uniqueID); + //lambdas + void ForEach(Lambda); + + //accessors + PlayerEntry* GetPlayer(int uniqueID); + sqlite3* SetDatabase(sqlite3* db) { return database = db; } + sqlite3* GetDatabase() { return database; } //update each player's position void Update(double delta); private: - std::map playerMap; - //database connection here + Container playerMap; + sqlite3* database = nullptr; }; #endif diff --git a/server/server_application.cpp b/server/server_application.cpp index 0b008b3..e64adbb 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -30,16 +30,18 @@ using namespace std; -void runSQLScript(sqlite3* db, std::string fname) { - //Run setup scripts +int runSQLScript(sqlite3* db, std::string fname) { ifstream is(fname); if (!is.is_open()) { - throw(runtime_error("Failed to run SQL script")); + return -1; } string script; getline(is, script, '\0'); is.close(); - sqlite3_exec(db, script.c_str(), nullptr, nullptr, nullptr); + //TODO: flesh out this error if needed + if (sqlite3_exec(db, script.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { + return -2; + } } //------------------------- @@ -47,6 +49,9 @@ void runSQLScript(sqlite3* db, std::string fname) { //------------------------- void ServerApplication::Init(int argc, char** argv) { + cout << "Beginning startup" << endl; + int ret = 0; + //load config config.Load("rsc\\config.cfg"); @@ -54,28 +59,45 @@ void ServerApplication::Init(int argc, char** argv) { if (SDL_Init(0)) { throw(runtime_error("Failed to initialize SDL")); } - cout << "initialized SDL" << endl; + cout << "Initialized SDL" << endl; //Init SDL_net if (SDLNet_Init()) { throw(runtime_error("Failed to initialize SDL_net")); } network.Open(config.Int("server.port"), sizeof(NetworkPacket)); - cout << "initialized SDL_net" << endl; + cout << "Initialized SDL_net" << endl; //Init SQL - int ret = sqlite3_open_v2(config["server.dbname"].c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr); + ret = sqlite3_open_v2(config["server.dbname"].c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr); if (ret != SQLITE_OK || !database) { - throw(runtime_error("Failed to open the server database")); + throw(runtime_error(string() + "Failed to initialize SQL: " + sqlite3_errmsg(database) )); } - cout << "initialized SQL" << endl; + playerMgr.SetDatabase(database); + cout << "Initialized SQL" << endl; //setup the database - runSQLScript(database, config["dir.scripts"] + "setup_server.sql"); - cout << "initialized " << config["server.dbname"] << endl; + if (runSQLScript(database, config["dir.scripts"] + "setup_server.sql")) { + throw(runtime_error("Failed to initialize SQL's setup script")); + } + cout << "Initialized SQL's setup script" << endl; //lua - //TODO + luaState = luaL_newstate(); + if (!luaState) { + throw(runtime_error("Failed to initialize lua")); + } + luaL_openlibs(luaState); + cout << "Initialized lua" << endl; + + //run the startup script + if (luaL_dofile(luaState, (config["dir.scripts"] + "setup_server.lua").c_str())) { + throw(runtime_error(string() + "Failed to initialize lua's setup script: " + lua_tostring(luaState, -1) )); + } + cout << "Initialized lua's setup script" << endl; + + //finalize the startup + cout << "Startup completed successfully" << endl; } void ServerApplication::Loop() { @@ -96,15 +118,19 @@ void ServerApplication::Loop() { } void ServerApplication::Quit() { + cout << "Shutting down" << endl; //empty the members //TODO: player manager //TODO: client manager //APIs + lua_close(luaState); + playerMgr.SetDatabase(nullptr); sqlite3_close_v2(database); network.Close(); SDLNet_Quit(); SDL_Quit(); + cout << "Shutdown finished" << endl; } //------------------------- diff --git a/server/server_application.hpp b/server/server_application.hpp index 5aeb872..bc033af 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -27,6 +27,7 @@ #include "udp_network_utility.hpp" //APIs +#include "lua/lua.hpp" #include "sqlite3/sqlite3.h" #include "SDL/SDL.h" @@ -73,13 +74,14 @@ private: sqlite3* database = nullptr; //lua - //TODO + lua_State* luaState = nullptr; //misc bool running = true; ConfigUtility config; ClientManager clientMgr; + PlayerManager playerMgr; }; #endif