Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 147 additions & 29 deletions src/main/java/dev/shared/halizeur/log_overlay/LogOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import eu.darkbot.api.PluginAPI;
import eu.darkbot.api.config.ConfigSetting;
Expand All @@ -18,6 +20,8 @@
import eu.darkbot.api.extensions.MapGraphics;
import eu.darkbot.api.managers.EventBrokerAPI;
import eu.darkbot.api.managers.GameLogAPI;
import eu.darkbot.api.managers.GameResourcesAPI;
import eu.darkbot.api.managers.GameResourcesAPI.TranslationMatcher;

/**
* Renders the latest in-game DarkOrbit log messages as an overlay on the
Expand All @@ -26,6 +30,11 @@
* Source: {@link GameLogAPI.LogMessageEvent} emitted by DarkBot for each
* new system message. Each line disappears after DISPLAY_MS ms so the
* canvas does not get cluttered.
*
* Filter: per category, the overlay matches messages against translation
* patterns from {@link GameResourcesAPI} (the official Bigpoint flashres
* keys), so the filter works on every game locale without per-language
* keyword maintenance.
Comment on lines 26 to +37
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove comments like this

*/
@Feature(name = "Log Overlay",
description = "Shows the latest in-game log messages as an overlay on the canvas",
Expand All @@ -38,37 +47,117 @@
private static final int MAX_LINES = 5;
private static final long DISPLAY_MS = 5000L;

/**
* Case-insensitive keywords that mark a log message as "interesting":
* resource / experience / honor gains, or error messages. Anything
* else (combat, movement, generic info) is filtered out.
*/
private static final String[] WHITELIST = {
// Gains (FR)
"gagn", "obtenu", "récup", "recup", "récompense", "recompense",
"collect", "ramass",
// Gains (EN)
"gained", "received", "reward", "earned",
// Currencies / resources
"uridium", "credit", "crédit", "honor", "honneur",
"experience", "expérience", "xp ",
"prometium", "endurium", "terbium", "prometid", "duranium",
"promerium", "seprom", "xenomit", "palladium",
// Boosters / drops
"drop", "booster",
// Errors (FR)
"impossible", "erreur", "échec", "echec", "refusé", "refuse",
"plein", "indisponible", "non disponible", "interdit",
// Errors (EN)
"error", "failed", "refused", "denied", "unavailable", "full",
"cannot", "can't"
private enum Cat {
GAINS, BOOSTERS, ERRORS, COMBAT
}

/** flashres keys whose translation describes a generic loot / reward
* message — collection, ore refining, level up, quest reward, etc. */
private static final String[] KEYS_GAINS = {
"oksammel", // "%! collected"
"boxfarming", // "Salvaging %!"
"farmresult", // "You collected:"
"auto_ore_update",
"special_item_found",
"lvlup_msg",
"quest_finish_reward_s",
"quest_finish_reward_p"
};

/** flashres keys for booster activation / banking-multiplier drops. */
private static final String[] KEYS_BOOSTERS = {
"booster_found",
"banking_doubler",
"banking_tripler",
"banking_quadruplicator",
"banking_multiplier_active"
};

/** flashres keys for action refusals: cargo full, no ammo, ability
* on cooldown, ammo purchase failed, ship missing weapons, etc. */
private static final String[] KEYS_ERRORS = {
"boxtoobig",
"boxdisabled",
"resourcedisabled",
"outofammo",
"rohstoffailed",
"loadfull",
"emptybat",
"ammobuy_fail_uri",
"ammobuy_fail_cre",
"ammobuy_fail_space",
"ammobuy_fail_offer",
"smartbomb_failed_mines",
"smartbomb_failed_xenomit",
"instashield_failed_mines",
"instashield_failed_xenomit",
"no_lasers_on_board",
"msg_laser_not_equipped",
"msg_rocketlauncher_not_equipped",
"loot_theft"
};

/** flashres key for the "X destroyed" combat message. */
private static final String[] KEYS_COMBAT = {
"destroyed"
};

/** Currency names. DarkOrbit keeps these mostly untranslated across
* locales, so a static list works as a substring filter against any
* raw log message. Lower-cased at init time for case-insensitive
* comparison. */
Comment on lines +105 to +108
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove comments like this

private static final String[] CURRENCY_NAMES = {
"uridium", "uri",
"credit", "crédit",
"honor", "honour", "honneur",
"experience", "expérience", "exp", " xp"
};

/** flashres keys whose translation is the localized name of an ore
* / resource. Resolved at init via {@link GameResourcesAPI#findTranslation}. */
private static final String[] RESOURCE_KEYS = {
"ore_prometium", "ore_endurium", "ore_terbium",
"ore_prometid", "ore_duranium", "ore_promerium",
"ore_seprom", "ore_xenomit", "ore_palladium", "ore_osmium"
};

private final Deque<Entry> entries = new ArrayDeque<>();
private final Map<Cat, List<TranslationMatcher>> matchers = new EnumMap<>(Cat.class);
private final List<String> currencyNeedles = new ArrayList<>();
private final List<String> resourceNeedles = new ArrayList<>();
private LogOverlayConfig config;

public LogOverlay(PluginAPI api) {
api.requireAPI(EventBrokerAPI.class).registerListener(this);

GameResourcesAPI res = api.requireAPI(GameResourcesAPI.class);
buildMatchers(res, Cat.GAINS, KEYS_GAINS);
buildMatchers(res, Cat.BOOSTERS, KEYS_BOOSTERS);
buildMatchers(res, Cat.ERRORS, KEYS_ERRORS);
buildMatchers(res, Cat.COMBAT, KEYS_COMBAT);

for (String name : CURRENCY_NAMES) {
this.currencyNeedles.add(name);
}
for (String key : RESOURCE_KEYS) {
res.findTranslation(key).ifPresent(t -> {
String lower = t.toLowerCase();
if (!this.resourceNeedles.contains(lower)) {
this.resourceNeedles.add(lower);
}
});
}
}

/** Pre-builds {@link TranslationMatcher} instances for every key in a
* category. Keys whose translation is missing in the active locale
* return {@link java.util.Optional#empty()} and are skipped silently. */
private void buildMatchers(GameResourcesAPI res, Cat cat, String[] keys) {
List<TranslationMatcher> list = new ArrayList<>();
for (String key : keys) {
res.getTranslationMatcher(key).ifPresent(list::add);
}
this.matchers.put(cat, list);
}

@Override
Expand Down Expand Up @@ -100,13 +189,42 @@
}

/**
* Whitelist filter: only display the message if it matches a keyword
* from {@link #WHITELIST} (case-insensitive).
* Filter: a message is kept when at least one checked category
* matches. Most categories use TranslationMatcher (built from the
* official Bigpoint translations); Currencies and Resources fall
* back to substring matching against localized names because the
* actual values appear inside the {@code %!} placeholder of the
* generic gain templates and are easier to detect this way.
*/
private boolean isAllowed(String msg) {

Check failure on line 199 in src/main/java/dev/shared/halizeur/log_overlay/LogOverlay.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=Darkbot-Plugins_SharedPlugin&issues=AZ4SuMBOc3BCtiyuV5ew&open=AZ4SuMBOc3BCtiyuV5ew&pullRequest=161
String lower = msg.toLowerCase();
for (String kw : WHITELIST) {
if (lower.contains(kw)) return true;
LogOverlayConfig.Categories c = this.config.categories;
if (c == null) return false;

if (c.gains && anyMatcherFinds(Cat.GAINS, msg)) return true;
if (c.boosters && anyMatcherFinds(Cat.BOOSTERS, msg)) return true;
if (c.errors && anyMatcherFinds(Cat.ERRORS, msg)) return true;
if (c.combat && anyMatcherFinds(Cat.COMBAT, msg)) return true;

if (c.currencies || c.resources) {
String lower = msg.toLowerCase();
if (c.currencies && containsAny(lower, this.currencyNeedles)) return true;
if (c.resources && containsAny(lower, this.resourceNeedles)) return true;
}
return false;
}

private boolean anyMatcherFinds(Cat cat, String msg) {
List<TranslationMatcher> list = this.matchers.get(cat);
if (list == null) return false;
for (TranslationMatcher m : list) {
if (m.find(msg)) return true;
}
return false;
}

private static boolean containsAny(String haystack, List<String> needles) {
for (String n : needles) {
if (haystack.contains(n)) return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,33 @@ public class LogOverlayConfig {

@Option("halizeur.log_overlay.enabled")
public boolean enabled = false;

@Option("halizeur.log_overlay.categories")
public Categories categories = new Categories();

/**
* Whitelist categories. A log message is shown when at least one
* checked category matches. Predefined keyword lists live in
* {@link LogOverlay} and stay in sync across game locales (FR + EN
* substrings are bundled together).
*/
public static class Categories {
@Option("halizeur.log_overlay.cat.gains")
public boolean gains = true;

@Option("halizeur.log_overlay.cat.currencies")
public boolean currencies = true;

@Option("halizeur.log_overlay.cat.resources")
public boolean resources = true;

@Option("halizeur.log_overlay.cat.boosters")
public boolean boosters = true;

@Option("halizeur.log_overlay.cat.errors")
public boolean errors = true;

@Option("halizeur.log_overlay.cat.combat")
public boolean combat = false;
}
}
8 changes: 8 additions & 0 deletions src/main/resources/dev/shared/lang/strings_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ general.enabled=Enable
# ----- Halizeur : Log Overlay -----
halizeur.log_overlay.config=Log Overlay
halizeur.log_overlay.enabled=Enabled
halizeur.log_overlay.categories=Categories to display
halizeur.log_overlay.categories.desc=Show only log messages whose content matches at least one checked category.
halizeur.log_overlay.cat.gains=Gains (loot, rewards)
halizeur.log_overlay.cat.currencies=Currencies (uridium, credits, honor, XP)
halizeur.log_overlay.cat.resources=Resources (prometium, endurium, …)
halizeur.log_overlay.cat.boosters=Boosters / drops
halizeur.log_overlay.cat.errors=Errors / refusals
halizeur.log_overlay.cat.combat=Combat / kills

do_gamer.simple_healing.hp_repair=HP Repair (Solace, Orcus, Aegis, Hammerclaw)
do_gamer.simple_healing.shield_repair=Shield Repair (Aegis, Hammerclaw)
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/dev/shared/lang/strings_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ general.enabled=Activer
# ----- Halizeur : Log Overlay -----
halizeur.log_overlay.config=Log Overlay
halizeur.log_overlay.enabled=Activé
halizeur.log_overlay.categories=Catégories à afficher
halizeur.log_overlay.categories.desc=N'affiche que les messages dont le contenu correspond à au moins une catégorie cochée.
halizeur.log_overlay.cat.gains=Gains (loot, récompenses)
halizeur.log_overlay.cat.currencies=Devises (uridium, crédits, honneur, XP)
halizeur.log_overlay.cat.resources=Ressources (prometium, endurium, …)
halizeur.log_overlay.cat.boosters=Boosters / drops
halizeur.log_overlay.cat.errors=Erreurs / refus
halizeur.log_overlay.cat.combat=Combat / kills

do_gamer.simple_healing.hp_repair=Réparation des PV (Solace, Orcus, Aegis, Hammerclaw)
do_gamer.simple_healing.shield_repair=Réparation du bouclier (Aegis, Hammerclaw)
Expand Down
Loading