2013-04-12 19:32:25 -07:00
|
|
|
package magic.model;
|
|
|
|
|
2017-04-08 03:29:49 -07:00
|
|
|
import java.util.Arrays;
|
2013-04-12 19:32:25 -07:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Iterator;
|
2019-12-07 23:03:57 -08:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2015-10-30 13:16:43 -07:00
|
|
|
import java.util.stream.Collectors;
|
2020-01-15 12:02:42 -08:00
|
|
|
|
2017-04-08 03:29:49 -07:00
|
|
|
import magic.model.choice.MagicCardChoiceResult;
|
2015-09-19 09:36:38 -07:00
|
|
|
import magic.model.phase.MagicPhaseType;
|
|
|
|
import magic.model.stack.MagicCardOnStack;
|
2013-04-12 19:32:25 -07:00
|
|
|
|
|
|
|
public class MagicMessage {
|
|
|
|
|
2015-10-25 09:38:04 -07:00
|
|
|
public static final char CARD_ID_DELIMITER = '~';
|
2015-09-19 08:55:33 -07:00
|
|
|
|
2017-05-15 04:07:59 -07:00
|
|
|
private final DuelPlayerConfig playerConfig;
|
|
|
|
private final int playerIndex;
|
2013-04-12 19:32:25 -07:00
|
|
|
private final int life;
|
|
|
|
private final int turn;
|
|
|
|
private final MagicPhaseType phaseType;
|
|
|
|
private final String text;
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2019-12-07 23:03:57 -08:00
|
|
|
MagicMessage(final MagicGame game, final MagicPlayer player, final String text) {
|
|
|
|
this.playerIndex = player.getIndex();
|
|
|
|
this.playerConfig = player.getConfig();
|
|
|
|
this.life = player.getLife();
|
|
|
|
this.turn = game.getTurn();
|
|
|
|
this.phaseType = game.getPhase().getType();
|
|
|
|
this.text = text;
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
|
|
|
|
2017-05-15 04:07:59 -07:00
|
|
|
public int getPlayerIndex() {
|
|
|
|
return playerIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DuelPlayerConfig getPlayerConfig() {
|
|
|
|
return playerConfig;
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
public int getLife() {
|
|
|
|
return life;
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
public int getTurn() {
|
|
|
|
return turn;
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
public MagicPhaseType getPhaseType() {
|
|
|
|
return phaseType;
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
public String getText() {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2019-12-07 23:03:57 -08:00
|
|
|
static void addNames(final StringBuilder builder, final Collection<String> names) {
|
2013-04-12 19:32:25 -07:00
|
|
|
if (!names.isEmpty()) {
|
2019-12-07 23:03:57 -08:00
|
|
|
boolean first = true;
|
2013-04-12 19:32:25 -07:00
|
|
|
boolean next;
|
2019-12-07 23:03:57 -08:00
|
|
|
final Iterator<String> iterator = names.iterator();
|
2013-04-12 19:32:25 -07:00
|
|
|
do {
|
2019-12-07 23:03:57 -08:00
|
|
|
final String name = iterator.next();
|
|
|
|
next = iterator.hasNext();
|
2013-04-12 19:32:25 -07:00
|
|
|
if (first) {
|
2019-12-07 23:03:57 -08:00
|
|
|
first = false;
|
2013-04-12 19:32:25 -07:00
|
|
|
} else if (next) {
|
|
|
|
builder.append(", ");
|
|
|
|
} else {
|
|
|
|
builder.append(" and ");
|
|
|
|
}
|
|
|
|
builder.append(name);
|
|
|
|
} while (next);
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2019-12-07 23:03:57 -08:00
|
|
|
private static Pattern replaceNameRegex = Pattern.compile("PN|SN|RN|\\bX\\b" + ARG.EVENQUOTES);
|
|
|
|
|
|
|
|
public static String replaceName(final String sourceText, final Object source, final Object player, final Object ref) {
|
|
|
|
StringBuffer result = null;
|
|
|
|
Matcher matcher = replaceNameRegex.matcher(sourceText);
|
|
|
|
while (matcher.find()) {
|
|
|
|
if (result == null) result = new StringBuffer(sourceText.length() + 128);
|
|
|
|
String replacement;
|
|
|
|
switch (matcher.group().charAt(0)) {
|
|
|
|
// This relies on the first letter of what could pass through the regex
|
|
|
|
// uniquely identifying the replacement.
|
|
|
|
case 'P': // "PN"
|
|
|
|
replacement = player.toString();
|
|
|
|
break;
|
|
|
|
case 'S': // "SN"
|
|
|
|
replacement = getCardToken(source);
|
|
|
|
break;
|
|
|
|
case 'R': // "RN"
|
|
|
|
replacement = getCardToken(ref);
|
|
|
|
break;
|
|
|
|
case 'X': // "\\bX\\b" + ARG.EVENQUOTES
|
|
|
|
replacement = getXCost(sourceText, ref);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("No replacement for " + matcher.group());
|
|
|
|
}
|
|
|
|
matcher.appendReplacement(result, replacement);
|
|
|
|
}
|
|
|
|
if (result == null) return sourceText;
|
|
|
|
matcher.appendTail(result);
|
|
|
|
return result.toString();
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2015-09-21 11:54:19 -07:00
|
|
|
public static String replaceChoices(final String sourceText, final Object[] choices) {
|
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
String result = sourceText;
|
2015-09-21 11:54:19 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
for (int idx = 0; result.indexOf('$') >= 0; idx++) {
|
2015-09-21 11:54:19 -07:00
|
|
|
|
2015-12-31 02:54:52 -08:00
|
|
|
final String choice = (idx < choices.length && choices[idx] != null)
|
2015-09-21 11:54:19 -07:00
|
|
|
? getCardToken(choices[idx])
|
|
|
|
: "";
|
|
|
|
|
|
|
|
final String replacement = !choice.isEmpty() ? " (" + choice + ")" : "";
|
2013-04-12 19:32:25 -07:00
|
|
|
result = result.replaceFirst("\\$", replacement);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2015-09-19 09:36:38 -07:00
|
|
|
|
2016-06-04 21:06:10 -07:00
|
|
|
private static String getXCost(final String sourceText, final Object obj) {
|
2019-12-07 23:03:57 -08:00
|
|
|
if (obj instanceof MagicPayedCost && !sourceText.contains("where X")) {
|
|
|
|
return "X (" + ((MagicPayedCost) obj).getX() + ")";
|
2015-12-13 17:22:49 -08:00
|
|
|
} else {
|
|
|
|
return "X";
|
|
|
|
}
|
|
|
|
}
|
2015-09-19 09:36:38 -07:00
|
|
|
|
2016-01-30 19:31:18 -08:00
|
|
|
public static String format(final String template, final Object... args) {
|
|
|
|
final Object[] strings = Arrays.stream(args).map(o -> getCardToken(o)).toArray();
|
|
|
|
return String.format(template, strings);
|
|
|
|
}
|
|
|
|
|
2015-09-21 11:54:19 -07:00
|
|
|
public static String getCardToken(final Object obj) {
|
2015-09-19 09:36:38 -07:00
|
|
|
|
|
|
|
if (obj == null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj instanceof MagicCard) {
|
|
|
|
final MagicCard card = (MagicCard) obj;
|
2019-12-07 23:03:57 -08:00
|
|
|
return "<" + card.getName() + CARD_ID_DELIMITER + card.getId() + ">";
|
2015-09-21 11:54:19 -07:00
|
|
|
}
|
2015-09-19 09:36:38 -07:00
|
|
|
|
2015-09-21 11:54:19 -07:00
|
|
|
if (obj instanceof MagicPermanent) {
|
2015-09-19 09:36:38 -07:00
|
|
|
final MagicPermanent card = (MagicPermanent) obj;
|
2019-12-07 23:03:57 -08:00
|
|
|
return "<" + card.getName() + CARD_ID_DELIMITER + card.getCard().getId() + ">";
|
2015-09-21 11:54:19 -07:00
|
|
|
}
|
2015-09-19 09:36:38 -07:00
|
|
|
|
2015-09-21 11:54:19 -07:00
|
|
|
if (obj instanceof MagicCardOnStack) {
|
2015-09-19 09:36:38 -07:00
|
|
|
final MagicCardOnStack card = (MagicCardOnStack) obj;
|
2019-12-07 23:03:57 -08:00
|
|
|
return "<" + card.getName() + CARD_ID_DELIMITER + card.getCard().getId() + ">";
|
2015-09-21 11:54:19 -07:00
|
|
|
}
|
2015-09-19 09:36:38 -07:00
|
|
|
|
2016-01-30 19:06:01 -08:00
|
|
|
if (obj instanceof MagicCardChoiceResult) {
|
|
|
|
final MagicCardChoiceResult cards = (MagicCardChoiceResult) obj;
|
|
|
|
return getTokenizedCardNames(cards);
|
|
|
|
}
|
|
|
|
|
2015-09-21 14:18:46 -07:00
|
|
|
// Please do not remove, thanks ~ lodici.
|
|
|
|
// System.err.printf("getCardToken() : %s (%s)\n", obj.toString(), obj.getClass());
|
|
|
|
|
2015-09-21 11:54:19 -07:00
|
|
|
return obj.toString();
|
|
|
|
|
2015-09-19 09:36:38 -07:00
|
|
|
}
|
2015-09-21 11:54:19 -07:00
|
|
|
|
2015-11-17 04:37:17 -08:00
|
|
|
public static String getCardToken(final String name, final MagicCard card) {
|
2019-12-07 23:03:57 -08:00
|
|
|
return "<" + name + CARD_ID_DELIMITER + card.getId() + ">";
|
2015-11-17 04:37:17 -08:00
|
|
|
}
|
|
|
|
|
2015-10-30 13:16:43 -07:00
|
|
|
public static String getTokenizedCardNames(final Collection<MagicCard> cards) {
|
|
|
|
return cards.stream()
|
|
|
|
.map(card -> MagicMessage.getCardToken(card))
|
2015-10-30 13:35:51 -07:00
|
|
|
.sorted()
|
2017-01-14 17:42:00 -08:00
|
|
|
.collect(Collectors.joining(", "));
|
2015-10-30 13:16:43 -07:00
|
|
|
}
|
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|