diff --git a/src/main/java/de/hda/fbi/db2/api/Lab03Game.java b/src/main/java/de/hda/fbi/db2/api/Lab03Game.java index f0431295bd08b16ac50a2d7682ca63f9fb173189..a69ae81e465428ba742868adecc41279e49bfa74 100644 --- a/src/main/java/de/hda/fbi/db2/api/Lab03Game.java +++ b/src/main/java/de/hda/fbi/db2/api/Lab03Game.java @@ -22,7 +22,7 @@ public abstract class Lab03Game { protected Lab02EntityManager lab02EntityManager; /** - * Setter for Lab01Data. + * Setter for Lab01Data. Do not touch this method. * * @param lab01Data lab01Data */ @@ -31,7 +31,7 @@ public abstract class Lab03Game { } /** - * Setter fro Lab02EntityManager. + * Setter fro Lab02EntityManager. Do not touch this method. * * @param lab02EntityManager lab02EntityManager */ @@ -40,68 +40,113 @@ public abstract class Lab03Game { } /** - * This method should create a game. For this you have to do the following things: - Ask for - * player - Ask for categories - Ask for maximum questions per category and choose questions + * Creates a new Player or retrieves it from the database. + * <p> + * This function shall try to retrieve the player with the given name playerName from the + * database. If no such player exists, it shall be created as a Java Object. It is not necessary + * to persist the Player yet. + * </p> * - * <p>You do not have to play the game here. This should only create a game + * <p>This function is primarily used for testing. There exists a version with user interaction + * which shall be used from the menu + * </p> * - * @return the game entity object. IMPORTANT: This has to be a entity. + * @param playerName The name for the new Player. + * @return Player object which was created or retrieved. + * @see Lab03Game#interactiveGetOrCreatePlayer() */ - public abstract Object createGame(); + public abstract Object getOrCreatePlayer(String playerName); /** - * This should create a game with the given arguments. You do not have to read data from console. - * The game should be create only with the given arguments. You do not have to play the game - * here. + * Creates a new Player or retrieves it from the database (interactive version). * - * @param playerName name of the player - * @param questions chosen questions - * @return a game object + * <p> + * This function shall ask the user for a player name, and then shall try to retrieve such a + * player from the database. If no such player exists, it shall be created as a Java Object. It is + * not necessary to persist the player yet. + * </p> + * + * <p>This function is primarily used for user interaction. There exists a version used for + * testing, @see getOrCreatePlayer</p> + * + * @return Player object which was created or retrieved. + * @see Lab03Game#getOrCreatePlayer(String) */ - public abstract Object createGame(String playerName, List<Object> questions); + public abstract Object interactiveGetOrCreatePlayer(); /** - * Here you have to play the game 'game'. + * This function shall generate a random list of questions which are from the given categories. + * + * <p>Per category there shall be a certain amount of questions chosen. If a category hosts less + * questions than that amount, then all of the questions shall be chosen. Questions shall be + * randomly selected. + * </p> * - * @param game the game that should be played + * <p>There is also an interactive version of this function which asks the user for categories + * instead of randomly selecting them.</p> + * + * @param categories A list of categories to select questions from + * @param amountOfQuestionsForCategory The amount of questions per category. If a category has + * less than this amount, then all questions of that category + * shall be selected. + * @return A List of randomly chosen Questions from the given Categories. + * @see Lab03Game#interactiveGetQuestions() */ - public abstract void playGame(Object game); + public abstract List<?> getQuestions(List<?> categories, int amountOfQuestionsForCategory); /** - * Simulate a game play. You do not have to read anything from console. + * This function shall generate a random list of questions after asking the user for categories. * - * @param game given game + * <p>In this function, ask the user for categories and the number of questions per category. + * Then, randomly select questions from those categories. Choose as many questions per category as + * were entered, as long as the category has that many questions. If there are fewer questions, + * then select all of them.</p> + * + * @return A List of randomly chosen Questions from categories which the user entered. + * @see Lab03Game#getQuestions(List, int) */ - public abstract void simulateGame(Object game); + public abstract List<?> interactiveGetQuestions(); /** - * This should return the appropriate player for the given game. + * This function creates a Game Object with the given player and questions, but without playing + * the game yet. + * + * <p>It is important that you neither play the game yet nor persist the game! This is just meant + * to create the game object.</p> * - * @param game given game for returning player - * @return player for given game + * @param player The Player which shall play the game. + * @param questions The Questions which shall be asked during the game. + * @return A Game Object which contains an unplayed game + * for the given player with the given questions. */ - public abstract Object getPlayer(Object game); + public abstract Object createGame(Object player, List<?> questions); /** - * Return the player entity with given name. + * This function simulates a game play without user interaction by randomly choosing answers. + * + * <p>There is also an interactive version of this function which shall be called from the menu. + * </p> * - * @param name name of the player - * @return the player entity + * @param game The Game Object which shall be played. + * @see Lab03Game#interactivePlayGame(Object) */ - public abstract Object getPlayer(String name); + public abstract void playGame(Object game); /** - * return the right answers of a played game. + * This function plays the given game with the user, that is by using user interaction. + * + * <p>This is the function that should be called from the menu. Here you can implement the + * necessary user interaction for the playing of the game.</p> * - * @param game given game - * @return number of right answers + * @param game The Game Object which shall be played. + * @see Lab03Game#playGame(Object) */ - public abstract int getRightAnswers(Object game); + public abstract void interactivePlayGame(Object game); /** - * persist the game object in the database. + * Persists a played game, including the player which played it. * - * @param game given game + * @param game The Game Object to be persisted. */ public abstract void persistGame(Object game); } diff --git a/src/main/java/de/hda/fbi/db2/controller/MenuController.java b/src/main/java/de/hda/fbi/db2/controller/MenuController.java index cbfdf84866882098477eaedca9385194b0e9f802..5d141b1cc6be919c97b5e6259b5da5bd55e991f2 100644 --- a/src/main/java/de/hda/fbi/db2/controller/MenuController.java +++ b/src/main/java/de/hda/fbi/db2/controller/MenuController.java @@ -4,6 +4,7 @@ import de.hda.fbi.db2.api.Lab03Game; import java.io.BufferedReader; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.util.List; /** * MenuController Created by l.koehler on 05.08.2019. @@ -76,8 +77,10 @@ public class MenuController { private void playTest() { Lab03Game gameController = controller.getLab03Game(); - Object game = gameController.createGame(); - gameController.playGame(game); + Object player = gameController.interactiveGetOrCreatePlayer(); + List<?> questions = gameController.interactiveGetQuestions(); + Object game = gameController.createGame(player, questions); + gameController.interactivePlayGame(game); gameController.persistGame(game); } diff --git a/src/test/java/de/hda/fbi/db2/test/Lab03Test.java b/src/test/java/de/hda/fbi/db2/test/Lab03Test.java index e39abc64248414a563d3c2218c0f28524289f02f..2456633e3ec3ee99ff31d6678419ed394ec465e9 100644 --- a/src/test/java/de/hda/fbi/db2/test/Lab03Test.java +++ b/src/test/java/de/hda/fbi/db2/test/Lab03Test.java @@ -5,13 +5,8 @@ import de.hda.fbi.db2.api.Lab03Game; import de.hda.fbi.db2.controller.Controller; import java.util.ArrayList; import java.util.List; -import javax.persistence.metamodel.Attribute; -import javax.persistence.metamodel.EmbeddableType; import javax.persistence.metamodel.EntityType; import javax.persistence.metamodel.Metamodel; -import javax.persistence.metamodel.PluralAttribute; -import javax.persistence.metamodel.Type; -import org.eclipse.persistence.internal.jpa.metamodel.EntityTypeImpl; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.FixMethodOrder; @@ -79,11 +74,13 @@ public class Lab03Test { Lab03Game gameController = controller.getLab03Game(); Lab01Data lab01Data = controller.getLab01Data(); - List<Object> questions = new ArrayList<>(); - questions.add(lab01Data.getQuestions().get(0)); - questions.add(lab01Data.getQuestions().get(1)); - Object game = gameController.createGame("PlayerName", questions); - gameController.simulateGame(game); + List<Object> categories = new ArrayList<>(); + categories.add(lab01Data.getCategories().get(0)); + categories.add(lab01Data.getCategories().get(1)); + List<?> questions = gameController.getQuestions(categories, 2); + Object player = gameController.getOrCreatePlayer("PlayerName"); + Object game = gameController.createGame(player, questions); + gameController.playGame(game); gameController.persistGame(game); } @@ -95,10 +92,12 @@ public class Lab03Test { Lab03Game gameController = controller.getLab03Game(); Lab01Data lab01Data = controller.getLab01Data(); - List<Object> questions = new ArrayList<>(); - questions.add(lab01Data.getQuestions().get(0)); - questions.add(lab01Data.getQuestions().get(1)); - Object game = gameController.createGame("PlayerName", questions); + List<Object> categories = new ArrayList<>(); + categories.add(lab01Data.getCategories().get(0)); + categories.add(lab01Data.getCategories().get(1)); + List<?> questions = gameController.getQuestions(categories, 2); + Object player = gameController.getOrCreatePlayer("PlayerName"); + Object game = gameController.createGame(player, questions); boolean gameFound = false; for (EntityType<?> clazzes : metaData.getEntities()) { @@ -120,12 +119,7 @@ public class Lab03Test { } Lab03Game gameController = controller.getLab03Game(); - Lab01Data lab01Data = controller.getLab01Data(); - List<Object> questions = new ArrayList<>(); - questions.add(lab01Data.getQuestions().get(0)); - questions.add(lab01Data.getQuestions().get(1)); - Object game = gameController.createGame("PlayerName", questions); - Object player = gameController.getPlayer(game); + Object player = gameController.getOrCreatePlayer("PlayerName"); boolean playerFound = false; for (EntityType<?> clazzes : metaData.getEntities()) { @@ -138,62 +132,4 @@ public class Lab03Test { Assert.fail("Could not find player class as entity"); } } - - @Test - public void test4FindReferences() { - if (metaData == null) { - Assert.fail("No MetaModel"); - } - - if (gameEntity == null) { - Assert.fail("No GameEntity found"); - } - - EntityTypeImpl questionEntity = null; - for (EntityType current : metaData.getEntities()) { - if (current.getName().toLowerCase().equals("question")) { - questionEntity = (EntityTypeImpl) current; - } - } - if (questionEntity == null) { - Assert.fail("No Question Entity found"); - } - - Type answerEntity = null; - for (EntityType current : metaData.getEntities()) { - if (current.getName().toLowerCase().equals("answer")) { - answerEntity = (EntityTypeImpl) current; - } - } - - for (EmbeddableType embeddable : metaData.getEmbeddables()) { - if (embeddable.getJavaType().getSimpleName().toLowerCase().equals("answer") - || embeddable.getJavaType().getSimpleName().toLowerCase().equals("answers")) { - answerEntity = embeddable; - } - } - - if (answerEntity == null) { - Assert.fail("No Answer Entity found"); - } - - boolean correct = false; - for (Attribute attr : gameEntity.getAttributes()) { - try { - PluralAttribute plrAttr = (PluralAttribute) attr; - if (plrAttr.getElementType().getJavaType() == questionEntity.getJavaType()) { - correct = true; - } - if (plrAttr.getElementType().getJavaType() == answerEntity.getJavaType()) { - correct = true; - } - } catch (Exception ignored) { - //This is expected - } - } - - if (!correct) { - Assert.fail("Could not find any right answer or question reference in game"); - } - } }