Added lua, added db connection to PlayerManager
I've also revised the startup process a bit.
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
print("Lua script check OK")
|
||||||
@@ -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) {
|
PlayerEntry* PlayerManager::GetPlayer(int uniqueID) {
|
||||||
for (auto& it : playerMap) {
|
for (auto& it : playerMap) {
|
||||||
if (it.first == uniqueID) {
|
if (it.first == uniqueID) {
|
||||||
|
|||||||
@@ -24,6 +24,9 @@
|
|||||||
|
|
||||||
#include "vector2.hpp"
|
#include "vector2.hpp"
|
||||||
|
|
||||||
|
#include "sqlite3/sqlite3.h"
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -36,24 +39,33 @@ struct PlayerEntry {
|
|||||||
|
|
||||||
class PlayerManager {
|
class PlayerManager {
|
||||||
public:
|
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
|
//These functions interact with the database
|
||||||
//*Deletion, *Load and *Unload returns: 0 success, -1 failure
|
//*Deletion, *Load and *Unload returns: 0 success, -1 failure
|
||||||
//*Creation returns the uniqueID, but doesn't load
|
//*Creation returns the uniqueID, but doesn't load
|
||||||
// that object; call *Load directly afterward
|
// that object; call *Load directly afterward
|
||||||
|
|
||||||
int HandlePlayerCreation (std::string name, std::string avatar);
|
int HandlePlayerCreation (std::string name, std::string avatar);
|
||||||
int HandlePlayerDeletion (int uniqueID);
|
int HandlePlayerDeletion (int uniqueID);
|
||||||
int HandlePlayerLoad (int uniqueID, int clientIndex);
|
int HandlePlayerLoad (int uniqueID, int clientIndex);
|
||||||
int HandlePlayerUnload (int uniqueID);
|
int HandlePlayerUnload (int uniqueID);
|
||||||
|
|
||||||
//basic accessor
|
//lambdas
|
||||||
|
void ForEach(Lambda);
|
||||||
|
|
||||||
|
//accessors
|
||||||
PlayerEntry* GetPlayer(int uniqueID);
|
PlayerEntry* GetPlayer(int uniqueID);
|
||||||
|
sqlite3* SetDatabase(sqlite3* db) { return database = db; }
|
||||||
|
sqlite3* GetDatabase() { return database; }
|
||||||
|
|
||||||
//update each player's position
|
//update each player's position
|
||||||
void Update(double delta);
|
void Update(double delta);
|
||||||
private:
|
private:
|
||||||
std::map<int, PlayerEntry> playerMap;
|
Container playerMap;
|
||||||
//database connection here
|
sqlite3* database = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -30,16 +30,18 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void runSQLScript(sqlite3* db, std::string fname) {
|
int runSQLScript(sqlite3* db, std::string fname) {
|
||||||
//Run setup scripts
|
|
||||||
ifstream is(fname);
|
ifstream is(fname);
|
||||||
if (!is.is_open()) {
|
if (!is.is_open()) {
|
||||||
throw(runtime_error("Failed to run SQL script"));
|
return -1;
|
||||||
}
|
}
|
||||||
string script;
|
string script;
|
||||||
getline(is, script, '\0');
|
getline(is, script, '\0');
|
||||||
is.close();
|
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) {
|
void ServerApplication::Init(int argc, char** argv) {
|
||||||
|
cout << "Beginning startup" << endl;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
//load config
|
//load config
|
||||||
config.Load("rsc\\config.cfg");
|
config.Load("rsc\\config.cfg");
|
||||||
|
|
||||||
@@ -54,28 +59,45 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
if (SDL_Init(0)) {
|
if (SDL_Init(0)) {
|
||||||
throw(runtime_error("Failed to initialize SDL"));
|
throw(runtime_error("Failed to initialize SDL"));
|
||||||
}
|
}
|
||||||
cout << "initialized SDL" << endl;
|
cout << "Initialized SDL" << endl;
|
||||||
|
|
||||||
//Init SDL_net
|
//Init SDL_net
|
||||||
if (SDLNet_Init()) {
|
if (SDLNet_Init()) {
|
||||||
throw(runtime_error("Failed to initialize SDL_net"));
|
throw(runtime_error("Failed to initialize SDL_net"));
|
||||||
}
|
}
|
||||||
network.Open(config.Int("server.port"), sizeof(NetworkPacket));
|
network.Open(config.Int("server.port"), sizeof(NetworkPacket));
|
||||||
cout << "initialized SDL_net" << endl;
|
cout << "Initialized SDL_net" << endl;
|
||||||
|
|
||||||
//Init SQL
|
//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) {
|
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
|
//setup the database
|
||||||
runSQLScript(database, config["dir.scripts"] + "setup_server.sql");
|
if (runSQLScript(database, config["dir.scripts"] + "setup_server.sql")) {
|
||||||
cout << "initialized " << config["server.dbname"] << endl;
|
throw(runtime_error("Failed to initialize SQL's setup script"));
|
||||||
|
}
|
||||||
|
cout << "Initialized SQL's setup script" << endl;
|
||||||
|
|
||||||
//lua
|
//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() {
|
void ServerApplication::Loop() {
|
||||||
@@ -96,15 +118,19 @@ void ServerApplication::Loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::Quit() {
|
void ServerApplication::Quit() {
|
||||||
|
cout << "Shutting down" << endl;
|
||||||
//empty the members
|
//empty the members
|
||||||
//TODO: player manager
|
//TODO: player manager
|
||||||
//TODO: client manager
|
//TODO: client manager
|
||||||
|
|
||||||
//APIs
|
//APIs
|
||||||
|
lua_close(luaState);
|
||||||
|
playerMgr.SetDatabase(nullptr);
|
||||||
sqlite3_close_v2(database);
|
sqlite3_close_v2(database);
|
||||||
network.Close();
|
network.Close();
|
||||||
SDLNet_Quit();
|
SDLNet_Quit();
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
cout << "Shutdown finished" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#include "udp_network_utility.hpp"
|
#include "udp_network_utility.hpp"
|
||||||
|
|
||||||
//APIs
|
//APIs
|
||||||
|
#include "lua/lua.hpp"
|
||||||
#include "sqlite3/sqlite3.h"
|
#include "sqlite3/sqlite3.h"
|
||||||
#include "SDL/SDL.h"
|
#include "SDL/SDL.h"
|
||||||
|
|
||||||
@@ -73,13 +74,14 @@ private:
|
|||||||
sqlite3* database = nullptr;
|
sqlite3* database = nullptr;
|
||||||
|
|
||||||
//lua
|
//lua
|
||||||
//TODO
|
lua_State* luaState = nullptr;
|
||||||
|
|
||||||
//misc
|
//misc
|
||||||
bool running = true;
|
bool running = true;
|
||||||
ConfigUtility config;
|
ConfigUtility config;
|
||||||
|
|
||||||
ClientManager clientMgr;
|
ClientManager clientMgr;
|
||||||
|
PlayerManager playerMgr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user