Skip to content

Commit

Permalink
Add check to create account if none exist on first startup
Browse files Browse the repository at this point in the history
  • Loading branch information
EmosewaMC committed Dec 8, 2024
1 parent e0acca3 commit 66507d8
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 7 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Darkflame Universe is a server emulator and does not distribute any LEGO® Unive
* Single player installs now no longer require building the server from source or installing development tools.
* Download the [latest release](https://github.com/DarkflameUniverse/DarkflameServer/releases) and extract the files into a folder inside your client.
* You should be able to see the folder with the server executables in the same folder as `legouniverse.exe`.
* To run the server, double-click MasterServer.exe.
* A default account called `admin` will be created with a password of `password`.
* To run the server, double-click `MasterServer.exe`.
* You will be asked to create an account the first time you run the server.
* When shutting down the server, it is highly recommended to click the `MasterServer.exe` window and hold `ctrl` while pressing `c` to stop the server.
* We are working on a way to make it so when you close the game, the server saves automatically alongside when you open the game, the server starts automatically.

Expand Down Expand Up @@ -283,8 +283,8 @@ systemctl stop darkflame.service
journalctl -xeu darkflame.service
```

### First admin user
Run `MasterServer -a` to get prompted to create an admin account. This method is only intended for the system administrator as a means to get started, do NOT use this method to create accounts for other users!
### First user or adding more users.
The first time you run `MasterServer`, you will be prompted to create an account. To create more accounts from the command line, `MasterServer -a` to get prompted to create an admin account. This method is only intended for the system administrator as a means to get started, do NOT use this method to create accounts for other users!

### Account management tool (Nexus Dashboard)
**If you are just using this server for yourself, you can skip setting up Nexus Dashboard**
Expand Down
2 changes: 2 additions & 0 deletions dDatabase/GameDatabase/ITables/IAccounts.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class IAccounts {

// Update the GameMaster level of an account.
virtual void UpdateAccountGmLevel(const uint32_t accountId, const eGameMasterLevel gmLevel) = 0;

virtual uint32_t GetAccountCount() = 0;
};

#endif //!__IACCOUNTS__H__
1 change: 1 addition & 0 deletions dDatabase/GameDatabase/MySQL/MySQLDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class MySQLDatabase : public GameDatabase {
void InsertUgcBuild(const std::string& modules, const LWOOBJID bigId, const std::optional<uint32_t> characterId) override;
void DeleteUgcBuild(const LWOOBJID bigId) override;
sql::PreparedStatement* CreatePreppedStmt(const std::string& query);
uint32_t GetAccountCount() override;
private:

// Generic query functions that can be used for any query.
Expand Down
5 changes: 5 additions & 0 deletions dDatabase/GameDatabase/MySQL/Tables/Accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ void MySQLDatabase::InsertNewAccount(const std::string_view username, const std:
void MySQLDatabase::UpdateAccountGmLevel(const uint32_t accountId, const eGameMasterLevel gmLevel) {
ExecuteUpdate("UPDATE accounts SET gm_level = ? WHERE id = ?;", static_cast<int32_t>(gmLevel), accountId);
}

uint32_t MySQLDatabase::GetAccountCount() {
auto res = ExecuteSelect("SELECT COUNT(*) as count FROM accounts;");
return res->next() ? res->getUInt("count") : 0;
}
1 change: 1 addition & 0 deletions dDatabase/GameDatabase/SQLite/SQLiteDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class SQLiteDatabase : public GameDatabase {
void IncrementNumWins(const uint32_t playerId, const uint32_t gameId) override;
void InsertUgcBuild(const std::string& modules, const LWOOBJID bigId, const std::optional<uint32_t> characterId) override;
void DeleteUgcBuild(const LWOOBJID bigId) override;
uint32_t GetAccountCount() override;
private:
CppSQLite3Statement CreatePreppedStmt(const std::string& query);

Expand Down
7 changes: 7 additions & 0 deletions dDatabase/GameDatabase/SQLite/Tables/Accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ void SQLiteDatabase::InsertNewAccount(const std::string_view username, const std
void SQLiteDatabase::UpdateAccountGmLevel(const uint32_t accountId, const eGameMasterLevel gmLevel) {
ExecuteUpdate("UPDATE accounts SET gm_level = ? WHERE id = ?;", static_cast<int32_t>(gmLevel), accountId);
}

uint32_t SQLiteDatabase::GetAccountCount() {
auto [_, res] = ExecuteSelect("SELECT COUNT(*) as count FROM accounts;");
if (res.eof()) return 0;

return res.getIntField("count");
}
1 change: 1 addition & 0 deletions dDatabase/GameDatabase/TestSQL/TestSQLDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class TestSQLDatabase : public GameDatabase {
void IncrementNumWins(const uint32_t playerId, const uint32_t gameId) override {};
void InsertUgcBuild(const std::string& modules, const LWOOBJID bigId, const std::optional<uint32_t> characterId) override {};
void DeleteUgcBuild(const LWOOBJID bigId) override {};
uint32_t GetAccountCount() override { return 0; };
};

#endif //!TESTSQLDATABASE_H
10 changes: 7 additions & 3 deletions dMasterServer/MasterServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,16 @@ int main(int argc, char** argv) {
}

// Run migrations should any need to be run.
MigrationRunner::RunSQLiteMigrations();
MigrationRunner::RunSQLiteMigrations();

//If the first command line argument is -a or --account then make the user
//input a username and password, with the password being hidden.
if (argc > 1 &&
(strcmp(argv[1], "-a") == 0 || strcmp(argv[1], "--account") == 0)) {
bool doesAnyAccountExist = Database::Get()->GetAccountCount() > 0;
if (!doesAnyAccountExist) {
LOG("No accounts exist in the database. Please create an account.");
}
if ((argc > 1 &&
(strcmp(argv[1], "-a") == 0 || strcmp(argv[1], "--account") == 0)) || !doesAnyAccountExist) {
std::string username;
std::string password;

Expand Down

0 comments on commit 66507d8

Please sign in to comment.