Improve GameModeConfig

This commit is contained in:
2025-10-26 12:34:16 +01:00
parent 3e918d031d
commit c6dbece550
36 changed files with 212 additions and 202 deletions
@@ -17,12 +17,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.data;
package de.steamwar.sql;
import de.steamwar.sql.SchematicType;
import lombok.ToString;
import java.io.File;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
@@ -32,10 +32,9 @@ import java.util.function.Function;
import java.util.stream.Collectors;
@ToString
public final class GameModeConfig<M, ST, W> {
public final class GameModeConfig<M, W> {
public static final Function<String, String> ToString = Function.identity();
public static final Function<String, SchematicType> ToSchematicType = SchematicType::fromDB;
public static final Function<File, String> ToStaticWarGear = __ -> "WarGear";
public static final Function<File, String> ToInternalName = file -> file.getName().replace(".yml", "");
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy HH:mm");
@@ -44,10 +43,11 @@ public final class GameModeConfig<M, ST, W> {
public final boolean loaded;
public final File configFile;
public final Server Server;
public final ServerConfig Server;
/**
* Submission deadline for schematics in 'dd.MM.yyyy HH:mm' format
*
* @implSpec {@code null} by default
*/
public final Date Deadline;
@@ -58,9 +58,9 @@ public final class GameModeConfig<M, ST, W> {
* @implSpec Disables check schem type if missing
*/
public final List<String> CheckQuestions;
public final Times Times;
public final Arena Arena;
public final Schematic<M, ST> Schematic;
public final TimesConfig Times;
public final ArenaConfig Arena;
public final SchematicConfig<M> Schematic;
/**
* The name of the game mode presented to the players
@@ -83,28 +83,39 @@ public final class GameModeConfig<M, ST, W> {
* @implSpec {@code +} by default
*/
public final String TeamChatPrefix;
public final Blue Blue;
public final Red Red;
public final BlueConfig Blue;
public final RedConfig Red;
/**
* The list of active win conditions
*/
public final List<W> WinConditions;
public final WinConditionParams<M> WinConditionParams;
public final Kits<M> Kits;
public final WinConditionParamsConfig<M> WinConditionParams;
public final KitsConfig<M> Kits;
/**
* A list of integers containing the waiting time of this enter stage in the fight
*/
public final List<Integer> EnterStages;
public final Techhider<M> Techhider;
public final TechhiderConfig<M> Techhider;
public GameModeConfig(File file, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper, Function<File, String> defaultGameName) {
YMLWrapper<M, ST, W> loader = new YMLWrapper<>(file, materialMapper, schematicTypeMapper, winconditionMapper);
private static final Field Schematic_TypeField;
static {
try {
Schematic_TypeField = SchematicConfig.class.getDeclaredField("Type");
Schematic_TypeField.setAccessible(true);
} catch (NoSuchFieldException e) {
throw new SecurityException(e.getMessage(), e);
}
}
public GameModeConfig(File file, Function<String, M> materialMapper, Function<String, W> winconditionMapper, Function<File, String> defaultGameName) {
YMLWrapper<M, W> loader = new YMLWrapper<>(file, materialMapper, winconditionMapper);
configFile = file;
loaded = loader.canLoad();
Server = new Server(loader.with("Server"));
Server = new ServerConfig(loader.with("Server"));
String deadlineString = loader.getString("Deadline", null);
if (deadlineString != null) {
@@ -120,25 +131,44 @@ public final class GameModeConfig<M, ST, W> {
}
CheckQuestions = loader.getStringList("CheckQuestions");
Times = new Times(loader.with("Times"));
Times = new TimesConfig(loader.with("Times"));
// Arena would be here to be in config order but needs Schematic.Size and EnterStages loaded afterwards
Schematic = new Schematic<>(loader.with("Schematic"));
Schematic = new SchematicConfig<>(loader.with("Schematic"));
GameName = loader.getString("GameName", defaultGameName.apply(file));
ActiveMonths = loader.getIntList("ActiveMonths");
TeamChatPrefix = loader.getString("TeamChatPrefix", "+");
Blue = new Blue(loader.with("Blue"));
Red = new Red(loader.with("Red"));
Blue = new BlueConfig(loader.with("Blue"));
Red = new RedConfig(loader.with("Red"));
WinConditions = Collections.unmodifiableList(loader.getStringList("WinConditions").stream().map(loader.winconditionMapper).collect(Collectors.toList()));
WinConditionParams = new WinConditionParams<>(loader.with("WinConditionParams"));
Kits = new Kits<>(loader.with("Kits"));
WinConditionParams = new WinConditionParamsConfig<>(loader.with("WinConditionParams"));
Kits = new KitsConfig<>(loader.with("Kits"));
EnterStages = loader.getIntList("EnterStages");
Techhider = new Techhider<>(loader.with("Techhider"));
Techhider = new TechhiderConfig<>(loader.with("Techhider"));
Arena = new Arena(loader.with("Arena"), Schematic.Size, EnterStages);
Arena = new ArenaConfig(loader.with("Arena"), Schematic.Size, EnterStages);
if (Schematic.Type != null) return;
if (!Schematic.loaded) return;
String Schematic_Type = loader.with("Schematic").getString("Type", "Normal");
if (Schematic_Type.equals("Normal")) return;
String Schematic_Shortcut = Schematic.Shortcut;
SchematicType checktype = null;
String material = loader.with("Schematic").getString("Material", "STONE_BUTTON");
if (!CheckQuestions.isEmpty()) {
checktype = new SchematicType("C" + Schematic_Type, "C" + Schematic_Shortcut, SchematicType.Type.CHECK_TYPE, null, material, true);
}
SchematicType current = new SchematicType(Schematic_Type, Schematic_Shortcut, Server.loaded ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, Deadline, Schematic.ManualCheck);
try {
Schematic_TypeField.set(Schematic, current);
} catch (IllegalAccessException e) {
throw new SecurityException(e.getMessage(), e);
}
}
@ToString
public static final class Server {
public static final class ServerConfig {
public final boolean loaded;
@@ -183,7 +213,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final boolean Ranked;
private Server(YMLWrapper loader) {
private ServerConfig(YMLWrapper loader) {
loaded = loader.canLoad();
Folder = loader.getString("Folder", null);
ServerJar = loader.getString("ServerJar", null);
@@ -196,7 +226,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class Times {
public static final class TimesConfig {
public final boolean loaded;
/**
@@ -234,7 +264,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final int SpectatorDuration;
private Times(YMLWrapper loader) {
private TimesConfig(YMLWrapper loader) {
loaded = loader.canLoad();
NoPlayersOnlineDuration = loader.getInt("NoPlayersOnlineDuration", 300);
PreSchemPasteDuration = loader.getInt("PreSchemPasteDuration", 120);
@@ -245,7 +275,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class Arena {
public static final class ArenaConfig {
public final boolean loaded;
@@ -259,12 +289,12 @@ public final class GameModeConfig<M, ST, W> {
/**
* The outer border of the arena, measured in blocks around the schematic areas
*/
public final Schem2Border Schem2Border;
public final Schem2BorderConfig Schem2Border;
/**
* The offset the teams spawn relative to the center of their area
*/
public final SpawnOffset SpawnOffset;
public final SpawnOffsetConfig SpawnOffset;
/**
* The size of the team areas are expanded around the schematics
@@ -308,11 +338,11 @@ public final class GameModeConfig<M, ST, W> {
*/
public final boolean NoFloor;
private Arena(YMLWrapper loader, Schematic.Size Size, List<Integer> EnterStages) {
private ArenaConfig(YMLWrapper loader, SchematicConfig.SizeConfig Size, List<Integer> EnterStages) {
loaded = loader.canLoad();
WaterDepth = loader.getInt("WaterDepth", 0);
Schem2Border = new Schem2Border(loader.with("Schem2Border"));
SpawnOffset = new SpawnOffset(loader.with("SpawnOffset"), Size);
Schem2Border = new Schem2BorderConfig(loader.with("Schem2Border"));
SpawnOffset = new SpawnOffsetConfig(loader.with("SpawnOffset"), Size);
BorderFromSchematic = loader.getInt("BorderFromSchematic", 21);
GroundWalkable = loader.getBoolean("GroundWalkable", true);
DisableSnowMelt = loader.getBoolean("DisableSnowMelt", false);
@@ -322,7 +352,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class Schem2Border {
public static final class Schem2BorderConfig {
public final boolean loaded;
@@ -336,7 +366,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final int z;
private Schem2Border(YMLWrapper loader) {
private Schem2BorderConfig(YMLWrapper loader) {
loaded = loader.canLoad();
x = loader.getInt("x", 24);
z = loader.getInt("z", 24);
@@ -344,7 +374,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class SpawnOffset {
public static final class SpawnOffsetConfig {
public final boolean loaded;
@@ -363,7 +393,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final double z;
private SpawnOffset(YMLWrapper loader, Schematic.Size Size) {
private SpawnOffsetConfig(YMLWrapper loader, SchematicConfig.SizeConfig Size) {
loaded = loader.canLoad();
x = loader.getDouble("x", 0);
y = loader.getDouble("y", Size.y);
@@ -373,31 +403,31 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class Schematic<M, ST> {
public static final class SchematicConfig<M> {
public final boolean loaded;
/**
* The size of the schematics
*/
public final Size Size;
public final SizeConfig Size;
/**
* Used for GameModes with a technic area
*/
public final Inset Inset;
public final InsetConfig Inset;
/**
* The schematic type that can be chosen in this arena
*
* @implSpec {@code Normal} by default
*/
public final ST Type;
public final SchematicType Type;
/**
* The schematic types that are also allowed to be chosen in this arena
*/
public final List<ST> SubTypes;
public final List<SchematicType> SubTypes;
/**
* Shortcut of the schematic type
@@ -496,10 +526,10 @@ public final class GameModeConfig<M, ST, W> {
*/
public final Map<Set<M>, Integer> Limited;
private Schematic(YMLWrapper<M, ST, ?> loader) {
private SchematicConfig(YMLWrapper<M, ?> loader) {
loaded = loader.canLoad();
Size = new Size(loader.with("Size"));
Inset = new Inset(loader.with("Inset"));
Size = new SizeConfig(loader.with("Size"));
Inset = new InsetConfig(loader.with("Inset"));
Type = loader.getSchematicType("Type", "Normal");
SubTypes = loader.getSchematicTypeList("SubTypes");
Shortcut = loader.getString("Shortcut", "");
@@ -532,7 +562,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class Size {
public static final class SizeConfig {
public final boolean loaded;
@@ -551,7 +581,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final int z;
private Size(YMLWrapper loader) {
private SizeConfig(YMLWrapper loader) {
loaded = loader.canLoad();
x = loader.getInt("x", 0);
y = loader.getInt("y", 0);
@@ -560,7 +590,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class Inset {
public static final class InsetConfig {
public final boolean loaded;
@@ -584,7 +614,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final int bottom;
private Inset(YMLWrapper loader) {
private InsetConfig(YMLWrapper loader) {
loaded = loader.canLoad();
x = loader.getInt("x", 0);
z = loader.getInt("z", 0);
@@ -595,7 +625,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class Blue {
public static final class BlueConfig {
public final boolean loaded;
@@ -609,7 +639,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final String Prefix;
private Blue(YMLWrapper loader) {
private BlueConfig(YMLWrapper loader) {
loaded = loader.canLoad();
Name = loader.getString("Name", "Blau");
Prefix = loader.getString("Prefix", "§3");
@@ -617,7 +647,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class Red {
public static final class RedConfig {
public final boolean loaded;
@@ -631,7 +661,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final String Prefix;
private Red(YMLWrapper loader) {
private RedConfig(YMLWrapper loader) {
loaded = loader.canLoad();
Name = loader.getString("Name", "Rot");
Prefix = loader.getString("Prefix", "§c");
@@ -639,7 +669,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class WinConditionParams<M> {
public static final class WinConditionParamsConfig<M> {
public final boolean loaded;
@@ -683,7 +713,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final int TechKoTime;
private WinConditionParams(YMLWrapper<M, ?, ?> loader) {
private WinConditionParamsConfig(YMLWrapper<M, ?> loader) {
loaded = loader.canLoad();
TimeoutTime = loader.getInt("TimeoutTime", 1200);
PercentWin = loader.getDouble("PercentWin", 7.0);
@@ -695,7 +725,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class Kits<M> {
public static final class KitsConfig<M> {
public final boolean loaded;
@@ -732,7 +762,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final List<M> ForbiddenItems;
private Kits(YMLWrapper<M, ?, ?> loader) {
private KitsConfig(YMLWrapper<M, ?> loader) {
loaded = loader.canLoad();
File = loader.getString("File", "kits.yml");
MemberDefault = loader.getString("MemberDefault", "default");
@@ -743,7 +773,7 @@ public final class GameModeConfig<M, ST, W> {
}
@ToString
public static final class Techhider<M> {
public static final class TechhiderConfig<M> {
public final boolean loaded;
@@ -771,7 +801,7 @@ public final class GameModeConfig<M, ST, W> {
*/
public final Set<String> HiddenBlockEntities;
private Techhider(YMLWrapper<M, ?, ?> loader) {
private TechhiderConfig(YMLWrapper<M, ?> loader) {
loaded = loader.canLoad();
Active = loader.getBoolean("Active", false);
ObfuscateWith = loader.getMaterial("ObfuscateWith", "end_stone");
@@ -816,12 +846,7 @@ public final class GameModeConfig<M, ST, W> {
public String getSchemTypeOrInternalName() {
if (Schematic.loaded) {
ST type = Schematic.Type;
if (type instanceof SchematicType) {
return ((SchematicType) type).toDB();
} else if (type instanceof String) {
return (String) type;
}
return Schematic.Type.toDB();
}
return configFile.getName().replace(".yml", "");
}
@@ -20,7 +20,6 @@
package de.steamwar.sql;
import de.steamwar.ImplementationProvider;
import de.steamwar.data.GameModeConfig;
import java.io.File;
@@ -29,7 +28,7 @@ public interface SQLWrapper {
File getSchemTypesFolder();
default void processSchematicType(GameModeConfig<String, String, String> gameModeConfig, SchematicType type) {
default void processSchematicType(GameModeConfig<String, String> gameModeConfig) {
}
void additionalExceptionMetadata(StringBuilder builder);
@@ -19,7 +19,6 @@
package de.steamwar.sql;
import de.steamwar.data.GameModeConfig;
import de.steamwar.sql.internal.SqlTypeMapper;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@@ -47,28 +46,18 @@ public class SchematicType {
File folder = SQLWrapper.impl.getSchemTypesFolder();
if (folder.exists()) {
for (File configFile : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().collect(Collectors.toList())) {
GameModeConfig<String, String, String> gameModeConfig = new GameModeConfig<>(configFile, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear);
if (!gameModeConfig.Schematic.loaded) continue;
String type = gameModeConfig.Schematic.Type;
assert type != null;
String shortcut = gameModeConfig.Schematic.Shortcut;
if (tmpFromDB.containsKey(type.toLowerCase()))
continue;
SchematicType checktype = null;
String material = gameModeConfig.Schematic.Material;
GameModeConfig<String, String> gameModeConfig = new GameModeConfig<>(configFile, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear);
if (gameModeConfig.Schematic.Type == null) continue;
if (tmpFromDB.containsKey(gameModeConfig.Schematic.Type.toDB())) continue;
SchematicType current = gameModeConfig.Schematic.Type;
if (!gameModeConfig.CheckQuestions.isEmpty()) {
checktype = new SchematicType("C" + type, "C" + shortcut, SchematicType.Type.CHECK_TYPE, null, material, true);
tmpTypes.add(checktype);
tmpFromDB.put(checktype.toDB(), checktype);
SchematicType checkType = current.checkType;
tmpTypes.add(checkType);
tmpFromDB.put(checkType.toDB(), checkType);
}
SchematicType current = new SchematicType(type, shortcut, gameModeConfig.Server.loaded ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, gameModeConfig.Deadline, gameModeConfig.Schematic.ManualCheck);
tmpTypes.add(current);
tmpFromDB.put(type.toLowerCase(), current);
SQLWrapper.impl.processSchematicType(gameModeConfig, current);
tmpFromDB.put(current.toDB(), current);
SQLWrapper.impl.processSchematicType(gameModeConfig);
}
}
time = System.currentTimeMillis() - time;
@@ -143,6 +132,7 @@ public class SchematicType {
}
public static SchematicType fromDB(String input) {
if (fromDB == null) return null;
return fromDB.get(input.toLowerCase());
}
@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.data;
package de.steamwar.sql;
import org.yaml.snakeyaml.Yaml;
@@ -31,18 +31,16 @@ import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
final class YMLWrapper<M, ST, W> {
final class YMLWrapper<M, W> {
final Function<String, M> materialMapper;
final Function<String, ST> schematicTypeMapper;
final Function<String, W> winconditionMapper;
private final boolean canLoad;
private final Map<String, Object> document;
YMLWrapper(File file, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper) {
YMLWrapper(File file, Function<String, M> materialMapper, Function<String, W> winconditionMapper) {
this.materialMapper = materialMapper;
this.schematicTypeMapper = schematicTypeMapper;
this.winconditionMapper = winconditionMapper;
Yaml yaml = new Yaml();
@@ -60,9 +58,8 @@ final class YMLWrapper<M, ST, W> {
this.canLoad = canLoad;
}
private YMLWrapper(boolean canLoad, Map<String, Object> document, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper) {
private YMLWrapper(boolean canLoad, Map<String, Object> document, Function<String, M> materialMapper, Function<String, W> winconditionMapper) {
this.materialMapper = materialMapper;
this.schematicTypeMapper = schematicTypeMapper;
this.winconditionMapper = winconditionMapper;
this.canLoad = canLoad;
this.document = document;
@@ -72,24 +69,26 @@ final class YMLWrapper<M, ST, W> {
return canLoad;
}
public YMLWrapper<M, ST, W> with(String path) {
public YMLWrapper<M, W> with(String path) {
if (document.containsKey(path)) {
Object value = document.get(path);
if (value instanceof Map) {
return new YMLWrapper<>(true, (Map) value, materialMapper, schematicTypeMapper, winconditionMapper);
return new YMLWrapper<>(true, (Map) value, materialMapper, winconditionMapper);
}
}
return new YMLWrapper<>(false, Collections.emptyMap(), materialMapper, schematicTypeMapper, winconditionMapper);
return new YMLWrapper<>(false, Collections.emptyMap(), materialMapper, winconditionMapper);
}
public <T> T get(String path, T defaultValue, Function<Object, T> mapper) {
Object value = this.document.get(path);
if (value == null) return defaultValue;
try {
return mapper.apply(value);
T mapped = mapper.apply(value);
if (mapped != null) return mapped;
} catch (ClassCastException e) {
return defaultValue;
// Ignore exception
}
return defaultValue;
}
public String getString(String path, String defaultValue) {
@@ -108,9 +107,9 @@ final class YMLWrapper<M, ST, W> {
return get(path, defaultValue, Boolean.class::cast);
}
public ST getSchematicType(String path, String defaultValue) {
public SchematicType getSchematicType(String path, String defaultValue) {
String schematicType = getString(path, defaultValue);
return schematicTypeMapper.apply(schematicType);
return SchematicType.fromDB(schematicType);
}
public M getMaterial(String path, String defaultValue) {
@@ -135,12 +134,12 @@ final class YMLWrapper<M, ST, W> {
return get(path, o -> (List<Integer>) o);
}
public List<ST> getSchematicTypeList(String path) {
public List<SchematicType> getSchematicTypeList(String path) {
List<String> list = getStringList(path);
if (list.isEmpty()) {
return Collections.emptyList();
} else {
return Collections.unmodifiableList(list.stream().map(schematicTypeMapper).collect(Collectors.toList()));
return Collections.unmodifiableList(list.stream().map(SchematicType::fromDB).collect(Collectors.toList()));
}
}