From 5996c744a2f0ada88c8f41f2d5db99794c5a18b1 Mon Sep 17 00:00:00 2001 From: Edi Date: Fri, 5 Jul 2019 19:09:43 +0200 Subject: [PATCH] Merge origin/master --- nbproject/build-impl.xml | 6 + nbproject/genfiles.properties | 6 +- nbproject/project.properties | 8 +- nbproject/project.xml | 12 ++ src/java/Game.java | 272 +++++++++++++++++++++++++++------- src/java/Item.java | 14 ++ src/java/Logic.java | 8 +- src/java/Puzzle.java | 18 +++ src/java/Room.java | 14 ++ 9 files changed, 296 insertions(+), 62 deletions(-) diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index 7f23e52..c0fc23d 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -998,11 +998,17 @@ exists or setup the property manually. For example like this: + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index 33a372f..7b68088 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -1,8 +1,8 @@ -build.xml.data.CRC32=b50c735c +build.xml.data.CRC32=20a1ec55 build.xml.script.CRC32=2d64e8d1 build.xml.stylesheet.CRC32=651128d4@1.77.1.1 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=b50c735c -nbproject/build-impl.xml.script.CRC32=74e621b3 +nbproject/build-impl.xml.data.CRC32=20a1ec55 +nbproject/build-impl.xml.script.CRC32=b4f5594b nbproject/build-impl.xml.stylesheet.CRC32=99ea4b56@1.77.1.1 diff --git a/nbproject/project.properties b/nbproject/project.properties index f81702a..a50e149 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -28,6 +28,9 @@ dist.war=${dist.dir}/${war.name} endorsed.classpath=\ ${libs.javaee-endorsed-api-7.0.classpath} excludes= +file.reference.jackson-annotations-2.9.0.jar=C:\\Users\\Edi\\Desktop\\jackson-annotations-2.9.0.jar +file.reference.jackson-core-2.9.9.jar=C:\\Users\\Edi\\Desktop\\jackson-core-2.9.9.jar +file.reference.jackson-databind-2.9.9.jar=C:\\Users\\Edi\\Desktop\\jackson-databind-2.9.9.jar includes=** j2ee.compile.on.save=true j2ee.copy.static.files.on.save=true @@ -42,7 +45,10 @@ j2ee.platform.wsit.classpath= j2ee.server.type=gfv3ee6 jar.compress=false javac.classpath=\ - ${libs.javaee-web-api-7.0.classpath} + ${libs.javaee-web-api-7.0.classpath}:\ + ${file.reference.jackson-annotations-2.9.0.jar}:\ + ${file.reference.jackson-core-2.9.9.jar}:\ + ${file.reference.jackson-databind-2.9.9.jar} # Space-separated list of extra javac options javac.compilerargs= javac.debug=true diff --git a/nbproject/project.xml b/nbproject/project.xml index dc2a753..c5b1864 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -10,6 +10,18 @@ ${libs.javaee-web-api-7.0.classpath} WEB-INF/lib + + ${file.reference.jackson-annotations-2.9.0.jar} + WEB-INF/lib + + + ${file.reference.jackson-core-2.9.9.jar} + WEB-INF/lib + + + ${file.reference.jackson-databind-2.9.9.jar} + WEB-INF/lib + diff --git a/src/java/Game.java b/src/java/Game.java index b519d4f..061b8f2 100644 --- a/src/java/Game.java +++ b/src/java/Game.java @@ -1,34 +1,50 @@ +import com.fasterxml.jackson.core.JsonEncoding; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; import java.io.BufferedReader; import java.io.DataOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import java.net.HttpURLConnection; +import java.util.Map; +import javax.json.stream.JsonGenerationException; public class Game { private static ArrayList rooms = new ArrayList(); private static ArrayList inventory = new ArrayList(); private static ArrayList story = new ArrayList(); + private static ArrayList items = new ArrayList(); + private static ArrayList puzzles = new ArrayList(); private boolean stoppFlag = false; + private Map jsonMap = null; + private String[] inputArray = null; - String[] inputArray = null; - String itemName = null; - String intentName = null; - String puzzleName = null; - String input = null; - Room currentRoom = null; + private String itemName = null; + private String intentName = null; + private String puzzleName = null; + private String input = null; + private Room currentRoom = null; - Room intro; - Room keller; - Puzzle durchgang; - Puzzle schloss; - Puzzle door; - Item schluessel; - Item stahlschluessel; + private Room intro; + private Room keller; + private Puzzle durchgang; + private Puzzle schloss; + private Puzzle door; + private Item schluessel; + private Item stahlschluessel; public Game() { try { @@ -38,7 +54,83 @@ public class Game { } } - public void setVariables() { + public Map readJSON() throws IOException, Exception { + + //TODO: Define path! + File file = new File("game.JSON"); + try { + FileReader fr = new FileReader(file); + BufferedReader br = new BufferedReader(fr); + String jsonObject = br.readLine(); + ObjectMapper objectMapper = new ObjectMapper(); + + try { + jsonMap = objectMapper.readValue(jsonObject, + new TypeReference>() { + }); + } catch (JsonParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (JsonMappingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return jsonMap; + + } catch (FileNotFoundException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + return null; + } + + public static String generateJSON() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + String s = ""; + try ( + JsonGenerator jGenerator + = mapper.getFactory().createGenerator( + new File("game.JSON"), + JsonEncoding.UTF8)) { + + jGenerator.writeStartObject(); + jGenerator.writeObjectField("room", rooms); + jGenerator.writeObjectField("puzzle", puzzles); + jGenerator.writeObjectField("item", items); + jGenerator.writeObjectField("story", story); + + jGenerator.writeEndObject(); + jGenerator.close(); + + } catch (JsonGenerationException e) { + e.printStackTrace(); + } catch (JsonMappingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader("game.JSON")); + } catch (FileNotFoundException ex) { + Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex); + } + StringBuilder sb = new StringBuilder(); + String line; + + while ((line = reader.readLine()) != null) { + s = s + (line + "\n"); + } + return s; + } + + public void setVariables() throws Exception { // Intro Raum intro = new Room("Intro"); @@ -58,7 +150,7 @@ public class Game { schloss = new Puzzle("Schloss"); schloss.setDescription("Es ist ein altes Schloss aus Metall, dass eine Einkerbung f�r einen Schl�ssel hat"); schloss.setSolvedText("Ja, es hat funktioniert! Mit einem Knacksen geht das Schloss auf und du kannst dich von deiner Beinfessel befreien. "); - + puzzles.add(schloss); keller.puzzles.add(schloss); //Ausgang @@ -67,6 +159,7 @@ public class Game { door.setGameOverFlag(true); door.setSolvedText("Geschafft! Die Tuer ist offen. Das Intro ist nun beendet"); keller.puzzles.add(door); + puzzles.add(door); //Item Object schluessel = new Item("Schluessel"); @@ -75,6 +168,7 @@ public class Game { schluessel.setDescription("Der Schl�ssel ist kalt und klein. Vielleicht hast du Gl�ck und er passt ins Schloss. Versuche es zu �ffnen!"); schloss.addItem(schluessel); keller.items.add(schluessel); + items.add(schluessel); door.setDependency(schloss); door.setDependencyText("Du musst erst das Schloss der Kette knacken um die Tür öffnen zu können"); @@ -85,6 +179,7 @@ public class Game { stahlschluessel.setPortableFlag(false); stahlschluessel.setDescription("Ein Stahlschlüssel"); intro.items.add(stahlschluessel); + items.add(stahlschluessel); rooms.add(intro); rooms.add(keller); @@ -99,18 +194,82 @@ public class Game { currentRoom = intro; stoppFlag = false; inventory = new ArrayList(); + log("JSON: " + generateJSON()); + readJSON(); + + log("Readjson fertig"); + + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); + + String jsonStringRoom = objectMapper.writeValueAsString(jsonMap.get("room")); + String jsonStringItem = objectMapper.writeValueAsString(jsonMap.get("item")); + String jsonStringPuzzle = objectMapper.writeValueAsString(jsonMap.get("puzzle")); + String jsonStringStory = objectMapper.writeValueAsString(jsonMap.get("story")); + + + + log("Item: " + jsonStringItem); + log("Room: " + jsonStringRoom); + log("Story: " + jsonStringStory); + log("Puzzle: " + jsonStringPuzzle); + + String[] st = objectMapper.readValue(jsonStringStory, String[].class); + log("Story Array OK"); + Item[] it = objectMapper.readValue(jsonStringItem, Item[].class); + log("Item Array OK"); + Puzzle[] pu = objectMapper.readValue(jsonStringPuzzle, Puzzle[].class); + log("Puzzle Array OK"); + Room[] ro = objectMapper.readValue(jsonStringRoom, Room[].class); + log("Room Array OK"); + + for(int i=0; i < ro.length; i++) + { + log("Room: " + ro[i].getName()); + log("Description: " + ro[i].getDescription()); + } + + for(int i=0; i < it.length; i++) + { + log("Item: " + it[i].getName()); + log("Description: " + it[i].getDescription()); + } + + + + for(int i=0; i < pu.length; i++) + { + log("Puzzle: " + pu[i].getName()); + log("Description: " + pu[i].getDescription()); + log("DependencyText: " + pu[i].getDependencyText()); + } + + + + for(int i=0; i < st.length; i++) + { + log("Story: " + st[i]); + } } public void play() throws Exception { log("play()"); log("Input: " + input); - + log("currentRoom: " + currentRoom.getName()); + + for (int i = 0; i < currentRoom.getPuzzles().size(); i++) { + log("Puzzle im Raum: " + currentRoom.getPuzzles().get(i).getName()); + } + for (int i = 0; i < currentRoom.getItems().size(); i++) { + log("Item im Raum: " + currentRoom.getItems().get(i).getName()); + } + input = input.replace("ä", "ae"); input = input.replace("ü", "ue"); input = input.replace("ö", "oe"); input = input.replace("ß", "ss"); - + if (input.contains(" ")) { inputArray = input.split(" "); @@ -134,22 +293,22 @@ public class Game { switch (intentName.toLowerCase()) { //intent case "lookaroundintent": - log("Switch: lookAroundIntent"); + log("Switch: LookAroundIntent"); if (itemName == null && puzzleName == null) { lookaround(); } break; //intent case "inspectintent": - log("Switch: inspectintent"); - if (itemName != null) { - inspect(itemName); - } else if (puzzleName != null) { - inspect(puzzleName); + log("Switch: InspectIntent"); + if (currentRoom.getItem(itemName) != null) { + inspect(currentRoom.getItem(itemName)); + } else if (currentRoom.getPuzzle(puzzleName) != null) { + inspect(currentRoom.getPuzzle(puzzleName)); } - break; - //intent - + break; + + //intent case "examineintent": log("Switch: ExamineIntent"); if (itemName == null && puzzleName == null) { @@ -160,14 +319,14 @@ public class Game { //intent case "takeintent": log("Switch: TakeIntent"); - if (itemName != null) { + if (currentRoom.getItem(itemName) != null) { take(currentRoom.getItem(itemName)); } break; //intent case "openintent": log("Switch: OpenIntent"); - if (puzzleName != null) { + if (currentRoom.getPuzzle(puzzleName) != null) { open(currentRoom.getPuzzle(puzzleName)); } break; @@ -190,7 +349,6 @@ public class Game { } } - } break; } @@ -198,16 +356,15 @@ public class Game { itemName = null; intentName = null; puzzleName = null; - + log("stoppFlag: " + stoppFlag); - + if (stoppFlag == true) { endGame(); } } - - public void log(String s) throws Exception - { + + public void log(String s) throws Exception { String url = "https://medinf.efi.th-nuernberg.de/tomcat/WebAdventure/Log"; URL obj = new URL(url); HttpURLConnection httpConn = (HttpURLConnection) obj.openConnection(); @@ -237,43 +394,44 @@ public class Game { } public void lookaround() throws Exception { - log("lookaround() "); + log("lookaround()"); say(currentRoom.getDescription()); } public void startGame() throws Exception { - log("startGame() "); + log("startGame()"); for (int i = 0; i < story.size(); i++) { say(story.get(i)); } } public void resetGame() throws Exception { - log("resetGame() "); + log("resetGame()"); startGame(); } - public void inspect(String s) throws Exception { - log("inspect(" + s + ") "); - Item currentItem=null; - if(currentRoom.getItem(s) != null) - { - currentItem= currentRoom.getItem(s); - log("currentItem: " + currentItem.getName()); - say(currentItem.getDescription()); + public void inspect(Puzzle currentPuzzle) throws Exception { + log("inspect(" + currentPuzzle.getName() + ")"); + + for (int i = 0; i < currentRoom.getItems().size(); i++) { + if (currentRoom.getPuzzles().get(i) == currentPuzzle) { + say(currentPuzzle.getDescription()); + } } - Puzzle currentPuzzle=null; - - if(currentRoom.getPuzzle(s) != null) - { - currentPuzzle = currentRoom.getPuzzle(s); - log("currentPuzzle " + currentPuzzle.getName()); - say(currentPuzzle.getDescription()); + } + + public void inspect(Item currentItem) throws Exception { + log("inspect(" + currentItem.getName() + ")"); + + for (int i = 0; i < currentRoom.getItems().size(); i++) { + if (currentRoom.getItems().get(i) == currentItem) { + say(currentItem.getDescription()); + } } } public void endGame() throws Exception { - log("endGame() "); + log("endGame()"); say("Glückwunsch das Spiel ist beendet!"); setVariables(); } @@ -309,7 +467,7 @@ public class Game { } public void say(String s) throws Exception { - log("say(" + s + ") "); + log("say(" + s + ")"); try { sendPost(s); } catch (Exception ex) { @@ -318,12 +476,12 @@ public class Game { } public void setInput(String s) throws Exception { - log("setInput(" + s + ") "); + log("setInput(" + s + ")"); input = s; } public void setRoom(Room nextRoom) throws Exception { - log("setRoom(" + nextRoom.getName() + ") " ); + log("setRoom(" + nextRoom.getName() + ")"); currentRoom = nextRoom; say(currentRoom.getDescription()); } @@ -348,7 +506,7 @@ public class Game { } public void open(Puzzle currentPuzzle) throws Exception { - log("open(" + currentPuzzle.getName() + ") "); + log("open(" + currentPuzzle.getName() + ") "); if (currentPuzzle.isSolved()) // bereits gelöst { say("ist bereits geöffnet"); diff --git a/src/java/Item.java b/src/java/Item.java index f636d0f..07aa213 100644 --- a/src/java/Item.java +++ b/src/java/Item.java @@ -5,6 +5,20 @@ public class Item private boolean portableFlag; private boolean hiddenFlag; private String description; + + public Item(String n, boolean p, boolean h, String d) + { + name = n; + portableFlag = p; + hiddenFlag = h; + description = d; + } + + public Item() + { + + } + public Item(String name) { this.name = name; diff --git a/src/java/Logic.java b/src/java/Logic.java index 76e46b6..b1bbabb 100644 --- a/src/java/Logic.java +++ b/src/java/Logic.java @@ -35,8 +35,14 @@ public class Logic extends HttpServlet { String context = ""; public void init() { - g.setVariables(); + try { + g.setVariables(); + } catch (Exception ex) { + Logger.getLogger(Logic.class.getName()).log(Level.SEVERE, null, ex); + } } + + protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception { diff --git a/src/java/Puzzle.java b/src/java/Puzzle.java index 2034a52..eb0055f 100644 --- a/src/java/Puzzle.java +++ b/src/java/Puzzle.java @@ -17,6 +17,24 @@ public class Puzzle this.name = name; } + public Puzzle() + { + + } + + public Puzzle(String n, ArrayList i, String d, boolean s, String st, boolean g, Room nx, String dt, Puzzle p) + { + name = n; + items = i; + description = d; + solved = s; + solvedText = st; + gameOverFlag = g; + nextRoom = nx; + dependencyText = dt; + dependency = p; + } + public boolean hasDependency() { return dependency != null; diff --git a/src/java/Room.java b/src/java/Room.java index a09d0fe..1cf112f 100644 --- a/src/java/Room.java +++ b/src/java/Room.java @@ -12,7 +12,21 @@ public class Room { this.name = name; } + + public Room() + { + + } + public Room(String n, String d, boolean g, ArrayList i, ArrayList p) + { + name = n; + description = d; + gameoverFlag = g; + items = i; + puzzles = p; + } + public Item getItem(String s) { for(Item i: items)