Skip to content

Commit

Permalink
[server] Retrieval of player data from db #6
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Apr 14, 2020
1 parent 4d79b4a commit 8722173
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
37 changes: 36 additions & 1 deletion src/server/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ int Database::connectUser(const std::string& name, const std::string& password)
return code;
}

// Bind values
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":name"), name.c_str(), -1, SQLITE_STATIC);

while (sqlite3_step(stmt) == SQLITE_ROW) {
found = true;
const char* pass = (const char*)sqlite3_column_text(stmt, 1);
Expand Down Expand Up @@ -274,7 +277,39 @@ int Database::deleteItem(const std::string& name, const int itemCode) {
}

sqlite3_finalize(stmt);
return code;;
return code;
}

/**
* \brief Get the basic info for a given user
* \param name of the requested user
* \return the associated info. Guild info is not set by this method.
*/
PlayerBasicInfo Database::getUserInfo(const std::string& name) {
PlayerBasicInfo info;
sqlite3_stmt* stmt = nullptr;

// Prepare query
int result = sqlite3_prepare_v2(db, "select name, gold, cash, gp, level from USERS where name = :name", -1, &stmt, NULL);
if (result != SQLITE_OK) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", sqlite3_errmsg(db));
}

// Bind values
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":name"), name.c_str(), -1, SQLITE_STATIC);

// Run query
while (sqlite3_step(stmt) == SQLITE_ROW) {
info.name = name;
info.gold = sqlite3_column_int(stmt, 1);
info.cash = sqlite3_column_int(stmt, 2);
info.points = sqlite3_column_int(stmt, 3);
info.level = static_cast<PlayerLevel>(sqlite3_column_int(stmt, 4));
}

sqlite3_finalize(stmt);

return info;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/server/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string>
#include <sqlite3.h>
#include "../common/commonitem.hpp"
#include "../common/commonplayer.hpp"

class Database {
public:
Expand All @@ -30,11 +31,12 @@ class Database {
int buyItem(const std::string& name, const int itemCode, ItemType type, ItemValidity validity);
int wearItem(const std::string& name, const int itemCode, ItemType type, bool wear);
int deleteItem(const std::string& name, const int itemCode);
PlayerBasicInfo getUserInfo(const std::string& name);

private:
void init();

sqlite3* db = nullptr;
sqlite3* db = nullptr; // database object
};

#endif //! _H_DATABASE

0 comments on commit 8722173

Please sign in to comment.