Commit 910a5749 authored by Melledy's avatar Melledy
Browse files

Refactor and optimize DatabaseHelper::checkIfPlayerExists

parent 06546707
......@@ -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) {
......
......@@ -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;
......
......@@ -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;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment