mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-29 22:08:49 +00:00
Refactor and optimize DatabaseHelper::checkIfPlayerExists
This commit is contained in:
parent
06546707e1
commit
910a5749f0
@ -36,7 +36,7 @@ public final class DefaultAuthenticators {
|
|||||||
// Check if account exists.
|
// Check if account exists.
|
||||||
if(account == null && ACCOUNT.autoCreate) {
|
if(account == null && ACCOUNT.autoCreate) {
|
||||||
// This account has been created AUTOMATICALLY. There will be no permissions added.
|
// 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.
|
// Check if the account was created successfully.
|
||||||
if(account == null) {
|
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) {
|
if (account == null) {
|
||||||
CommandHandler.sendMessage(null, translate(sender, "commands.account.exists"));
|
CommandHandler.sendMessage(null, translate(sender, "commands.account.exists"));
|
||||||
return;
|
return;
|
||||||
|
@ -22,32 +22,28 @@ import static com.mongodb.client.model.Filters.eq;
|
|||||||
|
|
||||||
public final class DatabaseHelper {
|
public final class DatabaseHelper {
|
||||||
public static Account createAccount(String username) {
|
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
|
// Unique names only
|
||||||
Account exists = DatabaseHelper.getAccountByName(username);
|
if (DatabaseHelper.checkIfAccountExists(username)) {
|
||||||
if (exists != null) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure there are no id collisions
|
// Make sure there are no id collisions
|
||||||
if (reservedId > 0) {
|
if (reservedUid > 0) {
|
||||||
// Cannot make account with the same uid as the server console
|
// 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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure not other accounts has that id as its reservedPlayerId
|
if (DatabaseHelper.checkIfAccountExists(reservedUid)) {
|
||||||
exists = DatabaseHelper.getAccountByPlayerId(reservedId);
|
|
||||||
if (exists != null) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure no existing player already has this id.
|
// Make sure no existing player already has this id.
|
||||||
Player existsPlayer = DatabaseHelper.getPlayerByUid(reservedId);
|
if (DatabaseHelper.checkIfPlayerExists(reservedUid)) {
|
||||||
if (existsPlayer != null) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,8 +53,8 @@ public final class DatabaseHelper {
|
|||||||
account.setUsername(username);
|
account.setUsername(username);
|
||||||
account.setId(Integer.toString(DatabaseManager.getNextId(account)));
|
account.setId(Integer.toString(DatabaseManager.getNextId(account)));
|
||||||
|
|
||||||
if (reservedId > 0) {
|
if (reservedUid > 0) {
|
||||||
account.setReservedPlayerUid(reservedId);
|
account.setReservedPlayerUid(reservedUid);
|
||||||
}
|
}
|
||||||
|
|
||||||
DatabaseHelper.saveAccount(account);
|
DatabaseHelper.saveAccount(account);
|
||||||
@ -108,6 +104,14 @@ public final class DatabaseHelper {
|
|||||||
return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", playerId)).first();
|
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) {
|
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.
|
// To delete an account, we need to also delete all the other documents in the database that reference the account.
|
||||||
// This should optimally be wrapped inside a transaction, to make sure an error thrown mid-way does not leave the
|
// This should optimally be wrapped inside a transaction, to make sure an error thrown mid-way does not leave the
|
||||||
@ -152,21 +156,21 @@ public final class DatabaseHelper {
|
|||||||
return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("accountId", account.getId())).first();
|
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;
|
return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", uid)).count() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized Player generatePlayerUid(Player character, int reservedId) {
|
public static synchronized Player generatePlayerUid(Player character, int reservedId) {
|
||||||
// Check if reserved id
|
// Check if reserved id
|
||||||
int id;
|
int id;
|
||||||
if (reservedId > 0 && !checkPlayerExists(reservedId)) {
|
if (reservedId > 0 && !checkIfPlayerExists(reservedId)) {
|
||||||
id = reservedId;
|
id = reservedId;
|
||||||
character.setUid(id);
|
character.setUid(id);
|
||||||
} else {
|
} else {
|
||||||
do {
|
do {
|
||||||
id = DatabaseManager.getNextId(character);
|
id = DatabaseManager.getNextId(character);
|
||||||
}
|
}
|
||||||
while (checkPlayerExists(id));
|
while (checkIfPlayerExists(id));
|
||||||
character.setUid(id);
|
character.setUid(id);
|
||||||
}
|
}
|
||||||
// Save to database
|
// Save to database
|
||||||
@ -177,13 +181,13 @@ public final class DatabaseHelper {
|
|||||||
public static synchronized int getNextPlayerId(int reservedId) {
|
public static synchronized int getNextPlayerId(int reservedId) {
|
||||||
// Check if reserved id
|
// Check if reserved id
|
||||||
int id;
|
int id;
|
||||||
if (reservedId > 0 && !checkPlayerExists(reservedId)) {
|
if (reservedId > 0 && !checkIfPlayerExists(reservedId)) {
|
||||||
id = reservedId;
|
id = reservedId;
|
||||||
} else {
|
} else {
|
||||||
do {
|
do {
|
||||||
id = DatabaseManager.getNextId(Player.class);
|
id = DatabaseManager.getNextId(Player.class);
|
||||||
}
|
}
|
||||||
while (checkPlayerExists(id));
|
while (checkIfPlayerExists(id));
|
||||||
}
|
}
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user