Skip to content

Commit

Permalink
Timeline customization import/export
Browse files Browse the repository at this point in the history
  • Loading branch information
xpdota committed Jan 10, 2024
1 parent 979f39b commit d8dda92
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,9 @@ public boolean enabledForJob(Job job) {
public @Nullable String getImportSource() {
return importSource;
}

@Override
public void setImportSource(@Nullable String importSource) {
this.importSource = importSource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

@JsonDeserialize(using = CustomTimelineDeserializer.class)
public interface CustomTimelineItem extends TimelineEntry {
void setImportSource(String importSource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class CustomTimelineLabel implements CustomTimelineItem {
public double time;
@JsonProperty("name")
public @Nullable String name;
@JsonProperty("importSource")
public @Nullable String importSource;

public static CustomTimelineLabel overrideFor(TimelineEntry item) {
if (!item.isLabel()) {
Expand Down Expand Up @@ -112,4 +114,15 @@ public static CustomTimelineLabel cloneFor(TimelineEntry other) {
out.time = other.time();
return out;
}

@Override
public void setImportSource(@Nullable String importSource) {
this.importSource = importSource;
}

@Nullable
@Override
public String getImportSource() {
return importSource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gg.xp.xivsupport.timelines;

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

public class TimelineCustomizationExport {
public Map<Long, TimelineCustomizations> timelineCustomizations;

public static TimelineCustomizationExport empty() {
TimelineCustomizationExport out = new TimelineCustomizationExport();
out.timelineCustomizations = new HashMap<>();
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.xp.xivsupport.timelines;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import gg.xp.xivsupport.persistence.UseJsonSer;

Expand All @@ -23,4 +24,9 @@ public void setEntries(List<CustomTimelineItem> entries) {
this.entries = new ArrayList<>(entries);
this.entries.sort(Comparator.naturalOrder());
}

@JsonIgnore
public boolean isEmpty() {
return entries.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.xp.xivsupport.timelines;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import gg.xp.reevent.events.BaseEvent;
import gg.xp.reevent.events.CurrentTimeSource;
Expand All @@ -9,7 +10,6 @@
import gg.xp.xivsupport.callouts.ModifiableCallout;
import gg.xp.xivsupport.callouts.ModifiedCalloutHandle;
import gg.xp.xivsupport.callouts.RawModifiedCallout;
import gg.xp.xivsupport.events.ACTLogLineEvent;
import gg.xp.xivsupport.events.actlines.events.MapChangeEvent;
import gg.xp.xivsupport.events.actlines.events.ZoneChangeEvent;
import gg.xp.xivsupport.events.actlines.events.actorcontrol.VictoryEvent;
Expand All @@ -36,11 +36,13 @@
import java.io.InputStream;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

Expand All @@ -49,6 +51,7 @@ public final class TimelineManager {
private static final Logger log = LoggerFactory.getLogger(TimelineManager.class);
private static final Map<Long, TimelineInfo> zoneIdToTimelineFile = new HashMap<>();
private static final ObjectMapper mapper = new ObjectMapper();
public static final String CUSTOMIZATION_EXPORT_SOURCE = "Exported Customization";
private final CurrentTimeSource timeSource;
private final BooleanSetting debugMode;
private final BooleanSetting prePullShow;
Expand Down Expand Up @@ -119,7 +122,7 @@ private static void ensureInit() {
}
}

public TimelineInfo getInfoForZone(long zoneId) {
public @Nullable TimelineInfo getInfoForZone(long zoneId) {
ensureInit();
return zoneIdToTimelineFile.get(zoneId);
}
Expand Down Expand Up @@ -371,4 +374,42 @@ public TimelineProcessor getCurrentProcessor() {
public CurrentTimeSource getTimeSource() {
return this.timeSource;
}

public TimelineCustomizationExport exportCustomizations(Collection<Long> zones) {
if (zones.isEmpty()) {
throw new IllegalArgumentException("Must specify at least one zone");
}
TimelineCustomizationExport out = TimelineCustomizationExport.empty();
zones.forEach(z -> out.timelineCustomizations.put(z, getCustomSettings(z)));
return out;
}

public String serializeCurrentCustomizations(Set<Long> zones) {
try {
return mapper.writeValueAsString(exportCustomizations(zones));
}
catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

public TimelineCustomizationExport deserializeCustomizations(String serialized) {
try {
return mapper.readValue(serialized, TimelineCustomizationExport.class);
}
catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

public ZoneTimelineDescription describeZone(long zoneId) {
TimelineInfo info = getInfoForZone(zoneId);
if (info == null) {
return new ZoneTimelineDescription(zoneId, null);
}
else {
return new ZoneTimelineDescription(zoneId, info.filename());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gg.xp.xivsupport.timelines;

import gg.xp.xivdata.data.*;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public final class ZoneTimelineDescription {
private final long zoneId;
private final @Nullable String filename;
private final @Nullable String dutyName;

public ZoneTimelineDescription(
long zoneId,
@Nullable String filename
) {
this.zoneId = zoneId;
this.dutyName = ZoneLibrary.capitalizedNameForZone((int) zoneId);
this.filename = filename;
}

public String getDescription() {
if (filename == null) {
if (dutyName == null) {
return "zone %s".formatted(zoneId);
}
else {
return "duty '%s' (zone %s)".formatted(dutyName, zoneId);
}
}
else {
if (dutyName == null) {
return "timeline '%s' (zone %s)".formatted(filename, zoneId);
}
else {
return "duty '%s' (timeline '%s', zone '%s')".formatted(dutyName, filename, zoneId);
}
}
}

public long getZoneId() {
return zoneId;
}

public @Nullable String getFilename() {
return filename;
}

public @Nullable String getDutyName() {
return dutyName;
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (ZoneTimelineDescription) obj;
return this.zoneId == that.zoneId &&
Objects.equals(this.filename, that.filename) &&
Objects.equals(this.dutyName, that.dutyName);
}

@Override
public int hashCode() {
return Objects.hash(zoneId, filename, dutyName);
}

@Override
public String toString() {
return "ZoneDescription[" +
"zoneId=" + zoneId + ", " +
"filename=" + filename + ", " +
"duty=" + dutyName + ']';
}

}
Loading

0 comments on commit d8dda92

Please sign in to comment.