2 // Copyright (c) 2013 Motoyuki Kasahara
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 2 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package jp.sourceforge.gamerandomizerlib;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.ListIterator;
26 public class SimpleRandomizerCommand {
28 RANDOMIZE, PRINT_GAMES, PRINT_EXPANSIONS, PRINT_CARDS,
33 // Usage: command [-d DIR] [-c|-g|-x] [-E] GAME [EXPANSION...]
35 public static void main (String[] args) {
36 OperationMode operationMode = OperationMode.RANDOMIZE;
37 CardComparisonMode comparisonMode
38 = CardComparisonMode.PRICE_THEN_EXPANSION;
40 ArrayList<String> argsList
41 = new ArrayList<String>(Arrays.asList(args));
47 while (!argsList.isEmpty()) {
48 String arg = argsList.get(0);
49 if (arg.equals("--")) {
52 } else if (arg.equals("-") || !arg.startsWith("-")) {
56 operationMode = OperationMode.PRINT_CARDS;
57 else if (arg.equals("-E"))
58 comparisonMode = CardComparisonMode.EXPANSION_THEN_PRICE;
59 else if (arg.equals("-d")) {
60 if (argsList.size() <= 1) {
61 System.err.println("missing argument to '-d'");
64 path = argsList.get(1);
66 } else if (arg.equals("-g")) {
67 operationMode = OperationMode.PRINT_GAMES;
68 } else if (arg.equals("-x")) {
69 operationMode = OperationMode.PRINT_EXPANSIONS;
71 System.err.println(String.format("unknown option '%s'",
77 if (argsList.isEmpty() && operationMode != OperationMode.PRINT_GAMES) {
78 System.err.println("no game ID specified");
83 // Load data of games.
85 List<Game> games = loadGames(path);
89 if (operationMode == OperationMode.PRINT_GAMES) {
97 String gameId = argsList.get(0);
99 Game gameBase = selectGame(games, gameId);
100 if (gameBase == null) {
101 System.err.println("unknown game: " + gameId);
106 // Choose expansions.
108 Game game = selectExpansions(gameBase, argsList);
112 if (operationMode == OperationMode.PRINT_EXPANSIONS) {
113 printExpansions(game.getExpansions());
115 } else if (operationMode == OperationMode.PRINT_CARDS) {
116 printCards(game.getCards(), comparisonMode);
120 List<Card> cards = selectCards(game);
124 printCards(cards, comparisonMode);
128 // Load data of games.
130 private static List<Game> loadGames(String path) {
131 List<Game> games = null;
133 GameBuilder builder = new GameBuilderXMLFile(path);
134 games = builder.buildAll();
135 } catch (GameBuilderException e) {
136 System.err.println(e.getMessage());
139 if (games.size() == 0) {
140 System.err.println("no game registered");
147 // Select a game to be used.
149 private static Game selectGame(List<Game> games, String id) {
150 ListIterator<Game> it = games.listIterator();
151 while (it.hasNext()) {
153 if (g.getId().equals(id))
160 // Select expansions to be used.
162 private static Game selectExpansions(Game gameBase,
164 Game game = new Game(gameBase);
165 if (!ids.isEmpty()) {
166 List<Expansion> xl = new ArrayList<Expansion>();
167 ListIterator<String> it = ids.listIterator();
168 while (it.hasNext()) {
169 String id = it.next();
170 Expansion x = game.getExpansion(id);
172 System.err.println("unknown expansion: " + id);
177 game.removeOtherExpansions(xl);
184 // Select cards randomly.
186 private static List<Card> selectCards(Game game) {
187 Randomizer rnd = new Randomizer(game);
188 List<Card> cards = new LinkedList<Card>();
189 if (rnd.select(cards) < game.getSelectionSize())
190 System.err.println("warning: not enough cards registered");
197 private static void printGames(List<Game> games) {
198 for (Game g : games) {
199 String msg = String.format("%-20s %-30s (%d)",
200 g.getId(), g.getTitle(), g.getSelectionSize());
201 System.out.println(msg);
208 private static void printExpansions(List<Expansion> expansions) {
209 for (Expansion x : expansions) {
210 String msg = String.format("%-20s %-30s", x.getId(),
212 System.out.println(msg);
219 private static void printCards(List<Card> cards, CardComparisonMode mode) {
220 List<Card> cl = new ArrayList<Card>(cards);
221 Collections.sort(cl, new CardComparator(mode));
224 System.out.println(c.getTitle());
225 System.out.println(String.format(" %-5s %-30s",
226 c.getPrice(), c.getExpansion().getTitle()));