aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfusion32 <marcopuzziello@gmail.com>2025-10-14 19:15:28 -0300
committerfusion32 <marcopuzziello@gmail.com>2025-10-14 19:15:28 -0300
commit00d91512b48c70c3cebcdb5ed37c601653425bf6 (patch)
treed709f7473e015c5f8274e0bbaddea95706787019 /src
parente1c2920ca25d5f69a8fd82eb26304fc0cbcc8684 (diff)
downloadquerymanager-00d91512b48c70c3cebcdb5ed37c601653425bf6.tar.gz
querymanager-00d91512b48c70c3cebcdb5ed37c601653425bf6.zip
GET_WORLD_CONFIG robustness + tidying up a few variable names
Diffstat (limited to 'src')
-rw-r--r--src/database_postgres.cc24
-rw-r--r--src/database_sqlite.cc65
-rw-r--r--src/hostcache.cc6
-rw-r--r--src/query.cc1
-rw-r--r--src/querymanager.hh1
5 files changed, 53 insertions, 44 deletions
diff --git a/src/database_postgres.cc b/src/database_postgres.cc
index 5397298..d6bd15b 100644
--- a/src/database_postgres.cc
+++ b/src/database_postgres.cc
@@ -18,8 +18,7 @@
// IMPORTANT(fusion): Address families used with INET and CIDR binary format.
// They're taken from `utils/inet.h` which is not included with libpq but should
-// be stable across different systems, mostly because AF_INET should be stable
-// across different systems.
+// be stable across different systems, mostly because AF_INET should be stable.
#define POSTGRESQL_AF_INET (AF_INET + 0)
#define POSTGRESQL_AF_INET6 (AF_INET + 1)
STATIC_ASSERT(POSTGRESQL_AF_INET == 2);
@@ -904,7 +903,7 @@ TDatabase *DatabaseOpen(void){
for(int i = 0; i <= 1; i += 1)
for(int j = 0; j <= 1; j += 1){
- LOG("TEXT (%d, %d)", i, j);
+ LOG("TEST (%d, %d)", i, j);
ParamBuffer Params;
ParamBegin(&Params, 2, i);
ParamTimestamp(&Params, Timestamp);
@@ -1018,7 +1017,7 @@ bool GetWorlds(TDatabase *Database, DynamicArray<TWorld> *Worlds){
bool GetWorldConfig(TDatabase *Database, int WorldID, TWorldConfig *WorldConfig){
ASSERT(Database != NULL && WorldConfig != NULL);
const char *Stmt = PrepareQuery(Database,
- "SELECT Type, RebootTime, Host, Port, MaxPlayers,"
+ "SELECT WorldID, Type, RebootTime, Host, Port, MaxPlayers,"
" PremiumPlayerBuffer, MaxNewbies, PremiumNewbieBuffer"
" FROM Worlds WHERE WorldID = $1::INTEGER");
if(Stmt == NULL){
@@ -1041,14 +1040,15 @@ bool GetWorldConfig(TDatabase *Database, int WorldID, TWorldConfig *WorldConfig)
// empty result set.
memset(WorldConfig, 0, sizeof(TWorldConfig));
if(PQntuples(Result) > 0){
- WorldConfig->Type = GetResultInt(Result, 0, 0);
- WorldConfig->RebootTime = GetResultInt(Result, 0, 1);
- StringBufCopy(WorldConfig->HostName, GetResultText(Result, 0, 2));
- WorldConfig->Port = GetResultInt(Result, 0, 3);
- WorldConfig->MaxPlayers = GetResultInt(Result, 0, 4);
- WorldConfig->PremiumPlayerBuffer = GetResultInt(Result, 0, 5);
- WorldConfig->MaxNewbies = GetResultInt(Result, 0, 6);
- WorldConfig->PremiumNewbieBuffer = GetResultInt(Result, 0, 7);
+ WorldConfig->WorldID = GetResultInt(Result, 0, 0);
+ WorldConfig->Type = GetResultInt(Result, 0, 1);
+ WorldConfig->RebootTime = GetResultInt(Result, 0, 2);
+ StringBufCopy(WorldConfig->HostName, GetResultText(Result, 0, 3));
+ WorldConfig->Port = GetResultInt(Result, 0, 4);
+ WorldConfig->MaxPlayers = GetResultInt(Result, 0, 5);
+ WorldConfig->PremiumPlayerBuffer = GetResultInt(Result, 0, 6);
+ WorldConfig->MaxNewbies = GetResultInt(Result, 0, 7);
+ WorldConfig->PremiumNewbieBuffer = GetResultInt(Result, 0, 8);
}
return true;
diff --git a/src/database_sqlite.cc b/src/database_sqlite.cc
index 85d8fc7..4bd991d 100644
--- a/src/database_sqlite.cc
+++ b/src/database_sqlite.cc
@@ -509,7 +509,7 @@ bool GetWorlds(TDatabase *Database, DynamicArray<TWorld> *Worlds){
bool GetWorldConfig(TDatabase *Database, int WorldID, TWorldConfig *WorldConfig){
ASSERT(Database != NULL && WorldConfig != NULL);
sqlite3_stmt *Stmt = PrepareQuery(Database,
- "SELECT Type, RebootTime, Host, Port, MaxPlayers,"
+ "SELECT WorldID, Type, RebootTime, Host, Port, MaxPlayers,"
" PremiumPlayerBuffer, MaxNewbies, PremiumNewbieBuffer"
" FROM Worlds WHERE WorldID = ?1");
if(Stmt == NULL){
@@ -523,19 +523,25 @@ bool GetWorldConfig(TDatabase *Database, int WorldID, TWorldConfig *WorldConfig)
return false;
}
- if(sqlite3_step(Stmt) != SQLITE_ROW){
+ int ErrorCode = sqlite3_step(Stmt);
+ if(ErrorCode != SQLITE_ROW && ErrorCode != SQLITE_DONE){
LOG_ERR("Failed to execute query: %s", sqlite3_errmsg(Database->Handle));
return false;
}
- WorldConfig->Type = sqlite3_column_int(Stmt, 0);
- WorldConfig->RebootTime = sqlite3_column_int(Stmt, 1);
- StringBufCopy(WorldConfig->HostName, (const char*)sqlite3_column_text(Stmt, 2));
- WorldConfig->Port = sqlite3_column_int(Stmt, 3);
- WorldConfig->MaxPlayers = sqlite3_column_int(Stmt, 4);
- WorldConfig->PremiumPlayerBuffer = sqlite3_column_int(Stmt, 5);
- WorldConfig->MaxNewbies = sqlite3_column_int(Stmt, 6);
- WorldConfig->PremiumNewbieBuffer = sqlite3_column_int(Stmt, 7);
+ memset(WorldConfig, 0, sizeof(TWorldConfig));
+ if(ErrorCode == SQLITE_ROW){
+ WorldConfig->WorldID = sqlite3_column_int(Stmt, 0);
+ WorldConfig->Type = sqlite3_column_int(Stmt, 1);
+ WorldConfig->RebootTime = sqlite3_column_int(Stmt, 2);
+ StringBufCopy(WorldConfig->HostName, (const char*)sqlite3_column_text(Stmt, 3));
+ WorldConfig->Port = sqlite3_column_int(Stmt, 4);
+ WorldConfig->MaxPlayers = sqlite3_column_int(Stmt, 5);
+ WorldConfig->PremiumPlayerBuffer = sqlite3_column_int(Stmt, 6);
+ WorldConfig->MaxNewbies = sqlite3_column_int(Stmt, 7);
+ WorldConfig->PremiumNewbieBuffer = sqlite3_column_int(Stmt, 8);
+ }
+
return true;
}
@@ -555,13 +561,13 @@ bool AccountExists(TDatabase *Database, int AccountID, const char *Email, bool *
return false;
}
- int ErrCode = sqlite3_step(Stmt);
- if(ErrCode != SQLITE_ROW && ErrCode != SQLITE_DONE){
+ int ErrorCode = sqlite3_step(Stmt);
+ if(ErrorCode != SQLITE_ROW && ErrorCode != SQLITE_DONE){
LOG_ERR("Failed to execute query: %s", sqlite3_errmsg(Database->Handle));
return false;
}
- *Result = (ErrCode == SQLITE_ROW);
+ *Result = (ErrorCode == SQLITE_ROW);
return true;
}
@@ -580,13 +586,13 @@ bool AccountNumberExists(TDatabase *Database, int AccountID, bool *Result){
return false;
}
- int ErrCode = sqlite3_step(Stmt);
- if(ErrCode != SQLITE_ROW && ErrCode != SQLITE_DONE){
+ int ErrorCode = sqlite3_step(Stmt);
+ if(ErrorCode != SQLITE_ROW && ErrorCode != SQLITE_DONE){
LOG_ERR("Failed to execute query: %s", sqlite3_errmsg(Database->Handle));
return false;
}
- *Result = (ErrCode == SQLITE_ROW);
+ *Result = (ErrorCode == SQLITE_ROW);
return true;
}
@@ -605,13 +611,13 @@ bool AccountEmailExists(TDatabase *Database, const char *Email, bool *Result){
return false;
}
- int ErrCode = sqlite3_step(Stmt);
- if(ErrCode != SQLITE_ROW && ErrCode != SQLITE_DONE){
+ int ErrorCode = sqlite3_step(Stmt);
+ if(ErrorCode != SQLITE_ROW && ErrorCode != SQLITE_DONE){
LOG_ERR("Failed to execute query: %s", sqlite3_errmsg(Database->Handle));
return false;
}
- *Result = (ErrCode == SQLITE_ROW);
+ *Result = (ErrorCode == SQLITE_ROW);
return true;
}
@@ -634,14 +640,14 @@ bool CreateAccount(TDatabase *Database, int AccountID, const char *Email, const
return false;
}
- int ErrCode = sqlite3_step(Stmt);
- if(ErrCode != SQLITE_DONE && ErrCode != SQLITE_CONSTRAINT){
+ int ErrorCode = sqlite3_step(Stmt);
+ if(ErrorCode != SQLITE_DONE && ErrorCode != SQLITE_CONSTRAINT){
LOG_ERR("Failed to execute query: %s", sqlite3_errmsg(Database->Handle));
return false;
}
// TODO(fusion): Maybe have a `ContraintError` output param?
- return (ErrCode == SQLITE_DONE);
+ return (ErrorCode == SQLITE_DONE);
}
bool GetAccountData(TDatabase *Database, int AccountID, TAccount *Account){
@@ -857,13 +863,13 @@ bool CharacterNameExists(TDatabase *Database, const char *Name, bool *Result){
return false;
}
- int ErrCode = sqlite3_step(Stmt);
- if(ErrCode != SQLITE_ROW && ErrCode != SQLITE_DONE){
+ int ErrorCode = sqlite3_step(Stmt);
+ if(ErrorCode != SQLITE_ROW && ErrorCode != SQLITE_DONE){
LOG_ERR("Failed to execute query: %s", sqlite3_errmsg(Database->Handle));
return false;
}
- *Result = (ErrCode == SQLITE_ROW);
+ *Result = (ErrorCode == SQLITE_ROW);
return false;
}
@@ -886,14 +892,14 @@ bool CreateCharacter(TDatabase *Database, int WorldID, int AccountID, const char
return false;
}
- int ErrCode = sqlite3_step(Stmt);
- if(ErrCode != SQLITE_DONE && ErrCode != SQLITE_CONSTRAINT){
+ int ErrorCode = sqlite3_step(Stmt);
+ if(ErrorCode != SQLITE_DONE && ErrorCode != SQLITE_CONSTRAINT){
LOG_ERR("Failed to execute query: %s", sqlite3_errmsg(Database->Handle));
return false;
}
// TODO(fusion): Same as `CreateAccount`?
- return (ErrCode == SQLITE_DONE);
+ return (ErrorCode == SQLITE_DONE);
}
bool GetCharacterID(TDatabase *Database, int WorldID, const char *CharacterName, int *CharacterID){
@@ -1250,7 +1256,8 @@ bool GetCharacterIndexEntries(TDatabase *Database, int WorldID, int MinimumChara
EntryIndex += 1;
}
- if(sqlite3_errcode(Database->Handle) != SQLITE_DONE){
+ int ErrorCode = sqlite3_errcode(Database->Handle);
+ if(ErrorCode != SQLITE_ROW && ErrorCode != SQLITE_DONE){
LOG_ERR("Failed to execute query: %s", sqlite3_errmsg(Database->Handle));
return false;
}
diff --git a/src/hostcache.cc b/src/hostcache.cc
index ef84def..f32b0f7 100644
--- a/src/hostcache.cc
+++ b/src/hostcache.cc
@@ -36,9 +36,9 @@ static bool DoResolveHostName(const char *HostName, int *OutAddr){
addrinfo Hints = {};
Hints.ai_family = AF_INET;
Hints.ai_socktype = SOCK_STREAM;
- int ErrCode = getaddrinfo(HostName, NULL, &Hints, &Result);
- if(ErrCode != 0){
- LOG_ERR("Failed to resolve hostname \"%s\": %s", HostName, gai_strerror(ErrCode));
+ int ErrorCode = getaddrinfo(HostName, NULL, &Hints, &Result);
+ if(ErrorCode != 0){
+ LOG_ERR("Failed to resolve hostname \"%s\": %s", HostName, gai_strerror(ErrorCode));
return false;
}
diff --git a/src/query.cc b/src/query.cc
index 2cf74ea..b9484c6 100644
--- a/src/query.cc
+++ b/src/query.cc
@@ -1342,6 +1342,7 @@ void ProcessCancelHouseTransfer(TDatabase *Database, TQuery *Query){
void ProcessLoadWorldConfig(TDatabase *Database, TQuery *Query){
TWorldConfig WorldConfig = {};
QUERY_STOP_IF(!GetWorldConfig(Database, Query->WorldID, &WorldConfig));
+ QUERY_FAIL_IF(WorldConfig.WorldID == 0);
int IPAddress;
QUERY_FAIL_IF(!ResolveHostName(WorldConfig.HostName, &IPAddress));
diff --git a/src/querymanager.hh b/src/querymanager.hh
index 66b049f..b5b8f0c 100644
--- a/src/querymanager.hh
+++ b/src/querymanager.hh
@@ -663,6 +663,7 @@ struct TWorld{
};
struct TWorldConfig{
+ int WorldID;
int Type;
int RebootTime;
char HostName[100];