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

fix images and concat #356

Merged
merged 1 commit into from
Nov 1, 2023
Merged
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
1 change: 0 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,6 @@ All functions are case insensitiv.
|max |`max(<expression1>, <expression2> ...)` |`max(4d6)` |returns the smallest elements (multiple if the smallest is not unique) of one or more inner expressions. Text is compared alphabetically
|sort asc |`asc(<expression1>, <expression2> ...)` |`asc(4d6)` |sorts all elements ascending of one or more inner expressions. Text is compared alphabetically
|sort desc |`desc(<expression1>, <expression2> ...)` |`desc(4d6)` |sorts all elements descending of one or more inner expressions. Text is compared alphabetically
|sort desc |`desc(<expression1>, <expression2> ...)` |`desc(4d6)` |sorts all elements descending of one or more inner expressions. Text is compared alphabetically
|chancel |`chancel(<expression>, <listA>, <listB>)` |`chancel(8d10, 10, 1)` |the elements of listA and listB (can also be single elements) chancel each other and remove each other from the result.
|replace |`replace(<expression>, <find>, <replace> ...)` |`replace(8d10, [9/10], 'bonus')` | each element in `<expression>` that matches on of the elements in `<find>` will be replaced with the elements in `<replace>`. `<replace>` can be an empty list `[]` or literal `''` and thereby removing the found elements. It is possible to add multiple <find>/<replace> pairs to replace different elements in one replace.
|if |`if(<boolean>,<true>,<false>)` |`if(1d6=?6,'six','not six')` or `if(1d6=?6,'six')` or `val('$r',1d6), if('$r'=?1,'one','$r'=?2,'two','else') |if `<boolean>` equal true then return the `<true>` expression or else the `<false>` expression. The `<false>` expression is optional, if it is missing and `<boolean>` is `false` then the result empty. It is possible to add more than `<boolean>,<true>` pair in the function, the result will be the `<true>` of the first true `<boolean>`, coming from left. All <boolean> must be non-empty and contain only on element (therefor can't contain only `val`). `val` are will only set in the first <true>. Use the following structure to use `if` to set different value in a `val`: `if(1d6>?4, val('$a',10), val('$a',-10))`, this will set '$a' to 10 if the 1d6 roll is bigger than 4 and to -10 otherwise.
Expand Down
2 changes: 1 addition & 1 deletion bot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
dependencies {
implementation(project(":discord-connector"))
implementation("com.github.twonirwana:dice-parser:0.7.1")
implementation("io.github.twonirwana:dice-evaluator:v0.5.1")
implementation("io.github.twonirwana:dice-evaluator:v0.5.2")

implementation(libs.log4j.to.slf4j)
implementation(libs.reactor.core)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.janno.discord.bot.dice.image;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
import lombok.NonNull;
Expand Down Expand Up @@ -41,11 +40,6 @@ public DiceStyleAndColor(@NonNull @JsonProperty("diceImageStyle") DiceImageStyle
.orElse(configuredDefaultColor));
}

@JsonIgnore
public int getDieHighAndWith() {
return diceImageStyle.getImageProvider().getDieHighAndWide();
}

@Override
public String toString() {
return DiceImageStyle.combineStyleAndColorName(diceImageStyle, configuredDefaultColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
Expand Down Expand Up @@ -161,9 +162,10 @@ private Supplier<? extends InputStream> createNewFileForRoll(Roll roll, File fil
)
.filter(l -> !l.isEmpty())
.toList();
final int singleDiceSize = diceStyleAndColor.getDieHighAndWith();
final int maxImageHight = images.stream().flatMap(Collection::stream)
.mapToInt(BufferedImage::getHeight).max().orElse(0);

BufferedImage separator = separatorImage.getUnchecked(singleDiceSize);
BufferedImage separator = separatorImage.getUnchecked(maxImageHight);
ImmutableList.Builder<BufferedImage> builder = ImmutableList.builder();
for (int i = 0; i < images.size() - 1; i++) {
builder.addAll(images.get(i));
Expand All @@ -179,7 +181,7 @@ private Supplier<? extends InputStream> createNewFileForRoll(Roll roll, File fil
final int numberOfImageLines = (int) Math.ceil(((double) allInOneLineWidth) / ((double) maxLineWidth));

final int w = Math.min(allInOneLineWidth, maxLineWidth);
final int h = singleDiceSize * numberOfImageLines;
final int h = maxImageHight * numberOfImageLines;
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);

Graphics g = combined.getGraphics();
Expand All @@ -193,7 +195,7 @@ private Supplier<? extends InputStream> createNewFileForRoll(Roll roll, File fil
} else {
currentLineWidth += image.getWidth();
}
g.drawImage(image, currentLineWidth - image.getWidth(), singleDiceSize * currentLine, image.getWidth(), singleDiceSize, null);
g.drawImage(image, currentLineWidth - image.getWidth(), maxImageHight * currentLine, image.getWidth(), image.getHeight(), null);
}

g.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public D6Dotted() {
.collect(ImmutableMap.toImmutableMap(Function.identity(), c -> new FileSidesDiceImageMap("d6_" + c, List.of(6))));
}

@Override
public int getDieHighAndWide() {
return 50;
}

@Override
public @NonNull String getDefaultColor() {
return WHITE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ public FateBasic() {
}
}

@Override
public int getDieHighAndWide() {
return 50;
}

@Override
public @NonNull String getDefaultColor() {
return BLACK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public interface ImageProvider {

@NonNull List<BufferedImage> getImageFor(Integer totalDieSides, Integer shownDieSide, @Nullable String color);

int getDieHighAndWide();

@NonNull String getDefaultColor();

@NonNull List<String> getSupportedColors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ public final class NoneProvider implements ImageProvider {
return List.of();
}

@Override
public int getDieHighAndWide() {
return 0;
}

@Override
public @NonNull String getDefaultColor() {
return "none";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public PolyhedralFileImageProvider(String styleFolder, List<String> supportedCol
this.supportedColors = supportedColors;
}

@Override
public int getDieHighAndWide() {
return 100;
}

@Override
public @NonNull String getDefaultColor() {
return defaultColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public class PolyhedralSvgWithColor implements ImageProvider {
.put("magenta", Color.magenta)
.put("cyan", Color.cyan)
.put("blue", Color.blue)
.put("indigo", new Color( 75, 0, 130))
.put("indigo", new Color(75, 0, 130))
.build();

private static final float IMAGE_SIZE = 100;

@Override
public @NonNull List<BufferedImage> getImageFor(Integer totalDieSides, Integer shownDieSide, String colorString) {
Expand Down Expand Up @@ -103,8 +103,8 @@ private BufferedImage getImageAndSetNumberAndColor(Integer totalDieSides, String
TranscoderInput transcoderInput = new TranscoderInput(IOUtils.toInputStream(svgString, StandardCharsets.UTF_8));

PNGTranscoder pngTranscoder = new PNGTranscoder();
pngTranscoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) getDieHighAndWide());
pngTranscoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float) getDieHighAndWide());
pngTranscoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, IMAGE_SIZE);
pngTranscoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, IMAGE_SIZE);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outputStream);

Expand All @@ -121,11 +121,6 @@ private BufferedImage getImageAndSetNumberAndColor(Integer totalDieSides, String
}
}

@Override
public int getDieHighAndWide() {
return 100;
}

@Override
public @NonNull String getDefaultColor() {
return RED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ void getImageForRoll_d6DotsBlackAndGold() throws ExpressionException, IOExceptio
Supplier<? extends InputStream> res = underTest.getImageForRoll(rolls, new DiceStyleAndColor(DiceImageStyle.d6_dots, "black_and_gold"));

assertThat(res).isNotNull();
assertThat(getDataHash(res)).isEqualTo("d2cbedfb456d593a51fd5339c95b802ef1dc6157279083e4f9e947b86744226f");
assertThat(getDataHash(res)).isEqualTo("acd0714a5dee4f9c3948933b194aa79786b5e4d46fd7e2d2c4386e7f90238527");
}


Expand Down