mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-14 06:14:45 +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
|
// Database
|
||||||
|
|
||||||
private static void initDatabases() {
|
private static void initDatabases() {
|
||||||
accountDatabase = new DatabaseManager(LunarRail.getConfig().getAccountDatabase());
|
|
||||||
|
|
||||||
if (LunarRail.getConfig().useSameDatabase) {
|
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 {
|
} 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
|
// Server enums
|
||||||
|
|
||||||
public enum ServerType {
|
public enum ServerType {
|
||||||
BOTH (true, true),
|
DISPATCH (0x1),
|
||||||
DISPATCH (true, false),
|
GAME (0x2),
|
||||||
GAME (false, true);
|
BOTH (0x3);
|
||||||
|
|
||||||
private final boolean runDispatch;
|
private final int flags;
|
||||||
private final boolean runGame;
|
|
||||||
|
|
||||||
private ServerType(boolean runDispatch, boolean runGame) {
|
private ServerType(int flags) {
|
||||||
this.runDispatch = runDispatch;
|
this.flags = flags;
|
||||||
this.runGame = runGame;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean runDispatch() {
|
public boolean runDispatch() {
|
||||||
return runDispatch;
|
return (this.flags & 0x1) == 0x1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean runGame() {
|
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.DatabaseInfo;
|
||||||
import emu.lunarcore.Config.InternalMongoInfo;
|
import emu.lunarcore.Config.InternalMongoInfo;
|
||||||
import emu.lunarcore.LunarRail;
|
import emu.lunarcore.LunarRail;
|
||||||
|
import emu.lunarcore.LunarRail.ServerType;
|
||||||
|
|
||||||
public final class DatabaseManager {
|
public final class DatabaseManager {
|
||||||
private MongoServer server;
|
private MongoServer server;
|
||||||
@@ -35,7 +36,7 @@ public final class DatabaseManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DatabaseManager(DatabaseInfo info) {
|
public DatabaseManager(DatabaseInfo info, ServerType type) {
|
||||||
// Variables
|
// Variables
|
||||||
String connectionString = info.getUri();
|
String connectionString = info.getUri();
|
||||||
|
|
||||||
@@ -58,16 +59,31 @@ public final class DatabaseManager {
|
|||||||
datastore = Morphia.createDatastore(gameMongoClient, info.getCollection(), mapperOptions);
|
datastore = Morphia.createDatastore(gameMongoClient, info.getCollection(), mapperOptions);
|
||||||
|
|
||||||
// Map classes
|
// Map classes
|
||||||
Class<?>[] entities = new Reflections(LunarRail.class.getPackageName())
|
var entities = new Reflections(LunarRail.class.getPackageName())
|
||||||
.getTypesAnnotatedWith(Entity.class)
|
.getTypesAnnotatedWith(Entity.class)
|
||||||
.stream()
|
.stream()
|
||||||
.filter(cls -> {
|
.filter(cls -> {
|
||||||
Entity e = cls.getAnnotation(Entity.class);
|
Entity e = cls.getAnnotation(Entity.class);
|
||||||
return e != null && !e.value().equals(Mapper.IGNORED_FIELDNAME);
|
return e != null && !e.value().equals(Mapper.IGNORED_FIELDNAME);
|
||||||
})
|
})
|
||||||
.toArray(Class<?>[]::new);
|
.toList();
|
||||||
|
|
||||||
datastore.getMapper().map(entities);
|
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
|
// Ensure indexes
|
||||||
ensureIndexes();
|
ensureIndexes();
|
||||||
|
|||||||
@@ -5,12 +5,14 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
import dev.morphia.annotations.*;
|
import dev.morphia.annotations.*;
|
||||||
import emu.lunarcore.LunarRail;
|
import emu.lunarcore.LunarRail;
|
||||||
|
import emu.lunarcore.database.AccountDatabaseOnly;
|
||||||
import emu.lunarcore.util.Crypto;
|
import emu.lunarcore.util.Crypto;
|
||||||
import emu.lunarcore.util.Snowflake32;
|
import emu.lunarcore.util.Snowflake32;
|
||||||
import emu.lunarcore.util.Utils;
|
import emu.lunarcore.util.Utils;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@AccountDatabaseOnly
|
||||||
@Entity(value = "accounts", useDiscriminator = false)
|
@Entity(value = "accounts", useDiscriminator = false)
|
||||||
public class Account {
|
public class Account {
|
||||||
@Id private String uid;
|
@Id private String uid;
|
||||||
|
|||||||
@@ -3,11 +3,13 @@ package emu.lunarcore.server.game;
|
|||||||
import dev.morphia.annotations.Entity;
|
import dev.morphia.annotations.Entity;
|
||||||
import dev.morphia.annotations.Id;
|
import dev.morphia.annotations.Id;
|
||||||
import emu.lunarcore.LunarRail;
|
import emu.lunarcore.LunarRail;
|
||||||
|
import emu.lunarcore.database.AccountDatabaseOnly;
|
||||||
import emu.lunarcore.proto.RegionEntryOuterClass.RegionEntry;
|
import emu.lunarcore.proto.RegionEntryOuterClass.RegionEntry;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@AccountDatabaseOnly
|
||||||
@Entity(value = "regions", useDiscriminator = false)
|
@Entity(value = "regions", useDiscriminator = false)
|
||||||
public class RegionInfo {
|
public class RegionInfo {
|
||||||
@Id private String id;
|
@Id private String id;
|
||||||
|
|||||||
Reference in New Issue
Block a user