Skip to content

Commit

Permalink
Merge pull request #99 from jxofficial/branch-timer
Browse files Browse the repository at this point in the history
Implement Custom Mode v1.0
  • Loading branch information
ShaunNgTX authored Oct 31, 2019
2 parents 985a9e8 + f17bf18 commit e0ebce6
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import seedu.revision.model.Model;
import seedu.revision.model.ModelManager;
import seedu.revision.model.quiz.Mode;
import seedu.revision.model.quiz.NormalMode;

/**
* Represents the result of a command execution.
Expand Down Expand Up @@ -43,7 +44,7 @@ public CommandResult() {
this.exit = false;
this.start = false;
this.showRestore = false;
this.mode = new Mode("normal");
this.mode = new NormalMode();
this.model = new ModelManager();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package seedu.revision.logic.commands.main;

import static java.util.Objects.requireNonNull;
import static seedu.revision.logic.parser.CliSyntax.PREFIX_MODE;

import seedu.revision.commons.core.Messages;
import seedu.revision.logic.commands.Command;
import seedu.revision.logic.parser.exceptions.ParseException;
import seedu.revision.model.Model;
Expand Down Expand Up @@ -34,10 +36,11 @@ public StartQuizCommand(Mode mode) {

@Override
public CommandResult execute(Model model) throws ParseException {
requireNonNull(model);
model.updateFilteredAnswerableList(this.mode.getCombinedPredicate());

//ListCommand quizList = new ListCommand(predicate);
//quizList.execute(model);
return new CommandResult().withFeedBack(MESSAGE_SUCCESS).withStart(true).withMode(mode).build();
return new CommandResult().withFeedBack(String.format(Messages.MESSAGE_ANSWERABLES_LISTED_OVERVIEW,
model.getFilteredAnswerableList().size())).withStart(true).withMode(mode).build();

}
}
23 changes: 21 additions & 2 deletions src/main/java/seedu/revision/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import seedu.revision.model.answerable.QuestionType;
import seedu.revision.model.answerable.answer.Answer;
import seedu.revision.model.category.Category;
import seedu.revision.model.quiz.CustomMode;
import seedu.revision.model.quiz.ArcadeMode;
import seedu.revision.model.quiz.Mode;
import seedu.revision.model.quiz.NormalMode;
import seedu.revision.ui.bar.Timer;

/**
* Contains utility methods used for parsing strings in the various *Parser classes.
Expand Down Expand Up @@ -131,7 +133,7 @@ public static Category parseCategory(String category) throws ParseException {
}

/**
* Parses {@code Collection<String> Categorys} into a {@code Set<Category>}.
* Parses {@code Collection<String> Categories} into a {@code Set<Category>}.
*/
public static Set<Category> parseCategories(Collection<String> categories) throws ParseException {
requireNonNull(categories);
Expand All @@ -142,6 +144,22 @@ public static Set<Category> parseCategories(Collection<String> categories) throw
return categorySet;
}


/**
* Parses {@code String time} into a valid {int time}
*
* @throws ParseException if the given {@code time} is < 1 seconds
*/
public static int parseTimer(String time) throws ParseException {
int convertedTime = (int) Double.parseDouble(time);
if (convertedTime < 1) {
throw new ParseException(Timer.MESSAGE_CONSTRAINTS);
}

return convertedTime;
}


/**
* Parses a {@code String mode} into a {@code Mode}.
* Leading and trailing whitespaces will be trimmed.
Expand All @@ -159,7 +177,8 @@ public static Mode parseMode(String mode) throws ParseException {
return new NormalMode();
case "arcade":
return new ArcadeMode();
//case "custom":
case "custom":
return new CustomMode();
default:
throw new ParseException("Invalid mode found at ParserUtil");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
import static seedu.revision.logic.parser.CliSyntax.PREFIX_DIFFICULTY;
import static seedu.revision.logic.parser.CliSyntax.PREFIX_MODE;
import static seedu.revision.logic.parser.CliSyntax.PREFIX_TIMER;
import static seedu.revision.model.Model.PREDICATE_SHOW_ALL_ANSWERABLE;

import java.util.function.Predicate;

import seedu.revision.logic.commands.main.StartQuizCommand;
import seedu.revision.logic.parser.ArgumentMultimap;
import seedu.revision.logic.parser.ArgumentTokenizer;
import seedu.revision.logic.parser.Parser;
import seedu.revision.logic.parser.ParserUtil;
import seedu.revision.logic.parser.exceptions.ParseException;
import seedu.revision.model.answerable.Answerable;
import seedu.revision.model.answerable.Difficulty;
import seedu.revision.model.answerable.predicates.CategoryPredicate;
import seedu.revision.model.answerable.predicates.DifficultyPredicate;
import seedu.revision.model.category.Category;
import seedu.revision.model.quiz.ArcadeMode;
import seedu.revision.model.quiz.Mode;
Expand All @@ -23,6 +29,8 @@
*/
public class StartQuizCommandParser implements Parser<StartQuizCommand> {

private Predicate<Answerable> combinedPredicate = PREDICATE_SHOW_ALL_ANSWERABLE ;

/**
* Parses the given {@code String} of arguments in the context of the StartQuizCommand
* and returns a StartQuizCommand object for execution.
Expand All @@ -37,11 +45,12 @@ public StartQuizCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, StartQuizCommand.MESSAGE_USAGE));
}


boolean optionalTimerPrefixIsPresent = argMultimap.getValue(PREFIX_TIMER).isPresent();
boolean optionalDifficultyPrefixIsPresent = argMultimap.getValue(PREFIX_DIFFICULTY).isPresent();
boolean optionalCategoryPrefixIsPresent = argMultimap.getValue(PREFIX_CATEGORY).isPresent();
boolean optionalPrefixesArePresent = optionalTimerPrefixIsPresent || optionalCategoryPrefixIsPresent
|| optionalDifficultyPrefixIsPresent;
|| optionalDifficultyPrefixIsPresent;

Mode mode;
Difficulty difficulty;
Expand Down Expand Up @@ -69,29 +78,32 @@ public StartQuizCommand parse(String args) throws ParseException {
mode = new ArcadeMode();
return new StartQuizCommand(mode);
}
//case "custom":
// Predicate<Answerable> combinedPredicate = PREDICATE_SHOW_ALL_ANSWERABLE;

// if (optionalCategoryPrefixIsPresent) {
// Category categoryToFilter = ParserUtil.parseCategory(argMultimap.getValue(PREFIX_CATEGORY).get());
// CategoryPredicate categoryPredicate = new CategoryPredicate(categoryToFilter);
// combinedPredicate = combinedPredicate.and(categoryPredicate);
// }

// if (optionalDifficultyPrefixIsPresent) {
// Difficulty difficultyToFilter = ParserUtil.parseDifficulty(argMultimap.getValue(
// PREFIX_DIFFICULTY).get());
// DifficultyPredicate difficultyPredicate = new DifficultyPredicate(difficultyToFilter);
// combinedPredicate = combinedPredicate.and(difficultyPredicate);
// }

// if (optionalTimerPrefixIsPresent) {
// time = ParserUtil.parseTimer(argMultimap.getValue(PREFIX_TIMER).get());
// }
// mode = new CustomMode().with
// break;
case "custom":

if (optionalCategoryPrefixIsPresent) {
Category categoryToFilter = ParserUtil.parseCategory(argMultimap.getValue(PREFIX_CATEGORY).get());
CategoryPredicate categoryPredicate = new CategoryPredicate(categoryToFilter);
combinedPredicate = combinedPredicate.and(categoryPredicate);
}

if (optionalDifficultyPrefixIsPresent) {
Difficulty difficultyToFilter = ParserUtil.parseDifficulty(argMultimap.getValue(PREFIX_DIFFICULTY).get());
DifficultyPredicate difficultyPredicate = new DifficultyPredicate(difficultyToFilter);
combinedPredicate = combinedPredicate.and(difficultyPredicate);
}

mode = mode.withCombinedPredicate(combinedPredicate);

if (optionalTimerPrefixIsPresent) {
time = ParserUtil.parseTimer(argMultimap.getValue(PREFIX_TIMER).get());
mode = mode.withTime(time);
}
mode = mode.build();
return new StartQuizCommand(mode);
default:
throw new ParseException(StartQuizCommand.MESSAGE_USAGE);
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, StartQuizCommand.MESSAGE_USAGE));
}


}
}
17 changes: 17 additions & 0 deletions src/main/java/seedu/revision/model/quiz/ArcadeMode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package seedu.revision.model.quiz;

import java.util.function.Predicate;

import seedu.revision.model.answerable.Answerable;

/** ArcadeMode class which has increasing difficulty each level and ends any time a user gets a question wrong. **/
public class ArcadeMode extends Mode {
/**
Expand All @@ -20,6 +24,19 @@ public int getLevelThreeTime() {
return time - 10;
}

@Override
public Mode withCombinedPredicate(Predicate<Answerable> combinedPredicate) {
return this;
}

@Override
public Mode withTime(int time) {
return this;
}

@Override
public Mode build() {
return this;
}
}

39 changes: 17 additions & 22 deletions src/main/java/seedu/revision/model/quiz/CustomMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
import java.util.function.Predicate;

import seedu.revision.model.answerable.Answerable;
import seedu.revision.model.answerable.predicates.CategoryPredicate;
import seedu.revision.model.answerable.predicates.DifficultyPredicate;

/**
* CustomMode which can be adapted according to user input.
*/
public class CustomMode extends Mode {
private int time;
private DifficultyPredicate difficultyPredicate;
private CategoryPredicate categoryPredicate;

public CustomMode(String value) {
super(value);
public CustomMode() {
super("custom");
this.time = NormalMode.NORMAL_MODE_TIME;
this.combinedPredicate = NormalMode.NORMAL_MODE_PREDICATE;
}
Expand All @@ -32,31 +27,31 @@ public CustomMode(String value, int time, Predicate<Answerable> combinedPredicat
this.combinedPredicate = combinedPredicate;
}

/**
* Sets the time for the custom mode.
* @param time time that will be set.
* @return {@CustomMode} object with its time set.
*/
public CustomMode withTime(int time) {
this.time = time;
return this;
}

/**
* Sets the combinedPredicate for the custom mode.
* @param combinedPredicate combinedPredicate that will be set.
* @return {@CustomMode} object with its combinedPredicate set.
* @return {@Mode} object with its combinedPredicate set.
*/
public CustomMode withCategoryPredicate(Predicate<Answerable> combinedPredicate) {
@Override
public Mode withCombinedPredicate(Predicate<Answerable> combinedPredicate) {
this.combinedPredicate = combinedPredicate;
return this;
}


/**
* Initialises the {@CustomMode} with the user chosen timer and predicate.
* @return {@CustomMode} with the customised time and predicate.
* Sets the time for the custom mode.
* @param time time that will be set.
* @return {@Mode} object with its time set.
*/
public CustomMode build() {
@Override
public Mode withTime(int time) {
this.time = time;
return this;
}

@Override
public Mode build() {
return new CustomMode(value, time, combinedPredicate);
}
}
19 changes: 14 additions & 5 deletions src/main/java/seedu/revision/model/quiz/Mode.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package seedu.revision.model.quiz;

import java.util.function.Predicate;

import static java.util.Objects.requireNonNull;
import static seedu.revision.commons.util.AppUtil.checkArgument;

import java.util.function.Predicate;

import seedu.revision.model.answerable.Answerable;

/**
* Represents the mode of a quiz in the Revision Tool.
* Guarantees: immutable; is valid as declared in {@link #isValidMode(String)}
*/
public class Mode {
public abstract class Mode {

public static final String MESSAGE_CONSTRAINTS = "Mode can only be normal / arcade / custom";

Expand All @@ -36,15 +36,16 @@ public Mode(String value) {
this.value = value;
}


/**
* Returns true if a given string is a valid question.
* Returns true if a given string is a valid mode.
*/
public static boolean isValidMode(String test) {
return test.matches(VALIDATION_REGEX);
}

public int getTime() {
return time;
return this.time;
}

public int getLevelTwoTime() {
Expand All @@ -58,6 +59,14 @@ public int getLevelThreeTime() {
public Predicate<Answerable> getCombinedPredicate() {
return combinedPredicate;
}

public abstract Mode withCombinedPredicate(Predicate<Answerable> combinedPredicate);

public abstract Mode withTime(int time);

public abstract Mode build();


@Override
public String toString() {
return value;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/seedu/revision/model/quiz/NormalMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,20 @@ public NormalMode() {
this.time = NORMAL_MODE_TIME;
this.combinedPredicate = NORMAL_MODE_PREDICATE;
}

@Override
public Mode withCombinedPredicate(Predicate<Answerable> combinedPredicate) {
return new NormalMode();
}

@Override
public Mode withTime(int time) {
return new NormalMode();
}

@Override
public Mode build() {
return new NormalMode();
}
}

2 changes: 2 additions & 0 deletions src/main/java/seedu/revision/ui/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.revision.ui;

import java.util.Optional;
import java.util.function.Predicate;
import java.util.logging.Logger;

import javafx.fxml.FXML;
Expand All @@ -18,6 +19,7 @@
import seedu.revision.model.AddressBook;
import seedu.revision.model.Model;
import seedu.revision.model.ReadOnlyAddressBook;
import seedu.revision.model.answerable.Answerable;
import seedu.revision.model.quiz.Mode;
import seedu.revision.model.util.SampleDataUtil;
import seedu.revision.ui.answerables.AnswerableListPanel;
Expand Down
Loading

0 comments on commit e0ebce6

Please sign in to comment.