magarena/src/magic/data/DuelConfig.java

175 lines
6.5 KiB
Java
Raw Normal View History

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;
import magic.model.MagicDeckProfile;
import magic.model.MagicDuel;
import magic.model.DuelPlayerConfig;
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
/**
* Represents the default settings when starting a new duel.
* <p>
* The settings are saved to CONFIG_FILENAME and are loaded
* 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();
// Properties file.
private static final String CONFIG_FILENAME = MagicDuel.getDuelFile().getName();
// 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
public static final int MAX_PLAYERS = 2;
2013-04-12 19:32:25 -07: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];
// CTR
2013-04-12 19:32:25 -07:00
public DuelConfig() {
// Ensure DuelConfig has valid PlayerProfile references.
// If missing then creates default profiles.
PlayerProfiles.refreshMap();
2015-04-19 12:29:20 -07:00
players[0] = new DuelPlayerConfig(
PlayerProfiles.getDefaultHumanPlayer(),
MagicDeckProfile.getDeckProfile(MagicDeckProfile.ANY_THREE)
);
2015-04-19 12:29:20 -07:00
players[1] = new DuelPlayerConfig(
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
public String getCube() {
return cube;
2013-04-12 19:32:25 -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
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
}
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
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
}
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);
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));
for (int index = 0; index < getPlayerConfigs().length; index++) {
getPlayerConfig(index).load(properties, getPlayerPrefix(index));
}
}
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() {
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());
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());
properties.setProperty(CUBE, cube);
for (int index = 0; index < getPlayerConfigs().length; index++) {
getPlayerConfig(index).save(properties, getPlayerPrefix(index));
}
}
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;
}
public int getGamesRequiredToWinDuel() {
return (int)Math.ceil(getNrOfGames()/2.0);
}
public DuelPlayerConfig getPlayerConfig(final int index) {
2015-04-19 12:29:20 -07:00
return players[index];
}
public DuelPlayerConfig[] getPlayerConfigs() {
2015-04-19 12:29:20 -07:00
return players;
}
public void setPlayerConfigs(DuelPlayerConfig[] aConfigs) {
players = aConfigs;
}
2013-04-12 19:32:25 -07:00
}