Added initial filter support for trivia files

This allows the game to filter trivia files using the category and difficulty filters. Question amount is currently ignored
This commit is contained in:
luca0N! 2021-03-16 00:48:01 -03:00
parent 35ade89e34
commit 7d92099a95
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
6 changed files with 178 additions and 9 deletions

View File

@ -42,6 +42,7 @@ import com.luca0n.joguitos.pluck.trivia.TriviaSource;
import com.luca0n.joguitos.pluck.trivia.TriviaCategory;
import com.luca0n.joguitos.pluck.trivia.TriviaDifficulty;
import com.luca0n.joguitos.pluck.trivia.TriviaQuery;
import com.luca0n.joguitos.pluck.trivia.TriviaFilters;
public class MainActivity extends BaseActivity {
@ -112,6 +113,11 @@ public class MainActivity extends BaseActivity {
Intent intent = new Intent(getApplicationContext(), TriviaGameActivity.class);
intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_SOURCE, TriviaSource.SERVER.getName()); // Specify the trivia source.
intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_QUERY, query);
intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_FILTERS,
new TriviaFilters.Builder(amount)
.category(category)
.difficulty(difficulty)
.build());
startActivity(intent);
});
@ -147,8 +153,18 @@ public class MainActivity extends BaseActivity {
// Check if the user canceled the operation.
if (extras == null)
break;
int amount = (int) spinnerNumber.getSelectedItem();
TriviaCategory category = (TriviaCategory) spinnerCategory.getSelectedItem();
TriviaDifficulty difficulty = (TriviaDifficulty) spinnerDifficulty.getSelectedItem();
Intent intent = new Intent(getApplicationContext(), TriviaGameActivity.class);
// Put filters on the extras object.
intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_FILTERS,
new TriviaFilters.Builder(amount)
.category(category)
.difficulty(difficulty)
.build());
intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_SOURCE, TriviaSource.FILE.getName()); // Specify the trivia source.
intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_SOURCE_DATA, extras.getData()); // Specify the trivia source data. In this case, since the user chose the "File" source option, we should put the file Uri object here.
startActivity(intent);

View File

@ -62,6 +62,7 @@ import com.luca0n.joguitos.pluck.trivia.TriviaGame;
import com.luca0n.joguitos.pluck.trivia.TriviaQuery;
import com.luca0n.joguitos.pluck.trivia.TriviaQuestion;
import com.luca0n.joguitos.pluck.trivia.TriviaSource;
import com.luca0n.joguitos.pluck.trivia.TriviaFilters;
import com.luca0n.joguitos.pluck.util.ApiUtil;
import com.luca0n.joguitos.pluck.util.SoundUtil;
@ -70,10 +71,12 @@ public class TriviaGameActivity extends BaseActivity
static final String EXTRA_TRIVIA_QUERY = "extra_trivia_query";
static final String EXTRA_TRIVIA_SOURCE = "extra_trivia_source";
static final String EXTRA_TRIVIA_SOURCE_DATA = "extra_trivia_source_data";
static final String EXTRA_TRIVIA_FILTERS = "extra_trivia_filters";
private final String STATE_TRIVIA_GAME = "state_trivia_game";
private TriviaGame game;
private TriviaSource source;
private TriviaFilters filters;
@BindView(R.id.progress_bar)
ProgressBar progressBar;
@ -102,6 +105,8 @@ public class TriviaGameActivity extends BaseActivity
Bundle bundle = getIntent().getExtras();
assert bundle != null;
TriviaQuery query = (TriviaQuery) bundle.get(EXTRA_TRIVIA_QUERY);
filters = (TriviaFilters) bundle.get(EXTRA_TRIVIA_FILTERS);
progressBar.setVisibility(View.VISIBLE);
@ -178,11 +183,8 @@ public class TriviaGameActivity extends BaseActivity
return;
} else {
try {
this.game = new TriviaGame(ApiUtil.jsonToQuestionArray(json));
this.game = new TriviaGame(ApiUtil.jsonToQuestionArray(json, filters));
} catch (Exception e) {
//onNoTriviaResults();
new AlertDialog.Builder(this)
.setMessage(e.toString()).show();
onException(e);
return;
}
@ -250,6 +252,10 @@ public class TriviaGameActivity extends BaseActivity
else
id = R.string.error_unknown;
new AlertDialog.Builder(this)
.setMessage(e.toString())
.show();
msg = getResources().getString(id);
Fragment errorFragment = TriviaGameErrorFragment.newInstance(msg);

View File

@ -110,7 +110,7 @@ public enum TriviaCategory {
return this.ID;
}
private String getName() {
public String getName() {
return this.name;
}

View File

@ -0,0 +1,128 @@
/*
Pluck: an open source trivia game for Android
Copyright (C) 2021 Joguitos do luca0N!
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
This game is a fork of LibreTrivia, and its source code is available at
<https://github.com/tryton-vanmeer/LibreTrivia>.
Contact us at <joguitos+pluck@luca0n.com>.
*/
package com.luca0n.joguitos.pluck.trivia;
import java.io.Serializable;
public class TriviaFilters implements Serializable {
private static final int DEFAULT_AMOUNT = 10;
private final int amount;
private final TriviaCategory category;
private final TriviaDifficulty difficulty;
private final TriviaType type;
private TriviaFilters(Builder builder) {
this.amount = builder.amount;
this.category = builder.category;
this.difficulty = builder.difficulty;
this.type = builder.type;
}
/**
* @return The desired amount of questions selected by the player.
* @since 2021-03-16
*/
public int getAmount(){
return amount;
}
/**
* @return The category selected by the player.
* @since 2021-03-16
*/
public TriviaCategory getCategory(){
return category;
}
/**
* @return The difficulty selected by the player.
* @since 2021-03-16
*/
public TriviaDifficulty getDifficulty(){
return difficulty;
}
/**
* @return The type selected by the player.
* @since 2021-03-16
*/
public TriviaType getType(){
return type;
}
public static class Builder {
private final int amount;
private TriviaCategory category;
private TriviaDifficulty difficulty;
private TriviaType type;
public Builder() {
this.amount = DEFAULT_AMOUNT;
}
public Builder(int amount) {
if (amount > 50) {
this.amount = 50;
} else {
this.amount = amount;
}
}
public Builder category(TriviaCategory category) {
this.category = category;
return this;
}
public Builder difficulty(TriviaDifficulty difficulty) {
this.difficulty = difficulty;
return this;
}
public Builder type(TriviaType type) {
this.type = type;
return this;
}
public TriviaFilters build() {
return new TriviaFilters(this);
}
}
@Override
public String toString() {
StringBuilder url = new StringBuilder();
url.append("amount=").append(this.amount);
if (this.category != null & this.category != TriviaCategory.ANY) {
url.append("&category=").append(this.category.getID());
}
if (this.difficulty != null & this.difficulty != TriviaDifficulty.ANY) {
url.append("&difficulty=").append(this.difficulty.getName());
}
if (this.type != null & this.type != TriviaType.ANY) {
url.append("&type=").append(this.type.getName());
}
return url.toString();
}
}

View File

@ -38,8 +38,8 @@ public class TriviaQuery implements Serializable {
this.amount = builder.amount;
this.category = builder.category;
this.difficulty = builder.difficulty;
this.base = builder.base;
this.type = builder.type;
this.base = builder.base;
}
public static class Builder {

View File

@ -50,9 +50,12 @@ import com.luca0n.joguitos.pluck.exceptions.NoTriviaResultsException;
import com.luca0n.joguitos.pluck.exceptions.InvalidTriviaFormatException;
import com.luca0n.joguitos.pluck.trivia.TriviaQuery;
import com.luca0n.joguitos.pluck.trivia.TriviaQuestion;
import com.luca0n.joguitos.pluck.trivia.TriviaCategory;
import com.luca0n.joguitos.pluck.trivia.TriviaDifficulty;
import com.luca0n.joguitos.pluck.trivia.TriviaQuestionBoolean;
import com.luca0n.joguitos.pluck.trivia.TriviaQuestionMultiple;
import com.luca0n.joguitos.pluck.trivia.TriviaType;
import com.luca0n.joguitos.pluck.trivia.TriviaFilters;
public class ApiUtil {
@ -100,12 +103,14 @@ public class ApiUtil {
return response;
}
public static String GET(TriviaQuery query) throws IOException {
public static String GET(TriviaQuery query)
throws IOException {
return GET(query.toString());
}
public static ArrayList<TriviaQuestion> jsonToQuestionArray(String json)
throws NoTriviaResultsException, InvalidTriviaFormatException {
public static ArrayList<TriviaQuestion> jsonToQuestionArray(String json,
TriviaFilters filters) throws NoTriviaResultsException,
InvalidTriviaFormatException {
JsonObject jsonObject;
try{
jsonObject = new JsonParser().parse(json).getAsJsonObject();
@ -126,6 +131,16 @@ public class ApiUtil {
JsonObject object = element.getAsJsonObject();
TriviaType type = TriviaType.get(object.get("type").getAsString());
TriviaCategory fc = filters.getCategory();
// Check if this question matches the user specified criteria.
if (fc != null &&
fc != TriviaCategory.ANY && !object.get("category").getAsString().equals(fc.getName()))
continue;
if (filters.getDifficulty() != TriviaDifficulty.ANY && !object.get("difficulty").getAsString().equals(filters.getDifficulty().getName()))
continue;
if (type == TriviaType.MULTIPLE) {
questions.add(TriviaQuestionMultiple.fromJson(object));
} else {
@ -133,6 +148,10 @@ public class ApiUtil {
}
}
// Check if there are no questions that match the specified user criteria.
if (questions.size() == 0)
throw new NoTriviaResultsException();
return questions;
}
}