magarena/src/magic/data/DuelConfig.java

163 lines
5.6 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;
2020-01-15 12:02:42 -08:00
import magic.model.DuelPlayerConfig;
import magic.model.MagicDeckProfile;
import magic.model.MagicDuel;
import magic.model.player.PlayerProfile;
import magic.model.player.PlayerProfiles;
import magic.utility.FileIO;
import magic.utility.SortedProperties;
2013-04-12 19:32:25 -07:00
public class DuelConfig {
private static final DuelConfig INSTANCE=new DuelConfig();
// Properties file keys.
private static final String START_LIFE = "config.life";
private static final String HAND_SIZE = "config.hand";
private static final String GAMES = "config.games";
private static final String CUBE = "config.cube";
private static final String PLAYER_ONE = "p1.profile";
private static final String PLAYER_TWO = "p2.profile";
2013-06-23 18:33:35 -07:00
public static final int MAX_PLAYERS = 2;
public static final int DEFAULT_GAMES = 7;
public static final int DEFAULT_LIFE = 20;
2013-04-12 19:32:25 -07:00
// default values.
private int startLife = DEFAULT_LIFE;
private int handSize = 7;
private int games = DEFAULT_GAMES;
private MagicFormat cube = MagicFormat.ALL;
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() {
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
public int getStartingLife(final int idx) {
return startLife + players[idx].getProfile().getExtraLife();
}
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 MagicFormat getCube() {
return cube;
2013-04-12 19:32:25 -07:00
}
public void setCube(final MagicFormat aCube) {
2015-05-26 06:37:56 -07:00
this.cube = aCube;
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
public void load(final Properties properties, final boolean loadPlayerDecks) {
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 = MagicCustomFormat.get(properties.getProperty(CUBE, cube.getName()));
loadPlayerConfigs(properties, loadPlayerDecks);
}
private void loadPlayerConfigs(final Properties properties, final boolean loadPlayerDecks) {
setPlayerProfile(0, PlayerProfile.getHumanPlayer(properties.getProperty(PLAYER_ONE)));
setPlayerProfile(1, PlayerProfile.getAiPlayer(properties.getProperty(PLAYER_TWO)));
for (int i = 0; i < getPlayerConfigs().length; i++) {
getPlayerConfig(i).setDeckProfile(
properties.getProperty(
getPlayerPrefix(i) + "deckProfile",
DeckType.Random.name() + ";" + MagicDeckProfile.ANY_THREE)
);
if (loadPlayerDecks) {
getPlayerConfig(i).loadDeck(properties, getPlayerPrefix(i));
}
}
}
2013-04-12 19:32:25 -07:00
public void load() {
final File configFile = MagicDuel.getLatestDuelFile();
final Properties properties = configFile.exists() ? FileIO.toProp(configFile) : new SortedProperties();
load(properties, false);
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) {
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));
properties.setProperty(CUBE, cube.getName());
2015-04-19 14:52:41 -07:00
properties.setProperty(PLAYER_ONE, players[0].getProfile().getId());
properties.setProperty(PLAYER_TWO, players[1].getProfile().getId());
for (int i = 0; i < getPlayerConfigs().length; i++) {
getPlayerConfig(i).save(properties, getPlayerPrefix(i));
}
}
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
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;
}
2015-04-19 14:52:41 -07:00
2013-04-12 19:32:25 -07:00
}