Added lua, added db connection to PlayerManager

I've also revised the startup process a bit.
This commit is contained in:
Kayne Ruse
2014-03-05 00:27:21 +11:00
parent 756d4e770d
commit f17fa0f345
6 changed files with 65 additions and 18 deletions
View File
+1
View File
@@ -0,0 +1 @@
print("Lua script check OK")
+6
View File
@@ -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) {
+17 -5
View File
@@ -24,6 +24,9 @@
#include "vector2.hpp"
#include "sqlite3/sqlite3.h"
#include <functional>
#include <map>
#include <string>
@@ -36,24 +39,33 @@ struct PlayerEntry {
class PlayerManager {
public:
//clarity typedefs
typedef std::map<int, PlayerEntry> Container;
typedef Container::iterator Iterator;
typedef std::function<void(Iterator)> 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<int, PlayerEntry> playerMap;
//database connection here
Container playerMap;
sqlite3* database = nullptr;
};
#endif
+38 -12
View File
@@ -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;
}
//-------------------------
+3 -1
View File
@@ -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