Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added the option to use all the stats in the conditions #357

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
import com.github.manolo8.darkbot.config.actions.Value;
import com.github.manolo8.darkbot.config.actions.ValueData;
import com.github.manolo8.darkbot.config.actions.parser.ParseUtil;
import eu.darkbot.api.game.stats.Stats;
import com.github.manolo8.darkbot.config.actions.parser.Values;
import com.github.manolo8.darkbot.core.manager.StatsManager;

import eu.darkbot.api.managers.StatsAPI;
import eu.darkbot.api.managers.StatsAPI.Key;

import org.jetbrains.annotations.Nullable;

import java.util.Locale;

@ValueData(name = "stat-type", description = "Gets a certain Stat type from a bot", example = "stat-type(experience, earned)")
public class StatTypeValue implements Value<Number>, Parser {
private Stats.General key;
private StatsAPI.Key key;
private StatsAPI.Stat stat;
private StatData dataType;

Expand All @@ -35,11 +39,18 @@ public class StatTypeValue implements Value<Number>, Parser {
}
}

private Stats.General getKeyFromString(String key) {
for (Stats.General stat : Stats.General.values()) {
if (stat.name().equalsIgnoreCase(key)) return stat;
}
return null;
private StatsAPI.Key getKeyFromString(String token) {
String[] tokenParts = token.split(":");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String[] tokenParts = token.split(":");
String[] tokenParts = token.split(":", 3);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't cap it at 3, you may have a string like:
plugin:something:general:uridium
which would be parsed as
namespace=plugin, category=general, name=uridium, leaving the "something" being completely ignored. This is faulty behavior, so you either limit it to 3 splits or you throw an error if there's more than 3, but it needs to be handled


String statNamespace = tokenParts.length == 3 ? tokenParts[0] : null;
dm94 marked this conversation as resolved.
Show resolved Hide resolved
String statCategory = tokenParts.length >= 2 ? tokenParts[tokenParts.length - 2] : null;
String statKey = tokenParts[tokenParts.length - 1];
dm94 marked this conversation as resolved.
Show resolved Hide resolved

return StatsManager.getStatKeys().stream()
.filter(s -> statNamespace == null || s.namespace().equalsIgnoreCase(statNamespace))
.filter(s -> statCategory == null || s.category().equalsIgnoreCase(statCategory))
.filter(s -> s.name().equalsIgnoreCase(statKey))
.findFirst().orElse(null);
}

public enum StatData {
Expand All @@ -56,15 +67,22 @@ public String toString() {

public static StatData of(String sd) {
for (StatData statData : StatData.values()) {
if (statData.toString().equalsIgnoreCase(sd)) return statData;
if (statData.toString().equalsIgnoreCase(sd)) {
return statData;
}
}
return null;
}
}

@Override
public String toString() {
return "stat-type(" + key.name().toLowerCase(Locale.ROOT) + "," + dataType + ")";
return "stat-type(" + getKeyFormatted(key) + "," + dataType + ")";
}

private String getKeyFormatted(Key keyToFormat) {
return ((keyToFormat.namespace() != null ? keyToFormat.namespace() + ":" : "") + keyToFormat.category() + ":"
+ keyToFormat.name()).toLowerCase(Locale.ROOT);
}

@Override
Expand All @@ -75,7 +93,10 @@ public String parse(String str) throws SyntaxException {
stat = null;

if (key == null) {
throw new SyntaxException("Unknown stat-type: '" + params[0] + "'", str, Stats.General.class);
throw new SyntaxException("Unknown stat-type: '" + params[0] + "'", str, Values.getMeta(getClass()),
StatsManager.getStatKeys().stream()
.map(this::getKeyFormatted)
.toArray(String[]::new));
}

params = ParseUtil.separate(params, getClass(), ",").split("\\)", 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.jetbrains.annotations.Nullable;

import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -123,6 +125,10 @@ private void registerImpl(Key key, StatImpl stat) {
statistics.put(StatKey.of(key), stat);
}

public static Collection<? extends StatsAPI.Key> getStatKeys() {
return Collections.unmodifiableSet(Main.INSTANCE.statsManager.statistics.keySet());
}

@Override
public void setStatValue(Key key, double v) {
if (key.namespace() == null) throw new UnsupportedOperationException();
Expand Down
Loading