Skip to content

Commit

Permalink
First batch of primary options
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas committed May 22, 2024
1 parent 75af5b7 commit cc7c808
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Populated by the game options field of the table that is passed to `CLobby:LaunchGame`
*
* @param autoTeams
* @param teamLock
* @param teamSpawn
Expand All @@ -13,19 +14,20 @@
* @param revealedCivilians
* @param scoreEnabled
* @param unitCap
* @param unRated
* @param unRanked
* @param victory
*/
public record GameOptions(AutoTeams autoTeams, TeamLock teamLock, TeamSpawn teamSpawn, boolean allowObservers,
boolean cheatsEnabled, boolean prebuiltUnits, boolean revealedCivilians, boolean scoreEnabled,
int unitCap, boolean unRated, Victory victory) {
public record GameOptions(AutoTeams autoTeams, TeamLock teamLock, TeamSpawn teamSpawn, Boolean allowObservers,
Boolean cheatsEnabled, Boolean prebuiltUnits, Boolean revealedCivilians, Boolean scoreEnabled,
Integer unitCap, String unRanked, Victory victory) {

public enum AutoTeams {
NONE("none", "None"),
MANUAL("manual", "Manual"),
TOP_VS_BOTTOM("tvsb", "Top versus bottom"),
LEFT_VS_RIGHT("lvsr", "Left versus right"),
EVEN_VS_UNEVEN("pvsi", "Even versus uneven");
EVEN_VS_UNEVEN("pvsi", "Even versus uneven"),
UNKNOWN("unknown", "Unknown");

public final String readable;

Expand All @@ -37,17 +39,17 @@ public enum AutoTeams {
}

public static AutoTeams findByKey(String key) {
for (AutoTeams autoTeam : values()) {
if (autoTeam.key.equals(key)) {
return autoTeam;
for (AutoTeams value : values()) {
if (value.key.equals(key)) {
return value;
}
}
throw new IllegalArgumentException("Unknown key: " + key);
return AutoTeams.UNKNOWN;
}
}

public enum TeamLock {
LOCKED("locked", "Locked"), UNLOCKED("unlocked", "Unlocked");
LOCKED("locked", "Locked"), UNLOCKED("unlocked", "Unlocked"), UNKNOWN("unknown", "Unknown");;

public final String readable;

Expand All @@ -59,12 +61,13 @@ public enum TeamLock {
}

public static TeamLock findByKey(String key) {
for (TeamLock teamLock : values()) {
if (teamLock.key.equals(key)) {
return teamLock;
for (TeamLock value : values()) {
if (value.key.equals(key)) {
return value;
}
}
throw new IllegalArgumentException("Unknown key: " + key);

return TeamLock.UNKNOWN;
}
}

Expand All @@ -77,7 +80,8 @@ public enum TeamSpawn {
RANDOM_REVEAL("random_reveal", "Random and revealed"),
BALANCED_REVEAL("balanced_reveal", "Balanced and revealed"),
BALANCED_REVEAL_MIRRORED("balanced_reveal_mirrored", "Mirror balanced and revealed"),
BALANCED_FLEX_REVEAL("balanced_flex_reveal", "Flexible balanced and revealed");
BALANCED_FLEX_REVEAL("balanced_flex_reveal", "Flexible balanced and revealed"),
UNKNOWN("unknown", "Unknown");

public final String readable;

Expand All @@ -89,20 +93,22 @@ public enum TeamSpawn {
}

public static TeamSpawn findByKey(String key) {
for (TeamSpawn teamSpawn : values()) {
if (teamSpawn.key.equals(key)) {
return teamSpawn;
for (TeamSpawn value : values()) {
if (value.key.equals(key)) {
return value;
}
}
throw new IllegalArgumentException("Unknown key: " + key);

return TeamSpawn.UNKNOWN;
}
}

public enum Victory {
DEMORALIZATION("demoralization", "Assasination"),
DOMINATION("domination", "Supremacy"),
ERADICATION("eradication", "Annihilation"),
SANDBOX("sandbox", "Sandbox");
SANDBOX("sandbox", "Sandbox"),
UNKNOWN("unknown", "Unknown");

public final String readable;

Expand All @@ -114,13 +120,68 @@ public enum Victory {
}

public static Victory findByKey(String key) {
for (Victory victory : values()) {
if (victory.key.equals(key)) {
return victory;
for (Victory value : values()) {
if (value.key.equals(key)) {
return value;
}
}

return Victory.UNKNOWN;
}
}

public enum SharedArmy {
OFF("demoralization", "Assasination"),
UNION("domination", "Supremacy"),
COMMON("eradication", "Annihilation"),
UNKNOWN("unknown", "Unknown");

public final String readable;

private final String key;

SharedArmy(String key, String readable) {
this.key = key;
this.readable = readable;
}

public static SharedArmy findByKey(String key) {
for (SharedArmy value : values()) {
if (value.key.equals(key)) {
return value;
}
}
throw new IllegalArgumentException("Unknown key: " + key);

return SharedArmy.UNKNOWN;
}
}

public enum Share {
FULLSHARE("FullShare", "Full share"),
SHAREUNTILDEATH("ShareUntilDeath", "Share until death"),
PARTIALSHARE("PartialShare", "Partial share"),
TRAITORS("TransferToKiller", "Traitors"),
DEFECTORS("Defectors", "Defectors"),
DESERTION("CivilianDeserter", "Desert to civilians"),
UNKNOWN("unknown", "Unknown");

public final String readable;

private final String key;

Share(String key, String readable) {
this.key = key;
this.readable = readable;
}

public static Share findByKey(String key) {
for (Share value : values()) {
if (value.key.equals(key)) {
return value;
}
}

return Share.UNKNOWN;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,36 @@ public static ReplayHeader parse(LittleEndianDataInputStream dataStream) throws

// retrieve and manage the game options
GameOptions primaryOptions = null;
HashMap<String, String> secondaryOptions = new HashMap<String, String>();
Map<String, String> secondaryOptions = null;
if (table.value().get("Options") instanceof LuaData.Table optionsTable) {

primaryOptions = new GameOptions(
GameOptions.AutoTeams.findByKey(optionsTable.getString("AutoTeams")),
GameOptions.TeamLock.findByKey(optionsTable.getString("TeamLock")),
GameOptions.TeamSpawn.findByKey(optionsTable.getString("TeamSpawn")),
optionsTable.getBool("AllowObservers"),
optionsTable.getBool("CheatsEnabled"),
optionsTable.getBool("PrebuiltUnits"),
optionsTable.getBool("RevealCivilians"),
optionsTable.getBool("Score"),
optionsTable.getInteger("UnitCap"),
optionsTable.getString("Unranked"),
GameOptions.Victory.findByKey(optionsTable.getString("Victory"))
);

optionsTable.removeKey("AutoTeams");
optionsTable.removeKey("TeamLock");
optionsTable.removeKey("TeamSpawn");
optionsTable.removeKey("AllowObservers");
optionsTable.removeKey("CheatsEnabled");
optionsTable.removeKey("PrebuiltUnits");
optionsTable.removeKey("RevealCivilians");
optionsTable.removeKey("Score");
optionsTable.removeKey("UnitCap");
optionsTable.removeKey("Unranked");
optionsTable.removeKey("Victory");

secondaryOptions = optionsTable.toMap();
}

Integer sizeX = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.faforever.commons.replay.shared;

import java.util.HashMap;
import java.util.Map;

public sealed interface LuaData {
Expand All @@ -10,6 +11,10 @@ record Bool(boolean value) implements LuaData {}

record Table(Map<java.lang.String, LuaData> value) implements LuaData {

public void removeKey(java.lang.String key) {
value.remove(key);
}

public Boolean getBool(java.lang.String key) {
if (value.get(key) instanceof LuaData.Bool (boolean bool)) {
return bool;
Expand Down Expand Up @@ -49,5 +54,33 @@ public LuaData.Table getTable(java.lang.String key) {

return null;
}

public Map<java.lang.String, java.lang.String> toMap() {
HashMap<java.lang.String, java.lang.String> map = new HashMap<java.lang.String, java.lang.String>();

value.forEach(
(k, v) -> {
switch (v) {
case LuaData.Nil e -> {
map.put(k, "nil");
}
case Bool bool -> {
map.put(k, java.lang.String.valueOf(bool.value()));
}
case Number number -> {
map.put(k, java.lang.String.valueOf(number.value()));
}
case String string -> {
map.put(k, java.lang.String.valueOf(string.value()));
}
case Table table -> {
map.put(k, java.lang.String.valueOf("table"));
}
}
}
);

return map;
}
}
}

0 comments on commit cc7c808

Please sign in to comment.