Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xpdota committed Dec 6, 2023
1 parent 6b383e9 commit 49b2485
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 207 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import gg.xp.reevent.events.Event;
import gg.xp.xivsupport.timelines.cbevents.CbEventTypes;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -95,7 +96,7 @@ public static List<TimelineEntry> parseMultiple(Collection<String> line) {
if (syncRaw == null) {
sync = null;
if (eventTypeRaw != null) {
CactbotEventTypes eventDef = CactbotEventTypes.valueOf(eventTypeRaw);
CbEventTypes eventDef = CbEventTypes.valueOf(eventTypeRaw);
String eventCondRaw = matcher.group("eventCond");
// TODO: support translation for this
Map<String, String> conditions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gg.xp.xivsupport.timelines.cbevents;

import java.util.function.Predicate;

/**
* Represents a conversion from some field (possibly nested) on an event, to a predicate that matches events.
* All cactbot netregices use string values regardless of the underlying data type, so this always takes a string.
*
* @param <X> The event type.
*/
@FunctionalInterface
interface CbConversion<X> {
/**
* Example: on a 21-line, we want to check if the ability ID is "12AB".
* We would call this with "12AB" as the argument, and it should return a predicate that checks that
* a given AbilityUsedEvent has an ability ID of 0x12AB.
*
* @param input The input string.
* @return The resulting predicate.
*/
Predicate<X> convert(String input);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package gg.xp.xivsupport.timelines.cbevents;

import gg.xp.xivsupport.events.actlines.events.NameIdPair;
import org.apache.commons.lang3.StringUtils;

import java.util.function.Function;
import java.util.regex.Pattern;

class CbConversions {

/**
* Convenience function for quickly making a ConvToCondition on an integer/long field.
* If the input string in the resulting ConvToCondition is a plain number (and not something that would require
* us to actually do regex), then the numbers will be compared directly.
*
* @param getter A function for getting the required value out of our event.
* @param base The numerical base, typically 10 or 16
* @param <X> The event type
* @return The condition matching the above requirements.
*/
static <X> CbConversion<X> intConv(Function<X, Long> getter, int base) {
return intConv(getter, base, 0);
}

/**
* Convenience function for quickly making a ConvToCondition on an integer/long field.
* If the input string in the resulting ConvToCondition is a plain number (and not something that would require
* us to actually do regex), then the numbers will be compared directly.
* <p>
* This version of the method allows you to specify that the number should be left-padded to a minimum number of
* characters, with zeroes. e.g. if the input is "00", and the value is "0", then in order for that to match, you
* would need to specify minDigits == 2.
*
* @param getter A function for getting the required value out of our event.
* @param base The numerical base, typically 10 or 16
* @param minDigits If ACT would left-pad the number with zeroes, then you should specify the minimum length
* of the number here so that the value can be similarly padded out.
* @param <X> The event type
* @return The condition matching the above requirements.
*/
static <X> CbConversion<X> intConv(Function<X, Long> getter, int base, int minDigits) {
return str -> {
try {
// Fast path - input is a number literal, so do a direct number comparison
long parsed = Long.parseLong(str, base);
return item -> getter.apply(item) == parsed;
}
catch (NumberFormatException ignored) {
// Slow path - input is a regex, so compile to regex first
Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE);
return item -> {
String asString = Long.toString(getter.apply(item), base);
if (minDigits > 1) {
asString = StringUtils.leftPad(asString, minDigits, '0');
}
return pattern.matcher(asString).matches();
};
}
};
}

static <X> CbConversion<X> strConv(Function<X, String> getter) {
return str -> {
Pattern pattern = Pattern.compile(str);
return item -> pattern.matcher(getter.apply(item)).matches();
};
}

static <X> CbConversion<X> id(Function<X, NameIdPair> getter) {
return intConv(e -> getter.apply(e).getId(), 16);
}

static <X> CbConversion<X> named(Function<X, NameIdPair> getter) {
return strConv(e -> getter.apply(e).getName());
}

static <X> CbConversion<X> boolToInt(Function<X, Boolean> getter) {
return str -> switch (str) {
case "0" -> (item -> !getter.apply(item));
case "1" -> (getter::apply);
default -> throw new IllegalArgumentException("Expected 0 or 1, got '%s'".formatted(str));
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package gg.xp.xivsupport.timelines.cbevents;

import gg.xp.reevent.events.Event;

import java.util.List;

public interface CbEventDesc<X extends Event> {

Class<X> getEventType();
List<CbfMap<? super X>> getFieldMappings();

}
Loading

0 comments on commit 49b2485

Please sign in to comment.