001package fr.aumgn.dac2.game;
002
003import java.util.HashMap;
004import java.util.Map;
005
006
007import fr.aumgn.dac2.DAC;
008import fr.aumgn.dac2.exceptions.UnknownGameType;
009import fr.aumgn.dac2.game.classic.ClassicGame;
010import fr.aumgn.dac2.game.colonnisation.Colonnisation;
011import fr.aumgn.dac2.game.start.GameStartData;
012import fr.aumgn.dac2.game.training.Training;
013
014/**
015 * Manages all different game modes
016 */
017public abstract class GameFactory {
018
019    private static Map<String, GameFactory> factories =
020            new HashMap<String, GameFactory>();
021    private static Map<String, GameFactory> byAliases =
022            new HashMap<String, GameFactory>();
023
024    /**
025     * Registers a GameFactory with the given name & aliases.
026     */
027    public static void register(String name, GameFactory factory,
028            String... aliases) {
029        factories.put(name, factory);
030
031        byAliases.put(name, factory);
032        for (String alias : aliases) {
033            byAliases.put(alias, factory);
034        }
035    }
036
037    public static GameFactory get(String name) {
038        return factories.get(name);
039    }
040
041    /**
042     * Gets the game factory registered for the given alias.
043     * 
044     * @throws UnknownGameType if the game factory can't be found.
045     */
046    public static GameFactory getByAlias(DAC dac, String alias) {
047        if (!byAliases.containsKey(alias)) {
048            throw new UnknownGameType(dac, alias);
049        }
050        return byAliases.get(alias);
051    }
052
053    /**
054     * The minimum required number of player to start
055     * a new game for this factory.
056     */
057    public int getMinimumPlayers() {
058        return 2;
059    }
060
061    /**
062     * Factory method which creates the game based on the given data.
063     */
064    public abstract Game createGame(DAC dac, GameStartData data);
065
066    static {
067        GameFactory.register("classic", new GameFactory() {
068
069            @Override
070            public Game createGame(DAC dac, GameStartData data) {
071                return new ClassicGame(dac, data);
072            }
073        }, "cl", "default", "def");
074
075        GameFactory.register("training", new GameFactory() {
076
077            @Override
078            public int getMinimumPlayers() {
079                return 1;
080            }
081
082            @Override
083            public Game createGame(DAC dac, GameStartData data) {
084                return new Training(dac, data);
085            }
086        }, "tr", "t");
087
088        GameFactory.register("colonnisation", new GameFactory() {
089
090            @Override
091            public Game createGame(DAC dac, GameStartData data) {
092                return new Colonnisation(dac, data);
093            }
094        }, "col", "c");
095    }
096}