mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-02-07 17:53:53 +08:00
Refactor config database settings
This commit is contained in:
parent
4daec68f0a
commit
89bd4b9aeb
@ -77,25 +77,25 @@ public final class DatabaseHelper {
|
||||
}
|
||||
|
||||
public static Account getAccountByName(String username) {
|
||||
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("username", username)).first();
|
||||
return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("username", username)).first();
|
||||
}
|
||||
|
||||
public static Account getAccountByToken(String token) {
|
||||
if(token == null) return null;
|
||||
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("token", token)).first();
|
||||
return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("token", token)).first();
|
||||
}
|
||||
|
||||
public static Account getAccountBySessionKey(String sessionKey) {
|
||||
if(sessionKey == null) return null;
|
||||
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("sessionKey", sessionKey)).first();
|
||||
return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("sessionKey", sessionKey)).first();
|
||||
}
|
||||
|
||||
public static Account getAccountById(String uid) {
|
||||
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("_id", uid)).first();
|
||||
return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("_id", uid)).first();
|
||||
}
|
||||
|
||||
public static Account getAccountByPlayerId(int playerId) {
|
||||
return DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("playerId", playerId)).first();
|
||||
return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("playerId", playerId)).first();
|
||||
}
|
||||
|
||||
public static void deleteAccount(Account target) {
|
||||
@ -104,37 +104,37 @@ public final class DatabaseHelper {
|
||||
// database in an inconsistent state, but unfortunately Mongo only supports that when we have a replica set ...
|
||||
|
||||
// Delete Mail.class data
|
||||
DatabaseManager.getDatabase().getCollection("mail").deleteMany(eq("ownerUid", target.getPlayerUid()));
|
||||
DatabaseManager.getGameDatabase().getCollection("mail").deleteMany(eq("ownerUid", target.getPlayerUid()));
|
||||
// Delete Avatar.class data
|
||||
DatabaseManager.getDatabase().getCollection("avatars").deleteMany(eq("ownerId", target.getPlayerUid()));
|
||||
DatabaseManager.getGameDatabase().getCollection("avatars").deleteMany(eq("ownerId", target.getPlayerUid()));
|
||||
// Delete GachaRecord.class data
|
||||
DatabaseManager.getDatabase().getCollection("gachas").deleteMany(eq("ownerId", target.getPlayerUid()));
|
||||
DatabaseManager.getGameDatabase().getCollection("gachas").deleteMany(eq("ownerId", target.getPlayerUid()));
|
||||
// Delete GameItem.class data
|
||||
DatabaseManager.getDatabase().getCollection("items").deleteMany(eq("ownerId", target.getPlayerUid()));
|
||||
DatabaseManager.getGameDatabase().getCollection("items").deleteMany(eq("ownerId", target.getPlayerUid()));
|
||||
|
||||
// Delete friendships.
|
||||
// Here, we need to make sure to not only delete the deleted account's friendships,
|
||||
// but also all friendship entries for that account's friends.
|
||||
DatabaseManager.getDatabase().getCollection("friendships").deleteMany(eq("ownerId", target.getPlayerUid()));
|
||||
DatabaseManager.getDatabase().getCollection("friendships").deleteMany(eq("friendId", target.getPlayerUid()));
|
||||
DatabaseManager.getGameDatabase().getCollection("friendships").deleteMany(eq("ownerId", target.getPlayerUid()));
|
||||
DatabaseManager.getGameDatabase().getCollection("friendships").deleteMany(eq("friendId", target.getPlayerUid()));
|
||||
|
||||
// Delete the player.
|
||||
DatabaseManager.getDatastore().find(Player.class).filter(Filters.eq("id", target.getPlayerUid())).delete();
|
||||
DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("id", target.getPlayerUid())).delete();
|
||||
|
||||
// Finally, delete the account itself.
|
||||
DatabaseManager.getDatastore().find(Account.class).filter(Filters.eq("id", target.getId())).delete();
|
||||
DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("id", target.getId())).delete();
|
||||
}
|
||||
|
||||
public static List<Player> getAllPlayers() {
|
||||
return DatabaseManager.getDatastore().find(Player.class).stream().toList();
|
||||
return DatabaseManager.getGameDatastore().find(Player.class).stream().toList();
|
||||
}
|
||||
|
||||
public static Player getPlayerById(int id) {
|
||||
return DatabaseManager.getDatastore().find(Player.class).filter(Filters.eq("_id", id)).first();
|
||||
return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", id)).first();
|
||||
}
|
||||
|
||||
public static boolean checkPlayerExists(int id) {
|
||||
return DatabaseManager.getDatastore().find(Player.class).filter(Filters.eq("_id", id)).first() != null;
|
||||
return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", id)).first() != null;
|
||||
}
|
||||
|
||||
public static synchronized Player createPlayer(Player character, int reservedId) {
|
||||
@ -151,7 +151,7 @@ public final class DatabaseHelper {
|
||||
character.setUid(id);
|
||||
}
|
||||
// Save to database
|
||||
DatabaseManager.getDatastore().save(character);
|
||||
DatabaseManager.getGameDatastore().save(character);
|
||||
return character;
|
||||
}
|
||||
|
||||
@ -170,48 +170,48 @@ public final class DatabaseHelper {
|
||||
}
|
||||
|
||||
public static void savePlayer(Player character) {
|
||||
DatabaseManager.getDatastore().save(character);
|
||||
DatabaseManager.getGameDatastore().save(character);
|
||||
}
|
||||
|
||||
public static void saveAvatar(Avatar avatar) {
|
||||
DatabaseManager.getDatastore().save(avatar);
|
||||
DatabaseManager.getGameDatastore().save(avatar);
|
||||
}
|
||||
|
||||
public static List<Avatar> getAvatars(Player player) {
|
||||
return DatabaseManager.getDatastore().find(Avatar.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
|
||||
return DatabaseManager.getGameDatastore().find(Avatar.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
|
||||
}
|
||||
|
||||
public static void saveItem(GameItem item) {
|
||||
DatabaseManager.getDatastore().save(item);
|
||||
DatabaseManager.getGameDatastore().save(item);
|
||||
}
|
||||
|
||||
public static boolean deleteItem(GameItem item) {
|
||||
DeleteResult result = DatabaseManager.getDatastore().delete(item);
|
||||
DeleteResult result = DatabaseManager.getGameDatastore().delete(item);
|
||||
return result.wasAcknowledged();
|
||||
}
|
||||
|
||||
public static List<GameItem> getInventoryItems(Player player) {
|
||||
return DatabaseManager.getDatastore().find(GameItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
|
||||
return DatabaseManager.getGameDatastore().find(GameItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
|
||||
}
|
||||
|
||||
public static List<Friendship> getFriends(Player player) {
|
||||
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
|
||||
return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
|
||||
}
|
||||
|
||||
public static List<Friendship> getReverseFriends(Player player) {
|
||||
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.eq("friendId", player.getUid())).stream().toList();
|
||||
return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.eq("friendId", player.getUid())).stream().toList();
|
||||
}
|
||||
|
||||
public static void saveFriendship(Friendship friendship) {
|
||||
DatabaseManager.getDatastore().save(friendship);
|
||||
DatabaseManager.getGameDatastore().save(friendship);
|
||||
}
|
||||
|
||||
public static void deleteFriendship(Friendship friendship) {
|
||||
DatabaseManager.getDatastore().delete(friendship);
|
||||
DatabaseManager.getGameDatastore().delete(friendship);
|
||||
}
|
||||
|
||||
public static Friendship getReverseFriendship(Friendship friendship) {
|
||||
return DatabaseManager.getDatastore().find(Friendship.class).filter(Filters.and(
|
||||
return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.and(
|
||||
Filters.eq("ownerId", friendship.getFriendId()),
|
||||
Filters.eq("friendId", friendship.getOwnerId())
|
||||
)).first();
|
||||
@ -222,7 +222,7 @@ public final class DatabaseHelper {
|
||||
}
|
||||
|
||||
public static List<GachaRecord> getGachaRecords(int ownerId, int page, int gachaType, int pageSize){
|
||||
return DatabaseManager.getDatastore().find(GachaRecord.class).filter(
|
||||
return DatabaseManager.getGameDatastore().find(GachaRecord.class).filter(
|
||||
Filters.eq("ownerId", ownerId),
|
||||
Filters.eq("gachaType", gachaType)
|
||||
).iterator(new FindOptions()
|
||||
@ -237,7 +237,7 @@ public final class DatabaseHelper {
|
||||
}
|
||||
|
||||
public static long getGachaRecordsMaxPage(int ownerId, int page, int gachaType, int pageSize){
|
||||
long count = DatabaseManager.getDatastore().find(GachaRecord.class).filter(
|
||||
long count = DatabaseManager.getGameDatastore().find(GachaRecord.class).filter(
|
||||
Filters.eq("ownerId", ownerId),
|
||||
Filters.eq("gachaType", gachaType)
|
||||
).count();
|
||||
@ -245,19 +245,19 @@ public final class DatabaseHelper {
|
||||
}
|
||||
|
||||
public static void saveGachaRecord(GachaRecord gachaRecord){
|
||||
DatabaseManager.getDatastore().save(gachaRecord);
|
||||
DatabaseManager.getGameDatastore().save(gachaRecord);
|
||||
}
|
||||
|
||||
public static List<Mail> getAllMail(Player player) {
|
||||
return DatabaseManager.getDatastore().find(Mail.class).filter(Filters.eq("ownerUid", player.getUid())).stream().toList();
|
||||
return DatabaseManager.getGameDatastore().find(Mail.class).filter(Filters.eq("ownerUid", player.getUid())).stream().toList();
|
||||
}
|
||||
|
||||
public static void saveMail(Mail mail) {
|
||||
DatabaseManager.getDatastore().save(mail);
|
||||
DatabaseManager.getGameDatastore().save(mail);
|
||||
}
|
||||
|
||||
public static boolean deleteMail(Mail mail) {
|
||||
DeleteResult result = DatabaseManager.getDatastore().delete(mail);
|
||||
DeleteResult result = DatabaseManager.getGameDatastore().delete(mail);
|
||||
return result.wasAcknowledged();
|
||||
}
|
||||
}
|
||||
|
@ -23,19 +23,19 @@ import emu.grasscutter.game.player.Player;
|
||||
import static emu.grasscutter.Configuration.*;
|
||||
|
||||
public final class DatabaseManager {
|
||||
private static Datastore datastore;
|
||||
private static Datastore gameDatastore;
|
||||
private static Datastore dispatchDatastore;
|
||||
|
||||
private static final Class<?>[] mappedClasses = new Class<?>[] {
|
||||
DatabaseCounter.class, Account.class, Player.class, Avatar.class, GameItem.class, Friendship.class, GachaRecord.class, Mail.class
|
||||
};
|
||||
|
||||
public static Datastore getDatastore() {
|
||||
return datastore;
|
||||
public static Datastore getGameDatastore() {
|
||||
return gameDatastore;
|
||||
}
|
||||
|
||||
public static MongoDatabase getDatabase() {
|
||||
return getDatastore().getDatabase();
|
||||
public static MongoDatabase getGameDatabase() {
|
||||
return getGameDatastore().getDatabase();
|
||||
}
|
||||
|
||||
// Yes. I very dislike this method. However, this will be good for now.
|
||||
@ -44,42 +44,42 @@ public final class DatabaseManager {
|
||||
if(SERVER.runMode == ServerRunMode.GAME_ONLY) {
|
||||
return dispatchDatastore;
|
||||
} else {
|
||||
return datastore;
|
||||
return gameDatastore;
|
||||
}
|
||||
}
|
||||
|
||||
public static void initialize() {
|
||||
// Initialize
|
||||
MongoClient mongoClient = MongoClients.create(DATABASE.connectionUri);
|
||||
MongoClient gameMongoClient = MongoClients.create(DATABASE.game.connectionUri);
|
||||
|
||||
// Set mapper options.
|
||||
MapperOptions mapperOptions = MapperOptions.builder()
|
||||
.storeEmpties(true).storeNulls(false).build();
|
||||
// Create data store.
|
||||
datastore = Morphia.createDatastore(mongoClient, DATABASE.collection, mapperOptions);
|
||||
gameDatastore = Morphia.createDatastore(gameMongoClient, DATABASE.game.collection, mapperOptions);
|
||||
// Map classes.
|
||||
datastore.getMapper().map(mappedClasses);
|
||||
gameDatastore.getMapper().map(mappedClasses);
|
||||
|
||||
// Ensure indexes
|
||||
try {
|
||||
datastore.ensureIndexes();
|
||||
gameDatastore.ensureIndexes();
|
||||
} catch (MongoCommandException exception) {
|
||||
Grasscutter.getLogger().info("Mongo index error: ", exception);
|
||||
// Duplicate index error
|
||||
if (exception.getCode() == 85) {
|
||||
// Drop all indexes and re add them
|
||||
MongoIterable<String> collections = datastore.getDatabase().listCollectionNames();
|
||||
MongoIterable<String> collections = gameDatastore.getDatabase().listCollectionNames();
|
||||
for (String name : collections) {
|
||||
datastore.getDatabase().getCollection(name).dropIndexes();
|
||||
gameDatastore.getDatabase().getCollection(name).dropIndexes();
|
||||
}
|
||||
// Add back indexes
|
||||
datastore.ensureIndexes();
|
||||
gameDatastore.ensureIndexes();
|
||||
}
|
||||
}
|
||||
|
||||
if(SERVER.runMode == ServerRunMode.GAME_ONLY) {
|
||||
MongoClient dispatchMongoClient = MongoClients.create(GAME_OPTIONS.databaseInfo.connectionUri);
|
||||
dispatchDatastore = Morphia.createDatastore(dispatchMongoClient, GAME_OPTIONS.databaseInfo.collection);
|
||||
MongoClient dispatchMongoClient = MongoClients.create(DATABASE.server.connectionUri);
|
||||
dispatchDatastore = Morphia.createDatastore(dispatchMongoClient, DATABASE.server.collection);
|
||||
|
||||
// Ensure indexes for dispatch server
|
||||
try {
|
||||
@ -101,14 +101,14 @@ public final class DatabaseManager {
|
||||
}
|
||||
|
||||
public static synchronized int getNextId(Class<?> c) {
|
||||
DatabaseCounter counter = getDatastore().find(DatabaseCounter.class).filter(Filters.eq("_id", c.getSimpleName())).first();
|
||||
DatabaseCounter counter = getGameDatastore().find(DatabaseCounter.class).filter(Filters.eq("_id", c.getSimpleName())).first();
|
||||
if (counter == null) {
|
||||
counter = new DatabaseCounter(c.getSimpleName());
|
||||
}
|
||||
try {
|
||||
return counter.getNextId();
|
||||
} finally {
|
||||
getDatastore().save(counter);
|
||||
getGameDatastore().save(counter);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ import emu.grasscutter.server.event.HandlerPriority;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
@ -90,6 +91,8 @@ public final class PluginManager {
|
||||
fileReader.close(); // Close the file reader.
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " has an invalid main class.");
|
||||
} catch (FileNotFoundException ignored) {
|
||||
Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " lacks a valid config file.");
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
Grasscutter.getLogger().error("Failed to load plugin: " + plugin.getName(), exception);
|
||||
|
@ -2,6 +2,8 @@ package emu.grasscutter.utils;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.Grasscutter.ServerDebugMode;
|
||||
import emu.grasscutter.Grasscutter.ServerRunMode;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.lang.reflect.Field;
|
||||
@ -15,7 +17,7 @@ import static emu.grasscutter.Grasscutter.config;
|
||||
*/
|
||||
public class ConfigContainer {
|
||||
private static int version() {
|
||||
return 1;
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,8 +71,13 @@ public class ConfigContainer {
|
||||
/* Option containers. */
|
||||
|
||||
public static class Database {
|
||||
public String connectionUri = "mongodb://localhost:27017";
|
||||
public String collection = "grasscutter";
|
||||
public DataStore server = new DataStore();
|
||||
public DataStore game = new DataStore();
|
||||
|
||||
public static class DataStore {
|
||||
public String connectionUri = "mongodb://localhost:27017";
|
||||
public String collection = "grasscutter";
|
||||
}
|
||||
}
|
||||
|
||||
public static class Structure {
|
||||
@ -86,8 +93,8 @@ public class ConfigContainer {
|
||||
}
|
||||
|
||||
public static class Server {
|
||||
public Grasscutter.ServerDebugMode debugLevel = Grasscutter.ServerDebugMode.NONE;
|
||||
public Grasscutter.ServerRunMode runMode = Grasscutter.ServerRunMode.HYBRID;
|
||||
public ServerDebugMode debugLevel = ServerDebugMode.NONE;
|
||||
public ServerRunMode runMode = ServerRunMode.HYBRID;
|
||||
|
||||
public Dispatch dispatch = new Dispatch();
|
||||
public Game game = new Game();
|
||||
@ -112,7 +119,7 @@ public class ConfigContainer {
|
||||
|
||||
public int bindPort = 443;
|
||||
/* This is the port used in URLs. */
|
||||
public int accessPort = 443;
|
||||
public int accessPort = 0;
|
||||
|
||||
public Encryption encryption = new Encryption();
|
||||
public Policies policies = new Policies();
|
||||
@ -128,7 +135,7 @@ public class ConfigContainer {
|
||||
|
||||
public int bindPort = 22102;
|
||||
/* This is the port used in the default region. */
|
||||
public int accessPort = 22102;
|
||||
public int accessPort = 0;
|
||||
|
||||
public GameOptions gameOptions = new GameOptions();
|
||||
public JoinOptions joinOptions = new JoinOptions();
|
||||
@ -155,16 +162,14 @@ public class ConfigContainer {
|
||||
}
|
||||
|
||||
public static class GameOptions {
|
||||
public GameOptions.InventoryLimits inventoryLimits = new GameOptions.InventoryLimits();
|
||||
public GameOptions.AvatarLimits avatarLimits = new GameOptions.AvatarLimits();
|
||||
public InventoryLimits inventoryLimits = new InventoryLimits();
|
||||
public AvatarLimits avatarLimits = new AvatarLimits();
|
||||
public int worldEntityLimit = 1000; // Unenforced. TODO: Implement.
|
||||
|
||||
public boolean watchGachaConfig = false;
|
||||
public boolean enableShopItems = true;
|
||||
public boolean staminaUsage = true;
|
||||
public GameOptions.Rates rates = new GameOptions.Rates();
|
||||
|
||||
public Database databaseInfo = new Database();
|
||||
public Rates rates = new Rates();
|
||||
|
||||
public static class InventoryLimits {
|
||||
public int weapons = 2000;
|
||||
|
Loading…
Reference in New Issue
Block a user