Add Sqlite support for players
parent
4aaa8b166b
commit
cfa6a1df53
|
@ -13,6 +13,7 @@ namespace fs = std::filesystem;
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "PlayerAttributes.h"
|
#include "PlayerAttributes.h"
|
||||||
|
|
||||||
|
@ -23,36 +24,68 @@ PlayerAttributes::PlayerAttributes(const std::string &sourceDirectory)
|
||||||
{
|
{
|
||||||
const string playersPath = sourceDirectory + "players";
|
const string playersPath = sourceDirectory + "players";
|
||||||
|
|
||||||
|
// if players.sqlite, could not be opened, then process the playerfiles
|
||||||
|
if (!extractPlayersSqlite(playersPath)) {
|
||||||
#ifdef HAVE_FILESYSTEM
|
#ifdef HAVE_FILESYSTEM
|
||||||
for (const auto &dirEntry : fs::directory_iterator(playersPath)) {
|
for (const auto &dirEntry : fs::directory_iterator(playersPath)) {
|
||||||
cout << dirEntry << std::endl;
|
cout << dirEntry << std::endl;
|
||||||
//dirEntry.path().filename();
|
//dirEntry.path().filename();
|
||||||
|
|
||||||
extractPlayer(dirEntry.path().string());
|
extractPlayer(dirEntry.path().string());
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
dir = opendir(playersPath.c_str());
|
dir = opendir(playersPath.c_str());
|
||||||
if (dir == NULL) {
|
if (dir == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
struct dirent *ent;
|
|
||||||
while ((ent = readdir(dir)) != NULL) {
|
|
||||||
if (ent->d_name[0] == '.') {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const string path = playersPath + '/' + ent->d_name;
|
struct dirent *ent;
|
||||||
|
while ((ent = readdir(dir)) != NULL) {
|
||||||
|
if (ent->d_name[0] == '.') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
extractPlayer(path);
|
const string path = playersPath + '/' + ent->d_name;
|
||||||
}
|
|
||||||
closedir(dir);
|
extractPlayer(path);
|
||||||
|
}
|
||||||
|
closedir(dir);
|
||||||
#endif // HAVE_FILESYSTEM
|
#endif // HAVE_FILESYSTEM
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool PlayerAttributes::extractPlayersSqlite(const std::string &playersPath)
|
||||||
|
{
|
||||||
|
const string playersDB = playersPath + ".sqlite";
|
||||||
|
const string statement = "SELECT name, posX, posY, posZ FROM player";
|
||||||
|
|
||||||
|
int sqlite_state = sqlite3_open_v2(playersDB.c_str(), &db, SQLITE_OPEN_READONLY | SQLITE_OPEN_PRIVATECACHE, nullptr);
|
||||||
|
if (sqlite_state == SQLITE_CANTOPEN) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sqlite_state != SQLITE_OK) {
|
||||||
|
throw std::runtime_error(std::string(sqlite3_errmsg(db)) + ", Database file: " + playersDB);
|
||||||
|
}
|
||||||
|
if (SQLITE_OK != sqlite3_prepare_v2(db, statement.c_str(), -1, &preparedStatement, nullptr)) {
|
||||||
|
throw std::runtime_error("Failed to prepare SQL statement (dataVersionStatement)");
|
||||||
|
}
|
||||||
|
|
||||||
|
while (sqlite3_step(preparedStatement) == SQLITE_ROW) {
|
||||||
|
Player player;
|
||||||
|
player.name = reinterpret_cast<const char*>(sqlite3_column_text(preparedStatement, 0));
|
||||||
|
player.x = sqlite3_column_double(preparedStatement, 1);
|
||||||
|
player.y = sqlite3_column_double(preparedStatement, 2);
|
||||||
|
player.z = sqlite3_column_double(preparedStatement, 3);
|
||||||
|
m_players.push_back(player);
|
||||||
|
};
|
||||||
|
sqlite3_finalize(preparedStatement);
|
||||||
|
sqlite3_close(db);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void PlayerAttributes::extractPlayer(const std::string &path)
|
void PlayerAttributes::extractPlayer(const std::string &path)
|
||||||
{
|
{
|
||||||
ifstream in;
|
ifstream in;
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sqlite3.h>
|
||||||
|
|
||||||
struct Player
|
struct Player
|
||||||
{
|
{
|
||||||
|
@ -24,5 +25,9 @@ public:
|
||||||
private:
|
private:
|
||||||
Players m_players;
|
Players m_players;
|
||||||
void extractPlayer(const std::string &path);
|
void extractPlayer(const std::string &path);
|
||||||
|
|
||||||
|
bool extractPlayersSqlite(const std::string &playersPath); //returns false if database could not found
|
||||||
|
sqlite3_stmt *preparedStatement = nullptr;
|
||||||
|
sqlite3 *db = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue