Skip to content

Commit

Permalink
Improved logging of broken scores
Browse files Browse the repository at this point in the history
  • Loading branch information
syd711 committed Dec 30, 2024
1 parent d1c10db commit f71f6e4
Showing 1 changed file with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import de.mephisto.vpin.server.games.Game;
import de.mephisto.vpin.server.highscores.Score;
import de.mephisto.vpin.server.highscores.parsing.listadapters.SortedScoreAdapter;
import de.mephisto.vpin.server.highscores.parsing.nvram.adapters.*;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -26,8 +25,10 @@ public class ScoreListFactory {
public static List<Score> create(@NonNull String raw, @NonNull Date createdAt, @Nullable Game game, List<String> titles) {
List<Score> scores = new ArrayList<>();
int gameId = -1;
String source = null;
if (game != null) {
gameId = game.getId();
source = game.getGameDisplayName() + "/" + game.getRom() + "/" + game.getHsFileName();
}

try {
Expand All @@ -51,7 +52,7 @@ public static List<Score> create(@NonNull String raw, @NonNull Date createdAt, @
String line = lines.get(i);
if (titles.contains(line.trim())) {
String scoreLine = lines.get(i + 1);
Score score = createTitledScore(createdAt, scoreLine, gameId);
Score score = createTitledScore(createdAt, scoreLine, source, gameId);
if (score != null) {
scores.add(score);
}
Expand All @@ -60,7 +61,7 @@ public static List<Score> create(@NonNull String raw, @NonNull Date createdAt, @
}

if (line.startsWith(index + ")") || line.startsWith("#" + index) || line.startsWith(index + "#") || line.indexOf(".:") == 1) {
Score score = createScore(createdAt, line, gameId);
Score score = createScore(createdAt, line, source, gameId);
if (score != null) {
score.setPosition(scores.size() + 1);
scores.add(score);
Expand Down Expand Up @@ -96,21 +97,21 @@ private static List<Score> filterDuplicates(List<Score> scores) {


@Nullable
private static Score createTitledScore(@NonNull Date createdAt, @NonNull String line, int gameId) {
private static Score createTitledScore(@NonNull Date createdAt, @NonNull String line, @Nullable String source, int gameId) {
String initials = "???";
if (line.trim().length() >= 3) {
initials = line.trim().substring(0, 3);

String scoreString = line.substring(4).trim();
double scoreValue = toNumericScore(scoreString);
double scoreValue = toNumericScore(scoreString, source);
if (scoreValue == -1) {
return null;
}

return new Score(createdAt, gameId, initials, null, scoreString, scoreValue, 1);
}

double scoreValue = toNumericScore(line.trim());
double scoreValue = toNumericScore(line.trim(), source);
if (scoreValue == -1) {
return null;
}
Expand All @@ -119,11 +120,11 @@ private static Score createTitledScore(@NonNull Date createdAt, @NonNull String
}

@Nullable
private static Score createScore(@NonNull Date createdAt, @NonNull String line, int gameId) {
private static Score createScore(@NonNull Date createdAt, @NonNull String line, @Nullable String source, int gameId) {
List<String> scoreLineSegments = Arrays.stream(line.trim().split(" ")).filter(s -> s.trim().length() > 0).collect(Collectors.toList());
if (scoreLineSegments.size() == 2) {
String score = scoreLineSegments.get(1);
double v = toNumericScore(score);
double v = toNumericScore(score, source);
if (v == -1) {
return null;
}
Expand All @@ -133,7 +134,7 @@ private static Score createScore(@NonNull Date createdAt, @NonNull String line,
if (scoreLineSegments.size() == 3) {
String score = scoreLineSegments.get(2);
String initials = scoreLineSegments.get(1);
double v = toNumericScore(score);
double v = toNumericScore(score, source);
if (v == -1) {
return null;
}
Expand All @@ -148,7 +149,7 @@ private static Score createScore(@NonNull Date createdAt, @NonNull String line,
}
String score = scoreLineSegments.get(scoreLineSegments.size() - 1);
String playerInitials = initials.toString().trim();
double v = toNumericScore(score);
double v = toNumericScore(score, source);
if (v == -1) {
return null;
}
Expand All @@ -158,13 +159,13 @@ private static Score createScore(@NonNull Date createdAt, @NonNull String line,
throw new UnsupportedOperationException("Could parse score line for game " + gameId + " '" + line + "'");
}

private static double toNumericScore(String score) {
private static double toNumericScore(@NonNull String score, @Nullable String source) {
try {
String cleanScore = ScoreFormatUtil.cleanScore(score);
return Double.parseDouble(cleanScore);
}
catch (NumberFormatException e) {
LOG.info("Failed to parse highscore string '" + score + "', ignoring segment '" + score + "'");
LOG.warn("Failed to parse numeric highscore string '{}', ignoring segment '{}', source: {}", score, score, source);
return -1;
}
}
Expand Down

0 comments on commit f71f6e4

Please sign in to comment.