aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/cli/CardbaseCLI.java
blob: 7a2e581933043f92f81ab78097502a16e29c082a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package eu.equalparts.cardbase.cli;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import eu.equalparts.cardbase.Cardbase;
import eu.equalparts.cardbase.data.Card;
import eu.equalparts.cardbase.data.FullCardSet;
import eu.equalparts.cardbase.data.CardSetInformation;
import eu.equalparts.cardbase.utils.MTGUniverse;

/**
 * This provides a lightweight CLI for interacting with cardbase files. 
 * 
 * @author Eduardo Pedroni
 */
public class CardbaseCLI {
	
	/**
	 * Enum type to store actions.
	 * 
	 * @author Eduardo Pedroni
	 */
	private enum Action {
		ADD, REMOVE;
		public Card card;
		public Integer count;
		/**
		 * Sets both fields at once.
		 * 
		 * @param card the card last modified.
		 * @param count the amount that was added or removed.
		 */
		public void set(Card card, Integer count) {
			this.card = card;
			this.count = count;
		}
	}

	/**
	 * Location of the help file.
	 */
	private static final String HELP_FILE_PATH = "/help";
	/**
	 * The last action performed by the user.
	 */
	private Action lastAction = null;
	/**
	 * The currently selected set, from which new cards are added.
	 */
	private FullCardSet selectedSet = null;
	/**
	 * The actual cardbase being interfaced with.
	 */
	private Cardbase cardbase;
	/**
	 * Printed to the console when the user enters the help command.
	 */
	private String help = "Not available, check project page on GitHub.";
	/**
	 * The cardbase file off which we are currently working, if any.
	 */
	private File cardbaseFile = null;
	/**
	 * Save flag is raised when cards are added or removed and causes a prompt to be shown
	 * if the user tries to exit with unsaved changed.
	 */
	private boolean savePrompt = false;
	/**
	 * Exit flag, program breaks out of the main loop when true.
	 */
	private boolean exit = false;

	/**
	 * Execute the interface.
	 * 
	 * @param args the first argument is the cardbase file. Further arguments are ignored.
	 */
	public static void main(String... args) {
		new CardbaseCLI(args).startInterface();
	}

	/**
	 * Reads in an optional cardbase JSON and initialises other necessary components.
	 * This does not actually produce the CLI, for that use {@code startInterface()}
	 * on the constructed object.
	 * 
	 * @param args a list of arguments. Only the first argument is used, as a cardbase JSON.
	 */
	private CardbaseCLI(String... args) {
		System.out.println("Welcome to Cardbase CLI!");

		// set debug flag if we are debugging
		if (Cardbase.DEBUG) System.out.println("Debug mode is on.");

		// make the CardbaseManager
		if (args.length > 0) {
			File cardbaseFile = new File(args[0]);
			if (cardbaseFile.exists() && cardbaseFile.isFile() && cardbaseFile.canRead()) {
				System.out.println("Loading cardbase from \"" + args[0] + "\".");
				try {
					cardbase = new Cardbase(cardbaseFile);
				} catch (JsonParseException e) {
					System.out.println("Error: poorly formatted cardbase, check the syntax and try again.");
					// although the problem could also be with the upstream CardSetList json.
					if (Cardbase.DEBUG) e.printStackTrace();
					System.exit(1);
				} catch (JsonMappingException e) {
					System.out.println("Error: unexpected fields found in cardbase, it may be from an old version?");
					if (Cardbase.DEBUG) e.printStackTrace();
					System.exit(1);
				} catch (IOException e) {
					System.out.println("Error: something went wrong reading cardbase file, abort...");
					if (Cardbase.DEBUG) e.printStackTrace();
					System.exit(1);
				}
			} else {
				System.out.println(args[0] + " appears to be invalid.");
				System.exit(0);
			}
		} else {
			System.out.println("No cardbase file was provided, creating a clean cardbase.");
			cardbase = new Cardbase();
		}

		// load help information
		InputStream is = CardbaseCLI.class.getResourceAsStream(HELP_FILE_PATH);
		if (is != null) {
			help = new Scanner(is).useDelimiter("\\Z").next();
		} else {
			System.out.println("Help file was not found, check the project page on GitHub for help instead.");
		}

	}

	/**
	 * Read stdin for user input, sanitise and interpret any commands entered.
	 */
	private void startInterface() {
		BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
		try {
			// the main loop
			while (!exit) {
				// print prompt
				System.out.print(selectedSet == null ? "> " : selectedSet.code + " > ");
				// condition input and interpret
				String[] raw = consoleReader.readLine().trim().split("[ \t]+");
				String command = raw[0];
				String[] args = Arrays.copyOfRange(raw, 1, raw.length);

				if (command.equalsIgnoreCase("help")) {
					help();
				} else if (command.equalsIgnoreCase("write")
						|| command.equalsIgnoreCase("save")) {
					write(args);
				} else if (command.equalsIgnoreCase("exit")) {
					exit();
				} else if (command.equalsIgnoreCase("sets")) {
					sets();
				} else if (command.equalsIgnoreCase("set")) {
					set(args);
				} else if (command.equalsIgnoreCase("glance")) {
					glance();
				} else if (command.equalsIgnoreCase("peruse")) {
					peruse(args);
				} else if (command.equalsIgnoreCase("undo")) {
					undo();
				} else if (command.equalsIgnoreCase("remove") 
						|| command.equalsIgnoreCase("rm")) {
					remove(args);
				} else {
					add(command, args);
				}
			}
		} catch (IOException e) {
			System.out.println("Error: something went wrong with stdin, exiting...");
			if (Cardbase.DEBUG) e.printStackTrace();
		}
	}

	/**
	 * Print help to console.
	 */
	public void help() {
		System.out.println(help);
	}

	/**
	 * Write current cardbase to file.
	 *
	 * @param args optionally the file to which to write.
	 */
	public void write(String[] args) {
		File outputFile;
		// user-provided file overrides everything else
		if (args.length > 0) {
			outputFile = new File(sanitiseFileName(args[0]));
		} else {
			outputFile = cardbaseFile;
		}

		if (outputFile != null) {
			if (outputFile.exists() && (!outputFile.isFile() || !outputFile.canWrite())) {
				System.out.println("Could not write to \"" + outputFile.getAbsolutePath() + "\".");
			} else {
				// handle these exceptions locally - they don't necessarily mean the program should exit
				try {
					cardbase.writeCardbase(outputFile);
					// we are now working off outputFile, which may or may not be the same as cardbaseFile at this point
					cardbaseFile = outputFile;
					System.out.println("Cardbase was saved to \"" + outputFile.getAbsolutePath() + "\". "
							+ "Subsequent writes will be done to this same file unless otherwise requested.");
					savePrompt = false;
				} catch (JsonGenerationException | JsonMappingException e) {
					System.out.println("Error: something terrible happened to the internal cardbase data structure. Oops.");
					if (Cardbase.DEBUG) e.printStackTrace();
				} catch (IOException e) {
					System.out.println("Error: lost contact with the output file, try again?");
					if (Cardbase.DEBUG) e.printStackTrace();
				}
			}
		} else {
			System.out.println("Please provide a file name.");
		}
	}

	/**
	 * Exit procedure.
	 */
	public void exit() {
		if (savePrompt) {
			System.out.println("Don't forget to save. If you really wish to quit without saving, type \"exit\" again.");
			savePrompt = false;
		} else {
			exit = true;
		}
	}

	/**
	 * Print a list of valid set codes.
	 */
	public void sets() {
		for (CardSetInformation set : MTGUniverse.getCardSetList()) {
			// CardSet has an overridden toString()
			System.out.println(set);
		}
	}

	/**
	 * Select a set.
	 * 
	 * @param args the code of the chosen set.
	 */
	public void set(String[] args) {
		if (args.length > 0) {
			try {
				selectedSet = MTGUniverse.getFullCardSet(args[0]);
				// if the set code is invalid, null is returned
				if (selectedSet != null) {
					System.out.println("Selected set: " + selectedSet.name + ".");
					// undoing is not allowed if the set is changed - it would get tricky
					lastAction = null;
				} else {
					System.out.println("\"" + args[0] + "\" does not correspond to any set (use \"sets\" to see all valid set codes).");
				}
			} catch (JsonParseException e) {
				System.out.println("Error: JSON fetched from upstream was not formatted properly.");
				if (Cardbase.DEBUG) e.printStackTrace();
			} catch (JsonMappingException e) {
				System.out.println("Error: JSON fetched from upstream does not match the data structure used internally.");
				if (Cardbase.DEBUG) e.printStackTrace();
			} catch (IOException e) {
				System.out.println("Error: JSON could not be fetched from upstream.");
				if (Cardbase.DEBUG) e.printStackTrace();
			}
		} else {
			System.out.println("Please enter a set code (use \"sets\" to see all valid set codes).");
		}
	}

	/**
	 * Print a brief list of the whole cardbase.
	 */
	public void glance() {
		int total = 0;
		for (Card card : cardbase.getCards()) {
			printGlance(card);
			total += card.count;
		}
		System.out.println("Total: " + total);
	}

	/**
	 * Print detailed information of a single card or the whole cardbase.
	 *
	 * @param args optionally a card within the set (by number) to peruse.
	 */
	public void peruse(String[] args) {
		// if a card is specified, peruse only that
		if (args.length > 0) {
			if (selectedSet != null) {
				Card card = cardbase.getCard(selectedSet.code, args[0]);
				if (card != null) {
					printPerusal(card);
				} else {
					System.out.println("Card not in cardbase.");
				}
			} else {
				System.out.println("Please select a set before perusing a specific card.");
			}
		} else {
			// peruse all cards in cardbase
			int total = 0;
			for (Card card : cardbase.getCards()) {
				printPerusal(card);
				total += card.count;
			}
			System.out.println("Total: " + total);
		}
	}

	/**
	 * Undo previous action.
	 */
	public void undo() {
		if (lastAction != null) {
			if (lastAction == Action.ADD) {
				removeCard(lastAction.card, lastAction.count);
			} else if (lastAction ==  Action.REMOVE) {
				addCard(lastAction.card, lastAction.count);
			}
			// can only undo once
			lastAction = null;
		} else {
			System.out.println("Nothing to undo.");
		}
	}

	/**
	 * Remove one or more of a card.
	 *
	 * @param args the set number of the card to remove and optionally the count to be removed.
	 */
	public void remove(String[] args) {
		if (selectedSet != null) {
			if (args.length > 0) {
				Card cardToRemove = selectedSet.getCardByNumber(args[0]);
				if (cardToRemove != null) {
					Integer count = 1;
					if (args.length > 1 && args[1].matches("[0-9]+")) {
						count = Integer.valueOf(args[1]);
						if (count <= 0) {
							System.out.println("Can't remove " + count + " cards.");
							return;
						}
					}
					removeCard(cardToRemove, count);
				} else {
					System.out.println(args[0] + " does not correspond to a card in " + selectedSet.name + ".");
				}
			} else {
				System.out.println("Please specify a card number to remove.");
			}
		} else {
			System.out.println("Select a set before removing cards.");
		}
	}

	/**
	 * Add one or more of a card.
	 *
	 * @param number the number of the card to add.
	 * @param args optionally the count to add.
	 */
	public void add(String number, String[] args) {
		if (selectedSet != null) {
			// a blank line after adding a card repeats the addition unlimitedly
			if (number.isEmpty()) {
				if (lastAction == Action.ADD)
					addCard(lastAction.card, lastAction.count);
			} else {
				Card cardToAdd = selectedSet.getCardByNumber(number);
				if (cardToAdd != null) {
					Integer count = 1;
					if (args.length > 0 && args[0].matches("[0-9]+")) {
						count = Integer.valueOf(args[0]);
						if (count <= 0) {
							System.out.println("Can't add " + count + " cards.");
							return;
						}
					}
					addCard(cardToAdd, count);
				} else {
					System.out.println(number + " does not correspond to a card in " + selectedSet.name + ".");
				}
			}
		} else {
			System.out.println("Select a set before adding cards.");
		}
	}
	
	/**
	 * Add the specified count of the specified card
	 * to the cardbase.
	 * 
	 * @param card the card to add.
	 * @param count the number of times to add it.
	 */
	private void addCard(Card card, Integer count) {
		cardbase.addCard(card, count);
		System.out.println("Added " + count + "x " + card.name + ".");
		savePrompt = true;
		lastAction = Action.ADD;
		lastAction.set(card, count);
	}

	/**
	 * Remove the specified count of the specified card
	 * from the cardbase.
	 * 
	 * @param card the card to remove.
	 * @param count the number of times to remove it.
	 */
	private void removeCard(Card card, Integer count) {
		Integer removed = cardbase.removeCard(card, count); 
		if (removed > 0) {
			System.out.println("Removed " + removed + "x " + card.name + ".");
			savePrompt = true;
			lastAction = Action.REMOVE;
			lastAction.set(card, removed);
		} else {
			System.out.println(card.name + " is not in the cardbase.");
		}

	}

	/**
	 * Return a {@code String} that is guaranteed to be a legal file name.
	 * 
	 * @param name the file name candidate to sanitise.
	 * @return the sanitised name.
	 */
	private String sanitiseFileName(String name) {
		// POSIX-compliant valid filename characters
		name = name.replaceAll("[^-_.A-Za-z0-9]", "");
		// extension is not indispensable, but good practice
		if (!name.endsWith(".cb")) {
			name = name.concat(".cb");
		}
		return name;
	}

	/**
	 * Prints a glance of the specified card. A glance contains simply
	 * the card count, name, set code and set number. 
	 * 
	 * @param card the card to glance.
	 */
	private void printGlance(Card card) {
		System.out.println(String.format("%1$-4d %2$s (%3$s, %4$s)", card.count, card.name, card.setCode, card.number));
	}

	/**
	 * Prints a perusal of the specified card. A perusal contains more
	 * information than a glance, but not every single field the card has
	 * as that would be too verbose while adding little value.
	 * 
	 * @param card the card to peruse.
	 */
	private void printPerusal(Card card) {
		printGlance(card);
		if (card.type != null) System.out.println("\t" + card.type);
		if (card.manaCost != null) System.out.println("\tCost: " + card.manaCost);
		if (card.power != null && card.toughness != null) System.out.println("\t" + card.power + "/" + card.toughness);
		if (card.loyalty != null) System.out.println("\tLoyalty: " + card.loyalty);

		if (card.text != null) System.out.println("\t" + card.text.replaceAll("\n", "\n\t"));
		if (card.flavor != null) System.out.println("\t" + card.flavor.replaceAll("\n", "\n\t"));

		if (card.rarity != null) System.out.println("\t" + card.rarity);
		if (card.multiverseid != null) System.out.println("\tMID: " + card.multiverseid);
		if (card.artist != null) System.out.println("\tIllus. " + card.artist);
	}
}