Skip to content

Commit

Permalink
[server] Retrieval of item data #6
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Apr 15, 2020
1 parent 8722173 commit 91c0b92
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/server/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,32 @@ int Database::wearItem(const std::string& name, const int itemCode, ItemType typ
return code;
}

/**
* \brief Get the list of items associated with a given user
* \param name name for the user
* TODO: define the proper way to return (or make use of) the retrieved list
*/
void Database::getItemsForUser(const std::string& name) {
sqlite3_stmt* stmt = nullptr;
int code = -1;

int result = sqlite3_prepare_v2(db, "select item, end_date, is_worn from PURCHASES where user = :name and date('now') <= date(end_date)", -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);

while (sqlite3_step(stmt) == SQLITE_ROW) {
int itemCode = sqlite3_column_int(stmt, 0);
const char* dateText = (const char*)sqlite3_column_text(stmt, 1);
bool isWorn = sqlite3_column_int(stmt, 2) == 1;
}

sqlite3_finalize(stmt);
}

/**
* \brief Remove an item
* \param name name for the user
Expand Down Expand Up @@ -343,7 +369,7 @@ void Database::init() {
"user text not null, " \
"item int not null, " \
"type int not null, " \
"end_date text not null, " \
"end_date date not null, " \
"is_worn int default 0);";

rc = sqlite3_exec(db, sql.c_str(), NULL, 0, &zErrMsg);
Expand Down
1 change: 1 addition & 0 deletions src/server/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Database {
int updateUser(const std::string &name, const int goldDelta, const int gpDelta = 0);
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);
void getItemsForUser(const std::string& name);
int deleteItem(const std::string& name, const int itemCode);
PlayerBasicInfo getUserInfo(const std::string& name);

Expand Down

0 comments on commit 91c0b92

Please sign in to comment.