magarena/src/magic/data/DuelConfig.java

231 lines
7.6 KiB
Java
Raw Normal View History

2013-04-12 19:32:25 -07:00
package magic.data;
import magic.MagicMain;
import magic.ai.MagicAI;
import magic.ai.MagicAIImpl;
import magic.model.MagicColor;
import magic.model.MagicDeckProfile;
import magic.model.player.AiPlayer;
import magic.model.player.PlayerProfile;
import magic.model.player.PlayerProfiles;
2013-04-12 19:32:25 -07:00
import java.io.File;
import java.io.IOException;
import java.util.Properties;
public class DuelConfig {
private static final DuelConfig INSTANCE=new DuelConfig();
private static final String ANY_DECK="@";
private static final String ANY_THREE="***";
private static final String ANY_TWO="**";
private static final String ANY_ONE="*";
2013-06-23 18:33:35 -07:00
private static final String CONFIG_FILENAME="newduel.cfg";
2013-04-12 19:32:25 -07:00
private static final String START_LIFE="life";
private static final String HAND_SIZE="hand";
private static final String GAMES="games";
private static final String PLAYER="player";
private static final String OPPONENT="opponent";
private static final String CUBE="cube";
// default values.
private int startLife = 20;
private int handSize = 7;
private int games = 7;
private String playerOneDeckGenerator = ANY_THREE;
private String playerTwoDeckGenerator = ANY_THREE;
private String cube = CubeDefinitions.getCubeNames()[0];
private PlayerProfile playerOne = null;
private PlayerProfile playerTwo = null;
// 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();
playerOne = PlayerProfiles.getDefaultHumanPlayer();
playerTwo = PlayerProfiles.getDefaultAiPlayer();
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
// CTR: copy constructor - a common way of creating a copy of an existing object.
2013-04-12 19:32:25 -07:00
public DuelConfig(final DuelConfig duelConfig) {
startLife=duelConfig.startLife;
handSize=duelConfig.handSize;
games=duelConfig.games;
playerOneDeckGenerator=duelConfig.playerOneDeckGenerator;
playerTwoDeckGenerator=duelConfig.playerTwoDeckGenerator;
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
public PlayerProfile getPlayerOneProfile() {
return playerOne;
2013-04-12 19:32:25 -07:00
}
public void setPlayerOneProfile(PlayerProfile playerProfile) {
this.playerOne = playerProfile;
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
public PlayerProfile getPlayerTwoProfile() {
return playerTwo;
2013-04-12 19:32:25 -07:00
}
public void setPlayerTwoProfile(PlayerProfile playerProfile) {
this.playerTwo = playerProfile;
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
}
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
public int getNrOfGames() {
return games;
}
2013-06-23 18:33:35 -07:00
private static MagicDeckProfile getMagicPlayerProfile(final String colorText) {
2013-04-12 19:32:25 -07:00
if (ANY_DECK.equals(colorText)) {
return new MagicDeckProfile("");
2013-04-12 19:32:25 -07:00
} else if (ANY_THREE.equals(colorText)) {
return new MagicDeckProfile(MagicColor.getRandomColors(3));
2013-04-12 19:32:25 -07:00
} else if (ANY_TWO.equals(colorText)) {
return new MagicDeckProfile(MagicColor.getRandomColors(2));
2013-04-12 19:32:25 -07:00
} else if (ANY_ONE.equals(colorText)) {
return new MagicDeckProfile(MagicColor.getRandomColors(1));
2013-04-12 19:32:25 -07:00
} else if (DeckGenerators.getInstance().getGeneratorNames().contains(colorText)) {
// custom deck generator
return new MagicDeckProfile("", colorText);
2013-04-12 19:32:25 -07:00
}
return new MagicDeckProfile(colorText);
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
public MagicDeckProfile getMagicPlayerProfile() {
return getMagicPlayerProfile(playerOneDeckGenerator);
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
public String getPlayerOneDeckGenerator() {
return playerOneDeckGenerator;
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
public void setPlayerOneDeckGenerator(final String deckGenerator) {
playerOneDeckGenerator = deckGenerator;
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
public String getPlayerTwoDeckGenerator() {
return playerTwoDeckGenerator;
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
public void setPlayerTwoDeckGenerator(final String deckGenerator) {
playerTwoDeckGenerator = deckGenerator;
2013-04-12 19:32:25 -07:00
}
2013-06-23 18:33:35 -07:00
public MagicDeckProfile getMagicOpponentProfile() {
return getMagicPlayerProfile(playerTwoDeckGenerator);
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 String getCube() {
return cube;
}
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
public void setCube(final String cube) {
this.cube=cube;
}
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
public MagicAI[] getPlayerAIs() {
final MagicAI playerAI;
if (playerTwo instanceof AiPlayer) {
final AiPlayer aiPlayer = (AiPlayer)playerTwo;
playerAI = aiPlayer.getAiType().getAI();
} else {
playerAI = MagicAIImpl.MMAB.getAI();
}
2013-04-12 19:32:25 -07:00
return new MagicAI[]{playerAI, playerAI};
}
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
public void load(final Properties properties) {
setPlayerOneProfile(PlayerProfile.getHumanPlayer(properties.getProperty("HumanPlayer")));
setPlayerTwoProfile(PlayerProfile.getAiPlayer(properties.getProperty("AiPlayer")));
2013-04-12 19:32:25 -07:00
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)));
playerOneDeckGenerator=properties.getProperty(PLAYER,playerOneDeckGenerator);
playerTwoDeckGenerator=properties.getProperty(OPPONENT,playerTwoDeckGenerator);
2013-04-12 19:32:25 -07:00
cube=properties.getProperty(CUBE,cube);
}
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) {
properties.setProperty("HumanPlayer", getPlayerOneProfile().getId());
properties.setProperty("AiPlayer", getPlayerTwoProfile().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));
properties.setProperty(PLAYER, playerOneDeckGenerator);
properties.setProperty(OPPONENT, playerTwoDeckGenerator);
properties.setProperty(CUBE, cube);
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=new Properties();
save(properties);
try { //save config
2013-06-23 18:33:35 -07:00
FileIO.toFile(getConfigFile(), properties, "Duel configuration");
2013-04-12 19:32:25 -07:00
System.err.println("Saved duel config");
} catch (final IOException ex) {
System.err.println("ERROR! Unable to save duel config");
}
}
2013-06-23 18:33:35 -07:00
2013-04-12 19:32:25 -07:00
private static File getConfigFile() {
return new File(MagicMain.getSavedDuelsPath(), CONFIG_FILENAME);
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;
}
/**
* Dependent on AI type, but approximately the maximum number of
* seconds the AI has to to make a decision.
* <p>
* Currently, this a user-adjustable setting from 1 to 8 seconds.
*/
public int getAiDifficulty() {
if (playerTwo instanceof AiPlayer) {
final AiPlayer player = (AiPlayer)playerTwo;
return player.getAiLevel();
} else {
return 6;
}
}
public int getAiExtraLife() {
if (playerTwo instanceof AiPlayer) {
final AiPlayer aiPlayer = (AiPlayer)playerTwo;
return aiPlayer.getExtraLife();
} else {
return 0;
}
}
2013-04-12 19:32:25 -07:00
}