|
|
@@ -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<Room> rooms = new ArrayList<Room>();
|
|
|
|
private static ArrayList<Item> inventory = new ArrayList<Item>();
|
|
|
|
private static ArrayList<String> story = new ArrayList<String>();
|
|
|
|
private static ArrayList<Item> items = new ArrayList<Item>();
|
|
|
|
private static ArrayList<Puzzle> puzzles = new ArrayList<Puzzle>();
|
|
|
|
private boolean stoppFlag = false;
|
|
|
|
|
|
|
|
String[] inputArray = null;
|
|
|
|
String itemName = null;
|
|
|
|
String intentName = null;
|
|
|
|
String puzzleName = null;
|
|
|
|
String input = null;
|
|
|
|
Room currentRoom = null;
|
|
|
|
|
|
|
|
Room intro;
|
|
|
|
Room keller;
|
|
|
|
Puzzle durchgang;
|
|
|
|
Puzzle schloss;
|
|
|
|
Puzzle door;
|
|
|
|
Item schluessel;
|
|
|
|
Item stahlschluessel;
|
|
|
|
private Map<String, Object> 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;
|
|
|
|
|
|
|
|
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<String, Object> 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<Map<String, Object>>() {
|
|
|
|
});
|
|
|
|
} 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<Item>();
|
|
|
|
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");
|