Skip to content

Commit

Permalink
Release 1.1.0 (#23)
Browse files Browse the repository at this point in the history
* Fix logging
* Change loot statistics to include reward chest loot
  • Loading branch information
benjaminkomen authored Feb 2, 2020
1 parent 94f237a commit efa57e2
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ target
!maven-wrapper.properties
!maven-wrapper.jar
!system.properties
!src/main/resources/log.properties
!src/main/resources/log4j.properties
src/main/resources/temp
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.wikia.tibia</groupId>
<artifactId>TibiaWikiBot</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>
<name>TibiaWikiBot</name>
<url>https://github.com/benjaminkomen/TibiaWikiBot</url>
Expand Down
76 changes: 67 additions & 9 deletions src/main/java/com/wikia/tibia/objects/LootStatisticsItem.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package com.wikia.tibia.objects;

import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;

@Slf4j
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
public class LootStatisticsItem {

private final String NUMBER_REGEX = "(\\d+)";
private final String NUMBER_RANGE_REGEX = "(\\d+)-(\\d+)";
private static final String NUMBER_REGEX = "^(\\d+)$";
private static final Pattern NUMBER_PATTERN = Pattern.compile(NUMBER_REGEX);
private static final String NUMBER_RANGE_REGEX = "^(\\d+)-(\\d+)$";
private static final Pattern NUMBER_RANGE_PATTERN = Pattern.compile(NUMBER_RANGE_REGEX);
private String itemName;
private String times;
private String amount;
Expand Down Expand Up @@ -45,16 +49,32 @@ public LootStatisticsItem add(LootStatisticsItem other) {
}

private String sumStringsAsInt(String left, String right) {
if (left == null && right == null) {
return "";
} else if (left == null) {
return right;
} else if (right == null) {
return left;
}
return String.valueOf(Integer.parseInt(left) + Integer.parseInt(right));
}

private String sumAmounts(String left, String right) {

if (left == null && right == null) {
return "";
} else if (left == null) {
return right;
} else if (right == null) {
return left;
}

// if both are a single number, take the maximum
if (left.matches(NUMBER_REGEX) && right.matches(NUMBER_REGEX)) {
String[] leftMatches = getMatches(left);
String[] leftMatches = getMatches(NUMBER_PATTERN, left);
var leftUpper = leftMatches[0];

String[] rightMatches = getMatches(right);
String[] rightMatches = getMatches(NUMBER_PATTERN, right);
var rightUpper = rightMatches[0];

var newUpper = Math.max(Integer.parseInt(leftUpper), Integer.parseInt(rightUpper));
Expand All @@ -64,10 +84,12 @@ private String sumAmounts(String left, String right) {

// if both are a range, take the lowest to the highest as new range
if (left.matches(NUMBER_RANGE_REGEX) && right.matches(NUMBER_RANGE_REGEX)) {
String[] leftMatches = getMatches(left);
String[] leftMatches = getMatches(NUMBER_RANGE_PATTERN, left);
leftMatches = leftMatches[0].split("-");
var leftLower = leftMatches[0];
var leftUpper = leftMatches[1];
String[] rightMatches = getMatches(right);
String[] rightMatches = getMatches(NUMBER_RANGE_PATTERN, right);
rightMatches = rightMatches[0].split("-");
var rightLower = rightMatches[0];
var rightUpper = rightMatches[1];

Expand All @@ -77,11 +99,47 @@ private String sumAmounts(String left, String right) {
return String.format("%s-%s", newLower, newUpper);
}

return left; // TODO this is not the best default
// if one is a single number and the other a range, compute the range taking the single number into account
if ((left.matches(NUMBER_REGEX) && right.matches(NUMBER_RANGE_REGEX)) ||
(left.matches(NUMBER_RANGE_REGEX) && right.matches(NUMBER_REGEX))) {

if (left.matches(NUMBER_REGEX)) {
String[] leftMatches = getMatches(NUMBER_PATTERN, left);
var leftNumber = leftMatches[0];

String[] rightMatches = getMatches(NUMBER_RANGE_PATTERN, right);
rightMatches = rightMatches[0].split("-");
var rightLower = rightMatches[0];
var rightUpper = rightMatches[1];

var newLower = Math.min(Integer.parseInt(leftNumber), Integer.parseInt(rightLower));
var newUpper = Math.max(Integer.parseInt(leftNumber), Integer.parseInt(rightUpper));

return String.format("%s-%s", newLower, newUpper);
}

if (right.matches(NUMBER_REGEX)) {
String[] rightMatches = getMatches(NUMBER_PATTERN, right);
var rightNumber = rightMatches[0];

String[] leftMatches = getMatches(NUMBER_RANGE_PATTERN, left);
leftMatches = leftMatches[0].split("-");
var leftLower = leftMatches[0];
var leftUpper = leftMatches[1];

var newLower = Math.min(Integer.parseInt(rightNumber), Integer.parseInt(leftLower));
var newUpper = Math.max(Integer.parseInt(rightNumber), Integer.parseInt(leftUpper));

return String.format("%s-%s", newLower, newUpper);
}
}

log.warn("Could not correctly sum amounts, neither left ({}) nor right ({}) is a valid number.", left, right);
return "";
}

private String[] getMatches(String left) {
return Pattern.compile(NUMBER_RANGE_REGEX)
private String[] getMatches(Pattern pattern, String left) {
return pattern
.matcher(left)
.results()
.map(MatchResult::group)
Expand Down
File renamed without changes.
95 changes: 50 additions & 45 deletions src/test/java/com/wikia/tibia/usecases/FixLootStatisticsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ public class FixLootStatisticsTest {
private FixLootStatistics target;
private CreatureRepository mockCreatureRepository;
private LootRepository mockLootRepository;
private Creature CreatureRat = makeCreatureRat();
private Creature CreatureAmazon = makeCreatureAmazon();
private Loot LootRat = makeLootRat();
private Loot LootAmazon = makeLootAmazon();

private Loot LootRatWithSword = makeLootRatWithSword();
private final Creature CreatureRat = makeCreatureRat();
private final Creature CreatureAmazon = makeCreatureAmazon();
private final LootWrapper LootRat = makeLootRat();
private final LootWrapper LootAmazon = makeLootAmazon();
private final LootWrapper LootRatWithSword = makeLootRatWithSword();

@Before
public void setup() {
Expand Down Expand Up @@ -122,51 +121,57 @@ private Creature makeCreatureAmazon() {
.build();
}

private static Loot makeLootRat() {
return Loot.builder()
.kills("14605")
.name("Rat")
.loot(new ArrayList<>(Arrays.asList(
LootStatisticsItem.builder().itemName("Empty").times("108").build(),
LootStatisticsItem.builder().itemName("Cheese").times("5741").build(),
LootStatisticsItem.builder().itemName("Gold Coin").times("14477").amount("1").total("20591").build()
)))
.version("9.63")
private static LootWrapper makeLootRat() {
return LootWrapper.builder()
.loot2(Loot.builder()
.kills("14605")
.name("Rat")
.loot(new ArrayList<>(Arrays.asList(
LootStatisticsItem.builder().itemName("Empty").times("108").build(),
LootStatisticsItem.builder().itemName("Cheese").times("5741").build(),
LootStatisticsItem.builder().itemName("Gold Coin").times("14477").amount("1").total("20591").build()
)))
.version("9.63")
.build())
.build();
}

private static Loot makeLootRatWithSword() {
return Loot.builder()
.kills("14605")
.name("Rat")
.loot(new ArrayList<>(Arrays.asList(
LootStatisticsItem.builder().itemName("Empty").times("108").build(),
LootStatisticsItem.builder().itemName("Cheese").times("5741").build(),
LootStatisticsItem.builder().itemName("Gold Coin").times("14477").amount("1").total("20591").build(),
LootStatisticsItem.builder().itemName("Sword").times("1").build()
)))
.version("9.63")
private static LootWrapper makeLootRatWithSword() {
return LootWrapper.builder()
.loot2(Loot.builder()
.kills("14605")
.name("Rat")
.loot(new ArrayList<>(Arrays.asList(
LootStatisticsItem.builder().itemName("Empty").times("108").build(),
LootStatisticsItem.builder().itemName("Cheese").times("5741").build(),
LootStatisticsItem.builder().itemName("Gold Coin").times("14477").amount("1").total("20591").build(),
LootStatisticsItem.builder().itemName("Sword").times("1").build()
)))
.version("9.63")
.build())
.build();
}

private Loot makeLootAmazon() {
return Loot.builder()
.kills("21983")
.name("Amazon")
.loot(new ArrayList<>(Arrays.asList(
LootStatisticsItem.builder().itemName("Empty").times("253").build(),
LootStatisticsItem.builder().itemName("Dagger").times("17606").amount("1").total("17606").build(),
LootStatisticsItem.builder().itemName("Skull").times("17581").amount("1-2").total("26316").build(),
LootStatisticsItem.builder().itemName("Gold Coin").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Brown Bread").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Sabre").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Girlish Hair Decoration").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Protective Charm").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Torch").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Crystal Necklace").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Small Ruby").times("1").amount("1").total("1").build()
)))
.version("8.6")
private LootWrapper makeLootAmazon() {
return LootWrapper.builder()
.loot2(Loot.builder()
.kills("21983")
.name("Amazon")
.loot(new ArrayList<>(Arrays.asList(
LootStatisticsItem.builder().itemName("Empty").times("253").build(),
LootStatisticsItem.builder().itemName("Dagger").times("17606").amount("1").total("17606").build(),
LootStatisticsItem.builder().itemName("Skull").times("17581").amount("1-2").total("26316").build(),
LootStatisticsItem.builder().itemName("Gold Coin").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Brown Bread").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Sabre").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Girlish Hair Decoration").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Protective Charm").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Torch").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Crystal Necklace").times("1").amount("1").total("2").build(),
LootStatisticsItem.builder().itemName("Small Ruby").times("1").amount("1").total("1").build()
)))
.version("8.6")
.build())
.build();
}
}

0 comments on commit efa57e2

Please sign in to comment.