From aa45dc28cee40f811b1af5ffb7452a0ddbb05d3b Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 28 May 2014 21:29:11 +0200 Subject: Refactor commands --- .../controller/commands/AleaCommand.java | 2 +- .../controller/commands/BackCommand.java | 6 ++++ src/esieequest/controller/commands/Command.java | 4 +-- src/esieequest/controller/commands/DoCommand.java | 5 +++ .../controller/commands/DropCommand.java | 2 +- src/esieequest/controller/commands/Executable.java | 2 +- .../controller/commands/ForwardCommand.java | 24 --------------- src/esieequest/controller/commands/GoCommand.java | 26 ++++++++++------ .../controller/commands/HelpCommand.java | 6 ++++ .../controller/commands/InventoryCommand.java | 6 ++++ .../controller/commands/LoadCommand.java | 5 +-- .../controller/commands/LookCommand.java | 5 +++ src/esieequest/controller/commands/NewCommand.java | 6 +--- .../controller/commands/QuitCommand.java | 5 +++ .../controller/commands/SaveCommand.java | 10 +++--- .../controller/commands/TakeCommand.java | 11 ++++++- .../controller/commands/TalkCommand.java | 28 ++++++++++++++--- .../controller/commands/ToggleSoundCommand.java | 6 ++++ .../controller/commands/TurnCommand.java | 11 +++++-- src/esieequest/controller/commands/UseCommand.java | 2 +- src/esieequest/model/Text.java | 7 +++++ src/esieequest/model/map/Room.java | 36 +++++++++++++++++++--- src/esieequest/view/app/UserInterface.java | 2 +- src/esieequest/view/web/WebInterface.java | 2 +- 24 files changed, 155 insertions(+), 64 deletions(-) delete mode 100644 src/esieequest/controller/commands/ForwardCommand.java (limited to 'src') diff --git a/src/esieequest/controller/commands/AleaCommand.java b/src/esieequest/controller/commands/AleaCommand.java index ff73537..093f088 100644 --- a/src/esieequest/controller/commands/AleaCommand.java +++ b/src/esieequest/controller/commands/AleaCommand.java @@ -20,7 +20,7 @@ public class AleaCommand implements Executable { final String forcedRoomName = argument; Room destinationRoom = null; - if (forcedRoomName != null) { + if (!forcedRoomName.isEmpty()) { try { destinationRoom = Room.valueOf(forcedRoomName.toUpperCase()); } catch (final Exception exception) { diff --git a/src/esieequest/controller/commands/BackCommand.java b/src/esieequest/controller/commands/BackCommand.java index d7ed8a3..b2322ed 100644 --- a/src/esieequest/controller/commands/BackCommand.java +++ b/src/esieequest/controller/commands/BackCommand.java @@ -1,6 +1,7 @@ package esieequest.controller.commands; import esieequest.model.Game; +import esieequest.model.Text; import esieequest.view.Viewable; /** @@ -13,6 +14,11 @@ public class BackCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { + if (!argument.isEmpty()) { + view.echo(Text.TOO_MANY_ARGUMENTS.toString()); + return; + } + game.getPlayer().goToPreviousRoom(); view.updateLocation(game.getPlayer().getCurrentRoom(), game.getPlayer() diff --git a/src/esieequest/controller/commands/Command.java b/src/esieequest/controller/commands/Command.java index 829ebf8..651d3da 100644 --- a/src/esieequest/controller/commands/Command.java +++ b/src/esieequest/controller/commands/Command.java @@ -28,7 +28,6 @@ public enum Command { // map related GO(new GoCommand()), - FORWARD(new ForwardCommand()), BACK(new BackCommand()), TURN(new TurnCommand()), LOOK(new LookCommand()), @@ -70,7 +69,8 @@ public enum Command { * the View */ public void execute(final String argument, final Game game, final Viewable view) { - this.command.execute(argument, game, view); + final String arg = argument == null ? "" : argument; + this.command.execute(arg, game, view); } /** diff --git a/src/esieequest/controller/commands/DoCommand.java b/src/esieequest/controller/commands/DoCommand.java index 646ac2a..cb596e8 100644 --- a/src/esieequest/controller/commands/DoCommand.java +++ b/src/esieequest/controller/commands/DoCommand.java @@ -16,6 +16,11 @@ public class DoCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { + if (!argument.isEmpty()) { + view.echo(Text.TOO_MANY_ARGUMENTS.toString()); + return; + } + final Side currentSide = game.getPlayer().getCurrentSide(); if (currentSide.hasCharacter()) { diff --git a/src/esieequest/controller/commands/DropCommand.java b/src/esieequest/controller/commands/DropCommand.java index dbdf03c..6ed2b81 100644 --- a/src/esieequest/controller/commands/DropCommand.java +++ b/src/esieequest/controller/commands/DropCommand.java @@ -18,7 +18,7 @@ public class DropCommand implements Executable { final String itemName = argument; - if (itemName == null) { + if (itemName.isEmpty()) { view.echo(Text.NO_ITEM_SPECIFIED.toString()); return; } diff --git a/src/esieequest/controller/commands/Executable.java b/src/esieequest/controller/commands/Executable.java index 156fc1c..ab84b78 100644 --- a/src/esieequest/controller/commands/Executable.java +++ b/src/esieequest/controller/commands/Executable.java @@ -14,7 +14,7 @@ public interface Executable { * Performs the task corresponding to the Command. * * @param argument - * the argument to pass to the Command + * the argument to pass to the Command (null if empty String) * @param game * the Game model * @param view diff --git a/src/esieequest/controller/commands/ForwardCommand.java b/src/esieequest/controller/commands/ForwardCommand.java deleted file mode 100644 index 9134fdc..0000000 --- a/src/esieequest/controller/commands/ForwardCommand.java +++ /dev/null @@ -1,24 +0,0 @@ -package esieequest.controller.commands; - -import esieequest.model.Game; -import esieequest.model.map.Direction; -import esieequest.view.Viewable; - -/** - * Allows the user to move forward in the Map (in the Room at the Direction he - * is facing). - * - * @author Pacien TRAN-GIRARD - */ -public class ForwardCommand implements Executable { - - @Override - public void execute(final String argument, final Game game, final Viewable view) { - - final Direction direction = game.getPlayer().getCurrentDirection(); - - Command.GO.execute(direction.name(), game, view); - - } - -} diff --git a/src/esieequest/controller/commands/GoCommand.java b/src/esieequest/controller/commands/GoCommand.java index a68f5ad..4bcd0e1 100644 --- a/src/esieequest/controller/commands/GoCommand.java +++ b/src/esieequest/controller/commands/GoCommand.java @@ -17,22 +17,30 @@ public class GoCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - final Direction direction; + Direction direction; - try { - direction = Direction.valueOf(argument.toUpperCase()); - } catch (final Exception exception) { - view.echo(Text.NO_SUCH_DIRECTION.toString()); - return; - } + if (!argument.isEmpty()) { - final Door door = game.getPlayer().getCurrentRoom().getSide(direction).getDoor(); + try { + direction = Direction.valueOf(argument.toUpperCase()); + } catch (final IllegalArgumentException e) { + view.echo(Text.NO_SUCH_DIRECTION.toString()); + return; + } + + } else { + + direction = game.getPlayer().getCurrentDirection(); - if (door == null) { + } + + if (!game.getPlayer().getCurrentRoom().getSide(direction).hasDoor()) { view.echo(Text.NO_DOOR.toString()); return; } + final Door door = game.getPlayer().getCurrentRoom().getSide(direction).getDoor(); + final boolean crossed = door.cross(game, view); if (!crossed) { diff --git a/src/esieequest/controller/commands/HelpCommand.java b/src/esieequest/controller/commands/HelpCommand.java index 4c4c39f..1c3c59b 100644 --- a/src/esieequest/controller/commands/HelpCommand.java +++ b/src/esieequest/controller/commands/HelpCommand.java @@ -1,6 +1,7 @@ package esieequest.controller.commands; import esieequest.model.Game; +import esieequest.model.Text; import esieequest.view.Viewable; /** @@ -13,6 +14,11 @@ public class HelpCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { + if (!argument.isEmpty()) { + view.echo(Text.TOO_MANY_ARGUMENTS.toString()); + return; + } + view.echo(Command.listCommandsNamesString()); } diff --git a/src/esieequest/controller/commands/InventoryCommand.java b/src/esieequest/controller/commands/InventoryCommand.java index 27936ce..6b5a5b1 100644 --- a/src/esieequest/controller/commands/InventoryCommand.java +++ b/src/esieequest/controller/commands/InventoryCommand.java @@ -1,6 +1,7 @@ package esieequest.controller.commands; import esieequest.model.Game; +import esieequest.model.Text; import esieequest.view.Viewable; /** @@ -13,6 +14,11 @@ public class InventoryCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { + if (!argument.isEmpty()) { + view.echo(Text.TOO_MANY_ARGUMENTS.toString()); + return; + } + view.echo(game.getPlayer().getInventory().listItemsNamesString() + game.getPlayer().getInventory().getWeightString()); diff --git a/src/esieequest/controller/commands/LoadCommand.java b/src/esieequest/controller/commands/LoadCommand.java index c15e6ba..65dab15 100644 --- a/src/esieequest/controller/commands/LoadCommand.java +++ b/src/esieequest/controller/commands/LoadCommand.java @@ -18,7 +18,8 @@ public class LoadCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - if (argument == null) { + if (argument.isEmpty()) { + view.echo(Text.MISSING_ARGUMENT.toString()); return; } @@ -41,7 +42,7 @@ public class LoadCommand implements Executable { try { game.newGame(false, false); game.deserialise(datas); - } catch (final Exception e) { + } catch (final IllegalArgumentException | ClassCastException | NullPointerException e) { view.echo(Text.DESERIALISING_ERROR_PREFIX.toString() + e.getMessage() + Text.DESERIALISING_ERROR_SUFFIX.toString()); e.printStackTrace(); diff --git a/src/esieequest/controller/commands/LookCommand.java b/src/esieequest/controller/commands/LookCommand.java index 305934f..d2650da 100644 --- a/src/esieequest/controller/commands/LookCommand.java +++ b/src/esieequest/controller/commands/LookCommand.java @@ -18,6 +18,11 @@ public class LookCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { + if (!argument.isEmpty()) { + view.echo(Text.TOO_MANY_ARGUMENTS.toString()); + return; + } + final ArrayList informations = new ArrayList(); informations.add(game.getPlayer().getCurrentRoom().getInformations()); diff --git a/src/esieequest/controller/commands/NewCommand.java b/src/esieequest/controller/commands/NewCommand.java index 8967820..50659c9 100644 --- a/src/esieequest/controller/commands/NewCommand.java +++ b/src/esieequest/controller/commands/NewCommand.java @@ -16,11 +16,7 @@ public class NewCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - boolean challengeMode = false; - - if (argument != null) { - challengeMode = argument.equals("challenge"); - } + final boolean challengeMode = argument.equals(Text.CHALLENGE_FLAG.toString()); game.newGame(true, challengeMode); diff --git a/src/esieequest/controller/commands/QuitCommand.java b/src/esieequest/controller/commands/QuitCommand.java index 024b35f..ebcd630 100644 --- a/src/esieequest/controller/commands/QuitCommand.java +++ b/src/esieequest/controller/commands/QuitCommand.java @@ -14,6 +14,11 @@ public class QuitCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { + if (!argument.isEmpty()) { + view.echo(Text.TOO_MANY_ARGUMENTS.toString()); + return; + } + view.echo(Text.QUIT.toString()); view.disableInput(); view.stopMusic(); diff --git a/src/esieequest/controller/commands/SaveCommand.java b/src/esieequest/controller/commands/SaveCommand.java index 3332059..95466e6 100644 --- a/src/esieequest/controller/commands/SaveCommand.java +++ b/src/esieequest/controller/commands/SaveCommand.java @@ -1,6 +1,7 @@ package esieequest.controller.commands; import esieequest.model.Game; +import esieequest.model.Text; import esieequest.view.Viewable; /** @@ -13,16 +14,15 @@ public class SaveCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - if (argument == null) { - return; - } + final String filePath = argument; - if (argument.isEmpty()) { + if (filePath.isEmpty()) { + view.echo(Text.FILE_PATH_NOT_SPECIFIED.toString()); return; } final String serialisedGame = game.serialise().toJSONString(); - view.save(argument, serialisedGame); + view.save(filePath, serialisedGame); } diff --git a/src/esieequest/controller/commands/TakeCommand.java b/src/esieequest/controller/commands/TakeCommand.java index 9f70f98..cd3f6c7 100644 --- a/src/esieequest/controller/commands/TakeCommand.java +++ b/src/esieequest/controller/commands/TakeCommand.java @@ -19,21 +19,30 @@ public class TakeCommand implements Executable { public void execute(final String argument, final Game game, final Viewable view) { Item item; - if (argument != null) { + + if (!argument.isEmpty()) { + final String itemName = argument; final Room currentRoom = game.getPlayer().getCurrentRoom(); + if (!currentRoom.hasItem(itemName)) { view.echo(Text.NO_SUCH_ITEM.toString()); return; } + item = currentRoom.getItem(itemName); + } else { + final Side currentSide = game.getPlayer().getCurrentSide(); + if (currentSide.getInventory().getSize() != 1) { view.echo(Text.NO_ITEM.toString()); return; } + item = currentSide.getInventory().getItem(); + } // handle inventory weight limit diff --git a/src/esieequest/controller/commands/TalkCommand.java b/src/esieequest/controller/commands/TalkCommand.java index 39c4932..19c9eae 100644 --- a/src/esieequest/controller/commands/TalkCommand.java +++ b/src/esieequest/controller/commands/TalkCommand.java @@ -2,6 +2,7 @@ package esieequest.controller.commands; import esieequest.model.Game; import esieequest.model.Text; +import esieequest.model.characters.Character; import esieequest.view.Viewable; /** @@ -14,12 +15,31 @@ public class TalkCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - if (!game.getPlayer().getCurrentSide().hasCharacter()) { - view.echo(Text.NO_CHARACTER.toString()); - return; + Character character = null; + + if (!argument.isEmpty()) { + + final String characterName = argument.toLowerCase(); + + if (!game.getPlayer().getCurrentRoom().hasCharacter(characterName)) { + view.echo(Text.NO_SUCH_CHARACTER.toString()); + return; + } + + character = game.getPlayer().getCurrentRoom().getCharacter(characterName); + + } else { + + if (!game.getPlayer().getCurrentSide().hasCharacter()) { + view.echo(Text.NO_CHARACTER.toString()); + return; + } + + character = game.getPlayer().getCurrentSide().getCharacter(); + } - game.getPlayer().getCurrentSide().getCharacter().talk(game, view); + character.talk(game, view); } diff --git a/src/esieequest/controller/commands/ToggleSoundCommand.java b/src/esieequest/controller/commands/ToggleSoundCommand.java index 0d8a62b..4cf0f7d 100644 --- a/src/esieequest/controller/commands/ToggleSoundCommand.java +++ b/src/esieequest/controller/commands/ToggleSoundCommand.java @@ -1,6 +1,7 @@ package esieequest.controller.commands; import esieequest.model.Game; +import esieequest.model.Text; import esieequest.view.Viewable; /** @@ -13,6 +14,11 @@ public class ToggleSoundCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { + if (!argument.isEmpty()) { + view.echo(Text.TOO_MANY_ARGUMENTS.toString()); + return; + } + view.toggleSound(); } diff --git a/src/esieequest/controller/commands/TurnCommand.java b/src/esieequest/controller/commands/TurnCommand.java index 722221f..0a597c9 100644 --- a/src/esieequest/controller/commands/TurnCommand.java +++ b/src/esieequest/controller/commands/TurnCommand.java @@ -17,11 +17,18 @@ public class TurnCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { + final String direction = argument.toUpperCase(); + + if (direction.isEmpty()) { + view.echo(Text.NO_DIRECTION_SPECIFIED.toString()); + return; + } + Orientation orientation; try { - orientation = Orientation.valueOf(argument.toUpperCase()); - } catch (final Exception exception) { + orientation = Orientation.valueOf(direction); + } catch (final IllegalArgumentException e) { view.echo(Text.NO_SUCH_DIRECTION.toString()); return; } diff --git a/src/esieequest/controller/commands/UseCommand.java b/src/esieequest/controller/commands/UseCommand.java index c90bb8f..c5317e8 100644 --- a/src/esieequest/controller/commands/UseCommand.java +++ b/src/esieequest/controller/commands/UseCommand.java @@ -17,7 +17,7 @@ public class UseCommand implements Executable { final String itemName = argument.toLowerCase(); - if (itemName == null) { + if (itemName.isEmpty()) { view.echo(Text.NO_ITEM_SPECIFIED.toString()); return; } diff --git a/src/esieequest/model/Text.java b/src/esieequest/model/Text.java index 4e489e5..666d6f1 100644 --- a/src/esieequest/model/Text.java +++ b/src/esieequest/model/Text.java @@ -10,6 +10,8 @@ public enum Text { // @formatter:off + CHALLENGE_FLAG("challenge"), + // interface DEFAULT_CONSOLE("Want to start a new game? Click on the 'New' button at the top left corner." + "\nMove your cursor over the buttons to know what they do." @@ -106,10 +108,15 @@ public enum Text { // errors UNKNOWN_COMMAND("Unknown command."), + TOO_MANY_ARGUMENTS("Too many arguments."), + MISSING_ARGUMENT("Missing argument."), + FILE_PATH_NOT_SPECIFIED("File path not specified."), NO_SUCH_EXIT("No such exit."), NO_SUCH_DIRECTION("No such direction."), + NO_DIRECTION_SPECIFIED("No direction specified."), NO_SUCH_ROOM("No such room."), NO_CHARACTER("No character."), + NO_SUCH_CHARACTER("No such character."), NO_ITEM("No item."), NO_ITEM_SPECIFIED("No item specified."), NO_SUCH_ITEM("No such item."), diff --git a/src/esieequest/model/map/Room.java b/src/esieequest/model/map/Room.java index e95b851..6d23163 100644 --- a/src/esieequest/model/map/Room.java +++ b/src/esieequest/model/map/Room.java @@ -166,6 +166,34 @@ public enum Room implements SerialisableObject { return charactersList; } + /** + * @param characterName + * the name of the Character to get (case insensitive) + * + * @return the Character + */ + public Character getCharacter(final String characterName) { + for (final Character character : this.listCharacters()) { + if (character.getName().toLowerCase().equals(characterName.toLowerCase())) { + return character; + } + } + + return null; + } + + /** + * Tells if the Side-s of the Room contain a Character referred by its name. + * + * @param characterName + * the name of the Character (case insensitive) + * + * @return if such Character was found + */ + public boolean hasCharacter(final String characterName) { + return this.getCharacter(characterName) != null; + } + /** * @return the list of all the Room's exits' Direction-s */ @@ -229,7 +257,7 @@ public enum Room implements SerialisableObject { * Retrieves an item referred by its name from the Side-s of the Room. * * @param itemName - * the name of the Item + * the name of the Item (case insensitive) * * @return the Item */ @@ -246,7 +274,7 @@ public enum Room implements SerialisableObject { * Tells if the Side-s of the Room contain an Item referred by its name. * * @param itemName - * the name of the Item + * the name of the Item (case insensitive) * * @return if such Item was found */ @@ -270,7 +298,7 @@ public enum Room implements SerialisableObject { * Removes an Item referred by its name from the Side-s of the Room. * * @param itemName - * the name of the Item to remove + * the name of the Item to remove (case insensitive) */ public void removeItem(final String itemName) { this.removeItem(this.getItem(itemName)); @@ -281,7 +309,7 @@ public enum Room implements SerialisableObject { * Room. * * @param itemName - * the name of the Item + * the name of the Item (case insensitive) * * @return the Item */ diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index f2b97f8..5910d2d 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -235,7 +235,7 @@ abstract class UserInterface implements Viewable, ActionListener { this.forwardButton = new JButton(Text.GO_FORWARD_BUTTON.toString()); this.forwardButton.setToolTipText(Text.GO_FORWARD_TOOLTIP.toString()); - this.forwardButton.setActionCommand(Command.FORWARD.name()); + this.forwardButton.setActionCommand(Command.GO.name()); this.forwardButton.setFont(font); this.forwardButton.setPreferredSize(squareButton); this.topControlPanel.add(this.forwardButton, BorderLayout.CENTER); diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 4366bff..3f722dc 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -249,7 +249,7 @@ class WebInterface extends Composite implements Viewable { } }); this.forwardButton.setTitle(Text.GO_FORWARD_TOOLTIP.toString()); - this.forwardButton.addClickHandler(this.makeClickHandler(Command.FORWARD.name())); + this.forwardButton.addClickHandler(this.makeClickHandler(Command.GO.name())); this.backButton.setTitle(Text.GO_BACK_TOOLTIP.toString()); this.backButton.addClickHandler(this.makeClickHandler(Command.BACK.name())); -- cgit v1.2.3