Grasscutter/src/main/java/emu/grasscutter/database/DatabaseHelper.java

180 lines
5.6 KiB
Java
Raw Normal View History

2022-04-17 12:43:07 +00:00
package emu.grasscutter.database;
import java.util.List;
2022-04-21 01:11:57 +00:00
import com.mongodb.client.result.DeleteResult;
import dev.morphia.query.experimental.filters.Filters;
2022-04-17 12:43:07 +00:00
import emu.grasscutter.GenshinConstants;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.avatar.GenshinAvatar;
import emu.grasscutter.game.friends.Friendship;
import emu.grasscutter.game.inventory.GenshinItem;
2022-04-21 01:11:57 +00:00
public final class DatabaseHelper {
2022-04-17 12:43:07 +00:00
public static Account createAccount(String username) {
return createAccountWithId(username, 0);
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static Account createAccountWithId(String username, int reservedId) {
// Unique names only
Account exists = DatabaseHelper.getAccountByName(username);
if (exists != null) {
return null;
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
// Make sure there are no id collisions
if (reservedId > 0) {
// Cannot make account with the same uid as the server console
if (reservedId == GenshinConstants.SERVER_CONSOLE_UID) {
return null;
}
exists = DatabaseHelper.getAccountByPlayerId(reservedId);
if (exists != null) {
return null;
}
}
// Account
Account account = new Account();
account.setUsername(username);
account.setId(Integer.toString(DatabaseManager.getNextId(account)));
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
if (reservedId > 0) {
account.setPlayerId(reservedId);
2022-04-21 01:11:57 +00:00
}
2022-04-17 12:43:07 +00:00
DatabaseHelper.saveAccount(account);
return account;
}
@Deprecated
public static Account createAccountWithPassword(String username, String password) {
// Unique names only
Account exists = DatabaseHelper.getAccountByName(username);
if (exists != null) {
return null;
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
// Account
Account account = new Account();
account.setId(Integer.toString(DatabaseManager.getNextId(account)));
account.setUsername(username);
account.setPassword(password);
2022-04-21 01:11:57 +00:00
DatabaseHelper.saveAccount(account);
2022-04-17 12:43:07 +00:00
return account;
}
public static void saveAccount(Account account) {
DatabaseManager.getDatastore().save(account);
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static Account getAccountByName(String username) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("username", username)).first();
2022-04-17 12:43:07 +00:00
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static Account getAccountByToken(String token) {
2022-04-21 01:11:57 +00:00
if(token == null) return null;
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("token", token)).first();
2022-04-17 12:43:07 +00:00
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static Account getAccountById(String uid) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("_id", uid)).first();
2022-04-17 12:43:07 +00:00
}
2022-04-21 01:11:57 +00:00
public static Account getAccountByPlayerId(int playerId) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("playerId", playerId)).first();
2022-04-17 12:43:07 +00:00
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static boolean deleteAccount(String username) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("username", username)).delete().getDeletedCount() > 0;
2022-04-17 12:43:07 +00:00
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static GenshinPlayer getPlayerById(int id) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(GenshinPlayer.class).filter(Filters.eq("_id", id)).first();
2022-04-17 12:43:07 +00:00
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static boolean checkPlayerExists(int id) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(GenshinPlayer.class).filter(Filters.eq("_id", id)).first() != null;
2022-04-17 12:43:07 +00:00
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static synchronized GenshinPlayer createPlayer(GenshinPlayer character, int reservedId) {
// Check if reserved id
2022-04-21 01:11:57 +00:00
int id;
2022-04-17 12:43:07 +00:00
if (reservedId > 0 && !checkPlayerExists(reservedId)) {
id = reservedId;
character.setUid(id);
2022-04-17 12:43:07 +00:00
} else {
do {
id = DatabaseManager.getNextId(character);
}
while (checkPlayerExists(id));
character.setUid(id);
2022-04-17 12:43:07 +00:00
}
// Save to database
DatabaseManager.getDatastore().save(character);
return character;
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static synchronized int getNextPlayerId(int reservedId) {
// Check if reserved id
2022-04-21 01:11:57 +00:00
int id;
2022-04-17 12:43:07 +00:00
if (reservedId > 0 && !checkPlayerExists(reservedId)) {
id = reservedId;
} else {
do {
id = DatabaseManager.getNextId(GenshinPlayer.class);
}
while (checkPlayerExists(id));
}
return id;
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static void savePlayer(GenshinPlayer character) {
DatabaseManager.getDatastore().save(character);
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static void saveAvatar(GenshinAvatar avatar) {
DatabaseManager.getDatastore().save(avatar);
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static List<GenshinAvatar> getAvatars(GenshinPlayer player) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(GenshinAvatar.class).filter(Filters.eq("playerId", player.getUid())).stream().toList();
2022-04-17 12:43:07 +00:00
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static void saveItem(GenshinItem item) {
DatabaseManager.getDatastore().save(item);
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static boolean deleteItem(GenshinItem item) {
2022-04-21 01:11:57 +00:00
DeleteResult result = DatabaseManager.getDatastore().delete(item);
2022-04-17 12:43:07 +00:00
return result.wasAcknowledged();
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static List<GenshinItem> getInventoryItems(GenshinPlayer player) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(GenshinItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
2022-04-17 12:43:07 +00:00
}
public static List<Friendship> getFriends(GenshinPlayer player) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("playerId", player.getUid())).stream().toList();
2022-04-17 12:43:07 +00:00
}
2022-04-21 01:11:57 +00:00
2022-04-17 12:43:07 +00:00
public static List<Friendship> getReverseFriends(GenshinPlayer player) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("friendId", player.getUid())).stream().toList();
2022-04-17 12:43:07 +00:00
}
public static void saveFriendship(Friendship friendship) {
DatabaseManager.getDatastore().save(friendship);
}
public static void deleteFriendship(Friendship friendship) {
DatabaseManager.getDatastore().delete(friendship);
}
public static Friendship getReverseFriendship(Friendship friendship) {
2022-04-21 01:11:57 +00:00
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.and(
Filters.eq("ownerId", friendship.getFriendId()),
Filters.eq("friendId", friendship.getOwnerId())
)).first();
2022-04-17 12:43:07 +00:00
}
}