From 2b9c7ce7e2a08464e412be2841f00f6524cef437 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 7 May 2014 17:09:45 +0200 Subject: Reimplement Inventory (ItemList) --- src/esieequest/controller/commands/DoCommand.java | 2 +- .../controller/commands/DropCommand.java | 11 +- .../controller/commands/InventoryCommand.java | 2 +- .../controller/commands/TakeCommand.java | 8 +- src/esieequest/controller/commands/UseCommand.java | 4 +- .../controller/utils/SerialisableList.java | 27 +++ src/esieequest/model/Game.java | 2 +- src/esieequest/model/Player.java | 132 +------------ src/esieequest/model/doors/LockedDoor.java | 2 +- src/esieequest/model/items/Inventory.java | 209 +++++++++++++++++++++ src/esieequest/model/items/Item.java | 4 +- src/esieequest/model/map/Room.java | 9 +- src/esieequest/model/map/Side.java | 52 ++--- src/net/pacien/util/IntrinsicMap.java | 10 +- src/net/pacien/util/Mappable.java | 4 +- 15 files changed, 283 insertions(+), 195 deletions(-) create mode 100644 src/esieequest/controller/utils/SerialisableList.java create mode 100644 src/esieequest/model/items/Inventory.java diff --git a/src/esieequest/controller/commands/DoCommand.java b/src/esieequest/controller/commands/DoCommand.java index 219dba9..95269de 100644 --- a/src/esieequest/controller/commands/DoCommand.java +++ b/src/esieequest/controller/commands/DoCommand.java @@ -23,7 +23,7 @@ public class DoCommand implements Executable { return; } - if (currentSide.hasItem()) { + if (currentSide.getInventory().getSize() == 1) { Command.TAKE.execute(argument, game, view); return; } diff --git a/src/esieequest/controller/commands/DropCommand.java b/src/esieequest/controller/commands/DropCommand.java index 8d2b820..09b2394 100644 --- a/src/esieequest/controller/commands/DropCommand.java +++ b/src/esieequest/controller/commands/DropCommand.java @@ -23,18 +23,13 @@ public class DropCommand implements Executable { return; } - if (!game.getPlayer().hasItem(itemName)) { + if (!game.getPlayer().getInventory().hasItem(itemName)) { view.echo(Text.NO_SUCH_ITEM.getText()); return; } - if (game.getPlayer().getCurrentSide().hasItem()) { - view.echo(Text.ROOM_INVENTORY_FULL.getText()); - return; - } - - final Item item = game.getPlayer().takeItem(itemName); - game.getPlayer().getCurrentSide().putItem(item); + final Item item = game.getPlayer().getInventory().takeItem(itemName); + game.getPlayer().getCurrentSide().getInventory().putItem(item); } diff --git a/src/esieequest/controller/commands/InventoryCommand.java b/src/esieequest/controller/commands/InventoryCommand.java index 0f95ead..a6ce080 100644 --- a/src/esieequest/controller/commands/InventoryCommand.java +++ b/src/esieequest/controller/commands/InventoryCommand.java @@ -13,7 +13,7 @@ public class InventoryCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - view.echo(game.getPlayer().listItemsNamesString() + game.getPlayer().getWeightString()); + view.echo(game.getPlayer().getInventory().listItemsNamesString() + game.getPlayer().getInventory().getWeightString()); } diff --git a/src/esieequest/controller/commands/TakeCommand.java b/src/esieequest/controller/commands/TakeCommand.java index 16327e4..8b8a461 100644 --- a/src/esieequest/controller/commands/TakeCommand.java +++ b/src/esieequest/controller/commands/TakeCommand.java @@ -29,23 +29,23 @@ public class TakeCommand implements Executable { item = currentRoom.getItem(itemName); } else { final Side currentSide = game.getPlayer().getCurrentSide(); - if (!currentSide.hasItem()) { + if (currentSide.getInventory().getSize() != 1) { view.echo(Text.NO_ITEM.getText()); return; } - item = currentSide.getItem(); + item = currentSide.getInventory().getItem(); } // handle inventory weight limit final int maximumWeight = game.getPlayer().getInventoryWeightLimit(); - final int futureWeight = game.getPlayer().getItemsWeight() + item.getWeight(); + final int futureWeight = game.getPlayer().getInventory().getTotalWeight() + item.getWeight(); if (futureWeight > maximumWeight) { view.echo(Text.INVENTORY_FULL.getText()); return; } game.getPlayer().getCurrentRoom().removeItem(item); - game.getPlayer().addItem(item); + game.getPlayer().getInventory().putItem(item); } diff --git a/src/esieequest/controller/commands/UseCommand.java b/src/esieequest/controller/commands/UseCommand.java index 56eaf00..00fc234 100644 --- a/src/esieequest/controller/commands/UseCommand.java +++ b/src/esieequest/controller/commands/UseCommand.java @@ -22,12 +22,12 @@ public class UseCommand implements Executable { return; } - if (!game.getPlayer().hasItem(itemName)) { + if (!game.getPlayer().getInventory().hasItem(itemName)) { view.echo(Text.NO_SUCH_ITEM.getText()); return; } - final Item item = game.getPlayer().getItem(itemName); + final Item item = game.getPlayer().getInventory().getItem(itemName); item.use(game, view); } diff --git a/src/esieequest/controller/utils/SerialisableList.java b/src/esieequest/controller/utils/SerialisableList.java new file mode 100644 index 0000000..8e2cb78 --- /dev/null +++ b/src/esieequest/controller/utils/SerialisableList.java @@ -0,0 +1,27 @@ +package esieequest.controller.utils; + +import org.json.simple.JSONArray; + +/** + * Represents an Object that can be serialised and deserialised. + * + * @author Pacien TRAN-GIRARD + */ +public interface SerialisableList { + + /** + * Serialises to a JSONObject. + * + * @return the JSONObject + */ + public JSONArray serialise(); + + /** + * Deserialises from a JSONObject. + * + * @param o + * the JSONObject + */ + public void deserialise(final JSONArray o); + +} diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 05fdcd2..bdcc698 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -178,7 +178,7 @@ public class Game implements SerialisableObject { * the Item */ private void i(final Room room, final Direction direction, final Item item) { - room.getSide(direction).setItem(item); + room.getSide(direction).getInventory().putItem(item); } /** diff --git a/src/esieequest/model/Player.java b/src/esieequest/model/Player.java index 68930e2..a0066dc 100644 --- a/src/esieequest/model/Player.java +++ b/src/esieequest/model/Player.java @@ -1,20 +1,17 @@ package esieequest.model; -import java.util.ArrayList; import java.util.Stack; import lombok.Getter; import lombok.Setter; import net.pacien.util.CleanJSONObject; -import net.pacien.util.IntrinsicMap; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import esieequest.controller.utils.EnumUtils; -import esieequest.controller.utils.ListUtils; import esieequest.controller.utils.SerialisableObject; -import esieequest.model.items.Item; +import esieequest.model.items.Inventory; import esieequest.model.map.Direction; import esieequest.model.map.Room; import esieequest.model.map.Side; @@ -40,8 +37,9 @@ public class Player implements SerialisableObject { @Setter private Direction currentDirection; - private static final String ITEMS_LABEL = "I"; - private final IntrinsicMap items; + private static final String INVENTORY_LABEL = "I"; + @Getter + private final Inventory inventory; private static final String INVENTORY_WEIGHT_LIMIT_LABEL = "W"; @Getter @@ -70,7 +68,7 @@ public class Player implements SerialisableObject { this.currentRoom = currentRoom; this.previousRooms = new Stack<>(); this.currentDirection = currentDirection; - this.items = new IntrinsicMap<>(); + this.inventory = new Inventory(); this.inventoryWeightLimit = carryWeightLimit; this.nbSteps = 0; this.nbStepsLimit = nbStepsLimit; @@ -103,80 +101,6 @@ public class Player implements SerialisableObject { this.previousRooms.clear(); } - /** - * Tells if the Player have a given Item. - * - * @param item - * the Item - * - * @return if the Player have the Item - */ - public boolean hasItem(final Item item) { - return this.items.containsValue(item); - } - - /** - * Tells if the Player have an Item referred by its name. - * - * @param itemName - * the name of the Item - * - * @return if the Player have the Item - */ - public boolean hasItem(final String itemName) { - return this.items.containsKey(itemName); - } - - /** - * @return the sum of all the Player's Item-s - */ - public int getItemsWeight() { - int totalWeight = 0; - for (final Item item : this.items.values()) { - totalWeight += item.getWeight(); - } - return totalWeight; - } - - /** - * Adds an Item to the Player's inventory. - * - * @param item - * the Item to add - */ - public void addItem(final Item item) { - this.items.put(item); - } - - /** - * Removes an Item from the Player's inventory. - * - * @param item - * the Item to remove - */ - public void removeItem(final Item item) { - this.items.remove(item); - } - - /** - * Removes an Item, referred by its name, from the Player's inventory. - * - * @param itemName - * the name of the Item to remove - */ - public void removeItem(final String itemName) { - this.removeItem(this.getItem(itemName)); - } - - /** - * Lists all the Player's Item-s names in a String. - * - * @return the list of the Item-s in a String - */ - public String listItemsNamesString() { - return ListUtils.listToString(new ArrayList(this.items.keySet()), Text.INVENTORY_PREFIX.getText(), Text.ITEMS_NO_ITEM.getText(), Text.INVENTORY_SUFFIX.getText()); - } - /** * @return the current Side where the Player is */ @@ -184,33 +108,6 @@ public class Player implements SerialisableObject { return this.currentRoom.getSide(this.currentDirection); } - /** - * Retrieves an Item, referred by its name, from the Player's inventory. - * - * @param itemName - * the name of the Item - * - * @return the Item - */ - public Item getItem(final String itemName) { - return this.items.get(itemName); - } - - /** - * Retrieves and removes an Item, referred by its name, from the Player's - * inventory. - * - * @param itemName - * the name of the Item - * - * @return the Item - */ - public Item takeItem(final String itemName) { - final Item item = this.getItem(itemName); - this.removeItem(item); - return item; - } - /** * Increments the step counter. * @@ -221,15 +118,6 @@ public class Player implements SerialisableObject { return this.nbSteps == this.nbStepsLimit; } - /** - * Returns informations about the Player's inventory weight. - * - * @return informations about the Player's inventory weight - */ - public String getWeightString() { - return Text.INVENTORY_WEIGHT_PREFIX.getText() + this.getItemsWeight() + Text.INVENTORY_WEIGHT_SUFFIX.getText(); - } - @Override public JSONObject serialise() { final CleanJSONObject o = new CleanJSONObject(); @@ -239,7 +127,7 @@ public class Player implements SerialisableObject { o.put(Player.CURRENT_DIRECTION_LABEL, this.currentDirection.name()); - o.put(Player.ITEMS_LABEL, EnumUtils.nameAll(this.items.values().toArray(new Item[0]))); + o.put(Player.INVENTORY_LABEL, this.inventory.serialise()); o.put(Player.INVENTORY_WEIGHT_LIMIT_LABEL, this.inventoryWeightLimit); o.put(Player.NB_STEPS_LABEL, this.nbSteps); @@ -261,10 +149,10 @@ public class Player implements SerialisableObject { this.currentDirection = Direction.valueOf((String) o.get(Player.CURRENT_DIRECTION_LABEL)); - this.items.clear(); - final Object itemsObject = o.get(Player.ITEMS_LABEL); - if (itemsObject != null) { - this.items.putAll(EnumUtils.valuesOf(Item.class, ((JSONArray) itemsObject).toArray(new String[0]))); + this.inventory.empty(); + final Object inventoryObject = o.get(Player.INVENTORY_LABEL); + if (inventoryObject != null) { + this.inventory.deserialise((JSONArray) inventoryObject); } this.inventoryWeightLimit = ((Long) o.get(Player.INVENTORY_WEIGHT_LIMIT_LABEL)).intValue(); diff --git a/src/esieequest/model/doors/LockedDoor.java b/src/esieequest/model/doors/LockedDoor.java index 20a453e..868623a 100644 --- a/src/esieequest/model/doors/LockedDoor.java +++ b/src/esieequest/model/doors/LockedDoor.java @@ -48,7 +48,7 @@ public class LockedDoor extends Door { * @return if the Player is authorised to pass */ private boolean isAuthorised(final Player player) { - return player.hasItem(this.key); + return player.getInventory().hasItem(this.key); } } diff --git a/src/esieequest/model/items/Inventory.java b/src/esieequest/model/items/Inventory.java new file mode 100644 index 0000000..68d1838 --- /dev/null +++ b/src/esieequest/model/items/Inventory.java @@ -0,0 +1,209 @@ +package esieequest.model.items; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + +import net.pacien.util.IntrinsicMap; + +import org.json.simple.JSONArray; + +import esieequest.controller.utils.EnumUtils; +import esieequest.controller.utils.ListUtils; +import esieequest.controller.utils.SerialisableList; +import esieequest.model.Text; + +/** + * Symbolises an inventory (ItemList) containing multiple Item-s. + * + * @author Pacien TRAN-GIRARD + * @author BenoƮt LUBRANO DI SBARAGLIONE + */ +public class Inventory implements SerialisableList { + + private final IntrinsicMap items; + + /** + * Creates an empty Inventory. + */ + public Inventory() { + this.items = new IntrinsicMap<>(); + } + + /** + * Tells if the Inventory contains an Item referred by its name. + * + * @param itemName + * the name of the Item + * + * @return true if the Player have the Item + */ + public boolean hasItem(final String itemName) { + return this.items.containsKey(itemName); + } + + /** + * Tells if the Inventory contains a given Item. + * + * @param item + * the Item + * + * @return true if the Player have the Item + */ + public boolean hasItem(final Item item) { + return this.items.containsValue(item); + } + + /** + * Returns the total weight of the Inventory. + * + * @return the sum of all the Item-s + */ + public int getTotalWeight() { + int totalWeight = 0; + for (final Item item : this.items.values()) { + totalWeight += item.getWeight(); + } + return totalWeight; + } + + /** + * Puts an Item in the Inventory. + * + * @param item + * the Item + */ + public void putItem(final Item item) { + this.items.put(item); + } + + /** + * Removes an Item, referred by its name, from the inventory. + * + * @param itemName + * the name of the Item to remove + */ + public void removeItem(final String itemName) { + this.items.remove(itemName); + } + + /** + * Removes an Item from the inventory. + * + * @param item + * the Item to remove + */ + public void removeItem(final Item item) { + this.items.values().remove(item); + } + + /** + * Removes all items from the Inventory. + */ + public void empty() { + this.items.clear(); + } + + /** + * Lists all the Item-s names in a String. + * + * @return the list of the Item-s in a String + */ + public String listItemsNamesString() { + return ListUtils.listToString(new ArrayList(this.items.keySet()), Text.INVENTORY_PREFIX.getText(), Text.ITEMS_NO_ITEM.getText(), Text.INVENTORY_SUFFIX.getText()); + } + + /** + * Retrieves an Item, referred by its name, from the inventory. + * + * @param itemName + * the name of the Item + * + * @return the Item + */ + public Item getItem(final String itemName) { + return this.items.get(itemName); + } + + /** + * Retrieves one Item from the Inventory. + * + * @return an item + */ + public Item getItem() { + for (final Item item : this.items.values()) { + return item; + } + return null; + } + + /** + * Retrieves and removes an Item, referred by its name, from the inventory. + * + * @param itemName + * the name of the Item + * + * @return the Item + */ + public Item takeItem(final String itemName) { + final Item item = this.getItem(itemName); + this.removeItem(item); + return item; + } + + /** + * Retrieves and removes one Item from the Inventory. + * + * @return the item + */ + public Item takeItem() { + final Item item = this.getItem(); + this.removeItem(item); + return item; + } + + /** + * Lists all Item-s in the Inventory. + * + * @return the list of the Item-s + */ + public Collection listItems() { + return this.items.values(); + } + + /** + * Returns informations about the inventory weight. + * + * @return informations about the inventory weight + */ + public String getWeightString() { + return Text.INVENTORY_WEIGHT_PREFIX.getText() + this.getTotalWeight() + Text.INVENTORY_WEIGHT_SUFFIX.getText(); + } + + /** + * Returns the number of Item-s contained in the Inventory. + * + * @return the size of the Inventory + */ + public int getSize() { + return this.items.size(); + } + + @Override + public JSONArray serialise() { + final JSONArray a = new JSONArray(); + + a.addAll(Arrays.asList(EnumUtils.nameAll(this.items.values().toArray(new Item[0])))); + + return a; + } + + @Override + public void deserialise(final JSONArray a) { + this.items.clear(); + if (a != null) { + this.items.putAll(EnumUtils.valuesOf(Item.class, a.toArray(new String[0]))); + } + } + +} diff --git a/src/esieequest/model/items/Item.java b/src/esieequest/model/items/Item.java index 70ef265..be70c2e 100644 --- a/src/esieequest/model/items/Item.java +++ b/src/esieequest/model/items/Item.java @@ -10,7 +10,7 @@ import esieequest.controller.utils.SerialisableObject; import esieequest.model.Game; import esieequest.view.Viewable; -public enum Item implements Mappable, SerialisableObject { +public enum Item implements Mappable, SerialisableObject { // @formatter:off @@ -72,7 +72,7 @@ public enum Item implements Mappable, SerialisableObject { } @Override - public Object getKey() { + public String getKey() { return this.item.getName().toLowerCase(); } diff --git a/src/esieequest/model/map/Room.java b/src/esieequest/model/map/Room.java index cfebc34..e335a92 100644 --- a/src/esieequest/model/map/Room.java +++ b/src/esieequest/model/map/Room.java @@ -142,9 +142,7 @@ public enum Room implements SerialisableObject { final ArrayList itemsList = new ArrayList<>(); for (final Side side : this.sides.values()) { - if (side.hasItem()) { - itemsList.add(side.getItem()); - } + itemsList.addAll(side.getInventory().listItems()); } return itemsList; @@ -256,10 +254,7 @@ public enum Room implements SerialisableObject { */ public void removeItem(final Item item) { for (final Side side : this.sides.values()) { - if (side.getItem() == item) { - side.removeItem(); - return; - } + side.getInventory().removeItem(item); } } diff --git a/src/esieequest/model/map/Side.java b/src/esieequest/model/map/Side.java index 9b3a489..2fde183 100644 --- a/src/esieequest/model/map/Side.java +++ b/src/esieequest/model/map/Side.java @@ -4,13 +4,14 @@ import lombok.Getter; import lombok.Setter; import net.pacien.util.CleanJSONObject; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; import esieequest.controller.utils.EnumUtils; import esieequest.controller.utils.SerialisableObject; import esieequest.model.characters.Character; import esieequest.model.doors.Door; -import esieequest.model.items.Item; +import esieequest.model.items.Inventory; /** * Represents the Side of a Room which can have a Door, an Item and a Character. @@ -23,24 +24,23 @@ public class Side implements SerialisableObject { @Setter private Door door; - private static final String ITEM_LABEL = "I"; + private static final String INVENTORY_LABEL = "I"; @Getter - @Setter - private Item item; + private final Inventory inventory; private static final String CHARACTER_LABEL = "C"; @Getter @Setter private Character character; - private Side(final Door door, final Item item, final Character character) { + private Side(final Door door, final Character character) { this.door = door; - this.item = item; + this.inventory = new Inventory(); this.character = character; } public Side() { - this(null, null, null); + this(null, null); } /** @@ -50,13 +50,6 @@ public class Side implements SerialisableObject { return this.door != null; } - /** - * @return if the Side has an Item - */ - public boolean hasItem() { - return this.item != null; - } - /** * @return if the Side has a Character */ @@ -64,32 +57,12 @@ public class Side implements SerialisableObject { return this.character != null; } - /** - * Removes the Item from the Side. - */ - public void removeItem() { - this.item = null; - } - - /** - * Puts the given Item in the Side. Does nothing if the Side has already an - * Item. - * - * @param item - * the Item to put - */ - public Item putItem(final Item item) { - final Item previousItem = this.item; - this.item = item; - return previousItem; - } - @Override public JSONObject serialise() { final CleanJSONObject o = new CleanJSONObject(); - if (this.item != null) { - o.put(Side.ITEM_LABEL, this.item.name()); + if (this.inventory != null) { + o.put(Side.INVENTORY_LABEL, this.inventory.serialise()); } if (this.character != null) { o.put(Side.CHARACTER_LABEL, this.character.name()); @@ -100,7 +73,12 @@ public class Side implements SerialisableObject { @Override public void deserialise(final JSONObject o) { - this.item = EnumUtils.valueOf(Item.class, (String) o.get(Side.ITEM_LABEL)); + this.inventory.empty(); + final Object inventoryObject = o.get(Side.INVENTORY_LABEL); + if (inventoryObject != null) { + this.inventory.deserialise((JSONArray) inventoryObject); + } + this.character = EnumUtils.valueOf(Character.class, (String) o.get(Side.CHARACTER_LABEL)); } diff --git a/src/net/pacien/util/IntrinsicMap.java b/src/net/pacien/util/IntrinsicMap.java index fe59ac2..c09965d 100644 --- a/src/net/pacien/util/IntrinsicMap.java +++ b/src/net/pacien/util/IntrinsicMap.java @@ -1,6 +1,3 @@ -/** - * - */ package net.pacien.util; import java.util.Collection; @@ -11,12 +8,12 @@ import java.util.Map; * @author pacien * */ -public class IntrinsicMap extends HashMap { +public class IntrinsicMap> extends HashMap { /** * */ - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 804991277001317260L; /** * @@ -47,9 +44,8 @@ public class IntrinsicMap extends HashMap { super(initialCapacity, loadFactor); } - @SuppressWarnings("unchecked") public V put(final V value) { - return super.put((K) value.getKey(), value); + return super.put(value.getKey(), value); } @Override diff --git a/src/net/pacien/util/Mappable.java b/src/net/pacien/util/Mappable.java index b5bbf06..804a275 100644 --- a/src/net/pacien/util/Mappable.java +++ b/src/net/pacien/util/Mappable.java @@ -1,7 +1,7 @@ package net.pacien.util; -public interface Mappable { +public interface Mappable { - public Object getKey(); + public K getKey(); } -- cgit v1.2.3