2013-04-12 19:32:25 -07:00
|
|
|
package magic.model;
|
|
|
|
|
2017-07-12 00:20:31 -07:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2015-04-05 03:45:29 -07:00
|
|
|
import java.util.Properties;
|
2020-01-15 12:02:42 -08:00
|
|
|
|
2015-04-19 15:56:53 -07:00
|
|
|
import magic.data.DeckType;
|
2014-03-13 04:58:48 -07:00
|
|
|
import magic.model.player.PlayerProfile;
|
2017-07-12 04:36:09 -07:00
|
|
|
import magic.utility.DeckParser;
|
2017-01-10 03:38:36 -08:00
|
|
|
import magic.utility.DeckUtils;
|
2013-04-12 19:32:25 -07:00
|
|
|
|
2015-04-19 12:21:25 -07:00
|
|
|
public class DuelPlayerConfig {
|
2013-04-12 19:32:25 -07:00
|
|
|
|
2015-04-19 16:03:38 -07:00
|
|
|
private static final String PLAYER_DECK = "deckProfile";
|
2015-04-19 15:51:22 -07:00
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
private MagicDeckProfile deckProfile;
|
2017-07-12 00:20:31 -07:00
|
|
|
private MagicDeck deck = new MagicDeck();
|
2015-04-17 08:07:20 -07:00
|
|
|
private PlayerProfile playerProfile;
|
2013-04-12 19:32:25 -07:00
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
// CTR
|
2015-04-19 12:21:25 -07:00
|
|
|
public DuelPlayerConfig(final PlayerProfile aPlayerProfile, final MagicDeckProfile aDeckProfile) {
|
2015-04-05 08:53:55 -07:00
|
|
|
playerProfile = aPlayerProfile;
|
2015-04-04 06:26:49 -07:00
|
|
|
deckProfile = aDeckProfile;
|
2014-03-13 04:58:48 -07:00
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
public String getName() {
|
2015-04-05 08:44:15 -07:00
|
|
|
return playerProfile.getPlayerName();
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
public void setDeckProfile(final MagicDeckProfile profile) {
|
2015-04-19 16:03:38 -07:00
|
|
|
this.deckProfile = profile;
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2015-04-19 16:03:38 -07:00
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
public MagicDeckProfile getDeckProfile() {
|
|
|
|
return deckProfile;
|
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 MagicDeck getDeck() {
|
|
|
|
return deck;
|
2013-06-23 18:33:35 -07:00
|
|
|
}
|
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
public void setDeck(final MagicDeck aDeck) {
|
|
|
|
deck.setContent(aDeck);
|
2017-05-11 15:01:09 -07:00
|
|
|
setDeckProfile(deck.getDeckType().name() + ";" + deck.getName());
|
2013-06-23 18:33:35 -07:00
|
|
|
}
|
|
|
|
|
2015-04-19 16:03:38 -07:00
|
|
|
private static String getDeckPrefix(final String prefix, final int index) {
|
|
|
|
return prefix + "deck" + index;
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2015-04-19 16:03:38 -07:00
|
|
|
|
2015-05-02 17:23:50 -07:00
|
|
|
public void setDeckProfile(final String deckPropertyValue) {
|
2015-04-19 15:56:53 -07:00
|
|
|
final DeckType deckType = DeckType.valueOf(deckPropertyValue.split(";", 0)[0]);
|
|
|
|
final String deckValue = deckPropertyValue.split(";", 0)[1];
|
|
|
|
setDeckProfile(MagicDeckProfile.getDeckProfile(deckType, deckValue));
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2017-01-07 17:43:43 -08:00
|
|
|
private long getDeckFileChecksum(Properties properties, String prefix) {
|
|
|
|
try {
|
|
|
|
return Long.valueOf(properties.getProperty(prefix + "deck.file.crc", "0"));
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 17:23:50 -07:00
|
|
|
public void loadDeck(final Properties properties, final String prefix) {
|
2017-07-12 00:20:31 -07:00
|
|
|
Map<String, Integer> cards = new HashMap<>();
|
2015-04-24 06:00:22 -07:00
|
|
|
for (int i = 1; i <= properties.size(); i++) {
|
|
|
|
final String deckPrefix = getDeckPrefix(prefix, i);
|
2013-04-12 19:32:25 -07:00
|
|
|
if (properties.containsKey(deckPrefix)) {
|
2015-04-24 06:00:22 -07:00
|
|
|
final String cardName = properties.getProperty(deckPrefix);
|
2017-07-12 00:20:31 -07:00
|
|
|
cards.put(cardName, cards.getOrDefault(cardName, 0) + 1);
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2015-12-31 02:54:52 -08:00
|
|
|
}
|
2017-07-12 00:20:31 -07:00
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
for (String cardName : cards.keySet()) {
|
|
|
|
sb.append(cards.get(cardName))
|
|
|
|
.append(" ")
|
|
|
|
.append(cardName)
|
|
|
|
.append("\n");
|
|
|
|
}
|
2017-07-12 04:36:09 -07:00
|
|
|
deck = DeckParser.parseText(sb.toString());
|
2017-01-07 17:43:43 -08:00
|
|
|
deck.setFilename(properties.getProperty(prefix + "deck.name", ""));
|
|
|
|
deck.setDeckType(DeckType.valueOf(properties.getProperty(prefix + "deck.file.type", "Random")));
|
|
|
|
deck.setDescription(properties.getProperty(prefix + "deck.desc", ""));
|
2017-01-10 03:38:36 -08:00
|
|
|
// only set non-zero checksum if original deck file on which duel
|
|
|
|
// deck was based has not changed since the duel was created.
|
|
|
|
long cs = getDeckFileChecksum(properties, prefix);
|
|
|
|
deck.setDeckFileChecksum(cs > 0 && DeckUtils.getDeckFileChecksum(deck) == cs ? cs : 0);
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2015-04-19 15:51:22 -07:00
|
|
|
public void save(final Properties properties, final String prefix) {
|
|
|
|
|
2017-01-07 17:43:43 -08:00
|
|
|
properties.setProperty(prefix + "deck.file.crc", Long.toString(deck.getDeckFileChecksum()));
|
|
|
|
properties.setProperty(prefix + "deck.file.type", deck.getDeckType().name());
|
|
|
|
properties.setProperty(prefix + "deck.name", deck.getName());
|
|
|
|
properties.setProperty(prefix + "deck.desc", deck.getDescription());
|
|
|
|
|
2015-04-19 15:51:22 -07:00
|
|
|
properties.setProperty(prefix + PLAYER_DECK,
|
|
|
|
deckProfile.getDeckType().name() + ";" + deckProfile.getDeckValue()
|
|
|
|
);
|
|
|
|
|
|
|
|
int index = 1;
|
2013-04-12 19:32:25 -07:00
|
|
|
for (final MagicCardDefinition cardDefinition : deck) {
|
2015-05-09 07:01:39 -07:00
|
|
|
properties.setProperty(getDeckPrefix(prefix, index++), cardDefinition.getDistinctName());
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
|
|
|
}
|
2014-03-06 01:45:19 -08:00
|
|
|
|
2015-04-06 08:28:04 -07:00
|
|
|
public PlayerProfile getProfile() {
|
2015-02-14 17:26:43 -08:00
|
|
|
return playerProfile;
|
2014-03-06 01:45:19 -08:00
|
|
|
}
|
2015-02-13 07:36:38 -08:00
|
|
|
|
2015-04-17 08:07:20 -07:00
|
|
|
public void setProfile(PlayerProfile aPlayerProfile) {
|
|
|
|
playerProfile = aPlayerProfile;
|
|
|
|
}
|
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|