OSDN Git Service

Initial commit.
[gamerandomizer/gamerandomizer.git] / GameRandomizer / src / jp / sourceforge / gamerandomizerlib / SimpleRandomizerCommand.java
1 //
2 // Copyright (c) 2013  Motoyuki Kasahara
3 //
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.
8 //
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.
13 //
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/>.
16 //
17 package jp.sourceforge.gamerandomizerlib;
18
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;
25
26 public class SimpleRandomizerCommand {
27         enum OperationMode {
28                 RANDOMIZE, PRINT_GAMES, PRINT_EXPANSIONS, PRINT_CARDS, 
29         }
30
31         //
32         // Main.
33         // Usage: command [-d DIR] [-c|-g|-x] [-E] GAME [EXPANSION...]
34         //
35         public static void main (String[] args) {
36                 OperationMode operationMode = OperationMode.RANDOMIZE;
37                 CardComparisonMode comparisonMode
38                         = CardComparisonMode.PRICE_THEN_EXPANSION;
39
40                 ArrayList<String> argsList
41                         = new ArrayList<String>(Arrays.asList(args));
42                 String path = ".";
43
44                 //
45                 // Parse arguments.
46                 //
47                 while (!argsList.isEmpty()) {
48                         String arg = argsList.get(0);
49                         if (arg.equals("--")) {
50                                 argsList.remove(0);
51                                 break;
52                         } else if (arg.equals("-") || !arg.startsWith("-")) {
53                                 break;
54                         }
55                         if (arg.equals("-c"))
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'");
62                                         System.exit(1);
63                                 }
64                                 path = argsList.get(1);
65                                 argsList.remove(0);
66                         } else if (arg.equals("-g")) {
67                                 operationMode = OperationMode.PRINT_GAMES;
68                         } else if (arg.equals("-x")) {
69                                 operationMode = OperationMode.PRINT_EXPANSIONS;
70                         } else {
71                                 System.err.println(String.format("unknown option '%s'",
72                                         argsList.get(0)));
73                                 System.exit(1);
74                         }
75                         argsList.remove(0);
76                 }
77                 if (argsList.isEmpty() && operationMode != OperationMode.PRINT_GAMES) {
78                         System.err.println("no game ID specified");
79                         System.exit(1);
80                 }
81
82                 //
83                 // Load data of games.
84                 //
85                 List<Game> games = loadGames(path);
86                 if (games == null)
87                         System.exit(1);
88
89                 if (operationMode == OperationMode.PRINT_GAMES) {
90                         printGames(games);
91                         System.exit(0);
92                 }
93
94                 //
95                 // Choose game.
96                 //
97                 String gameId = argsList.get(0);
98                 argsList.remove(0);
99                 Game gameBase = selectGame(games, gameId);
100                 if (gameBase == null) {
101                         System.err.println("unknown game: " + gameId);
102                         System.exit(1);
103                 }
104
105                 //
106                 // Choose expansions.
107                 //
108                 Game game = selectExpansions(gameBase, argsList);
109                 if (game == null)
110                         System.exit(1);
111
112                 if (operationMode == OperationMode.PRINT_EXPANSIONS) {
113                         printExpansions(game.getExpansions());
114                         System.exit(0);
115                 } else if (operationMode == OperationMode.PRINT_CARDS) {
116                         printCards(game.getCards(), comparisonMode);
117                         System.exit(0);
118                 }
119
120                 List<Card> cards = selectCards(game);
121                 if (cards == null)
122                         System.exit(1);
123
124                 printCards(cards, comparisonMode);
125         }
126
127         //
128         // Load data of games.
129         //
130         private static List<Game> loadGames(String path) {
131                 List<Game> games = null;
132                 try {            
133                         GameBuilder builder = new GameBuilderXMLFile(path);
134                         games = builder.buildAll();
135                 } catch (GameBuilderException e) {
136                         System.err.println(e.getMessage());
137                         return null;
138                 }
139                 if (games.size() == 0) {
140                         System.err.println("no game registered");
141                         return null;
142                 }
143                 return games;
144         }
145
146         //
147         // Select a game to be used.
148         //
149         private static Game selectGame(List<Game> games, String id) {
150                 ListIterator<Game> it = games.listIterator();
151                 while (it.hasNext()) {
152                         Game g = it.next();
153                         if (g.getId().equals(id))
154                                 return g;
155                 }
156                 return null;
157         }
158
159         //
160         // Select expansions to be used.
161         //
162         private static Game selectExpansions(Game gameBase,
163                 List<String> ids) {
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);
171                                 if (x == null) {
172                                         System.err.println("unknown expansion: " + id);
173                                         return null;
174                                 }
175                                 xl.add(x);
176                         }
177                         game.removeOtherExpansions(xl);
178                 }
179
180                 return game;
181         }
182
183         //
184         // Select cards randomly.
185         //
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");
191                 return cards;
192         }
193
194         //
195         // Print games.
196         //
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);
202                 }
203         }
204
205         //
206         // Print expansions.
207         //
208         private static void printExpansions(List<Expansion> expansions) {
209                 for (Expansion x : expansions) {
210                         String msg = String.format("%-20s  %-30s", x.getId(),
211                                 x.getTitle());
212                         System.out.println(msg);
213                 }
214         }
215
216         //
217         // Print cards.
218         //
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));
222
223                 for (Card c : cl) {
224                         System.out.println(c.getTitle());
225                         System.out.println(String.format("    %-5s  %-30s",
226                                 c.getPrice(), c.getExpansion().getTitle()));
227                 }
228         }
229 }