Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
910a5749
Commit
910a5749
authored
Jun 03, 2022
by
Melledy
Browse files
Refactor and optimize DatabaseHelper::checkIfPlayerExists
parent
06546707
Changes
3
Show whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/auth/DefaultAuthenticators.java
View file @
910a5749
...
...
@@ -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
.
createAccountWith
I
d
(
requestData
.
account
,
0
);
account
=
DatabaseHelper
.
createAccountWith
Ui
d
(
requestData
.
account
,
0
);
// Check if the account was created successfully.
if
(
account
==
null
)
{
...
...
src/main/java/emu/grasscutter/command/commands/AccountCommand.java
View file @
910a5749
...
...
@@ -44,7 +44,7 @@ public final class AccountCommand implements CommandHandler {
}
}
emu
.
grasscutter
.
game
.
Account
account
=
DatabaseHelper
.
createAccountWith
I
d
(
username
,
uid
);
emu
.
grasscutter
.
game
.
Account
account
=
DatabaseHelper
.
createAccountWith
Ui
d
(
username
,
uid
);
if
(
account
==
null
)
{
CommandHandler
.
sendMessage
(
null
,
translate
(
sender
,
"commands.account.exists"
));
return
;
...
...
src/main/java/emu/grasscutter/database/DatabaseHelper.java
View file @
910a5749
...
...
@@ -22,32 +22,28 @@ import static com.mongodb.client.model.Filters.eq;
public
final
class
DatabaseHelper
{
public
static
Account
createAccount
(
String
username
)
{
return
createAccountWith
I
d
(
username
,
0
);
return
createAccountWith
Ui
d
(
username
,
0
);
}
public
static
Account
createAccountWith
I
d
(
String
username
,
int
reserved
I
d
)
{
public
static
Account
createAccountWith
Ui
d
(
String
username
,
int
reserved
Ui
d
)
{
// 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
(
reserved
I
d
>
0
)
{
if
(
reserved
Ui
d
>
0
)
{
// Cannot make account with the same uid as the server console
if
(
reserved
I
d
==
GameConstants
.
SERVER_CONSOLE_UID
)
{
if
(
reserved
Ui
d
==
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
(
reserved
I
d
>
0
)
{
account
.
setReservedPlayerUid
(
reserved
I
d
);
if
(
reserved
Ui
d
>
0
)
{
account
.
setReservedPlayerUid
(
reserved
Ui
d
);
}
DatabaseHelper
.
saveAccount
(
account
);
...
...
@@ -108,6 +104,14 @@ public final class DatabaseHelper {
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.
// 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
();
}
public
static
boolean
checkPlayerExists
(
int
uid
)
{
public
static
boolean
check
If
PlayerExists
(
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
&&
!
check
If
PlayerExists
(
reservedId
))
{
id
=
reservedId
;
character
.
setUid
(
id
);
}
else
{
do
{
id
=
DatabaseManager
.
getNextId
(
character
);
}
while
(
checkPlayerExists
(
id
));
while
(
check
If
PlayerExists
(
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
&&
!
check
If
PlayerExists
(
reservedId
))
{
id
=
reservedId
;
}
else
{
do
{
id
=
DatabaseManager
.
getNextId
(
Player
.
class
);
}
while
(
checkPlayerExists
(
id
));
while
(
check
If
PlayerExists
(
id
));
}
return
id
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment