From 910a5749f0b9caf1ba84378bb9b617b0652a8a8d Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Fri, 3 Jun 2022 19:32:10 -0700 Subject: [PATCH] Refactor and optimize DatabaseHelper::checkIfPlayerExists --- .../auth/DefaultAuthenticators.java | 2 +- .../command/commands/AccountCommand.java | 2 +- .../grasscutter/database/DatabaseHelper.java | 44 ++++++++++--------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main/java/emu/grasscutter/auth/DefaultAuthenticators.java b/src/main/java/emu/grasscutter/auth/DefaultAuthenticators.java index 8fea0253b..905535380 100644 --- a/src/main/java/emu/grasscutter/auth/DefaultAuthenticators.java +++ b/src/main/java/emu/grasscutter/auth/DefaultAuthenticators.java @@ -36,7 +36,7 @@ public final class DefaultAuthenticators { // Check if account exists. if(account == null && ACCOUNT.autoCreate) { // This account has been created AUTOMATICALLY. There will be no permissions added. - account = DatabaseHelper.createAccountWithId(requestData.account, 0); + account = DatabaseHelper.createAccountWithUid(requestData.account, 0); // Check if the account was created successfully. if(account == null) { diff --git a/src/main/java/emu/grasscutter/command/commands/AccountCommand.java b/src/main/java/emu/grasscutter/command/commands/AccountCommand.java index 4bb369886..0d50b3787 100644 --- a/src/main/java/emu/grasscutter/command/commands/AccountCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/AccountCommand.java @@ -44,7 +44,7 @@ public final class AccountCommand implements CommandHandler { } } - emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithId(username, uid); + emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithUid(username, uid); if (account == null) { CommandHandler.sendMessage(null, translate(sender, "commands.account.exists")); return; diff --git a/src/main/java/emu/grasscutter/database/DatabaseHelper.java b/src/main/java/emu/grasscutter/database/DatabaseHelper.java index fdd9c289e..5defbca6b 100644 --- a/src/main/java/emu/grasscutter/database/DatabaseHelper.java +++ b/src/main/java/emu/grasscutter/database/DatabaseHelper.java @@ -22,32 +22,28 @@ import static com.mongodb.client.model.Filters.eq; public final class DatabaseHelper { public static Account createAccount(String username) { - return createAccountWithId(username, 0); + return createAccountWithUid(username, 0); } - public static Account createAccountWithId(String username, int reservedId) { + public static Account createAccountWithUid(String username, int reservedUid) { // Unique names only - Account exists = DatabaseHelper.getAccountByName(username); - if (exists != null) { + if (DatabaseHelper.checkIfAccountExists(username)) { return null; } // Make sure there are no id collisions - if (reservedId > 0) { + if (reservedUid > 0) { // Cannot make account with the same uid as the server console - if (reservedId == GameConstants.SERVER_CONSOLE_UID) { + if (reservedUid == GameConstants.SERVER_CONSOLE_UID) { return null; } - - // Make sure not other accounts has that id as its reservedPlayerId - exists = DatabaseHelper.getAccountByPlayerId(reservedId); - if (exists != null) { + + if (DatabaseHelper.checkIfAccountExists(reservedUid)) { return null; } - + // Make sure no existing player already has this id. - Player existsPlayer = DatabaseHelper.getPlayerByUid(reservedId); - if (existsPlayer != null) { + if (DatabaseHelper.checkIfPlayerExists(reservedUid)) { return null; } } @@ -57,8 +53,8 @@ public final class DatabaseHelper { account.setUsername(username); account.setId(Integer.toString(DatabaseManager.getNextId(account))); - if (reservedId > 0) { - account.setReservedPlayerUid(reservedId); + if (reservedUid > 0) { + account.setReservedPlayerUid(reservedUid); } DatabaseHelper.saveAccount(account); @@ -107,6 +103,14 @@ public final class DatabaseHelper { public static Account getAccountByPlayerId(int playerId) { return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", playerId)).first(); } + + public static boolean checkIfAccountExists(String name) { + return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("username", name)).count() > 0; + } + + public static boolean checkIfAccountExists(int reservedUid) { + return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", reservedUid)).count() > 0; + } public static void deleteAccount(Account target) { // To delete an account, we need to also delete all the other documents in the database that reference the account. @@ -152,21 +156,21 @@ public final class DatabaseHelper { return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("accountId", account.getId())).first(); } - public static boolean checkPlayerExists(int uid) { + public static boolean checkIfPlayerExists(int uid) { return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", uid)).count() > 0; } public static synchronized Player generatePlayerUid(Player character, int reservedId) { // Check if reserved id int id; - if (reservedId > 0 && !checkPlayerExists(reservedId)) { + if (reservedId > 0 && !checkIfPlayerExists(reservedId)) { id = reservedId; character.setUid(id); } else { do { id = DatabaseManager.getNextId(character); } - while (checkPlayerExists(id)); + while (checkIfPlayerExists(id)); character.setUid(id); } // Save to database @@ -177,13 +181,13 @@ public final class DatabaseHelper { public static synchronized int getNextPlayerId(int reservedId) { // Check if reserved id int id; - if (reservedId > 0 && !checkPlayerExists(reservedId)) { + if (reservedId > 0 && !checkIfPlayerExists(reservedId)) { id = reservedId; } else { do { id = DatabaseManager.getNextId(Player.class); } - while (checkPlayerExists(id)); + while (checkIfPlayerExists(id)); } return id; }