Skip to content

Commit

Permalink
Refactored EventFieldDefaults
Browse files Browse the repository at this point in the history
Added OptKampagne default
  • Loading branch information
Alf-Melmac committed May 15, 2021
1 parent 12cabab commit cdbb8b6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 17 deletions.
5 changes: 3 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.webalf.slotbot.model.annotations;

import org.atteo.classindex.IndexAnnotated;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Alf
* @since 15.05.2021
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@IndexAnnotated
public @interface EventFieldDefault {
String eventTypeName();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.webalf.slotbot.util.eventfield;

import de.webalf.slotbot.controller.website.FileController;
import de.webalf.slotbot.model.annotations.EventFieldDefault;
import de.webalf.slotbot.model.dtos.EventFieldDefaultDto;
import lombok.experimental.UtilityClass;

Expand All @@ -15,9 +16,8 @@
* @since 27.04.2021
*/
@UtilityClass
public class Arma3FieldUtils {
public static final String EVENT_TYPE_NAME = "Arma 3";

@EventFieldDefault(eventTypeName = "Arma 3")
public final class Arma3FieldUtils {
private static final List<String> MOD_PACKS = List.of("2103_ArmaMachtBock", "2104_ArmaMachtBock_GM", "2105_ArmaMachtBock_VN",
"Joined_Operations_2020", "Alliance_2021v1");

Expand All @@ -34,7 +34,8 @@ public class Arma3FieldUtils {
"Summa", "Summa winter", "Takistan", "Takistan Mountains", "Tanoa", "United Sahrani", "Utes", "Vinjesvingen",
"Virolahti", "Virtuelle Realität", "Werferlingen", "Zargabad");

public static final List<EventFieldDefaultDto> FIELDS = List.of(
@SuppressWarnings("unused") //EventFieldUtils#eventTypeNameToFieldDefaults
static final List<EventFieldDefaultDto> FIELDS = List.of(
EventFieldDefaultDto.builder().title("Respawn").type(BOOLEAN).build(),
EventFieldDefaultDto.builder().title("Modpack").type(TEXT_WITH_SELECTION).selection(MOD_PACKS)
.text(MOD_PACKS.get(0)).build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import de.webalf.slotbot.model.EventField;
import de.webalf.slotbot.model.EventType;
import de.webalf.slotbot.model.annotations.EventFieldDefault;
import de.webalf.slotbot.model.dtos.EventFieldDefaultDto;
import de.webalf.slotbot.model.enums.EventFieldType;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.atteo.classindex.ClassIndex;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;

import static de.webalf.slotbot.model.enums.EventFieldType.SELECTION;
import static de.webalf.slotbot.model.enums.EventFieldType.TEXT_WITH_SELECTION;
Expand All @@ -19,6 +20,7 @@
* @since 10.05.2021
*/
@UtilityClass
@Slf4j
public final class EventFieldUtils {
/**
* Returns a link if the given {@link EventField} references something.
Expand All @@ -36,19 +38,30 @@ public static String buildOptionalLink(EventField eventField) {
return null;
}

private static final Map<String, List<EventFieldDefaultDto>> eventTypeNameToFieldDefaults = new HashMap<>();
static {
ClassIndex.getAnnotated(EventFieldDefault.class)
.forEach(aClass -> {
try {
//noinspection unchecked
eventTypeNameToFieldDefaults.put(
aClass.getAnnotation(EventFieldDefault.class).eventTypeName(),
(List<EventFieldDefaultDto>) aClass.getDeclaredField("FIELDS").get(null)
);
} catch (NoSuchFieldException | IllegalAccessException e) {
log.error("Wrong implementation of EventFieldDefault: " + aClass.getName(), e);
}
});
}

/**
* Returns the default {@link EventField}s for the given {@link EventType#name}
* Returns the default {@link EventFieldDefaultDto}s for the given {@link EventType#name}
*
* @param eventTypeName name of event type
* @return matching default fields (including only field titles)
* @return matching default fields
*/
public static List<EventFieldDefaultDto> getDefault(String eventTypeName) {
switch (eventTypeName) {
case Arma3FieldUtils.EVENT_TYPE_NAME:
return Arma3FieldUtils.FIELDS;
default:
return Collections.emptyList();
}
return eventTypeNameToFieldDefaults.get(eventTypeName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.webalf.slotbot.util.eventfield;

import de.webalf.slotbot.model.annotations.EventFieldDefault;
import de.webalf.slotbot.model.dtos.EventFieldDefaultDto;
import lombok.experimental.UtilityClass;

import java.util.List;

import static de.webalf.slotbot.model.enums.EventFieldType.BOOLEAN;
import static de.webalf.slotbot.model.enums.EventFieldType.TEXT;

/**
* @author Alf
* @since 14.05.2021
*/
@UtilityClass
@EventFieldDefault(eventTypeName = "OPT Kampagne")
public class OptKampagneFieldUtils {
@SuppressWarnings("unused") //EventFieldUtils#eventTypeNameToFieldDefaults
static final List<EventFieldDefaultDto> FIELDS = List.of(
EventFieldDefaultDto.builder().title("Karte").type(TEXT).text("Livonia").build(),
EventFieldDefaultDto.builder().title("Modpack").type(TEXT)
.text("https://opt4.net/forum/index.php?thread/219-wie-bekomme-ich-die-n%C3%B6tigen-mods-zum-mitspielen").build(),
EventFieldDefaultDto.builder().title("Respawn").type(BOOLEAN).text("true").build()
);
}

0 comments on commit cdbb8b6

Please sign in to comment.