From 688634ae5a5aaf663159032e67d2132ea61c5d5f Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 4 May 2014 17:37:41 +0200 Subject: Implement "save" and "load" --- src/esieequest/Main.java | 13 +- src/esieequest/controller/GameEngine.java | 28 +- src/esieequest/controller/Utils.java | 43 -- src/esieequest/controller/commands/Command.java | 4 +- src/esieequest/controller/commands/GoCommand.java | 2 +- .../controller/commands/LoadCommand.java | 29 +- src/esieequest/controller/commands/NewCommand.java | 8 +- .../controller/commands/SaveCommand.java | 3 +- .../controller/commands/TakeCommand.java | 2 +- .../controller/commands/TurnCommand.java | 2 +- src/esieequest/controller/utils/EnumUtils.java | 159 ++++++ src/esieequest/controller/utils/ListUtils.java | 43 ++ .../controller/utils/SerialisableObject.java | 26 + src/esieequest/controller/utils/package-info.java | 4 + src/esieequest/esieequest.gwt.xml | 1 + src/esieequest/model/Game.java | 81 ++- src/esieequest/model/Player.java | 77 ++- src/esieequest/model/Text.java | 9 +- src/esieequest/model/characters/Character.java | 81 ++- .../model/characters/MovingCharacter.java | 37 +- .../model/characters/SimpleCharacter.java | 52 ++ src/esieequest/model/items/Beamer.java | 21 + src/esieequest/model/items/Item.java | 26 +- src/esieequest/model/items/SimpleItem.java | 17 +- src/esieequest/model/map/Direction.java | 1 + src/esieequest/model/map/Room.java | 69 ++- src/esieequest/model/map/Side.java | 32 +- src/esieequest/view/app/UserInterface.java | 1 + src/esieequest/view/text/TextInterface.java | 2 +- src/esieequest/view/web/Main.java | 4 +- src/esieequest/view/web/WebInterface.java | 3 +- src/esieequest/view/web/WebInterface.ui.xml | 2 +- src/net/pacien/util/CleanJSONObject.java | 48 ++ src/net/pacien/util/IntrinsicMap.java | 23 +- src/org/json/simple/ItemList.java | 158 ++++++ src/org/json/simple/JSONArray.java | 390 +++++++++++++ src/org/json/simple/JSONAware.java | 14 + src/org/json/simple/JSONObject.java | 143 +++++ src/org/json/simple/JSONStreamAware.java | 18 + src/org/json/simple/JSONValue.java | 317 +++++++++++ src/org/json/simple/jsonsimple.gwt.xml | 4 + src/org/json/simple/parser/ContainerFactory.java | 26 + src/org/json/simple/parser/ContentHandler.java | 110 ++++ src/org/json/simple/parser/JSONParser.java | 544 ++++++++++++++++++ src/org/json/simple/parser/ParseException.java | 92 +++ src/org/json/simple/parser/Yylex.java | 625 +++++++++++++++++++++ src/org/json/simple/parser/Yytoken.java | 60 ++ src/rejava/io/Reader.java | 20 + src/rejava/io/StringReader.java | 51 ++ src/rejava/io/StringWriter.java | 29 + src/rejava/io/Writer.java | 16 + src/rejava/io/package-info.java | 6 + src/rejava/io/rejavaio.gwt.xml | 3 + 53 files changed, 3437 insertions(+), 142 deletions(-) delete mode 100644 src/esieequest/controller/Utils.java create mode 100644 src/esieequest/controller/utils/EnumUtils.java create mode 100644 src/esieequest/controller/utils/ListUtils.java create mode 100644 src/esieequest/controller/utils/SerialisableObject.java create mode 100644 src/esieequest/controller/utils/package-info.java create mode 100644 src/esieequest/model/characters/SimpleCharacter.java create mode 100644 src/net/pacien/util/CleanJSONObject.java create mode 100644 src/org/json/simple/ItemList.java create mode 100644 src/org/json/simple/JSONArray.java create mode 100644 src/org/json/simple/JSONAware.java create mode 100644 src/org/json/simple/JSONObject.java create mode 100644 src/org/json/simple/JSONStreamAware.java create mode 100644 src/org/json/simple/JSONValue.java create mode 100644 src/org/json/simple/jsonsimple.gwt.xml create mode 100644 src/org/json/simple/parser/ContainerFactory.java create mode 100644 src/org/json/simple/parser/ContentHandler.java create mode 100644 src/org/json/simple/parser/JSONParser.java create mode 100644 src/org/json/simple/parser/ParseException.java create mode 100644 src/org/json/simple/parser/Yylex.java create mode 100644 src/org/json/simple/parser/Yytoken.java create mode 100644 src/rejava/io/Reader.java create mode 100644 src/rejava/io/StringReader.java create mode 100644 src/rejava/io/StringWriter.java create mode 100644 src/rejava/io/Writer.java create mode 100644 src/rejava/io/package-info.java create mode 100644 src/rejava/io/rejavaio.gwt.xml (limited to 'src') diff --git a/src/esieequest/Main.java b/src/esieequest/Main.java index 6e7c0f2..f8b013f 100755 --- a/src/esieequest/Main.java +++ b/src/esieequest/Main.java @@ -6,7 +6,6 @@ import java.util.List; import javax.swing.JApplet; import esieequest.controller.GameEngine; -import esieequest.model.Game; import esieequest.view.Viewable; import esieequest.view.app.Applet; import esieequest.view.app.Window; @@ -32,9 +31,8 @@ public class Main extends JApplet { */ @Override public void init() { - final Game game = new Game(); final Applet applet = new Applet(this); - new GameEngine(game, applet); + new GameEngine(applet); } /** @@ -47,7 +45,6 @@ public class Main extends JApplet { */ public static void main(final String[] args) { final List arguments = Arrays.asList(args); - Game game; Viewable view; if (arguments.contains("--file")) { @@ -62,13 +59,7 @@ public class Main extends JApplet { view = new Window(); } - if (arguments.contains("--challenge")) { - game = new Game(true); - } else { - game = new Game(); - } - - new GameEngine(game, view); + new GameEngine(view); } } diff --git a/src/esieequest/controller/GameEngine.java b/src/esieequest/controller/GameEngine.java index c3a8725..df83384 100644 --- a/src/esieequest/controller/GameEngine.java +++ b/src/esieequest/controller/GameEngine.java @@ -25,7 +25,7 @@ public class GameEngine { * @param view * the view */ - public GameEngine(final Game game, final Viewable view) { + public GameEngine(final Viewable view, final Game game) { this.game = game; this.view = view; @@ -34,6 +34,28 @@ public class GameEngine { this.view.show(); } + /** + * Instantiates a game with the given view and mode. + * + * @param view + * the view + * @param challengeMode + * the mode + */ + public GameEngine(final Viewable view, final boolean challengeMode) { + this(view, new Game(challengeMode)); + } + + /** + * Instantiates a game with the given view. + * + * @param view + * the view + */ + public GameEngine(final Viewable view) { + this(view, false); + } + /** * Interprets a command. * @@ -41,7 +63,7 @@ public class GameEngine { * the command String */ public void interpret(final String inputString) { - final Input input = new Input(inputString.toLowerCase()); + final Input input = new Input(inputString/* .toLowerCase() */); final Command command = input.getCommand(); if (command == null) { @@ -56,7 +78,7 @@ public class GameEngine { } /** - * Actions executed every time a Command is entered. + * Performs routine actions executed every time a Command is entered. */ private void executeRoutines() { MovingCharacter.moveAll(); diff --git a/src/esieequest/controller/Utils.java b/src/esieequest/controller/Utils.java deleted file mode 100644 index d45f58c..0000000 --- a/src/esieequest/controller/Utils.java +++ /dev/null @@ -1,43 +0,0 @@ -package esieequest.controller; - -import java.util.List; - -import com.google.common.base.Joiner; - -import esieequest.model.Text; - -/** - * A set of custom utility methods. - * - * @author Pacien TRAN-GIRARD - */ -public class Utils { - - /** - * Converts a List into a String with a given prefix, suffix and - * placeholder used if the List is empty. - * - * @param list - * the List to convert - * @param prefix - * the prefix - * @param empty - * the placeholder - * @param suffix - * the suffix - * @return the converted String - */ - public static String listToString(final List list, final String prefix, final String empty, final String suffix) { - final StringBuilder str = new StringBuilder(prefix); - - if (list.isEmpty()) { - str.append(empty); - } else { - str.append(Joiner.on(Text.LIST_SEPARATOR.getText()).join(list)); - } - str.append(suffix); - - return str.toString(); - } - -} diff --git a/src/esieequest/controller/commands/Command.java b/src/esieequest/controller/commands/Command.java index 7ab6961..094b0cf 100644 --- a/src/esieequest/controller/commands/Command.java +++ b/src/esieequest/controller/commands/Command.java @@ -3,7 +3,7 @@ package esieequest.controller.commands; import java.util.ArrayList; import java.util.List; -import esieequest.controller.Utils; +import esieequest.controller.utils.ListUtils; import esieequest.model.Game; import esieequest.model.Text; import esieequest.view.Viewable; @@ -94,7 +94,7 @@ public enum Command { * @return a String listing all the names of the Command-s */ public static String listCommandsNamesString() { - return Utils.listToString(Command.listCommandsNames(), Text.HELP_PREFIX.getText(), null, Text.HELP_SUFFIX.getText()); + return ListUtils.listToString(Command.listCommandsNames(), Text.HELP_PREFIX.getText(), null, Text.HELP_SUFFIX.getText()); } } diff --git a/src/esieequest/controller/commands/GoCommand.java b/src/esieequest/controller/commands/GoCommand.java index 9ebdf49..1c528fa 100644 --- a/src/esieequest/controller/commands/GoCommand.java +++ b/src/esieequest/controller/commands/GoCommand.java @@ -40,7 +40,7 @@ public class GoCommand implements Executable { } view.updateLocation(game.getPlayer().getCurrentRoom(), direction, game.getPlayer().getCurrentSide()); - + // handle challenge mode if (game.getPlayer().walk()) { view.echo(Text.CHALLENGE_FAILED.getText()); diff --git a/src/esieequest/controller/commands/LoadCommand.java b/src/esieequest/controller/commands/LoadCommand.java index 79ccf60..807b3b3 100644 --- a/src/esieequest/controller/commands/LoadCommand.java +++ b/src/esieequest/controller/commands/LoadCommand.java @@ -1,6 +1,11 @@ package esieequest.controller.commands; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import org.json.simple.parser.ParseException; + import esieequest.model.Game; +import esieequest.model.Text; import esieequest.view.Viewable; /** @@ -12,7 +17,29 @@ public class LoadCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - // TODO Auto-generated method stub + + final String jsonString = argument; + + JSONObject datas = new JSONObject(); + try { + datas = (JSONObject) JSONValue.parseWithException(jsonString); + } catch (final ParseException e) { + view.echo(Text.PARSING_ERROR_PREFIX.getText() + e.getMessage() + Text.PARSING_ERROR_SUFFIX.getText()); + return; + } + + try { + game.newGame(false, false); + game.deserialise(datas); + } catch (final Exception e) { + view.echo(Text.DESERIALISING_ERROR_PREFIX.getText() + e.getMessage() + Text.DESERIALISING_ERROR_SUFFIX.getText()); + e.printStackTrace(); + return; + } + + // view.updateQuest(); + // view.updateInventory(game.getPlayer().getItems()); + view.updateLocation(game.getPlayer().getCurrentRoom(), game.getPlayer().getCurrentDirection(), game.getPlayer().getCurrentSide()); } diff --git a/src/esieequest/controller/commands/NewCommand.java b/src/esieequest/controller/commands/NewCommand.java index 00fe2fa..7b510e4 100644 --- a/src/esieequest/controller/commands/NewCommand.java +++ b/src/esieequest/controller/commands/NewCommand.java @@ -14,7 +14,13 @@ public class NewCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - game.newGame(); + boolean challengeMode = false; + + if (argument != null) { + challengeMode = argument.equals("challenge"); + } + + game.newGame(true, challengeMode); view.enable(); diff --git a/src/esieequest/controller/commands/SaveCommand.java b/src/esieequest/controller/commands/SaveCommand.java index 641b329..9e32762 100644 --- a/src/esieequest/controller/commands/SaveCommand.java +++ b/src/esieequest/controller/commands/SaveCommand.java @@ -12,7 +12,8 @@ public class SaveCommand implements Executable { @Override public void execute(final String argument, final Game game, final Viewable view) { - // TODO Auto-generated method stub + + view.echo(game.serialise().toJSONString()); } diff --git a/src/esieequest/controller/commands/TakeCommand.java b/src/esieequest/controller/commands/TakeCommand.java index 705fb6e..16327e4 100644 --- a/src/esieequest/controller/commands/TakeCommand.java +++ b/src/esieequest/controller/commands/TakeCommand.java @@ -37,7 +37,7 @@ public class TakeCommand implements Executable { } // handle inventory weight limit - final int maximumWeight = game.getPlayer().getCarryWeightLimit(); + final int maximumWeight = game.getPlayer().getInventoryWeightLimit(); final int futureWeight = game.getPlayer().getItemsWeight() + item.getWeight(); if (futureWeight > maximumWeight) { view.echo(Text.INVENTORY_FULL.getText()); diff --git a/src/esieequest/controller/commands/TurnCommand.java b/src/esieequest/controller/commands/TurnCommand.java index 4f6eb4c..e28ad2c 100644 --- a/src/esieequest/controller/commands/TurnCommand.java +++ b/src/esieequest/controller/commands/TurnCommand.java @@ -31,7 +31,7 @@ public class TurnCommand implements Executable { game.getPlayer().setCurrentDirection(newDirection); view.updateLocation(game.getPlayer().getCurrentRoom(), newDirection, game.getPlayer().getCurrentSide()); - + } } diff --git a/src/esieequest/controller/utils/EnumUtils.java b/src/esieequest/controller/utils/EnumUtils.java new file mode 100644 index 0000000..03a7150 --- /dev/null +++ b/src/esieequest/controller/utils/EnumUtils.java @@ -0,0 +1,159 @@ +package esieequest.controller.utils; + +import java.util.ArrayList; +import java.util.List; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +/** + * A collection of utilities to manipulate and serialise Enum-s. + * + * @author Pacien TRAN-GIRARD + */ +public class EnumUtils { + + private static final String KEY_LABEL = "K"; + private static final String VALUE_LABEL = "V"; + + /** + * Safely names an Enum value. + * + * @param enumValue + * the Enum value + * + * @return the name of the Enum value + */ + public static > String name(final E enumValue) { + if (enumValue != null) { + return enumValue.name(); + } + return null; + } + + /** + * Safely names Enum values from an array. + * + * @param enumValues + * the array of the Enum values + * + * @return the array of the Enum names, with null for missing elements + */ + public static > String[] nameAll(final E[] enumValues) { + + final ArrayList names = new ArrayList<>(); + + for (final E enumValue : enumValues) { + names.add(EnumUtils. name(enumValue)); + } + + return names.toArray(new String[0]); + + } + + /** + * Retrieves safely the Enum value of a String. + * + * @param enumType + * the Enum type + * @param name + * the name of the Enum value to retrieve + * + * @return the Enum value, or null if not found + */ + public static > E valueOf(final Class enumType, final String name) { + + if (name == null) { + return null; + } + + try { + return Enum.valueOf(enumType, name); + } catch (final Exception e) { + return null; + } + + } + + /** + * Retrieves all Enum values by their name. + * + * @param enumType + * the Enum type + * @param names + * the array of names of the Enum values to retrieve + * + * @return the array of Enum values, with null for missing elements + */ + public static > List valuesOf(final Class enumType, final String[] names) { + + final ArrayList values = new ArrayList<>(); + + if (names == null) { + return values; + } + + for (final String name : names) { + values.add(EnumUtils.valueOf(enumType, name)); + } + + return values; + + } + + /** + * Serialises all Enum objects into a JSONArray, ignoring empty objects. + * + * @param serialisableEnumObjects + * an array of SerialisableEnumObject-s + * + * @return the JSONArray of all the objects + */ + public static & SerialisableObject> JSONArray serialiseEnumObjects(final E[] serialisableEnumObjects) { + + final JSONArray jsonArray = new JSONArray(); + + for (final E serialisableEnumObject : serialisableEnumObjects) { + + final JSONObject serialisedValue = serialisableEnumObject.serialise(); + + if (!serialisedValue.isEmpty()) { + final JSONObject jsonObject = new JSONObject(); + jsonObject.put(EnumUtils.KEY_LABEL, serialisableEnumObject.name()); + jsonObject.put(EnumUtils.VALUE_LABEL, serialisedValue); + jsonArray.add(jsonObject); + } + + } + + return jsonArray; + + } + + /** + * Deserialises objects of an Enum type from a JSONArray. + * + * @param enumType + * the Enum type + * @param jsonArray + * the JSONArray + */ + public static > void deserialiseEnumObjects(final Class enumType, final JSONArray jsonArray) { + + if (jsonArray == null) { + return; + } + + for (final Object object : jsonArray) { + + final JSONObject jsonObject = (JSONObject) object; + final String key = (String) jsonObject.get(EnumUtils.KEY_LABEL); + + final SerialisableObject serialisableObject = (SerialisableObject) EnumUtils.valueOf(enumType, key); + serialisableObject.deserialise((JSONObject) jsonObject.get(EnumUtils.VALUE_LABEL)); + + } + + } + +} diff --git a/src/esieequest/controller/utils/ListUtils.java b/src/esieequest/controller/utils/ListUtils.java new file mode 100644 index 0000000..cb90edc --- /dev/null +++ b/src/esieequest/controller/utils/ListUtils.java @@ -0,0 +1,43 @@ +package esieequest.controller.utils; + +import java.util.List; + +import com.google.common.base.Joiner; + +import esieequest.model.Text; + +/** + * A set of custom utility methods. + * + * @author Pacien TRAN-GIRARD + */ +public class ListUtils { + + /** + * Converts a List into a String with a given prefix, suffix and + * placeholder used if the List is empty. + * + * @param list + * the List to convert + * @param prefix + * the prefix + * @param empty + * the placeholder + * @param suffix + * the suffix + * @return the converted String + */ + public static String listToString(final List list, final String prefix, final String empty, final String suffix) { + final StringBuilder str = new StringBuilder(prefix); + + if (list.isEmpty()) { + str.append(empty); + } else { + str.append(Joiner.on(Text.LIST_SEPARATOR.getText()).join(list)); + } + str.append(suffix); + + return str.toString(); + } + +} diff --git a/src/esieequest/controller/utils/SerialisableObject.java b/src/esieequest/controller/utils/SerialisableObject.java new file mode 100644 index 0000000..30b9c19 --- /dev/null +++ b/src/esieequest/controller/utils/SerialisableObject.java @@ -0,0 +1,26 @@ +package esieequest.controller.utils; + +import org.json.simple.JSONObject; + +/** + * Represents an Object that can be serialised and deserialised. + * + * @author Pacien TRAN-GIRARD + */ +public interface SerialisableObject { + + /** + * Serialises to a JSONObject. + * + * @return the JSONObject + */ + public JSONObject serialise(); + + /** + * Deserialises from a JSONObject. + * + * @param o the JSONObject + */ + public void deserialise(JSONObject o); + +} diff --git a/src/esieequest/controller/utils/package-info.java b/src/esieequest/controller/utils/package-info.java new file mode 100644 index 0000000..088a127 --- /dev/null +++ b/src/esieequest/controller/utils/package-info.java @@ -0,0 +1,4 @@ +/** + * A general utility package. + */ +package esieequest.controller.utils; diff --git a/src/esieequest/esieequest.gwt.xml b/src/esieequest/esieequest.gwt.xml index b7dbcbf..330781b 100644 --- a/src/esieequest/esieequest.gwt.xml +++ b/src/esieequest/esieequest.gwt.xml @@ -20,6 +20,7 @@ so that your app can take advantage of the latest GWT module capabilities. + diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java index 549fdc8..7f124e7 100644 --- a/src/esieequest/model/Game.java +++ b/src/esieequest/model/Game.java @@ -1,7 +1,12 @@ package esieequest.model; +import net.pacien.util.CleanJSONObject; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import esieequest.controller.utils.SerialisableObject; import esieequest.model.characters.Character; -import esieequest.model.characters.Sumobot; import esieequest.model.doors.Door; import esieequest.model.doors.HiddenDoor; import esieequest.model.doors.LockedDoor; @@ -16,17 +21,24 @@ import esieequest.model.map.Room; * * @author Pacien TRAN-GIRARD */ -public class Game { +public class Game implements SerialisableObject { - private static final int DEFAULT_INVENTORY_LIMIT = 10; - private static final int DEFAULT_STEPS_LIMIT = 0; - private static final int CHALLENGE_STEPS_LIMIT = 50; - private static final Direction DEFAULT_DIRECTION = Direction.NORTH; - private static final Room DEFAULT_ROOM = Room.AMPHITHEATER_SEAT; + public static final int DEFAULT_INVENTORY_LIMIT = 10; + public static final int DEFAULT_STEPS_LIMIT = 0; + public static final int CHALLENGE_STEPS_LIMIT = 50; + public static final Direction DEFAULT_DIRECTION = Direction.NORTH; + public static final Room DEFAULT_ROOM = Room.AMPHITHEATER_SEAT; + private static final String CHALLENGE_LABEL = "L"; private final boolean challenge; + + private static final String PLAYER_LABEL = "P"; private Player player; + private static final String ROOMS_LABEL = "R"; + private static final String ITEMS_LABEL = "I"; + private static final String CHARACTERS_LABEL = "C"; + /** * Creates a Game with a step limit for the Player. * @@ -35,7 +47,6 @@ public class Game { */ public Game(final boolean challenge) { this.challenge = challenge; - this.connectRooms(); } /** @@ -48,10 +59,16 @@ public class Game { /** * Initialises a new Game. */ - public void newGame() { - this.player = new Player(Game.DEFAULT_ROOM, Game.DEFAULT_DIRECTION, Game.DEFAULT_INVENTORY_LIMIT, this.challenge ? Game.CHALLENGE_STEPS_LIMIT : Game.DEFAULT_STEPS_LIMIT); - this.addItems(); - this.addCharacters(); + public void newGame(final boolean populate, final boolean challengeMode) { + Room.createAllSides(); + this.connectRooms(); + + this.setPlayer(new Player(Game.DEFAULT_ROOM, Game.DEFAULT_DIRECTION, Game.DEFAULT_INVENTORY_LIMIT, challengeMode ? Game.CHALLENGE_STEPS_LIMIT : Game.DEFAULT_STEPS_LIMIT)); + + if (populate) { + this.addItems(); + this.addCharacters(); + } } /** @@ -61,10 +78,20 @@ public class Game { return this.player; } + /** + * Sets the player + * + * @param player + * the Player to set + */ + public void setPlayer(final Player player) { + this.player = player; + } + /** * Connects Room-s together using Door-s. */ - private void connectRooms() { + public void connectRooms() { this.d(Room.AMPHITHEATER_SEAT, Direction.NORTH, Room.AMPHITHEATER_STAGE); this.d(Room.AMPHITHEATER_STAGE, Direction.WEST, Room.CAFETERIA); this.d(Room.CAFETERIA, Direction.NORTH, Room.CAFETERIA_STREET); @@ -143,7 +170,7 @@ public class Game { /** * Adds Item-s in the map. */ - private void addItems() { + public void addItems() { this.i(Room.STORAGE_ROOM, Direction.WEST, Item.STORAGE_CUBE); this.i(Room.STORAGE_ROOM, Direction.EAST, Item.SAFETY_CUBE); this.i(Room.STORAGE_ROOM, Direction.NORTH, Item.BLACK_HOLE); @@ -170,8 +197,8 @@ public class Game { /** * Adds Character-s to the map. */ - private void addCharacters() { - this.c(Room.LOCKED_ROOM, Direction.SOUTH, new Sumobot(Room.LOCKED_ROOM, Direction.SOUTH)); + public void addCharacters() { + this.c(Room.LOCKED_ROOM, Direction.SOUTH, Character.SUMOBOT); } /** @@ -188,4 +215,26 @@ public class Game { room.getSide(direction).setCharacter(character); } + @Override + public JSONObject serialise() { + final CleanJSONObject o = new CleanJSONObject(); + + o.put(Game.CHALLENGE_LABEL, this.challenge); + + o.put(Game.PLAYER_LABEL, this.player.serialise()); + o.put(Game.ROOMS_LABEL, Room.serialiseAll()); + o.put(Game.ITEMS_LABEL, Item.serialiseAll()); + o.put(Game.CHARACTERS_LABEL, Character.serialiseAll()); + + return o; + } + + @Override + public void deserialise(final JSONObject o) { + this.player.deserialise((JSONObject) o.get(Game.PLAYER_LABEL)); + Room.deserialiseAll((JSONArray) o.get(Game.ROOMS_LABEL)); + Item.deserialiseAll((JSONArray) o.get(Game.ITEMS_LABEL)); + Character.deserialiseAll((JSONArray) o.get(Game.CHARACTERS_LABEL)); + } + } diff --git a/src/esieequest/model/Player.java b/src/esieequest/model/Player.java index ec7b0b5..371de84 100644 --- a/src/esieequest/model/Player.java +++ b/src/esieequest/model/Player.java @@ -3,8 +3,15 @@ package esieequest.model; import java.util.ArrayList; import java.util.Stack; +import net.pacien.util.CleanJSONObject; import net.pacien.util.IntrinsicMap; -import esieequest.controller.Utils; + +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.map.Direction; import esieequest.model.map.Room; @@ -16,17 +23,28 @@ import esieequest.model.map.Side; * @author Pacien TRAN-GIRARD * @author Benoît LUBRANO DI SBARAGLIONE */ -public class Player { +public class Player implements SerialisableObject { + private static final String CURRENT_ROOM_LABEL = "R"; private Room currentRoom; + + private static final String PREVIOUS_ROOMS_LABEL = "H"; private final Stack previousRooms; + + private static final String CURRENT_DIRECTION_LABEL = "D"; private Direction currentDirection; + private static final String ITEMS_LABEL = "I"; private final IntrinsicMap items; - private final int carryWeightLimit; + private static final String INVENTORY_WEIGHT_LIMIT_LABEL = "W"; + private int inventoryWeightLimit; + + private static final String NB_STEPS_LABEL = "S"; private int nbSteps; - private final int nbStepsLimit; + + private static final String NB_STEPS_LIMIT_LABEL = "L"; + private int nbStepsLimit; /** * Creates a Player with a current location (Room and Direction) and limits @@ -46,7 +64,7 @@ public class Player { this.previousRooms = new Stack<>(); this.currentDirection = currentDirection; this.items = new IntrinsicMap<>(); - this.carryWeightLimit = carryWeightLimit; + this.inventoryWeightLimit = carryWeightLimit; this.nbSteps = 0; this.nbStepsLimit = nbStepsLimit; } @@ -91,8 +109,8 @@ public class Player { /** * @return the inventory weight limit */ - public int getCarryWeightLimit() { - return this.carryWeightLimit; + public int getInventoryWeightLimit() { + return this.inventoryWeightLimit; } /** @@ -215,7 +233,7 @@ public class Player { * @return the list of the Item-s in a String */ public String listItemsNamesString() { - return Utils.listToString(new ArrayList(this.items.keySet()), Text.INVENTORY_PREFIX.getText(), Text.ITEMS_NO_ITEM.getText(), Text.INVENTORY_SUFFIX.getText()); + return ListUtils.listToString(new ArrayList(this.items.keySet()), Text.INVENTORY_PREFIX.getText(), Text.ITEMS_NO_ITEM.getText(), Text.INVENTORY_SUFFIX.getText()); } /** @@ -271,4 +289,47 @@ public class Player { return Text.INVENTORY_WEIGHT_PREFIX.getText() + this.getItemsWeight() + Text.INVENTORY_WEIGHT_SUFFIX.getText(); } + @Override + public JSONObject serialise() { + final CleanJSONObject o = new CleanJSONObject(); + + o.put(Player.CURRENT_ROOM_LABEL, this.currentRoom.name()); + o.put(Player.PREVIOUS_ROOMS_LABEL, EnumUtils.nameAll(this.previousRooms.toArray(new Room[0]))); + + 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_WEIGHT_LIMIT_LABEL, this.inventoryWeightLimit); + + o.put(Player.NB_STEPS_LABEL, this.nbSteps); + o.put(Player.NB_STEPS_LIMIT_LABEL, this.nbStepsLimit); + + return o; + } + + @Override + public void deserialise(final JSONObject o) { + + this.currentRoom = Room.valueOf((String) o.get(Player.CURRENT_ROOM_LABEL)); + + this.previousRooms.clear(); + final Object previousRoomsObject = o.get(Player.PREVIOUS_ROOMS_LABEL); + if (previousRoomsObject != null) { + this.previousRooms.addAll(EnumUtils.valuesOf(Room.class, ((JSONArray) previousRoomsObject).toArray(new String[0]))); + } + + 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.inventoryWeightLimit = ((Long) o.get(Player.INVENTORY_WEIGHT_LIMIT_LABEL)).intValue(); + this.nbSteps = ((Long) o.get(Player.NB_STEPS_LIMIT_LABEL)).intValue(); + this.nbStepsLimit = ((Long) o.get(Player.NB_STEPS_LIMIT_LABEL)).intValue(); + + } + } diff --git a/src/esieequest/model/Text.java b/src/esieequest/model/Text.java index 283ccab..1b47c6f 100644 --- a/src/esieequest/model/Text.java +++ b/src/esieequest/model/Text.java @@ -68,7 +68,14 @@ public enum Text { NO_ITEM_SPECIFIED("No item specified."), NO_SUCH_ITEM("No such item."), NO_USE("This item has no use."), - NOTHING_TO_DO("Nothing to do."); + NOTHING_TO_DO("Nothing to do."), + + PARSING_ERROR_PREFIX("Parsing error: "), + PARSING_ERROR_SUFFIX(""), + DESERIALISING_ERROR_PREFIX("Deserialising error: "), + DESERIALISING_ERROR_SUFFIX(""), + + ; // @formatter:on diff --git a/src/esieequest/model/characters/Character.java b/src/esieequest/model/characters/Character.java index 94421d7..9af68b0 100644 --- a/src/esieequest/model/characters/Character.java +++ b/src/esieequest/model/characters/Character.java @@ -1,36 +1,69 @@ package esieequest.model.characters; -/** - * A character that can talk with the player. - * - * @author Pacien TRAN-GIRARD - */ -public abstract class Character { +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; - private final String name; +import esieequest.controller.utils.EnumUtils; +import esieequest.controller.utils.SerialisableObject; +import esieequest.model.map.Direction; +import esieequest.model.map.Room; - /** - * Creates a Character with the given name. - * - * @param name - * the name of the Character - */ - public Character(final String name) { - this.name = name; +public enum Character implements SerialisableObject { + + // @formatter:off + + SUMOBOT(new Sumobot(Room.LOCKED_ROOM, Direction.SOUTH)), + + ; + + // @formatter:on + + private final SimpleCharacter character; + + Character(final SimpleCharacter character) { + this.character = character; } - /** - * Makes the Character talk. Called when the player uses the TALK Command. - * - * @return what the Character says - */ - public abstract String talk(); + public String getName() { + return this.character.getName(); + } + + public String talk() { + return this.character.talk(); + } /** - * @return the name of the Character + * @return the character */ - public String getName() { - return this.name; + public SimpleCharacter getCharacter() { + return this.character; + } + + public static Character getCharacter(final SimpleCharacter simpleCharacter) { + for (final Character character : Character.values()) { + if (character.getCharacter() == simpleCharacter) { + return character; + } + } + return null; + } + + @Override + public JSONObject serialise() { + return this.character.serialise(); + } + + @Override + public void deserialise(final JSONObject o) { + this.character.deserialise(o); + } + + public static JSONArray serialiseAll() { + return EnumUtils.serialiseEnumObjects(Character.values()); + } + + public static void deserialiseAll(final JSONArray a) { + EnumUtils.deserialiseEnumObjects(Character.class, a); } } diff --git a/src/esieequest/model/characters/MovingCharacter.java b/src/esieequest/model/characters/MovingCharacter.java index 439b111..b8b3acb 100644 --- a/src/esieequest/model/characters/MovingCharacter.java +++ b/src/esieequest/model/characters/MovingCharacter.java @@ -4,6 +4,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; +import net.pacien.util.CleanJSONObject; + +import org.json.simple.JSONObject; + import esieequest.model.map.Direction; import esieequest.model.map.Room; @@ -12,9 +16,8 @@ import esieequest.model.map.Room; * * @author Pacien TRAN-GIRARD */ -public abstract class MovingCharacter extends Character { +public abstract class MovingCharacter extends SimpleCharacter { - private static final String Random = null; private static List instances; static { @@ -30,8 +33,13 @@ public abstract class MovingCharacter extends Character { } } + private static final String CURRENT_ROOM_LABEL = "C"; private Room currentRoom; + + private static final String CURRENT_DIRECTION_LABEL = "D"; private Direction currentDirection; + + private static final String CAN_MOVE_LABEL = "M"; private boolean canMove; /** @@ -125,9 +133,32 @@ public abstract class MovingCharacter extends Character { this.currentRoom.getSide(this.currentDirection).setCharacter(null); final Direction newDirection = direction.getOpposite(); - destination.getSide(newDirection).setCharacter(this); + final Character character = Character.getCharacter(this); + destination.getSide(newDirection).setCharacter(character); this.currentRoom = destination; this.currentDirection = newDirection; } + @Override + public JSONObject serialise() { + final CleanJSONObject o = new CleanJSONObject(); + + if (this.currentRoom != null) { + o.put(MovingCharacter.CURRENT_ROOM_LABEL, this.currentRoom.name()); + } + if (this.currentDirection != null) { + o.put(MovingCharacter.CURRENT_DIRECTION_LABEL, this.currentDirection.name()); + } + o.put(MovingCharacter.CAN_MOVE_LABEL, this.canMove); + + return o; + } + + @Override + public void deserialise(final JSONObject o) { + this.currentRoom = Room.valueOf((String) o.get(MovingCharacter.CURRENT_ROOM_LABEL)); + this.currentDirection = Direction.valueOf((String) o.get(MovingCharacter.CURRENT_DIRECTION_LABEL)); + this.canMove = (Boolean) o.get(MovingCharacter.CAN_MOVE_LABEL); + } + } diff --git a/src/esieequest/model/characters/SimpleCharacter.java b/src/esieequest/model/characters/SimpleCharacter.java new file mode 100644 index 0000000..f81bf8b --- /dev/null +++ b/src/esieequest/model/characters/SimpleCharacter.java @@ -0,0 +1,52 @@ +package esieequest.model.characters; + +import net.pacien.util.CleanJSONObject; + +import org.json.simple.JSONObject; + +import esieequest.controller.utils.SerialisableObject; + +/** + * A character that can talk with the player. + * + * @author Pacien TRAN-GIRARD + */ +public abstract class SimpleCharacter implements SerialisableObject { + + private final String name; + + /** + * Creates a Character with the given name. + * + * @param name + * the name of the Character + */ + public SimpleCharacter(final String name) { + this.name = name; + } + + /** + * Makes the Character talk. Called when the player uses the TALK Command. + * + * @return what the Character says + */ + public abstract String talk(); + + /** + * @return the name of the Character + */ + public String getName() { + return this.name; + } + + @Override + public JSONObject serialise() { + return new CleanJSONObject(); + } + + @Override + public void deserialise(final JSONObject o) { + return; + } + +} diff --git a/src/esieequest/model/items/Beamer.java b/src/esieequest/model/items/Beamer.java index 3a4ade9..49e75a4 100644 --- a/src/esieequest/model/items/Beamer.java +++ b/src/esieequest/model/items/Beamer.java @@ -1,5 +1,9 @@ package esieequest.model.items; +import net.pacien.util.CleanJSONObject; + +import org.json.simple.JSONObject; + import esieequest.model.Game; import esieequest.model.Text; import esieequest.model.map.Room; @@ -14,6 +18,7 @@ import esieequest.view.Viewable; */ public class Beamer extends SimpleItem { + private static final String ROOM_LABEL = "R"; private Room room; /** @@ -38,4 +43,20 @@ public class Beamer extends SimpleItem { } } + @Override + public JSONObject serialise() { + final CleanJSONObject jsonObject = new CleanJSONObject(); + + if (this.room != null) { + jsonObject.put(Beamer.ROOM_LABEL, this.room.name()); + } + + return jsonObject; + } + + @Override + public void deserialise(final JSONObject o) { + this.room = Room.valueOf((String) o.get(Beamer.ROOM_LABEL)); + } + } diff --git a/src/esieequest/model/items/Item.java b/src/esieequest/model/items/Item.java index 491b3ef..70ef265 100644 --- a/src/esieequest/model/items/Item.java +++ b/src/esieequest/model/items/Item.java @@ -1,10 +1,16 @@ package esieequest.model.items; import net.pacien.util.Mappable; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import esieequest.controller.utils.EnumUtils; +import esieequest.controller.utils.SerialisableObject; import esieequest.model.Game; import esieequest.view.Viewable; -public enum Item implements Mappable { +public enum Item implements Mappable, SerialisableObject { // @formatter:off @@ -70,4 +76,22 @@ public enum Item implements Mappable { return this.item.getName().toLowerCase(); } + @Override + public JSONObject serialise() { + return this.item.serialise(); + } + + @Override + public void deserialise(final JSONObject o) { + this.item.deserialise(o); + } + + public static JSONArray serialiseAll() { + return EnumUtils.serialiseEnumObjects(Item.values()); + } + + public static void deserialiseAll(final JSONArray a) { + EnumUtils.deserialiseEnumObjects(Item.class, a); + } + } diff --git a/src/esieequest/model/items/SimpleItem.java b/src/esieequest/model/items/SimpleItem.java index 59de4d3..d0ff387 100644 --- a/src/esieequest/model/items/SimpleItem.java +++ b/src/esieequest/model/items/SimpleItem.java @@ -1,5 +1,10 @@ package esieequest.model.items; +import net.pacien.util.CleanJSONObject; + +import org.json.simple.JSONObject; + +import esieequest.controller.utils.SerialisableObject; import esieequest.model.Game; import esieequest.model.Text; import esieequest.view.Viewable; @@ -10,7 +15,7 @@ import esieequest.view.Viewable; * * @author Pacien TRAN-GIRARD */ -public class SimpleItem { +public class SimpleItem implements SerialisableObject { private final String name; private final int weight; @@ -83,4 +88,14 @@ public class SimpleItem { view.echo(Text.NO_USE.getText()); } + @Override + public JSONObject serialise() { + return new CleanJSONObject(); + } + + @Override + public void deserialise(final JSONObject o) { + return; + } + } diff --git a/src/esieequest/model/map/Direction.java b/src/esieequest/model/map/Direction.java index 577cb40..2515120 100644 --- a/src/esieequest/model/map/Direction.java +++ b/src/esieequest/model/map/Direction.java @@ -48,6 +48,7 @@ public enum Direction { * * @return the Direction related to the given Orientation */ + @SuppressWarnings("incomplete-switch") public Direction rotate(final Orientation orientation) { switch (orientation) { case LEFT: diff --git a/src/esieequest/model/map/Room.java b/src/esieequest/model/map/Room.java index c0f8f9a..cfebc34 100644 --- a/src/esieequest/model/map/Room.java +++ b/src/esieequest/model/map/Room.java @@ -6,9 +6,16 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import net.pacien.util.CleanJSONObject; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + import com.google.common.base.Joiner; -import esieequest.controller.Utils; +import esieequest.controller.utils.EnumUtils; +import esieequest.controller.utils.ListUtils; +import esieequest.controller.utils.SerialisableObject; import esieequest.model.Text; import esieequest.model.characters.Character; import esieequest.model.doors.Door; @@ -21,7 +28,7 @@ import esieequest.model.items.Item; * @author Pacien TRAN-GIRARD * @author Benoît LUBRANO DI SBARAGLIONE */ -public enum Room { +public enum Room implements SerialisableObject { // @formatter:off @@ -63,10 +70,11 @@ public enum Room { // @formatter:on private final String description; + private final EnumMap sides; /** - * Creates a room with a description and associated sides. + * Creates a room with a description and associated Side-s. * * @param description * the description of the room @@ -76,6 +84,12 @@ public enum Room { Room(final String description) { this.description = description; this.sides = new EnumMap(Direction.class); + } + + /** + * Creates all Side-s. + */ + public void createSides() { for (final Direction direction : Direction.values()) { this.sides.put(direction, new Side()); } @@ -125,7 +139,7 @@ public enum Room { * @return the list of all the Room's Item-s */ public List listItems() { - final ArrayList itemsList = new ArrayList(); + final ArrayList itemsList = new ArrayList<>(); for (final Side side : this.sides.values()) { if (side.hasItem()) { @@ -140,7 +154,7 @@ public enum Room { * @return the list of all the Room's Character-s. */ public List listCharacters() { - final ArrayList charactersList = new ArrayList(); + final ArrayList charactersList = new ArrayList<>(); for (final Side side : this.sides.values()) { if (side.hasCharacter()) { @@ -198,9 +212,9 @@ public enum Room { final ArrayList informations = new ArrayList(); informations.add(Text.LOCATION_PREFIX + this.getDescription() + Text.LOCATION_SUFFIX); - informations.add(Utils.listToString(this.listExitsDirectionsNames(), Text.EXITS_PREFIX.getText(), Text.EXITS_NO_EXIT.getText(), Text.EXITS_SUFFIX.getText())); - informations.add(Utils.listToString(this.listItemsNames(), Text.ITEMS_PREFIX.getText(), Text.ITEMS_NO_ITEM.getText(), Text.ITEMS_SUFFIX.getText())); - informations.add(Utils.listToString(this.listCharactersNames(), Text.CHARACTERS_PREFIX.getText(), Text.CHARACTERS_NO_CHARACTER.getText(), Text.CHARACTERS_SUFFIX.getText())); + informations.add(ListUtils.listToString(this.listExitsDirectionsNames(), Text.EXITS_PREFIX.getText(), Text.EXITS_NO_EXIT.getText(), Text.EXITS_SUFFIX.getText())); + informations.add(ListUtils.listToString(this.listItemsNames(), Text.ITEMS_PREFIX.getText(), Text.ITEMS_NO_ITEM.getText(), Text.ITEMS_SUFFIX.getText())); + informations.add(ListUtils.listToString(this.listCharactersNames(), Text.CHARACTERS_PREFIX.getText(), Text.CHARACTERS_NO_CHARACTER.getText(), Text.CHARACTERS_SUFFIX.getText())); return Joiner.on(Text.SPACE.getText()).join(informations); } @@ -274,4 +288,43 @@ public enum Room { return item; } + /** + * Creates all Side-s of all Room-s. + */ + public static void createAllSides() { + for (final Room room : Room.values()) { + room.createSides(); + } + } + + @Override + public JSONObject serialise() { + final CleanJSONObject o = new CleanJSONObject(); + + for (final Direction direction : this.sides.keySet()) { + final JSONObject serialisedSide = this.sides.get(direction).serialise(); + o.put(direction.name(), serialisedSide); + } + + return o; + } + + @Override + public void deserialise(final JSONObject o) { + for (final Direction direction : this.sides.keySet()) { + final JSONObject jsonSide = (JSONObject) o.get(direction.name()); + if (jsonSide != null) { + this.sides.get(direction).deserialise(jsonSide); + } + } + } + + public static JSONArray serialiseAll() { + return EnumUtils.serialiseEnumObjects(Room.values()); + } + + public static void deserialiseAll(final JSONArray a) { + EnumUtils.deserialiseEnumObjects(Room.class, a); + } + } diff --git a/src/esieequest/model/map/Side.java b/src/esieequest/model/map/Side.java index 092c280..7d85b8d 100644 --- a/src/esieequest/model/map/Side.java +++ b/src/esieequest/model/map/Side.java @@ -1,5 +1,11 @@ package esieequest.model.map; +import net.pacien.util.CleanJSONObject; + +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; @@ -9,10 +15,14 @@ import esieequest.model.items.Item; * * @author Pacien TRAN-GIRARD */ -public class Side { +public class Side implements SerialisableObject { private Door door; + + private static final String ITEM_LABEL = "I"; private Item item; + + private static final String CHARACTER_LABEL = "C"; private Character character; private Side(final Door door, final Item item, final Character character) { @@ -111,4 +121,24 @@ public class Side { } } + @Override + public JSONObject serialise() { + final CleanJSONObject o = new CleanJSONObject(); + + if (this.item != null) { + o.put(Side.ITEM_LABEL, this.item.name()); + } + if (this.character != null) { + o.put(Side.CHARACTER_LABEL, this.character.name()); + } + + return o; + } + + @Override + public void deserialise(final JSONObject o) { + this.item = EnumUtils.valueOf(Item.class, (String) o.get(Side.ITEM_LABEL)); + this.character = EnumUtils.valueOf(Character.class, (String) o.get(Side.CHARACTER_LABEL)); + } + } diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index c44358f..f492090 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -256,6 +256,7 @@ abstract class UserInterface implements Viewable, ActionListener { */ private void setControlsState(final boolean state) { this.inputField.setEnabled(state); + this.gameButtons.get("saveButton").setEnabled(state); for (final JButton vButton : this.controlButtons.values()) { vButton.setEnabled(state); } diff --git a/src/esieequest/view/text/TextInterface.java b/src/esieequest/view/text/TextInterface.java index 8f37c53..3fc2800 100644 --- a/src/esieequest/view/text/TextInterface.java +++ b/src/esieequest/view/text/TextInterface.java @@ -83,7 +83,7 @@ abstract class TextInterface implements Viewable { } @Override - public void updateLocation(Room room, Direction direction, Side side) { + public void updateLocation(final Room room, final Direction direction, final Side side) { this.echo(room.getInformations()); } diff --git a/src/esieequest/view/web/Main.java b/src/esieequest/view/web/Main.java index 5417ca2..9ff3279 100644 --- a/src/esieequest/view/web/Main.java +++ b/src/esieequest/view/web/Main.java @@ -3,7 +3,6 @@ package esieequest.view.web; import com.google.gwt.core.client.EntryPoint; import esieequest.controller.GameEngine; -import esieequest.model.Game; /** * The web view entry point and initialisation class. The recursively imported @@ -20,9 +19,8 @@ public class Main implements EntryPoint { */ @Override public void onModuleLoad() { - final Game game = new Game(); final WebInterface webInterface = new WebInterface(); - new GameEngine(game, webInterface); + new GameEngine(webInterface); } } diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 08dfe4f..69f3c4e 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -143,7 +143,7 @@ class WebInterface extends Composite implements Viewable { Event.addNativePreviewHandler(new NativePreviewHandler() { @Override public void onPreviewNativeEvent(final NativePreviewEvent event) { - final int key = event.getNativeEvent().getKeyCode(); + //final int key = event.getNativeEvent().getKeyCode(); } }); } @@ -184,6 +184,7 @@ class WebInterface extends Composite implements Viewable { */ private void setControlsEnabled(final boolean state) { this.inputField.setEnabled(state); + this.saveButton.setEnabled(state); this.inventoryButton.setEnabled(state); this.actionButton.setEnabled(state); this.forwardButton.setEnabled(state); diff --git a/src/esieequest/view/web/WebInterface.ui.xml b/src/esieequest/view/web/WebInterface.ui.xml index 8ded623..473358d 100644 --- a/src/esieequest/view/web/WebInterface.ui.xml +++ b/src/esieequest/view/web/WebInterface.ui.xml @@ -15,7 +15,7 @@ ui:field="questLabel" /> - + diff --git a/src/net/pacien/util/CleanJSONObject.java b/src/net/pacien/util/CleanJSONObject.java new file mode 100644 index 0000000..0982ca8 --- /dev/null +++ b/src/net/pacien/util/CleanJSONObject.java @@ -0,0 +1,48 @@ +package net.pacien.util; + +import java.util.Collection; +import java.util.Map; + +import org.json.simple.JSONObject; + +public class CleanJSONObject extends JSONObject { + + /** + * + */ + private static final long serialVersionUID = 6700060746446264070L; + + public CleanJSONObject() { + super(); + } + + @Override + public Object put(final String key, final Object value) { + if (value == null) { + return null; + } + return super.put(key, value); + } + + public Object put(final String key, final Map map) { + if (map.isEmpty()) { + return null; + } + return super.put(key, map); + } + + public Object put(final String key, final Collection collection) { + if (collection.isEmpty()) { + return null; + } + return super.put(key, collection); + } + + public Object put(final String key, final Object[] array) { + if (array.length == 0) { + return null; + } + return super.put(key, array); + } + +} diff --git a/src/net/pacien/util/IntrinsicMap.java b/src/net/pacien/util/IntrinsicMap.java index a68dcbe..fe59ac2 100644 --- a/src/net/pacien/util/IntrinsicMap.java +++ b/src/net/pacien/util/IntrinsicMap.java @@ -3,12 +3,13 @@ */ package net.pacien.util; +import java.util.Collection; import java.util.HashMap; import java.util.Map; /** * @author pacien - * + * */ public class IntrinsicMap extends HashMap { @@ -27,14 +28,14 @@ public class IntrinsicMap extends HashMap { /** * @param initialCapacity */ - public IntrinsicMap(int initialCapacity) { + public IntrinsicMap(final int initialCapacity) { super(initialCapacity); } /** * @param m */ - public IntrinsicMap(Map m) { + public IntrinsicMap(final Map m) { super(m); } @@ -42,18 +43,24 @@ public class IntrinsicMap extends HashMap { * @param initialCapacity * @param loadFactor */ - public IntrinsicMap(int initialCapacity, float loadFactor) { + public IntrinsicMap(final int initialCapacity, final float loadFactor) { super(initialCapacity, loadFactor); } - + @SuppressWarnings("unchecked") - public V put(V value) { + public V put(final V value) { return super.put((K) value.getKey(), value); } - + @Override - public V put(K key, V value) { + public V put(final K key, final V value) { return this.put(value); } + public void putAll(final Collection collection) { + for (final V value : collection) { + this.put(value); + } + } + } diff --git a/src/org/json/simple/ItemList.java b/src/org/json/simple/ItemList.java new file mode 100644 index 0000000..fc98549 --- /dev/null +++ b/src/org/json/simple/ItemList.java @@ -0,0 +1,158 @@ +/* + * $Id: ItemList.java,v 1.1 2006/04/15 14:10:48 platform Exp $ + * Created on 2006-3-24 + */ +package org.json.simple; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +/** + * |a:b:c| => |a|,|b|,|c| |:| => ||,|| |a:| => |a|,|| + * + * @author FangYidong + */ +public class ItemList { + private String sp = ","; + @SuppressWarnings("rawtypes") + List items = new ArrayList(); + + public ItemList() { + } + + public ItemList(final String s) { + this.split(s, this.sp, this.items); + } + + public ItemList(final String s, final String sp) { + this.sp = s; + this.split(s, sp, this.items); + } + + public ItemList(final String s, final String sp, final boolean isMultiToken) { + this.split(s, sp, this.items, isMultiToken); + } + + @SuppressWarnings("rawtypes") + public List getItems() { + return this.items; + } + + public String[] getArray() { + return (String[]) this.items.toArray(); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void split(final String s, final String sp, final List append, final boolean isMultiToken) { + if ((s == null) || (sp == null)) { + return; + } + if (isMultiToken) { + final StringTokenizer tokens = new StringTokenizer(s, sp); + while (tokens.hasMoreTokens()) { + append.add(tokens.nextToken().trim()); + } + } else { + this.split(s, sp, append); + } + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void split(final String s, final String sp, final List append) { + if ((s == null) || (sp == null)) { + return; + } + int pos = 0; + int prevPos = 0; + do { + prevPos = pos; + pos = s.indexOf(sp, pos); + if (pos == -1) { + break; + } + append.add(s.substring(prevPos, pos).trim()); + pos += sp.length(); + } while (pos != -1); + append.add(s.substring(prevPos).trim()); + } + + public void setSP(final String sp) { + this.sp = sp; + } + + @SuppressWarnings("unchecked") + public void add(final int i, final String item) { + if (item == null) { + return; + } + this.items.add(i, item.trim()); + } + + @SuppressWarnings("unchecked") + public void add(final String item) { + if (item == null) { + return; + } + this.items.add(item.trim()); + } + + @SuppressWarnings("unchecked") + public void addAll(final ItemList list) { + this.items.addAll(list.items); + } + + public void addAll(final String s) { + this.split(s, this.sp, this.items); + } + + public void addAll(final String s, final String sp) { + this.split(s, sp, this.items); + } + + public void addAll(final String s, final String sp, final boolean isMultiToken) { + this.split(s, sp, this.items, isMultiToken); + } + + /** + * @param i + * 0-based + * @return + */ + public String get(final int i) { + return (String) this.items.get(i); + } + + public int size() { + return this.items.size(); + } + + @Override + public String toString() { + return this.toString(this.sp); + } + + public String toString(final String sp) { + final StringBuffer sb = new StringBuffer(); + + for (int i = 0; i < this.items.size(); i++) { + if (i == 0) { + sb.append(this.items.get(i)); + } else { + sb.append(sp); + sb.append(this.items.get(i)); + } + } + return sb.toString(); + + } + + public void clear() { + this.items.clear(); + } + + public void reset() { + this.sp = ","; + this.items.clear(); + } +} diff --git a/src/org/json/simple/JSONArray.java b/src/org/json/simple/JSONArray.java new file mode 100644 index 0000000..e2c7042 --- /dev/null +++ b/src/org/json/simple/JSONArray.java @@ -0,0 +1,390 @@ +/* + * $Id: JSONArray.java,v 1.1 2006/04/15 14:10:48 platform Exp $ + * Created on 2006-4-10 + */ +package org.json.simple; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +import rejava.io.StringWriter; +import rejava.io.Writer; + +/** + * A JSON array. JSONObject supports java.util.List interface. + * + * @author FangYidong + */ +public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware { + private static final long serialVersionUID = 3957988303675231981L; + + /** + * Constructs an empty JSONArray. + */ + public JSONArray() { + super(); + } + + /** + * Constructs a JSONArray containing the elements of the specified + * collection, in the order they are returned by the collection's iterator. + * + * @param c + * the collection whose elements are to be placed into this + * JSONArray + */ + public JSONArray(final Collection c) { + super(c); + } + + /** + * Encode a list into JSON text and write it to out. If this list is also a + * JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific + * behaviours will be ignored at this top level. + * + * @see org.json.simple.JSONValue#writeJSONString(Object, Writer) + * + * @param collection + * @param out + */ + public static void writeJSONString(final Collection collection, final Writer out) throws IOException { + if (collection == null) { + out.write("null"); + return; + } + + boolean first = true; + final Iterator iter = collection.iterator(); + + out.write('['); + while (iter.hasNext()) { + if (first) { + first = false; + } else { + out.write(','); + } + + final Object value = iter.next(); + if (value == null) { + out.write("null"); + continue; + } + + JSONValue.writeJSONString(value, out); + } + out.write(']'); + } + + @Override + public void writeJSONString(final Writer out) throws IOException { + JSONArray.writeJSONString(this, out); + } + + /** + * Convert a list to JSON text. The result is a JSON array. If this list is + * also a JSONAware, JSONAware specific behaviours will be omitted at this + * top level. + * + * @see org.json.simple.JSONValue#toJSONString(Object) + * + * @param collection + * @return JSON text, or "null" if list is null. + */ + public static String toJSONString(final Collection collection) { + final StringWriter writer = new StringWriter(); + + try { + JSONArray.writeJSONString(collection, writer); + return writer.toString(); + } catch (final IOException e) { + // This should never happen for a StringWriter + throw new RuntimeException(e); + } + } + + public static void writeJSONString(final byte[] array, final Writer out) throws IOException { + if (array == null) { + out.write("null"); + } else if (array.length == 0) { + out.write("[]"); + } else { + out.write("["); + out.write(String.valueOf(array[0])); + + for (int i = 1; i < array.length; i++) { + out.write(","); + out.write(String.valueOf(array[i])); + } + + out.write("]"); + } + } + + public static String toJSONString(final byte[] array) { + final StringWriter writer = new StringWriter(); + + try { + JSONArray.writeJSONString(array, writer); + return writer.toString(); + } catch (final IOException e) { + // This should never happen for a StringWriter + throw new RuntimeException(e); + } + } + + public static void writeJSONString(final short[] array, final Writer out) throws IOException { + if (array == null) { + out.write("null"); + } else if (array.length == 0) { + out.write("[]"); + } else { + out.write("["); + out.write(String.valueOf(array[0])); + + for (int i = 1; i < array.length; i++) { + out.write(","); + out.write(String.valueOf(array[i])); + } + + out.write("]"); + } + } + + public static String toJSONString(final short[] array) { + final StringWriter writer = new StringWriter(); + + try { + JSONArray.writeJSONString(array, writer); + return writer.toString(); + } catch (final IOException e) { + // This should never happen for a StringWriter + throw new RuntimeException(e); + } + } + + public static void writeJSONString(final int[] array, final Writer out) throws IOException { + if (array == null) { + out.write("null"); + } else if (array.length == 0) { + out.write("[]"); + } else { + out.write("["); + out.write(String.valueOf(array[0])); + + for (int i = 1; i < array.length; i++) { + out.write(","); + out.write(String.valueOf(array[i])); + } + + out.write("]"); + } + } + + public static String toJSONString(final int[] array) { + final StringWriter writer = new StringWriter(); + + try { + JSONArray.writeJSONString(array, writer); + return writer.toString(); + } catch (final IOException e) { + // This should never happen for a StringWriter + throw new RuntimeException(e); + } + } + + public static void writeJSONString(final long[] array, final Writer out) throws IOException { + if (array == null) { + out.write("null"); + } else if (array.length == 0) { + out.write("[]"); + } else { + out.write("["); + out.write(String.valueOf(array[0])); + + for (int i = 1; i < array.length; i++) { + out.write(","); + out.write(String.valueOf(array[i])); + } + + out.write("]"); + } + } + + public static String toJSONString(final long[] array) { + final StringWriter writer = new StringWriter(); + + try { + JSONArray.writeJSONString(array, writer); + return writer.toString(); + } catch (final IOException e) { + // This should never happen for a StringWriter + throw new RuntimeException(e); + } + } + + public static void writeJSONString(final float[] array, final Writer out) throws IOException { + if (array == null) { + out.write("null"); + } else if (array.length == 0) { + out.write("[]"); + } else { + out.write("["); + out.write(String.valueOf(array[0])); + + for (int i = 1; i < array.length; i++) { + out.write(","); + out.write(String.valueOf(array[i])); + } + + out.write("]"); + } + } + + public static String toJSONString(final float[] array) { + final StringWriter writer = new StringWriter(); + + try { + JSONArray.writeJSONString(array, writer); + return writer.toString(); + } catch (final IOException e) { + // This should never happen for a StringWriter + throw new RuntimeException(e); + } + } + + public static void writeJSONString(final double[] array, final Writer out) throws IOException { + if (array == null) { + out.write("null"); + } else if (array.length == 0) { + out.write("[]"); + } else { + out.write("["); + out.write(String.valueOf(array[0])); + + for (int i = 1; i < array.length; i++) { + out.write(","); + out.write(String.valueOf(array[i])); + } + + out.write("]"); + } + } + + public static String toJSONString(final double[] array) { + final StringWriter writer = new StringWriter(); + + try { + JSONArray.writeJSONString(array, writer); + return writer.toString(); + } catch (final IOException e) { + // This should never happen for a StringWriter + throw new RuntimeException(e); + } + } + + public static void writeJSONString(final boolean[] array, final Writer out) throws IOException { + if (array == null) { + out.write("null"); + } else if (array.length == 0) { + out.write("[]"); + } else { + out.write("["); + out.write(String.valueOf(array[0])); + + for (int i = 1; i < array.leng