2013-04-12 19:32:25 -07:00
|
|
|
package magic.model;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Iterator;
|
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-09-19 08:55:33 -07:00
|
|
|
public static final char CARD_ID_DELIMITER = '#';
|
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
private final MagicPlayer player;
|
|
|
|
private final int life;
|
|
|
|
private final int turn;
|
|
|
|
private final MagicPhaseType phaseType;
|
|
|
|
private final String text;
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
MagicMessage(final MagicGame game,final MagicPlayer player,final String text) {
|
|
|
|
this.player=player;
|
|
|
|
this.life=player.getLife();
|
|
|
|
this.turn=game.getTurn();
|
|
|
|
this.phaseType=game.getPhase().getType();
|
|
|
|
this.text=text;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MagicPlayer getPlayer() {
|
|
|
|
return player;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addNames(final StringBuilder builder,final Collection<String> names) {
|
|
|
|
if (!names.isEmpty()) {
|
|
|
|
boolean first=true;
|
|
|
|
boolean next;
|
|
|
|
final Iterator<String> iterator=names.iterator();
|
|
|
|
do {
|
|
|
|
final String name=iterator.next();
|
2013-06-23 18:33:35 -07:00
|
|
|
next=iterator.hasNext();
|
2013-04-12 19:32:25 -07:00
|
|
|
if (first) {
|
|
|
|
first=false;
|
|
|
|
} else if (next) {
|
|
|
|
builder.append(", ");
|
|
|
|
} else {
|
|
|
|
builder.append(" and ");
|
|
|
|
}
|
|
|
|
builder.append(name);
|
|
|
|
} while (next);
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
public static String replaceName(final String sourceText,final Object source, final Object player, final Object ref) {
|
2015-09-19 09:36:38 -07:00
|
|
|
final String sn = source instanceof MagicObject
|
|
|
|
? getCardToken((MagicObject) source)
|
|
|
|
: source.toString();
|
2013-04-12 19:32:25 -07:00
|
|
|
return sourceText
|
2015-04-06 21:54:51 -07:00
|
|
|
.replaceAll("PN", player.toString())
|
2015-09-19 09:36:38 -07:00
|
|
|
.replaceAll("SN", sn)
|
2015-04-09 22:08:02 -07:00
|
|
|
.replaceAll("RN", ref.toString());
|
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 static String replaceChoices(final String sourceText,final Object[] choices) {
|
|
|
|
String result = sourceText;
|
|
|
|
for (int idx = 0; result.indexOf('$') >= 0; idx++) {
|
|
|
|
final String choice = (idx < choices.length && choices[idx] != null) ? choices[idx].toString() : "";
|
|
|
|
final String replacement = (!choice.isEmpty()) ? " (" + choice + ")" : "";
|
|
|
|
result = result.replaceFirst("\\$", replacement);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2015-09-19 09:36:38 -07:00
|
|
|
|
|
|
|
private static final String CARD_TOKEN = "<%s" + CARD_ID_DELIMITER + "%d>";
|
|
|
|
|
2015-09-19 11:00:55 -07:00
|
|
|
public static String getCardToken(final MagicObject obj) {
|
2015-09-19 09:36:38 -07:00
|
|
|
|
|
|
|
if (obj == null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj instanceof MagicCard) {
|
|
|
|
final MagicCard card = (MagicCard) obj;
|
|
|
|
return String.format(CARD_TOKEN, card.getName(), card.getId());
|
|
|
|
|
|
|
|
} else if (obj instanceof MagicPermanent) {
|
|
|
|
final MagicPermanent card = (MagicPermanent) obj;
|
|
|
|
return String.format(CARD_TOKEN, card.getName(), card.getCard().getId());
|
|
|
|
|
|
|
|
} else if (obj instanceof MagicCardOnStack) {
|
|
|
|
final MagicCardOnStack card = (MagicCardOnStack) obj;
|
|
|
|
return String.format(CARD_TOKEN, card.getName(), card.getCard().getId());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
System.err.println("MagicSource object not supported : " + obj.getClass());
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|