mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-13 22:04:36 +01:00
Dont map account database entites in the gameserver database and vice versa
This commit is contained in:
@@ -108,12 +108,20 @@ public class LunarRail {
|
||||
// Database
|
||||
|
||||
private static void initDatabases() {
|
||||
accountDatabase = new DatabaseManager(LunarRail.getConfig().getAccountDatabase());
|
||||
|
||||
if (LunarRail.getConfig().useSameDatabase) {
|
||||
gameDatabase = accountDatabase;
|
||||
// Setup account and game database
|
||||
accountDatabase = new DatabaseManager(LunarRail.getConfig().getAccountDatabase(), serverType);
|
||||
// Optimization: Dont run a 2nd database manager if we are not running a gameserver
|
||||
if (serverType.runGame()) {
|
||||
gameDatabase = accountDatabase;
|
||||
}
|
||||
} else {
|
||||
gameDatabase = new DatabaseManager(LunarRail.getConfig().getGameDatabase());
|
||||
// Run separate databases
|
||||
accountDatabase = new DatabaseManager(LunarRail.getConfig().getAccountDatabase(), ServerType.DISPATCH);
|
||||
// Optimization: Dont run a 2nd database manager if we are not running a gameserver
|
||||
if (serverType.runGame()) {
|
||||
gameDatabase = new DatabaseManager(LunarRail.getConfig().getGameDatabase(), ServerType.GAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,24 +161,22 @@ public class LunarRail {
|
||||
// Server enums
|
||||
|
||||
public enum ServerType {
|
||||
BOTH (true, true),
|
||||
DISPATCH (true, false),
|
||||
GAME (false, true);
|
||||
DISPATCH (0x1),
|
||||
GAME (0x2),
|
||||
BOTH (0x3);
|
||||
|
||||
private final boolean runDispatch;
|
||||
private final boolean runGame;
|
||||
private final int flags;
|
||||
|
||||
private ServerType(boolean runDispatch, boolean runGame) {
|
||||
this.runDispatch = runDispatch;
|
||||
this.runGame = runGame;
|
||||
private ServerType(int flags) {
|
||||
this.flags = flags;
|
||||
}
|
||||
|
||||
public boolean runDispatch() {
|
||||
return runDispatch;
|
||||
return (this.flags & 0x1) == 0x1;
|
||||
}
|
||||
|
||||
public boolean runGame() {
|
||||
return runGame;
|
||||
return (this.flags & 0x2) == 0x2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package emu.lunarcore.database;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AccountDatabaseOnly {
|
||||
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import dev.morphia.query.filters.Filters;
|
||||
import emu.lunarcore.Config.DatabaseInfo;
|
||||
import emu.lunarcore.Config.InternalMongoInfo;
|
||||
import emu.lunarcore.LunarRail;
|
||||
import emu.lunarcore.LunarRail.ServerType;
|
||||
|
||||
public final class DatabaseManager {
|
||||
private MongoServer server;
|
||||
@@ -35,7 +36,7 @@ public final class DatabaseManager {
|
||||
|
||||
}
|
||||
|
||||
public DatabaseManager(DatabaseInfo info) {
|
||||
public DatabaseManager(DatabaseInfo info, ServerType type) {
|
||||
// Variables
|
||||
String connectionString = info.getUri();
|
||||
|
||||
@@ -58,16 +59,31 @@ public final class DatabaseManager {
|
||||
datastore = Morphia.createDatastore(gameMongoClient, info.getCollection(), mapperOptions);
|
||||
|
||||
// Map classes
|
||||
Class<?>[] entities = new Reflections(LunarRail.class.getPackageName())
|
||||
var entities = new Reflections(LunarRail.class.getPackageName())
|
||||
.getTypesAnnotatedWith(Entity.class)
|
||||
.stream()
|
||||
.filter(cls -> {
|
||||
Entity e = cls.getAnnotation(Entity.class);
|
||||
return e != null && !e.value().equals(Mapper.IGNORED_FIELDNAME);
|
||||
})
|
||||
.toArray(Class<?>[]::new);
|
||||
|
||||
datastore.getMapper().map(entities);
|
||||
.toList();
|
||||
|
||||
if (type.runDispatch()) {
|
||||
// Only map account related entities
|
||||
var map = entities.stream().filter(cls -> {
|
||||
return cls.getAnnotation(AccountDatabaseOnly.class) != null;
|
||||
}).toArray(Class<?>[]::new);
|
||||
|
||||
datastore.getMapper().map(map);
|
||||
}
|
||||
if (type.runGame()) {
|
||||
// Only map game related entities
|
||||
var map = entities.stream().filter(cls -> {
|
||||
return cls.getAnnotation(AccountDatabaseOnly.class) == null;
|
||||
}).toArray(Class<?>[]::new);
|
||||
|
||||
datastore.getMapper().map(map);
|
||||
}
|
||||
|
||||
// Ensure indexes
|
||||
ensureIndexes();
|
||||
|
||||
@@ -5,12 +5,14 @@ import java.util.stream.Stream;
|
||||
|
||||
import dev.morphia.annotations.*;
|
||||
import emu.lunarcore.LunarRail;
|
||||
import emu.lunarcore.database.AccountDatabaseOnly;
|
||||
import emu.lunarcore.util.Crypto;
|
||||
import emu.lunarcore.util.Snowflake32;
|
||||
import emu.lunarcore.util.Utils;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AccountDatabaseOnly
|
||||
@Entity(value = "accounts", useDiscriminator = false)
|
||||
public class Account {
|
||||
@Id private String uid;
|
||||
|
||||
@@ -3,11 +3,13 @@ package emu.lunarcore.server.game;
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
import emu.lunarcore.LunarRail;
|
||||
import emu.lunarcore.database.AccountDatabaseOnly;
|
||||
import emu.lunarcore.proto.RegionEntryOuterClass.RegionEntry;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@AccountDatabaseOnly
|
||||
@Entity(value = "regions", useDiscriminator = false)
|
||||
public class RegionInfo {
|
||||
@Id private String id;
|
||||
|
||||
Reference in New Issue
Block a user