Skip to content

Commit

Permalink
Get end time from stream schedule payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Oct 14, 2023
1 parent 03a372a commit 024750f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

@Mod.EventBusSubscriber(modid = LTExtras.MODID)
Expand All @@ -30,37 +29,37 @@ public class SchedulePlaceholders {
private static Instant lastFetchTime = Instant.EPOCH;

static {
registerPlaceholder("current/title", (ctx, current, next) -> PlaceholderResult.value(current.shortDescription()));
registerPlaceholder("current/description", (ctx, current, next) -> PlaceholderResult.value(current.longDescription()));
registerPlaceholder("current/hosts", (ctx, current, next) -> formatHosts(current));
registerPlaceholder("current/start", (ctx, current, next) -> formatLocalTime(ctx, current.time()));
registerPlaceholder("current/end", (ctx, current, next) -> next != null ? formatLocalTime(ctx, next.time()) : UNKNOWN);

registerPlaceholderNext("next/title", (ctx, next) -> PlaceholderResult.value(next.shortDescription()));
registerPlaceholderNext("next/description", (ctx, next) -> PlaceholderResult.value(next.longDescription()));
registerPlaceholderNext("next/hosts", (ctx, next) -> formatHosts(next));
registerPlaceholderNext("next/start", (ctx, next) -> formatLocalTime(ctx, next.time()));
registerPlaceholderNext("next/time_until", (ctx, next) -> formatTimeUntil(next));
registerForEntry(false);
registerForEntry(true);
}

private static void registerPlaceholder(final String id, final PlaceholderFunction function) {
private static void registerForEntry(final boolean next) {
final String prefix = next ? "next" : "current";
registerPlaceholder(prefix + "/title", false, (ctx, entry) -> PlaceholderResult.value(entry.shortDescription()));
registerPlaceholder(prefix + "/description", false, (ctx, entry) -> PlaceholderResult.value(entry.longDescription()));
registerPlaceholder(prefix + "/hosts", false, (ctx, entry) -> formatHosts(entry));
registerPlaceholder(prefix + "/start", false, (ctx, entry) -> formatLocalTime(ctx, entry.startTime()));
registerPlaceholder(prefix + "/end", false, (ctx, entry) -> formatLocalTime(ctx, entry.endTime()));
registerPlaceholder(prefix + "/time_until", true, (ctx, entry) -> formatTimeUntil(entry));
}

private static void registerPlaceholder(final String id, final boolean next, final PlaceholderFunction function) {
Placeholders.register(new ResourceLocation(LTExtras.MODID, "schedule/" + id), (ctx, arg) -> {
final StreamSchedule schedule = SchedulePlaceholders.schedule;
if (schedule == null) {
return UNKNOWN;
}
final StreamSchedule.State state = schedule.stateAt(Instant.now());
if (state != null) {
return function.get(ctx, state.currentEntry(), state.nextEntry());
final StreamSchedule.Entry entry = next ? state.nextEntry() : state.currentEntry();
if (entry != null) {
return function.get(ctx, entry);
}
}
return UNKNOWN;
});
}

private static void registerPlaceholderNext(final String id, final BiFunction<PlaceholderContext, StreamSchedule.Entry, PlaceholderResult> function) {
registerPlaceholder(id, (ctx, current, next) -> next != null ? function.apply(ctx, next) : UNKNOWN);
}

private static PlaceholderResult formatHosts(final StreamSchedule.Entry entry) {
return PlaceholderResult.value(entry.hosts().stream()
.map(StreamSchedule.Host::name)
Expand All @@ -69,7 +68,7 @@ private static PlaceholderResult formatHosts(final StreamSchedule.Entry entry) {
}

private static PlaceholderResult formatTimeUntil(final StreamSchedule.Entry entry) {
Duration duration = Duration.between(Instant.now(), entry.time());
Duration duration = Duration.between(Instant.now(), entry.startTime());
if (duration.isNegative()) {
duration = Duration.ZERO;
}
Expand Down Expand Up @@ -104,6 +103,6 @@ public static void onServerTick(final TickEvent.ServerTickEvent event) {
}

private interface PlaceholderFunction {
PlaceholderResult get(PlaceholderContext ctx, StreamSchedule.Entry current, @Nullable StreamSchedule.Entry next);
PlaceholderResult get(PlaceholderContext ctx, StreamSchedule.Entry entry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,25 @@ public static CompletableFuture<Optional<StreamSchedule>> fetch() {
public State stateAt(final Instant time) {
for (int i = 0; i < entries.size(); i++) {
final Entry entry = entries.get(i);
if (!entry.time().isBefore(time)) {
if (!entry.startTime().isBefore(time)) {
final Entry nextEntry = i + 1 < entries.size() ? entries.get(i + 1) : null;
return new State(entry, nextEntry);
}
}
return null;
}

public record Entry(String shortDescription, String longDescription, Instant time, List<Host> hosts) {
private static final Codec<Instant> TIME_CODEC = MoreCodecs.localDateTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS")).xmap(
public record Entry(String shortDescription, String longDescription, Instant startTime, Instant endTime, List<Host> hosts) {
private static final Codec<Instant> TIME_CODEC = MoreCodecs.localDateTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).xmap(
localTime -> localTime.atOffset(ZoneOffset.UTC).toInstant(),
instant -> instant.atOffset(ZoneOffset.UTC).toLocalDateTime()
);

public static final Codec<Entry> CODEC = RecordCodecBuilder.create(i -> i.group(
Codec.STRING.fieldOf("short_desc").forGetter(Entry::shortDescription),
Codec.STRING.optionalFieldOf("long_desc", "").forGetter(Entry::longDescription),
TIME_CODEC.fieldOf("time").forGetter(Entry::time),
TIME_CODEC.fieldOf("time").forGetter(Entry::startTime),
TIME_CODEC.fieldOf("end_time").forGetter(Entry::endTime),
Host.CODEC.listOf().fieldOf("hosts").forGetter(Entry::hosts)
).apply(i, Entry::new));
}
Expand Down

0 comments on commit 024750f

Please sign in to comment.