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 String deviceId = null; private boolean stoppFlag = false; private Map jsonMap = null; private String[] inputArray = null; private String itemName = null; private String intentName = null; private String puzzleName = null; private String input = null; private Room currentRoom = null; public Game() { try { startGame(); } catch (Exception ex) { Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex); } } 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 writeJSON(File f) throws IOException { ObjectMapper mapper = new ObjectMapper(); String s = ""; try ( JsonGenerator jGenerator = mapper.getFactory().createGenerator(f,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 Item getItem(String s) { for (int i = 0; i < items.size(); i++) { if (items.get(i).getName().toLowerCase().equals(s.toLowerCase())) { if (currentRoom.getItems().contains(items.get(i).getId())) { return items.get(i); } } } return null; } public Puzzle getPuzzle(String s) { for (int i = 0; i < items.size(); i++) { if (items.get(i).getName().toLowerCase().equals(s.toLowerCase())) { if (currentRoom.getPuzzles().contains(puzzles.get(i).getId())) { return puzzles.get(i); } } } return null; } public String getId() { return deviceId; } public void setVariables() throws Exception { readJSON(); log("Readjson fertig"); currentRoom = rooms.get(0); stoppFlag = false; inventory = new ArrayList<>(); //log("JSON: " + writeJSON()); 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)); //getPuzzles().get(i).getName()); } for (int i = 0; i < currentRoom.getItems().size(); i++) { log("Item im Raum: " + currentRoom.getItems().get(i)); //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(" "); for (String s : inputArray) { if (getItem(s) != null) { itemName = getItem(s).getName(); log("itemName: " + itemName); } else if (getPuzzle(s) != null) { puzzleName = getPuzzle(s).getName(); log("PuzzleName: " + puzzleName); } else { intentName = s; log("intentName: " + intentName); } } } else { intentName = input; log("intentName: " + intentName); } switch (intentName.toLowerCase()) { //intent case "lookaroundintent": log("Switch: LookAroundIntent"); if (itemName == null && puzzleName == null) { lookaround(); } break; //intent case "inspectintent": log("Switch: InspectIntent"); if (getItem(itemName) != null) { inspect(getItem(itemName)); } else if (getPuzzle(puzzleName) != null) { inspect(getPuzzle(puzzleName)); } break; //intent case "examineintent": log("Switch: ExamineIntent"); if (itemName == null && puzzleName == null) { examine(); } break; //intent case "takeintent": log("Switch: TakeIntent"); if (getItem(itemName) != null) { take(getItem(itemName)); } break; //intent case "openintent": log("Switch: OpenIntent"); if (getPuzzle(puzzleName) != null) { open(getPuzzle(puzzleName)); } break; //intent case "resetintent": log("Switch: ResetIntent"); if (itemName == null && puzzleName == null) { resetGame(); } break; //intent case "skipintent": log("Switch: SkipIntent"); if (itemName == null && puzzleName == null) { resetGame(); for (int i = 0; i < currentRoom.puzzles.size(); i++) { if (getPuzzleById(currentRoom.puzzles.get(i)).getNextRoom() != null) { setRoom(getRoomById(getPuzzleById(currentRoom.puzzles.get(i)).getNextRoom())); } } } break; } itemName = null; intentName = null; puzzleName = null; log("stoppFlag: " + stoppFlag); if (stoppFlag == true) { endGame(); } } public Room getRoomById(int i) { for (int j = 0; j < rooms.size(); j++) { if (rooms.get(j).getId() == i) { return rooms.get(j); } } return null; } public Item getItemById(int i) { for (int j = 0; j < items.size(); j++) { if (items.get(j).getId() == i) { return items.get(j); } } return null; } public Puzzle getPuzzleById(int i) { for (int j = 0; j < puzzles.size(); j++) { if (puzzles.get(j).getId() == i) { return puzzles.get(j); } } return null; } 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(); //add reuqest header httpConn.setRequestMethod("POST"); httpConn.setRequestProperty("User-Agent", "Mozilla/5.0"); httpConn.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // Send post request httpConn.setDoOutput(true); DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream()); String urlParameters = "log=" + s; wr.writeBytes(urlParameters); wr.flush(); wr.close(); BufferedReader in = new BufferedReader( new InputStreamReader(httpConn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); } public void lookaround() throws Exception { log("lookaround()"); say(currentRoom.getDescription()); } public void startGame() throws Exception { log("startGame()"); for (int i = 0; i < story.size(); i++) { say(story.get(i)); } } public void resetGame() throws Exception { log("resetGame()"); startGame(); } 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.getId()) { 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.getId()) { say(currentItem.getDescription()); } } } public void endGame() throws Exception { log("endGame()"); say("Glückwunsch das Spiel ist beendet!"); setVariables(); } public void sendPost(String s) throws Exception { s = s.replace(" ", "%20"); String url = "https://medinf.efi.th-nuernberg.de/tomcat/WebAdventure/IO"; URL obj = new URL(url); HttpURLConnection httpConn = (HttpURLConnection) obj.openConnection(); //add reuqest header httpConn.setRequestMethod("POST"); httpConn.setRequestProperty("User-Agent", "Mozilla/5.0"); httpConn.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // Send post request httpConn.setDoOutput(true); DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream()); String urlParameters = "alexaText=" + s; wr.writeBytes(urlParameters); wr.flush(); wr.close(); BufferedReader in = new BufferedReader( new InputStreamReader(httpConn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); } public void say(String s) throws Exception { log("say(" + s + ")"); try { sendPost(s); } catch (Exception ex) { Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex); } } public void setInput(String s) throws Exception { log("setInput(" + s + ")"); input = s; } public void setRoom(Room nextRoom) throws Exception { log("setRoom(" + nextRoom.getName() + ")"); currentRoom = nextRoom; say(currentRoom.getDescription()); } public void examine() throws Exception { log("examine() : " + getInventory()); if (getInventory().isEmpty()) { say("Deine Taschen sind leer"); } else if (inventory.size() >= 1) { say("In deinen Taschen befindet sich: "); } inventory.forEach((bag) -> { try { say(bag.getName()); } catch (Exception ex) { Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex); } }); } public void open(Puzzle currentPuzzle) throws Exception { log("open(" + currentPuzzle.getName() + ") "); if (currentPuzzle.isSolved()) // bereits gelöst { say("ist bereits geöffnet"); if (currentPuzzle.getNextRoom() != null) // wenn es eine Tür ist die in den nächsten Raum führt { setRoom(getRoomById(currentPuzzle.getNextRoom())); // wechselt in den nächsten Raum } return; } if (!currentPuzzle.hasDependency() || getPuzzleById(currentPuzzle.getDependency()).isSolved()) // ist keine Abhängigkeit vorhanden oder aber die Abhängigkeit ist gelöst { if (currentPuzzle.getItems().isEmpty() || checkPuzzleItemsInInventory(currentPuzzle.getId())) // keine Items zum lösen notwendig oder alle Items zum lösen befinden sich im Inventar { currentPuzzle.setSolved(true); // auf gelöst setzen say(currentPuzzle.getSolvedText()); // Lösungstext if (currentRoom.getGameoverFlag() || currentPuzzle.getGameOverFlag()) // Wenn es das letzte Rätsel im Spiel war oder man vom Angreifer erwischt wurde { stoppFlag = true; // Spiel wird nun beendet } if (currentPuzzle.getNextRoom() != null) // wenn es eine Tür ist die in den nächsten Raum führt { setRoom(getRoomById(currentPuzzle.getNextRoom())); // wechselt in den nächsten Raum } } else // Item zum lösen fehlt { say("das geht leider nicht, da fehlt noch etwas"); } } if (!getPuzzleById(currentPuzzle.getDependency()).isSolved()) // Abhängigkeit nicht gelöst { say(currentPuzzle.getDependencyText()); } } public void take(Item currentItem) throws Exception { log("take(" + currentItem.getName() + ") "); if ((!inventory.contains(currentItem)) && (currentItem.isPortableFlag() == true)) { inventory.add(currentItem); currentRoom.items.remove(currentItem); say(currentItem.getName() + " wurde deiner Tasche hinzugefuegt"); } else if (currentItem.isPortableFlag() == false) { say("Das Item kann nicht mitgenommen werden"); } else { say("Das Item befindet sich bereits in deiner Tasche!"); } } public boolean checkPuzzleItemsInInventory(int puzzleId) { Puzzle p = getPuzzleById(puzzleId); for (int i = 0; i < p.getItems().size(); i++) { if (inventory.contains(getItemById(p.getItems().get(i)))) { } else { return false; } } return true; } public ArrayList getInventory() { return inventory; } }