2013-04-12 19:32:25 -07:00
|
|
|
package magic.data;
|
|
|
|
|
2014-08-17 01:16:03 -07:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.Properties;
|
2014-03-13 03:24:17 -07:00
|
|
|
import magic.model.MagicDeckProfile;
|
2015-04-17 07:38:45 -07:00
|
|
|
import magic.model.MagicDuel;
|
2015-04-19 12:21:25 -07:00
|
|
|
import magic.model.DuelPlayerConfig;
|
2014-03-06 01:45:19 -08:00
|
|
|
import magic.model.player.PlayerProfile;
|
|
|
|
import magic.model.player.PlayerProfiles;
|
2014-08-17 01:16:03 -07:00
|
|
|
import magic.utility.MagicFileSystem;
|
|
|
|
import magic.utility.MagicFileSystem.DataPath;
|
2013-04-12 19:32:25 -07:00
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
/**
|
|
|
|
* Represents the default settings when starting a new duel.
|
|
|
|
* <p>
|
2015-04-17 07:38:45 -07:00
|
|
|
* The settings are saved to CONFIG_FILENAME and are loaded
|
2014-03-13 04:58:48 -07:00
|
|
|
* whenever the user starts a new duel.
|
|
|
|
* <p>
|
|
|
|
* Note that references to decks are specifically to deck
|
|
|
|
* profiles and not actual decks (which are generated later).
|
|
|
|
*/
|
2013-04-12 19:32:25 -07:00
|
|
|
public class DuelConfig {
|
|
|
|
|
|
|
|
private static final DuelConfig INSTANCE=new DuelConfig();
|
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
// Properties file.
|
2015-04-17 07:38:45 -07:00
|
|
|
private static final String CONFIG_FILENAME = MagicDuel.getDuelFile().getName();
|
2014-03-13 04:58:48 -07:00
|
|
|
// Properties file keys.
|
|
|
|
private static final String START_LIFE = "life";
|
|
|
|
private static final String HAND_SIZE = "hand";
|
|
|
|
private static final String GAMES = "games";
|
|
|
|
private static final String CUBE = "cube";
|
|
|
|
private static final String PLAYER_ONE = "playerOne";
|
|
|
|
private static final String PLAYER_TWO = "playerTwo";
|
|
|
|
private static final String PLAYER_ONE_DECK = "playerOneDeck";
|
|
|
|
private static final String PLAYER_TWO_DECK = "playerTwoDeck";
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
public static final int MAX_PLAYERS = 2;
|
2013-04-12 19:32:25 -07:00
|
|
|
|
2014-03-06 01:45:19 -08:00
|
|
|
// default values.
|
|
|
|
private int startLife = 20;
|
|
|
|
private int handSize = 7;
|
|
|
|
private int games = 7;
|
|
|
|
private String cube = CubeDefinitions.getCubeNames()[0];
|
2015-04-19 12:29:20 -07:00
|
|
|
private DuelPlayerConfig[] players = new DuelPlayerConfig[MAX_PLAYERS];
|
2014-03-06 01:45:19 -08:00
|
|
|
|
|
|
|
// CTR
|
2013-04-12 19:32:25 -07:00
|
|
|
public DuelConfig() {
|
2015-04-17 08:07:20 -07:00
|
|
|
|
2014-03-06 01:45:19 -08:00
|
|
|
// Ensure DuelConfig has valid PlayerProfile references.
|
|
|
|
// If missing then creates default profiles.
|
|
|
|
PlayerProfiles.refreshMap();
|
2015-04-17 08:07:20 -07:00
|
|
|
|
2015-04-19 12:29:20 -07:00
|
|
|
players[0] = new DuelPlayerConfig(
|
2015-04-17 08:07:20 -07:00
|
|
|
PlayerProfiles.getDefaultHumanPlayer(),
|
|
|
|
MagicDeckProfile.getDeckProfile(MagicDeckProfile.ANY_THREE)
|
|
|
|
);
|
2015-04-19 12:29:20 -07:00
|
|
|
players[1] = new DuelPlayerConfig(
|
2015-04-17 08:07:20 -07:00
|
|
|
PlayerProfiles.getDefaultAiPlayer(),
|
|
|
|
MagicDeckProfile.getDeckProfile(MagicDeckProfile.ANY_THREE)
|
|
|
|
);
|
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 getStartLife() {
|
|
|
|
return startLife;
|
|
|
|
}
|
|
|
|
public void setStartLife(final int startLife) {
|
|
|
|
this.startLife = startLife;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getHandSize() {
|
|
|
|
return handSize;
|
|
|
|
}
|
|
|
|
public void setHandSize(final int handSize) {
|
|
|
|
this.handSize = handSize;
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
public void setNrOfGames(final int aGames) {
|
2013-06-23 18:33:35 -07:00
|
|
|
this.games = aGames;
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
|
|
|
public int getNrOfGames() {
|
|
|
|
return games;
|
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
public String getCube() {
|
|
|
|
return cube;
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2014-03-13 04:58:48 -07:00
|
|
|
public void setCube(final String cube) {
|
|
|
|
this.cube=cube;
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
public PlayerProfile getPlayerProfile(final int playerIndex) {
|
2015-04-19 12:29:20 -07:00
|
|
|
return players[playerIndex].getProfile();
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2014-03-13 04:58:48 -07:00
|
|
|
public void setPlayerProfile(final int playerIndex, final PlayerProfile playerProfile) {
|
2015-04-19 12:29:20 -07:00
|
|
|
players[playerIndex].setProfile(playerProfile);
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
public MagicDeckProfile getPlayerDeckProfile(final int playerIndex) {
|
2015-04-19 12:29:20 -07:00
|
|
|
return players[playerIndex].getDeckProfile();
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2015-04-17 08:07:20 -07:00
|
|
|
|
2014-03-13 04:58:48 -07:00
|
|
|
public void setPlayerDeckProfile(final int playerIndex, final DeckType deckType, final String deckValue) {
|
2015-04-19 12:29:20 -07:00
|
|
|
players[playerIndex].setDeckProfile(MagicDeckProfile.getDeckProfile(deckType, deckValue));
|
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 void load(final Properties properties) {
|
|
|
|
startLife=Integer.parseInt(properties.getProperty(START_LIFE,Integer.toString(startLife)));
|
|
|
|
handSize=Integer.parseInt(properties.getProperty(HAND_SIZE,Integer.toString(handSize)));
|
|
|
|
games=Integer.parseInt(properties.getProperty(GAMES,Integer.toString(games)));
|
|
|
|
cube=properties.getProperty(CUBE,cube);
|
2014-03-13 04:58:48 -07:00
|
|
|
setPlayerProfile(0, PlayerProfile.getHumanPlayer(properties.getProperty(PLAYER_ONE)));
|
|
|
|
setPlayerProfile(1, PlayerProfile.getAiPlayer(properties.getProperty(PLAYER_TWO)));
|
|
|
|
setPlayerDeckProfile(0, properties.getProperty(PLAYER_ONE_DECK, DeckType.Random + ";" + MagicDeckProfile.ANY_THREE));
|
|
|
|
setPlayerDeckProfile(1, properties.getProperty(PLAYER_TWO_DECK, DeckType.Random + ";" + MagicDeckProfile.ANY_THREE));
|
2015-04-19 14:45:52 -07:00
|
|
|
for (int index = 0; index < getPlayerConfigs().length; index++) {
|
2015-04-19 14:43:56 -07:00
|
|
|
getPlayerConfig(index).load(properties, getPlayerPrefix(index));
|
2015-04-17 13:36:59 -07:00
|
|
|
}
|
2014-03-13 04:58:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setPlayerDeckProfile(final int playerIndex, final String deckPropertyValue) {
|
|
|
|
final DeckType deckType = DeckType.valueOf(deckPropertyValue.split(";", 0)[0]);
|
|
|
|
final String deckValue = deckPropertyValue.split(";", 0)[1];
|
|
|
|
setPlayerDeckProfile(playerIndex, deckType, deckValue);
|
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 void load() {
|
2014-03-06 01:45:19 -08:00
|
|
|
final File configFile = getConfigFile();
|
|
|
|
final Properties properties = configFile.exists() ? FileIO.toProp(configFile) : new Properties();
|
|
|
|
load(properties);
|
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 void save(final Properties properties) {
|
2015-04-19 12:29:20 -07:00
|
|
|
properties.setProperty(PLAYER_ONE, players[0].getProfile().getId());
|
|
|
|
properties.setProperty(PLAYER_TWO, players[1].getProfile().getId());
|
2014-03-06 01:45:19 -08:00
|
|
|
properties.setProperty(START_LIFE, Integer.toString(startLife));
|
|
|
|
properties.setProperty(HAND_SIZE, Integer.toString(handSize));
|
2013-04-12 19:32:25 -07:00
|
|
|
properties.setProperty(GAMES, Integer.toString(games));
|
2015-04-19 12:29:20 -07:00
|
|
|
properties.setProperty(PLAYER_ONE_DECK, players[0].getDeckProfile().getDeckType().name() + ";" + players[0].getDeckProfile().getDeckValue());
|
|
|
|
properties.setProperty(PLAYER_TWO_DECK, players[1].getDeckProfile().getDeckType().name() + ";" + players[1].getDeckProfile().getDeckValue());
|
2014-03-06 01:45:19 -08:00
|
|
|
properties.setProperty(CUBE, cube);
|
2015-04-19 14:45:52 -07:00
|
|
|
for (int index = 0; index < getPlayerConfigs().length; index++) {
|
2015-04-19 14:43:56 -07:00
|
|
|
getPlayerConfig(index).save(properties, getPlayerPrefix(index));
|
2015-04-17 13:36:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getPlayerPrefix(final int index) {
|
|
|
|
return "p"+(index+1)+".";
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|
2013-06-23 18:33:35 -07:00
|
|
|
|
2013-04-12 19:32:25 -07:00
|
|
|
private static File getConfigFile() {
|
2014-08-17 01:16:03 -07:00
|
|
|
return MagicFileSystem.getDataPath(DataPath.DUELS).resolve(CONFIG_FILENAME).toFile();
|
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 DuelConfig getInstance() {
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
2014-03-11 01:12:15 -07:00
|
|
|
|
2015-03-22 13:15:37 -07:00
|
|
|
public int getGamesRequiredToWinDuel() {
|
|
|
|
return (int)Math.ceil(getNrOfGames()/2.0);
|
|
|
|
}
|
2015-04-17 08:07:20 -07:00
|
|
|
|
2015-04-19 14:43:56 -07:00
|
|
|
public DuelPlayerConfig getPlayerConfig(final int index) {
|
2015-04-19 12:29:20 -07:00
|
|
|
return players[index];
|
2015-04-17 08:07:20 -07:00
|
|
|
}
|
2015-04-17 13:36:59 -07:00
|
|
|
|
2015-04-19 14:45:52 -07:00
|
|
|
public DuelPlayerConfig[] getPlayerConfigs() {
|
2015-04-19 12:29:20 -07:00
|
|
|
return players;
|
2015-04-17 13:36:59 -07:00
|
|
|
}
|
|
|
|
|
2015-04-19 14:41:38 -07:00
|
|
|
public void setPlayerConfigs(DuelPlayerConfig[] aConfigs) {
|
|
|
|
players = aConfigs;
|
2015-04-17 13:36:59 -07:00
|
|
|
}
|
2013-04-12 19:32:25 -07:00
|
|
|
}
|