Implemented saving of user accounts (read more)
To modify an existing user account, change the in memory copy and then call the function SaveUserAccount(uid), where uid is the index of the modified account. If the save function is not called immediately, then the behaviour of the server is undefined.
This commit is contained in:
@@ -79,9 +79,9 @@ private:
|
|||||||
//TODO: manage the database
|
//TODO: manage the database
|
||||||
int CreateUserAccount(std::string username, int clientIndex);
|
int CreateUserAccount(std::string username, int clientIndex);
|
||||||
int LoadUserAccount(std::string username, int clientIndex);
|
int LoadUserAccount(std::string username, int clientIndex);
|
||||||
void SaveUserAccount(std::string username);
|
int SaveUserAccount(int uid);
|
||||||
void UnloadUserAccount(std::string username);
|
void UnloadUserAccount(int uid);
|
||||||
void DeleteUserAccount(std::string username);
|
void DeleteUserAccount(int uid);
|
||||||
|
|
||||||
//TODO: combat systems
|
//TODO: combat systems
|
||||||
|
|
||||||
|
|||||||
@@ -47,11 +47,14 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) {
|
|||||||
newClient.address = packet.meta.srcAddress;
|
newClient.address = packet.meta.srcAddress;
|
||||||
|
|
||||||
//debug
|
//debug
|
||||||
std::cout << "Function Return: " << LoadUserAccount(packet.clientInfo.username, ClientData::uidCounter) << std::endl;
|
int uid = LoadUserAccount(packet.clientInfo.username, ClientData::uidCounter);
|
||||||
|
if (uid < 0) {
|
||||||
for (auto& it : accountMap) {
|
std::cerr << "Error: Account already loaded: " << uid << std::endl;
|
||||||
std::cout << "Account(" << it.first << "): " << it.second.username << std::endl;
|
return;
|
||||||
}
|
}
|
||||||
|
accountMap[uid].blackListed = true;
|
||||||
|
accountMap[uid].whiteListed = false;
|
||||||
|
SaveUserAccount(uid);
|
||||||
|
|
||||||
//TODO: move this into the character management code
|
//TODO: move this into the character management code
|
||||||
//create the new character
|
//create the new character
|
||||||
|
|||||||
@@ -31,6 +31,7 @@
|
|||||||
|
|
||||||
static const char* CREATE_USER_ACCOUNT = "INSERT INTO UserAccounts (username) VALUES (?);";
|
static const char* CREATE_USER_ACCOUNT = "INSERT INTO UserAccounts (username) VALUES (?);";
|
||||||
static const char* LOAD_USER_ACCOUNT = "SELECT * FROM UserAccounts WHERE username = ?;";
|
static const char* LOAD_USER_ACCOUNT = "SELECT * FROM UserAccounts WHERE username = ?;";
|
||||||
|
static const char* SAVE_USER_ACCOUNT = "INSERT OR REPLACE INTO UserAccounts VALUES (?, ?, ?, ?);";
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Define the methods
|
//Define the methods
|
||||||
@@ -113,17 +114,54 @@ int ServerApplication::LoadUserAccount(std::string username, int clientIndex) {
|
|||||||
throw(std::runtime_error(std::string() + "Unknown SQL error in LoadUserAccount: " + sqlite3_errmsg(database) ));
|
throw(std::runtime_error(std::string() + "Unknown SQL error in LoadUserAccount: " + sqlite3_errmsg(database) ));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::SaveUserAccount(std::string username) {
|
int ServerApplication::SaveUserAccount(int uid) {
|
||||||
//save this user account, replacing it if it exists
|
//save this user account from memory, replacing it if it exists in the database
|
||||||
//TODO
|
//DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID.
|
||||||
|
|
||||||
|
//this method fails if this account is not loaded
|
||||||
|
if (accountMap.find(uid) == accountMap.end()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
AccountData& account = accountMap[uid];
|
||||||
|
sqlite3_stmt* statement = nullptr;
|
||||||
|
|
||||||
|
//prep
|
||||||
|
if (sqlite3_prepare_v2(database, SAVE_USER_ACCOUNT, -1, &statement, nullptr) != SQLITE_OK) {
|
||||||
|
throw( std::runtime_error(std::string() + "Failed to prepare an SQL statement: " + sqlite3_errmsg(database)) );
|
||||||
|
}
|
||||||
|
|
||||||
|
//parameters
|
||||||
|
bool ret = false;
|
||||||
|
ret |= sqlite3_bind_int(statement, 1, uid) != SQLITE_OK;
|
||||||
|
ret |= sqlite3_bind_text(statement, 2, account.username.c_str(), account.username.size() + 1, SQLITE_STATIC) != SQLITE_OK;
|
||||||
|
ret |= sqlite3_bind_int(statement, 3, account.blackListed) != SQLITE_OK;
|
||||||
|
ret |= sqlite3_bind_int(statement, 4, account.whiteListed) != SQLITE_OK;
|
||||||
|
|
||||||
|
//check for binding errors
|
||||||
|
if (ret) {
|
||||||
|
throw( std::runtime_error(std::string() + "Failed to replace a prepared statement's parameter: " + sqlite3_errmsg(database)) );
|
||||||
|
}
|
||||||
|
|
||||||
|
//execute
|
||||||
|
if (sqlite3_step(statement) != SQLITE_DONE) {
|
||||||
|
//if this fails, than something went horribly wrong
|
||||||
|
sqlite3_finalize(statement);
|
||||||
|
throw( std::runtime_error(std::string() + "Unknown SQL error when saving an account: " + sqlite3_errmsg(database)) );
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlite3_finalize(statement);
|
||||||
|
|
||||||
|
//successful execution
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::UnloadUserAccount(std::string username) {
|
void ServerApplication::UnloadUserAccount(int uid) {
|
||||||
//save this user account, and then unload it
|
//save this user account, and then unload it
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::DeleteUserAccount(std::string username) {
|
void ServerApplication::DeleteUserAccount(int uid) {
|
||||||
//delete a user account from the database, and remove it from memory
|
//delete a user account from the database, and remove it from memory
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user