Skip to content

Commit

Permalink
Add CardExporter - creates string representation of cards for twitch,
Browse files Browse the repository at this point in the history
  • Loading branch information
avolny committed Apr 8, 2020
1 parent c0f02e2 commit f8e69d5
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 2 deletions.
210 changes: 210 additions & 0 deletions src/main/java/str_exporter/CardExporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package str_exporter;

import basemod.BaseMod;
import basemod.abstracts.CustomCard;
import basemod.abstracts.DynamicVariable;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.cards.DescriptionLine;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import org.apache.logging.log4j.Logger;

import java.util.HashSet;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CardExporter {

public static Logger logger = SlayTheRelicsExporter.logger;

public static final String[] TEXT = CardCrawlGame.languagePack.getUIString("TipHelper").TEXT;

public static String exportDeck() {

StringBuilder sb = new StringBuilder();

boolean nonempty = buildCards(sb);
// ;;; delimits cards from keywords
sb.append(nonempty ? ";": ";;;");
buildKeywords(sb);

return sb.toString();
}

private static void buildKeywords(StringBuilder sb) {
HashSet<String> keywords = new HashSet<>();

if (CardCrawlGame.isInARun()) {
CardGroup deck = CardCrawlGame.dungeon.player.masterDeck;

for (int i = 0; i < deck.group.size(); i++) {
for (String keyword : deck.group.get(i).keywords) {
keywords.add(keyword);
}
}
}

Iterator<String> iter = keywords.iterator();

while(iter.hasNext()) {
String word = iter.next();

if (word.equals("[R]") || word.equals("[G]") || word.equals("[B]") || word.equals("[W]") || word.equals("[E]")) {
sb.append(word);
sb.append(' ');
sb.append(TEXT[0]); // word Energy
} else {
sb.append(BaseMod.getKeywordTitle(word));
}

sb.append(";");
sb.append(BaseMod.getKeywordDescription(word));

if(iter.hasNext())
sb.append(";;");
}
}

private static boolean buildCards(StringBuilder sb) {
//returns true if deck has 1 or more cards

int size = 0;

if (CardCrawlGame.isInARun()) {
CardGroup deck = CardCrawlGame.dungeon.player.masterDeck;
for (int i = 0; i < deck.group.size(); i++) {
buildCard(sb, deck.group.get(i));
sb.append(";;");
}

size = deck.group.size();
}

return size > 0;
}

private static void buildCard(StringBuilder sb, AbstractCard card) {

String name = sanitize(card.name);
String desc = parseDescription(card);
int timesUpgraded = card.timesUpgraded;
int cost = card.cost;

if (timesUpgraded > 0) {
name = colorizeString(name, "#g");
}

sb.append(name);
sb.append(";");
sb.append(cost);
sb.append(';');
sb.append(desc);
sb.append(';');
sb.append(getImgPath(card));
}

private static String getImgPath(AbstractCard card) {
if (card instanceof CustomCard) {
String[] parts = ((CustomCard) card).textureImg.split("/");

return parts[0] + "/" + parts[parts.length - 1];
} else {
// could remove text
return card.assetUrl + ".png";
}
}

private static String sanitize(String s) {
return s.replaceAll(";", ":");
}

private static String colorizeString(String s, String colorPrefix) {
String[] parts = s.split(" ");
StringBuilder sb = new StringBuilder();

for (int i = 0; i < parts.length; i++) {
sb.append(colorPrefix);
sb.append(parts[i]);
if (i < parts.length - 1)
sb.append(' ');
}

return sb.toString();
}

private static String parseDescription(AbstractCard card) {

StringBuilder sb = new StringBuilder();

for (int i = 0; i < card.description.size(); i++) {
DescriptionLine line = card.description.get(i);

sb.append(line.text);

if(i < card.description.size() - 1)
sb.append(" ");
}

String[] parts = sb.toString().split(" ");
sb.setLength(0);

Pattern patternDynVar = Pattern.compile("!(.+)!(.*)");
Pattern patternKeyword = Pattern.compile("\\*(.+)");

for (int i = 0; i < parts.length; i++) {
String part = sanitize(parts[i]);

Matcher matcherDynVar = patternDynVar.matcher(part);
Matcher matcherKeyword = patternKeyword.matcher(part);

if (matcherDynVar.find()) { // !MYVAR!
part = matcherDynVar.group(1);
String end = matcherDynVar.group(2);

// Main body of method
// StringBuilder stringBuilder = new StringBuilder();
int num = 0;
DynamicVariable dv = BaseMod.cardDynamicVariableMap.get(part);

String color = "";
if (dv != null) {
if (dv.isModified(card)) {
num = dv.value(card);
if (num >= dv.baseValue(card)) {
color = "#g";
} else {
color = "#r";
}
} else {
num = dv.baseValue(card);
}

} else {
logger.error("No dynamic card variable found for key \"" + part + "\"!");
}

sb.append(color);
sb.append(num);
sb.append(end);

} else if (matcherKeyword.find()) { // *Word
sb.append("#y");
sb.append(matcherKeyword.group(1));
} else { // Replace color codes for now

part = part.replaceAll("\\[[a-zA-Z_]*\\]", "");
part = part.replaceAll("\\[#[A-Ea-e0-9]*\\]", "");
part = part.replaceAll("\\[\\]", "");
part = part.replaceAll("\\[\\[", "[");

sb.append(part);
}

if (i < parts.length - 1)
sb.append(" ");
}

return sb.toString();
}
}
28 changes: 27 additions & 1 deletion src/main/java/str_exporter/SlayTheRelicsExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
import com.evacipated.cardcrawl.modthespire.Patcher;
import com.evacipated.cardcrawl.modthespire.lib.SpireConfig;
import com.evacipated.cardcrawl.modthespire.lib.SpireInitializer;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.cards.DescriptionLine;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.potions.AbstractPotion;
Expand Down Expand Up @@ -58,7 +62,7 @@ public class SlayTheRelicsExporter implements
private JSONMessageBuilder json_builder;

// private static final long MAX_BROADCAST_PERIOD_MILLIS = 250;
private static final long MAX_CHECK_PERIOD_MILLIS = 100;
private static final long MAX_CHECK_PERIOD_MILLIS = 1000;
public static SlayTheRelicsExporter instance = null;

public static Properties strDefaultSettings = new Properties();
Expand Down Expand Up @@ -129,6 +133,23 @@ public static void initialize()
instance = new SlayTheRelicsExporter();
}

// private void printDeck() {
// StringBuilder sb = new StringBuilder();
//
// if (CardCrawlGame.isInARun()) {
// CardGroup deck = CardCrawlGame.dungeon.player.masterDeck;
// for (AbstractCard card : deck.group) {
//// sb.append(CardExporter.exportCard(card));
// sb.append(";;");
//// logger.info(CardExporter.exportCard(card));
// }
// }
//
// logger.info(sb.toString());
// logger.info("deck has {} characters", sb.toString().length());
// logger.info("compressed deck has {} characters", StringCompression.compress(sb.toString()).length());
// }

private void queue_check() {
checkNextUpdate = true;
}
Expand All @@ -147,6 +168,11 @@ private void broadcast() {
long end = System.nanoTime();

BackendBroadcaster.queueMessage(json);
//
String deck = CardExporter.exportDeck();
logger.info(deck);
String compdeck = StringCompression.compress(deck);
// logger.info(compdeck);
// logger.info("json builder took " + (end - start) / 1e6 + " milliseconds");
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/str_exporter/StringCompression.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static String compress(String s, String delims) {

long end = System.nanoTime();

// SlayTheRelicsExporter.logger.info(String.format("compression, original len: %s new len: %s ratio %.2f, duration %.2f ms", uncompressedLength, compressedLength, compressedLength * 1f / uncompressedLength, (end-start)/1e6));
SlayTheRelicsExporter.logger.info(String.format("compression, original len: %s new len: %s ratio %.2f, duration %.2f ms", uncompressedLength, compressedLength, compressedLength * 1f / uncompressedLength, (end-start)/1e6));
return s;
}

Expand Down

0 comments on commit f8e69d5

Please sign in to comment.