Skip to content

Commit

Permalink
dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Joker9944 committed May 25, 2020
1 parent 4ef517a commit cdf1894
Show file tree
Hide file tree
Showing 23 changed files with 340 additions and 238 deletions.
10 changes: 8 additions & 2 deletions src/main/java/online/vonarx/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package online.vonarx;

import com.beust.jcommander.JCommander;
import online.vonarx.di.app.DaggerAppComponent;
import com.beust.jcommander.ParameterException;
import online.vonarx.di.DaggerAppComponent;
import online.vonarx.models.AppParameters;

public class Main {
Expand All @@ -14,7 +15,12 @@ public class Main {
.build();

public static void main(final String[] args) {
jCommander.parse(args);
try {
jCommander.parse(args);
} catch (final ParameterException e) {
jCommander.usage();
return;
}
if (appParameters.printHelp()) {
jCommander.usage();
return;
Expand Down
38 changes: 23 additions & 15 deletions src/main/java/online/vonarx/components/App.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
package online.vonarx.components;

import com.google.common.collect.ImmutableMap;
import online.vonarx.constants.Mode;
import online.vonarx.formatter.Formatter;
import online.vonarx.models.AppParameters;
import online.vonarx.models.Actor;
import online.vonarx.save.SaveFactory;

import javax.inject.Inject;
import java.io.PrintStream;
import java.util.List;
import java.util.Map;

public class App {

private final AppParameters appParameters;
private final BinaryLoader binaryLoader;
private final SaveFactory saveFactory;
private final Formatter formatter;
private final BinaryProvider binaryProvider;
private final PrintStream out;
private final Map<Mode, SaveFactory> saveFactoryMap;
private final Map<Mode, Formatter> formatterMap;

@Inject
public App(final AppParameters appParameters,
final BinaryLoader binaryLoader, final SaveFactory saveFactory,
final Formatter formatter) {
this.appParameters = appParameters;
this.binaryLoader = binaryLoader;
this.saveFactory = saveFactory;
this.formatter = formatter;
public App(final BinaryProvider binaryProvider, final PrintStream out,
final Map<Mode, SaveFactory> saveFactoryMap,
final Map<Mode, Formatter> formatterMap) {
this.binaryProvider = binaryProvider;
this.out = out;
this.saveFactoryMap = saveFactoryMap;
this.formatterMap = formatterMap;
}

public void run() {
final var saveBinary = binaryLoader.readBinary(appParameters.saveFilepath());
final var save = saveFactory.create(saveBinary);
final var formattedSave = formatter.format(save);
final var saveBinary = binaryProvider.provideBinary();
final var actorsByModeMapBuilder = ImmutableMap.<Mode, List<Actor>>builder();
saveFactoryMap.forEach((mode, saveFactory) -> actorsByModeMapBuilder.put(mode, saveFactory.create(saveBinary)));
final var actorsByMode = actorsByModeMapBuilder.build();
formatterMap.forEach((mode, formatter) -> out.println(formatter.format(actorsByMode.get(mode))));
}
}
18 changes: 0 additions & 18 deletions src/main/java/online/vonarx/components/BinaryLoader.java

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/java/online/vonarx/components/BinaryProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package online.vonarx.components;

import lombok.SneakyThrows;
import online.vonarx.models.AppParameters;
import org.apache.commons.io.IOUtils;

import javax.inject.Inject;

public class BinaryProvider {

private final AppParameters parameters;

@Inject
public BinaryProvider(final AppParameters parameters) {this.parameters = parameters;}

@SneakyThrows
public byte[] provideBinary() {
return IOUtils.toByteArray(parameters.saveFilepath());
}
}
90 changes: 0 additions & 90 deletions src/main/java/online/vonarx/components/SaveFactory.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import online.vonarx.constants.Zone;
import online.vonarx.formatter.ListFormatter;
import online.vonarx.models.Actor;
import online.vonarx.models.AppParameters;

import javax.inject.Inject;
import java.util.List;
Expand All @@ -14,8 +15,8 @@
public class ListAdventureFormatter extends ListFormatter {

@Inject
public ListAdventureFormatter(final boolean showIdentifiers, final boolean showEngineActors) {
super(showIdentifiers, showEngineActors);
public ListAdventureFormatter(final AppParameters parameters) {
super(parameters);
}

@Override
Expand All @@ -25,7 +26,7 @@ protected String formatList(final Map<Biome, Map<Zone, Map<Type, List<Actor>>>>
sb.append(Mode.ADVENTURE.displayName()).append(" (").append(biome.displayName()).append(")").append("\n");
zones.values().forEach(types -> types.forEach((type, actors) -> {
sb.append("\t").append(type.displayName()).append("\n");
actors.forEach((actor) -> sb.append("\t\t").append(" - ").append(this.printActor(actor)).append("\n"));
actors.forEach((actor) -> sb.append("\t").append(" - ").append(this.printActor(actor)).append("\n"));
}));
});
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package online.vonarx.components.formatters;

import online.vonarx.constants.Biome;
import online.vonarx.constants.Mode;
import online.vonarx.constants.Type;
import online.vonarx.constants.Zone;
import online.vonarx.formatter.ListFormatter;
import online.vonarx.models.Actor;
import online.vonarx.models.AppParameters;

import javax.inject.Inject;
import java.util.List;
Expand All @@ -14,20 +14,21 @@
public class ListStoryFormatter extends ListFormatter {

@Inject
public ListStoryFormatter(final boolean showIdentifiers, final boolean showEngineActors) {
super(showIdentifiers, showEngineActors);
public ListStoryFormatter(final AppParameters parameters) {
super(parameters);
}

@Override
protected String formatList(final Map<Biome, Map<Zone, Map<Type, List<Actor>>>> groupedActors) {
final var sb = new StringBuilder();
final var sb = new StringBuilder()
.append("Story").append("\n");
groupedActors.forEach((biome, zones) -> {
sb.append(biome.displayName()).append("\n");
sb.append("\t").append(biome.displayName()).append("\n");
zones.forEach((zone, types) -> {
sb.append("\t").append(zone.displayName()).append("\n");
sb.append("\t\t").append(zone.displayName()).append("\n");
types.forEach((type, actors) -> {
sb.append("\t\t").append(type.displayName()).append("\n");
actors.forEach((actor) -> sb.append("\t\t - ").append(this.printActor(actor)).append("\n"));
sb.append("\t\t\t").append(type.displayName()).append("\n");
actors.forEach((actor) -> sb.append("\t\t\t").append(" - ").append(this.printActor(actor)).append("\n"));
});
sb.append("\n");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mitchtalmadge.asciidata.table.formats.TableFormatAbstract;
import online.vonarx.formatter.TableFormatter;
import online.vonarx.models.Actor;
import online.vonarx.models.AppParameters;

import javax.inject.Inject;
import java.util.List;
Expand All @@ -22,10 +23,17 @@ public class TableAdventureFormatter extends TableFormatter {
private final boolean showIdentifiers;

@Inject
public TableAdventureFormatter(final boolean showIdentifiers, final boolean hideRedundantActors,
final TableFormatAbstract tableFormat) {
super(hideRedundantActors, tableFormat);
this.showIdentifiers = showIdentifiers;
public TableAdventureFormatter(final AppParameters parameters, final TableFormatAbstract tableFormat) {
super(parameters, tableFormat);
this.showIdentifiers = parameters.showIdentifiers();
}

@Override
protected String tableTitle(final List<Actor> actors) {
if (actors.size() == 0)
return "Adventure";
else
return "Adventure (" + actors.get(0).biome().displayName() + ")";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mitchtalmadge.asciidata.table.formats.TableFormatAbstract;
import online.vonarx.formatter.TableFormatter;
import online.vonarx.models.Actor;
import online.vonarx.models.AppParameters;

import javax.inject.Inject;
import java.util.List;
Expand All @@ -24,10 +25,14 @@ public class TableStoryFormatter extends TableFormatter {
private final boolean showIdentifiers;

@Inject
public TableStoryFormatter(final boolean showIdentifiers, final boolean hideRedundantActors,
final TableFormatAbstract tableFormat) {
super(hideRedundantActors, tableFormat);
this.showIdentifiers = showIdentifiers;
public TableStoryFormatter(final AppParameters parameters, final TableFormatAbstract tableFormat) {
super(parameters, tableFormat);
this.showIdentifiers = parameters.showIdentifiers();
}

@Override
protected String tableTitle(final List<Actor> actors) {
return "Story";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package online.vonarx.components.save;

import online.vonarx.components.ActorFactory;
import online.vonarx.components.dictionaries.ZoneDictionary;
import online.vonarx.save.SaveFactory;

import javax.inject.Inject;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

public class AdventureSaveFactory extends SaveFactory {

private static final Pattern START_ADVENTURE_MODE_PATTERN = Pattern.compile("^/Game/World_.+/Quests/Quest_AdventureMode/Quest_AdventureMode_[a-zA-Z]+_\\d{2}\\.Quest_AdventureMode_[a-zA-Z]+_\\d{2}$");
private static final Pattern END_ADVENTURE_MODE_PATTERN = Pattern.compile("^/Game/World_.+/Quests/Quest_AdventureMode/Quest_AdventureMode_.+\\.Quest_AdventureMode_.+_C$");

@Inject
public AdventureSaveFactory(final ActorFactory actorFactory, final ZoneDictionary zoneDictionary) {
super(actorFactory, zoneDictionary);
}

@Override
protected List<String> extractIdentifiers(final List<String> identifiers) {
final var startIndexName = identifiers.stream()
.filter(name -> START_ADVENTURE_MODE_PATTERN.matcher(name).matches())
.reduce((first, second) -> second);
final var endIndexName = identifiers.stream()
.filter(name -> END_ADVENTURE_MODE_PATTERN.matcher(name).matches())
.findFirst();
if (startIndexName.isEmpty() || endIndexName.isEmpty()) {
System.out.println("No adventure mode found.");
return Collections.emptyList();
}
final var startIndex = identifiers.indexOf(startIndexName.get());
final var endIndex = identifiers.indexOf(endIndexName.get());
return identifiers.subList(startIndex, endIndex);
}
}
Loading

0 comments on commit cdf1894

Please sign in to comment.