Game-service restructured; stable game flow; less waiting dialogs

This commit is contained in:
Caesar2011
2017-04-24 00:21:56 +02:00
parent 362e387d26
commit e4f0f805c5
23 changed files with 983 additions and 759 deletions

View File

@@ -5,22 +5,22 @@ package de.hwr_berlin.it14.postgrachelor.Exceptions;
* Exception to indicate a not instantiated service
*/
public class NotInstantiatedException extends Exception {
public class NotInitializedException extends Exception {
private static final long serialVersionUID = 1997753363232807009L;
public NotInstantiatedException() {
public NotInitializedException() {
super();
}
public NotInstantiatedException(String message) {
public NotInitializedException(String message) {
super(message);
}
public NotInstantiatedException(Throwable cause) {
public NotInitializedException(Throwable cause) {
super(cause);
}
public NotInstantiatedException(String message, Throwable cause) {
public NotInitializedException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -11,7 +11,7 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInstantiatedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInitializedException;
import de.hwr_berlin.it14.postgrachelor.Services.LoginService;
@@ -54,7 +54,7 @@ public class LoginFragment extends Fragment {
try {
LoginService.doLogin(login_edit.getText().toString());
} catch (NotInstantiatedException e) {
} catch (NotInitializedException e) {
e.printStackTrace();
}

View File

@@ -15,12 +15,13 @@ import java.util.HashMap;
import java.util.Set;
import java.util.UUID;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInstantiatedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInitializedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotLoggedInException;
import de.hwr_berlin.it14.postgrachelor.Services.GameService;
import de.hwr_berlin.it14.postgrachelor.Services.HighscoreService;
import de.hwr_berlin.it14.postgrachelor.Services.LoginService;
import de.hwr_berlin.it14.postgrachelor.Types.RequestActivityInterface;
import de.hwr_berlin.it14.postgrachelor.Types.StoredStates;
public class MainActivity extends AppCompatActivity implements RequestActivityInterface {
@@ -97,7 +98,7 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn
if (savedInstanceState != null) {
this.fragmentState = savedInstanceState.getInt("selected fragment");
this.selectFragment(savedInstanceState.getInt("selected fragment"));
}
}
@@ -111,11 +112,6 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn
public void onLoginEvent(String name, String uid) {
Log.d("Activity fragment", "onLogin - name: " + name + " - uid: " + uid);
that.selectFragment(FragmentState.MAIN);
try {
HighscoreService.updateHighscores();
} catch (NotInstantiatedException | NotLoggedInException e) {
e.printStackTrace();
}
}
@Override
@@ -128,16 +124,11 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn
@Override
public void onGameStateChangeEvent(int previous, int state) {
Log.d("Activity fragment", "onGameStateChange - previous: " + previous + " - state: " + state);
if (state == GameService.States.RUNNING || state == GameService.States.ON_HOLD_LOADING || state == GameService.States.ON_HOLD_RESULT)
if (state == StoredStates.RUNNING || state == StoredStates.ON_HOLD_LOADING || state == StoredStates.ON_HOLD_RESULT)
that.selectFragment(FragmentState.QUESTION);
else if (previous == GameService.States.ON_HOLD_RESULT && state == GameService.States.END) {
else if (previous == StoredStates.ON_HOLD_RESULT && state == StoredStates.END) {
that.selectFragment(FragmentState.QUESTION_END);
try {
HighscoreService.updateHighscores();
} catch (NotInstantiatedException | NotLoggedInException e) {
e.printStackTrace();
}
} else if (state == GameService.States.PAUSED || state == GameService.States.END) {
} else if (state == StoredStates.PAUSED || state == StoredStates.END) {
that.selectFragment(FragmentState.MAIN);
}
}
@@ -152,12 +143,10 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn
@Override
public void onBackPressed()
{
//this.selectFragment(FragmentState.MAIN);
boolean success;
try {
success = GameService.pauseGame();
HighscoreService.updateHighscores();
} catch (NotInstantiatedException | NotLoggedInException e) {
} catch (NotInitializedException e) {
success = false;
e.printStackTrace();
}
@@ -165,6 +154,13 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn
super.onBackPressed();
}
@Override
protected void onResume() {
super.onResume();
Log.d(NAME, "Resuming");
selectFragment(this.fragmentState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
@@ -188,6 +184,7 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn
}
private void selectFragment(int item) {
boolean change = this.fragmentState != item;
this.fragmentState = item;
// Handle navigation view item clicks here.
try {
@@ -197,7 +194,7 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn
} else if (isLoggedIn && (item == FragmentState.LOGIN)) {
return;
}
} catch (NotInstantiatedException e) {
} catch (NotInitializedException e) {
e.printStackTrace();
return;
}
@@ -217,6 +214,13 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn
fragmentTransaction.replace(R.id.relative_content, mainFragment, MainFragment.NAME);
fragmentTransaction.commit();
}
if (change) {
try {
HighscoreService.updateHighscores();
} catch (NotInitializedException | NotLoggedInException e) {
e.printStackTrace();
}
}
} else if (item == FragmentState.QUESTION_END) {
QuestionEndFragment fragment = (QuestionEndFragment) myFragmentManager.findFragmentByTag(QuestionEndFragment.NAME);
if (fragment == null) {

View File

@@ -19,13 +19,14 @@ import org.json.JSONObject;
import java.util.Iterator;
import java.util.Locale;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInstantiatedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInitializedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotLoggedInException;
import de.hwr_berlin.it14.postgrachelor.Services.GameService;
import de.hwr_berlin.it14.postgrachelor.Services.HighscoreService;
import de.hwr_berlin.it14.postgrachelor.Services.LoginService;
import de.hwr_berlin.it14.postgrachelor.Types.Highscores;
import de.hwr_berlin.it14.postgrachelor.Types.HighscoresCategories;
import de.hwr_berlin.it14.postgrachelor.Types.StoredStates;
import de.hwr_berlin.it14.postgrachelor.Utils.JsonRequestPG;
/**
@@ -76,7 +77,6 @@ public class MainFragment extends Fragment {
public void onLoginEvent(String name, String uid) {
TextView textView = (TextView) view.findViewById(R.id.fragment_main_status).findViewById(R.id.fragment_user_name);
textView.setText(name);
Log.d(NAME, "onLogin", new Throwable());
}
@Override
@@ -133,7 +133,6 @@ public class MainFragment extends Fragment {
textView.setText(String.format(Locale.getDefault(), "%1$d", category.getScore()));
catProgress = (ProgressBar) categoryView.findViewById(R.id.progressBarCategory);
Log.d(NAME, "all: " + scores.getAll() + " - place: " + category.getPlace() + " - prog: " + (scores.getAll() - category.getPlace() + 1));
catProgress.setProgress(0);
catProgress.setMax(scores.getAll());
catProgress.setProgress(scores.getAll() - category.getPlace() + 1);
@@ -162,7 +161,7 @@ public class MainFragment extends Fragment {
View userGameBackground = view.findViewById(R.id.fragment_main_game).findViewById(R.id.background);
TextView userGameTextView = (TextView) view.findViewById(R.id.fragment_main_game).findViewById(R.id.title);
ImageButton userStatusImage = (ImageButton) view.findViewById(R.id.fragment_main_game).findViewById(R.id.image_btn);
if (state == GameService.States.END || state == GameService.States.UNINITIALIZED) {
if (state == StoredStates.END || state == StoredStates.UNINITIALIZED) {
userGameBackground.setBackgroundResource(android.R.color.holo_green_dark);
userGameTextView.setText(R.string.start_test);
userStatusImage.setImageResource(android.R.drawable.ic_media_play);
@@ -181,14 +180,14 @@ public class MainFragment extends Fragment {
@Override
public void onClick(View v) {
try {
int state = GameService.getState();
int state = GameService.getState().getState();
Log.d(NAME, "Game click: "+state);
if (state == GameService.States.END || state == GameService.States.UNINITIALIZED) {
if (state == StoredStates.END || state == StoredStates.UNINITIALIZED) {
GameService.startGame();
} else {
GameService.resumeGame();
}
} catch (NotInstantiatedException | NotLoggedInException e) {
} catch (NotInitializedException | NotLoggedInException e) {
e.printStackTrace();
}
}
@@ -217,11 +216,11 @@ public class MainFragment extends Fragment {
}
});
} catch (NotInstantiatedException ignored) {
} catch (NotInitializedException ignored) {
} catch (NotLoggedInException e) {
try {
LoginService.setLogout(15999, "Not logged in");
} catch (NotInstantiatedException ignored) {
} catch (NotInitializedException ignored) {
}
}
}

View File

@@ -11,9 +11,10 @@ import android.widget.TextView;
import java.util.Locale;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInstantiatedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInitializedException;
import de.hwr_berlin.it14.postgrachelor.Services.GameService;
import de.hwr_berlin.it14.postgrachelor.Types.GameScores;
import de.hwr_berlin.it14.postgrachelor.Types.Scores;
import de.hwr_berlin.it14.postgrachelor.Types.StoredScores;
import de.hwr_berlin.it14.postgrachelor.Utils.Conversion;
@@ -53,7 +54,7 @@ public class QuestionEndFragment extends Fragment {
public void onClick(View v) {
try {
GameService.endGame();
} catch (NotInstantiatedException e) {
} catch (NotInitializedException e) {
e.printStackTrace();
}
}
@@ -61,7 +62,7 @@ public class QuestionEndFragment extends Fragment {
GameService.addGameEndEventListener(NAME, new GameService.OnGameEndEventListener() {
@Override
public void onGameEndEvent(GameScores scores) {
public void onGameEndEvent(Scores scores) {
Log.d(NAME, "onGameEndEvent" + scores);
if (scores != null) {
TextView textView = (TextView) view.findViewById(R.id.textViewScore);

View File

@@ -17,11 +17,13 @@ import android.widget.TextView;
import java.util.Locale;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NoCurrentQuestionException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInstantiatedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInitializedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotLoggedInException;
import de.hwr_berlin.it14.postgrachelor.Services.GameService;
import de.hwr_berlin.it14.postgrachelor.Types.Question;
import de.hwr_berlin.it14.postgrachelor.Types.QuestionResult;
import de.hwr_berlin.it14.postgrachelor.Types.Result;
import de.hwr_berlin.it14.postgrachelor.Types.StoredStates;
import de.hwr_berlin.it14.postgrachelor.Types.StoredTimings;
import de.hwr_berlin.it14.postgrachelor.Types.Timings;
import de.hwr_berlin.it14.postgrachelor.Utils.Conversion;
import de.hwr_berlin.it14.postgrachelor.Utils.ReverseInterpolator;
@@ -79,7 +81,7 @@ public class QuestionFragment extends Fragment {
if (GameService.isNextNeeded())
try {
GameService.runNext();
} catch (NotInstantiatedException e) {
} catch (NotInitializedException e) {
e.printStackTrace();
}
}
@@ -90,26 +92,28 @@ public class QuestionFragment extends Fragment {
questionAnswerViews[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (GameService.isNextNeeded()) {
try {
GameService.runNext();
} catch (NotInstantiatedException e) {
e.printStackTrace();
}
} else {
try {
if (GameService.getState() != GameService.States.ON_HOLD_RESULT)
if (GameService.isNextNeeded()) {
try {
GameService.runNext();
} catch (NotInitializedException e) {
e.printStackTrace();
}
} else if (GameService.getState().getState() != StoredStates.ON_HOLD_RESULT) {
try {
GameService.answer((int) v.getTag());
} catch (NotInstantiatedException | NotLoggedInException | NoCurrentQuestionException e) {
e.printStackTrace();
} catch (NotInitializedException | NotLoggedInException | NoCurrentQuestionException e) {
e.printStackTrace();
}
} else {
GameService.haltGame();
}
}
}
});
}
Context context = getActivity().getApplicationContext();
final int colorBgClicked = ContextCompat.getColor(context, android.R.color.holo_red_dark);
final int colorBgFalse = ContextCompat.getColor(context, android.R.color.holo_red_dark);
final int colorBgClicked = ContextCompat.getColor(context, android.R.color.holo_orange_dark);
final int colorBg = ContextCompat.getColor(context, android.R.color.holo_green_dark);
final int colorText = ContextCompat.getColor(context, android.R.color.black);
final Animation animation = AnimationUtils.loadAnimation(context, R.anim.scale_up);
@@ -123,10 +127,11 @@ public class QuestionFragment extends Fragment {
}
}
});
GameService.addNextQuestionEventListener(NAME, new GameService.OnNextQuestionEventListener() {
GameService.addQuestionUpdateEventListener(NAME, new GameService.OnQuestionUpdateEventListener() {
@Override
public void onNextQuestionEvent(Question question) {
Log.d(NAME, "onNextQuestionEvent - question: " + question);
public void onQuestionUpdateEvent(Question question, int answer, Result result) {
Log.d(NAME, "onQuestionUpdateEvent - question: " + question);
for (int i = 0; i < 4; i++) {
questionAnswerViews[i].setBackgroundColor(
ContextCompat.getColor(view.getContext(), R.color.colorPrimary)
@@ -135,6 +140,7 @@ public class QuestionFragment extends Fragment {
ContextCompat.getColor(view.getContext(), android.R.color.white)
);
}
if (question != null) {
questionTitleView.setText(question.getQuestion());
questionCategoryView.setText(view.getContext().getResources().getString(R.string.category, question.getCategory()));
@@ -148,28 +154,16 @@ public class QuestionFragment extends Fragment {
questionAnswerViews[i].setText("");
}
}
}
});
GameService.addResultEventListener(NAME, new GameService.OnResultEventListener() {
@Override
public void onResultEvent(QuestionResult result) {
Log.d(NAME, "onResultEvent - result: " + result);
if (result != null) {
int answerID;
try {
answerID = GameService.getAnswer();
} catch (NotInstantiatedException e) {
answerID = -1;
e.printStackTrace();
}
Log.d(NAME, "onResultEvent - answerID: " + answerID);
if (answerID != -1) {
questionAnswerViews[answerID].setBackgroundColor(colorBgClicked);
questionAnswerViews[result.getCorrectPos()].setBackgroundColor(colorBg);
questionAnswerViews[result.getCorrectPos()].setTextColor(colorText);
}
if (answer != -1 && result != null) {
questionAnswerViews[answer].setBackgroundColor(colorBgFalse);
} else if (answer != -1) {
questionAnswerViews[answer].setBackgroundColor(colorBgClicked);
}
if (result != null && result.getCorrectPos()>=0 && result.getCorrectPos()<=3) {
questionAnswerViews[result.getCorrectPos()].setBackgroundColor(colorBg);
questionAnswerViews[result.getCorrectPos()].setTextColor(colorText);
totalView.setText(Conversion.intToStr(result.getTotal()));
scoreView.setText(Conversion.intToStr(result.getScore()));
@@ -180,14 +174,16 @@ public class QuestionFragment extends Fragment {
}
}
});
GameService.addGameStateChangeEventListener(NAME, new GameService.OnGameStateChangeEventListener() {
@Override
public void onGameStateChangeEvent(int previous, int state) {
Log.d(NAME, Conversion.intToStr(previous, 5)+Conversion.intToStr(state,5));
if (previous != GameService.States.ON_HOLD_RESULT && state == GameService.States.RUNNING)
if (previous != StoredStates.ON_HOLD_RESULT && state == StoredStates.RUNNING)
totalView.setText(R.string._0);
}
});
return view;
}
}

View File

@@ -11,9 +11,8 @@ import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInstantiatedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInitializedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotLoggedInException;
import de.hwr_berlin.it14.postgrachelor.Types.GameScores;
import de.hwr_berlin.it14.postgrachelor.Types.Highscores;
import de.hwr_berlin.it14.postgrachelor.Types.HighscoresUser;
import de.hwr_berlin.it14.postgrachelor.Utils.Conversion;
@@ -42,12 +41,12 @@ public class HighscoreService {
public static void addHighscoreUpdateEventListener(OnHighscoreUpdateEventListener onLoginEventListener) {
highscoreUpdateEventListeners.add(onLoginEventListener);
onLoginEventListener.onHighscoreUpdateEvent(HighscoreService.latestScores);
onLoginEventListener.onHighscoreUpdateEvent(getLatestScores());
}
private static void emitHighscoreUpdateEvent() {
for (OnHighscoreUpdateEventListener listener: highscoreUpdateEventListeners) {
listener.onHighscoreUpdateEvent(latestScores);
listener.onHighscoreUpdateEvent(getLatestScores());
}
}
@@ -57,11 +56,11 @@ public class HighscoreService {
instantiated = true;
}
public static boolean available() {
private static boolean available() {
return (HighscoreService.latestScores != null);
}
public static Highscores getLatestScores() {
private static Highscores getLatestScores() {
return HighscoreService.latestScores;
}
@@ -70,22 +69,24 @@ public class HighscoreService {
emitHighscoreUpdateEvent();
}
public static void updateHighscores() throws NotInstantiatedException, NotLoggedInException {
public static void updateHighscores() throws NotInitializedException, NotLoggedInException {
HighscoreService.updateHighscores(null);
}
public static void updateHighscores(final JsonRequestPG.AsyncResponse asyncResponse) throws NotInstantiatedException, NotLoggedInException {
//if (lastUpdate >= System.currentTimeMillis()-MIN_REFRESH_RATE)
// return;
//lastUpdate = System.currentTimeMillis();
public static void updateHighscores(final JsonRequestPG.AsyncResponse asyncResponse) throws NotInitializedException, NotLoggedInException {
if (lastUpdate >= System.currentTimeMillis()-MIN_REFRESH_RATE && available()) {
emitHighscoreUpdateEvent();
return;
}
lastUpdate = System.currentTimeMillis();
if (!instantiated)
throw new NotInstantiatedException();
throw new NotInitializedException();
HashMap<String, String> params = new HashMap<>();
String uid = LoginService.getLoginUID();
Log.d(NAME, uid);
params.put("uid", LoginService.getLoginUID());
JsonRequestPG requester = new JsonRequestPG("highscores.php", params, activity, new JsonRequestPG.AsyncResponse() {
JsonRequestPG requester = new JsonRequestPG("highscores.php", params, /*activity*/null, new JsonRequestPG.AsyncResponse() {
@Override
public void processFinish(JSONObject output) {
// never reached if not instantiated
@@ -124,7 +125,7 @@ public class HighscoreService {
if (status==15102) { // user uid not found
try {
LoginService.setLogout(status, message);
} catch (NotInstantiatedException e) {
} catch (NotInitializedException e) {
e.printStackTrace();
}
}

View File

@@ -9,7 +9,7 @@ import org.json.JSONObject;
import java.util.HashMap;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInstantiatedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInitializedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotLoggedInException;
import de.hwr_berlin.it14.postgrachelor.Utils.JsonRequestPG;
@@ -36,11 +36,9 @@ public class LoginService {
if (isLoggedInSave()) {
try {
onLoginEventListener.onLoginEvent(getLoginName(), getLoginUID());
} catch (NotInstantiatedException | NotLoggedInException e) {
} catch (NotInitializedException | NotLoggedInException e) {
e.printStackTrace();
}
} else {
//onLoginEventListener.onLogoutEvent(-6, null);
}
}
@@ -65,7 +63,7 @@ public class LoginService {
try {
name = getLoginName();
uid = getLoginUIDSave();
} catch (NotInstantiatedException e) {
} catch (NotInitializedException e) {
e.printStackTrace();
}
if (isLoggedInSave()) {
@@ -75,41 +73,41 @@ public class LoginService {
}
}
public static boolean isLoggedIn() throws NotInstantiatedException {
public static boolean isLoggedIn() throws NotInitializedException {
if (!instantiated)
throw new NotInstantiatedException();
throw new NotInitializedException();
return settings.getBoolean("isLoggedIn", false);
}
private static boolean isLoggedInSave() {
try {
return isLoggedIn();
} catch (NotInstantiatedException e) {
} catch (NotInitializedException e) {
return false;
}
}
private static String getLoginName() throws NotInstantiatedException {
private static String getLoginName() throws NotInitializedException {
if (!LoginService.isLoggedIn())
return "";
return settings.getString("loginName", "");
}
static String getLoginUID() throws NotInstantiatedException, NotLoggedInException {
static String getLoginUID() throws NotInitializedException, NotLoggedInException {
if (!LoginService.isLoggedIn())
throw new NotLoggedInException();
return settings.getString("loginUID", "");
}
static String getLoginUIDSave() throws NotInstantiatedException {
private static String getLoginUIDSave() throws NotInitializedException {
if (!LoginService.isLoggedIn())
return "";
return settings.getString("loginUID", "");
}
private static void setLogin(String name, String uid) throws NotInstantiatedException {
private static void setLogin(String name, String uid) throws NotInitializedException {
if (!instantiated)
throw new NotInstantiatedException();
throw new NotInitializedException();
SharedPreferences.Editor editor = settings.edit();
editor.putString("loginName", name);
editor.putString("loginUID", uid);
@@ -118,9 +116,9 @@ public class LoginService {
emitLoginEvent(name, uid);
}
public static void setLogout(int status, String message) throws NotInstantiatedException {
public static void setLogout(int status, String message) throws NotInitializedException {
if (!instantiated)
throw new NotInstantiatedException();
throw new NotInitializedException();
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.apply();
@@ -130,20 +128,20 @@ public class LoginService {
private static void setLoginSave(String name, String uid) {
try {
setLogin(name, uid);
} catch (NotInstantiatedException ignored) {
} catch (NotInitializedException ignored) {
}
}
private static void setLogoutSave(int status, String message) {
try {
setLogout(status, message);
} catch (NotInstantiatedException ignored) {
} catch (NotInitializedException ignored) {
}
}
public static void doLogin(String name) throws NotInstantiatedException {
public static void doLogin(String name) throws NotInitializedException {
if (!instantiated)
throw new NotInstantiatedException();
throw new NotInitializedException();
HashMap<String, String> params = new HashMap<>();
params.put("name", name);
JsonRequestPG requester = new JsonRequestPG("register.php", params, activity, new JsonRequestPG.AsyncResponse() {

View File

@@ -8,12 +8,14 @@ package de.hwr_berlin.it14.postgrachelor.Types;
public class Question {
private final String category;
private final int categoryID;
private final String token;
private final String question;
private final String[] answers;
public Question(String category, int categoryID, String question, String[] answers) {
public Question(String category, int categoryID, String token, String question, String[] answers) {
this.category = category;
this.categoryID = categoryID;
this.token = token;
this.question = question;
this.answers = answers;
}
@@ -22,10 +24,14 @@ public class Question {
return category;
}
public int getCategoryID() {
int getCategoryID() {
return categoryID;
}
public String getToken() {
return token;
}
public String getQuestion() {
return question;
}

View File

@@ -5,13 +5,13 @@ package de.hwr_berlin.it14.postgrachelor.Types;
* Contains statistics of answering the last question
*/
public class QuestionResult {
public class Result {
private final int score;
private final int correctPos;
private final int total;
private final boolean correct;
public QuestionResult(boolean correct, int score, int correctPos, int total) {
public Result(boolean correct, int score, int correctPos, int total) {
this.correct = correct;
this.score = score;
this.correctPos = correctPos;

View File

@@ -1,16 +1,15 @@
package de.hwr_berlin.it14.postgrachelor.Types;
/**
* Created by Sebastian on 30.03.2017.
* Games scores on game end
* Created by Sebastian on 22.04.2017.
* Scores of a game
*/
public class GameScores {
public class Scores {
private final int score;
private final int time;
public GameScores(int score, int time) {
public Scores(int score, int time) {
this.score = score;
this.time = time;
}

View File

@@ -0,0 +1,25 @@
package de.hwr_berlin.it14.postgrachelor.Types;
/**
* Created by Sebastian on 23.04.2017.
* Current and previous game state
*/
public class States {
private final int previous;
private final int state;
public States(int previous, int state) {
this.previous = previous;
this.state = state;
}
public int getPrevious() {
return previous;
}
public int getState() {
return state;
}
}

View File

@@ -0,0 +1,153 @@
package de.hwr_berlin.it14.postgrachelor.Types;
import android.content.SharedPreferences;
import android.util.Log;
/**
* Created by Sebastian on 22.04.2017.
* Stored next entity
*/
public class StoredNextEntity extends StoredObject {
private class Next extends StoredObject {
private static final String STR_CATEGORY = "category";
private String category;
private static final String STR_CATEGORY_ID = "categoryID";
private int categoryID;
private static final String STR_TOKEN = "token";
private String token;
private static final String STR_QUESTION = "question";
private String question;
private static final String STR_ANSWER0 = "answer0";
private String answer0;
private static final String STR_ANSWER1 = "answer1";
private String answer1;
private static final String STR_ANSWER2 = "answer2";
private String answer2;
private static final String STR_ANSWER3 = "answer3";
private String answer3;
private Next(SharedPreferences prefs, String prefix) {
super(prefs, prefix+".next");
this.category = this.loadFromPref(STR_CATEGORY, "");
this.categoryID = this.loadFromPref(STR_CATEGORY_ID, -1);
this.token = this.loadFromPref(STR_TOKEN, "");
this.question = this.loadFromPref(STR_QUESTION, "");
this.answer0 = this.loadFromPref(STR_ANSWER0, "");
this.answer1 = this.loadFromPref(STR_ANSWER1, "");
this.answer2 = this.loadFromPref(STR_ANSWER2, "");
this.answer3 = this.loadFromPref(STR_ANSWER3, "");
}
private void set(Question question) {
this.category = question.getCategory();
this.saveToPref(STR_CATEGORY, this.category);
this.categoryID = question.getCategoryID();
this.saveToPref(STR_CATEGORY_ID, this.categoryID);
this.token = question.getToken();
this.saveToPref(STR_TOKEN, this.token);
this.question = question.getQuestion();
this.saveToPref(STR_QUESTION, this.question);
this.answer0 = question.getAnswers()[0];
this.saveToPref(STR_ANSWER0, this.answer0);
this.answer1 = question.getAnswers()[1];
this.saveToPref(STR_ANSWER1, this.answer1);
this.answer2 = question.getAnswers()[2];
this.saveToPref(STR_ANSWER2, this.answer2);
this.answer3 = question.getAnswers()[3];
this.saveToPref(STR_ANSWER3, this.answer3);
this.unremove();
}
public Question get() {
if (!this.isLoaded())
return null;
return new Question(this.category, this.categoryID, this.token, this.question, new String[]{
this.answer0,
this.answer1,
this.answer2,
this.answer3
});
}
protected void remove() {
super.remove();
}
}
private class End extends StoredObject {
private static final String STR_SCORE = "score";
private int score;
private static final String STR_TIME = "time";
private int time;
private End(SharedPreferences prefs, String prefix) {
super(prefs, prefix+".end");
this.score = this.loadFromPref(STR_SCORE, 0);
this.time = this.loadFromPref(STR_TIME, 0);
}
private void set(Scores scores) {
this.score = scores.getScore();
this.saveToPref(STR_SCORE, this.score);
this.time = scores.getTime();
this.saveToPref(STR_TIME, this.time);
this.unremove();
}
private Scores get() {
if (!this.isLoaded())
return null;
return new Scores(this.score, this.time);
}
@Override
public void remove() {
super.remove();
}
}
private final Next next;
private final End end;
public StoredNextEntity(SharedPreferences prefs, String prefix) {
super(prefs, prefix+".nextEntity");
this.next = new Next(prefs, prefix+".nextEntity");
this.end = new End(prefs, prefix+".nextEntity");
}
public void setNext(Question question) {
this.next.set(question);
this.end.remove();
this.unremove();
}
public void setEnd(Scores scores) {
this.end.set(scores);
this.next.remove();
this.unremove();
}
public Question getNext() {
Log.d("getNext", String.valueOf(this.next.get() != null));
return this.next.get();
}
public Scores getEnd() {
Log.d("getEnd", String.valueOf(this.end.get() != null));
return this.end.get();
}
public boolean isNext() {
return this.next.isLoaded();
}
public boolean isEnd() {
return this.end.isLoaded();
}
public void remove() {
super.remove();
}
}

View File

@@ -1,49 +1,90 @@
package de.hwr_berlin.it14.postgrachelor.Types;
import android.content.SharedPreferences;
import android.util.Log;
import android.util.NoSuchPropertyException;
import java.util.NoSuchElementException;
/**
* Created by Sebastian on 18.04.2017.
* Stored object
*/
public class StoredObject {
abstract class StoredObject {
private SharedPreferences prefs;
private String prefix;
public StoredObject(SharedPreferences prefs, String prefix){
StoredObject(SharedPreferences prefs, String prefix){
this.prefs = prefs;
this.prefix = prefix;
}
protected void saveToPref(String name, String value) {
protected void remove() {
SharedPreferences.Editor editor = this.prefs.edit();
editor.putBoolean("L"+this.prefix, false);
editor.apply();
Log.d("StoredObject-remove", "L"+this.prefix);
}
void unremove() {
SharedPreferences.Editor editor = this.prefs.edit();
editor.putBoolean("L"+this.prefix, true);
editor.apply();
Log.d("StoredObject-unremove", "L"+this.prefix);
}
public boolean isLoaded() {
return this.prefs.getBoolean("L"+this.prefix, false);
}
void saveToPref(String name, String value) {
SharedPreferences.Editor editor = this.prefs.edit();
editor.putString(this.prefix+"."+name, value);
editor.apply();
}
protected void saveToPref(String name, int value) {
void saveToPref(String name, int value) {
SharedPreferences.Editor editor = this.prefs.edit();
editor.putInt(this.prefix+"."+name, value);
editor.apply();
}
protected void saveToPref(String name, long value) {
void saveToPref(String name, long value) {
SharedPreferences.Editor editor = this.prefs.edit();
editor.putLong(this.prefix+"."+name, value);
editor.apply();
}
protected String loadString(String name) {
return this.prefs.getString(name, "");
void saveToPref(String name, boolean value) {
SharedPreferences.Editor editor = this.prefs.edit();
editor.putBoolean(this.prefix+"."+name, value);
editor.apply();
}
protected int loadInt(String name) {
return this.prefs.getInt(name, 0);
String loadFromPref(String name, String defValue) {
if (!this.isLoaded())
return defValue;
return this.prefs.getString(this.prefix+"."+name, defValue);
}
protected long loadLong(String name) {
return this.prefs.getLong(name, 0);
int loadFromPref(String name, int defValue) {
if (!this.isLoaded())
return defValue;
return this.prefs.getInt(this.prefix+"."+name, defValue);
}
long loadFromPref(String name, long defValue) {
if (!this.isLoaded())
return defValue;
return this.prefs.getLong(this.prefix+"."+name, defValue);
}
boolean loadFromPref(String name, boolean defValue) {
if (!this.isLoaded())
return defValue;
return this.prefs.getBoolean(this.prefix+"."+name, defValue);
}
}

View File

@@ -0,0 +1,207 @@
package de.hwr_berlin.it14.postgrachelor.Types;
import android.content.SharedPreferences;
/**
* Created by Sebastian on 22.04.2017.
* Stored question object
*/
public class StoredQuestion extends StoredObject {
private class Question extends StoredObject {
private static final String STR_CATEGORY = "category";
private String category;
private static final String STR_CATEGORY_ID = "categoryID";
private int categoryID;
private static final String STR_TOKEN = "token";
private String token;
private static final String STR_QUESTION = "question";
private String question;
private static final String STR_ANSWER0 = "answer0";
private String answer0;
private static final String STR_ANSWER1 = "answer1";
private String answer1;
private static final String STR_ANSWER2 = "answer2";
private String answer2;
private static final String STR_ANSWER3 = "answer3";
private String answer3;
private Question(SharedPreferences prefs, String prefix) {
super(prefs, prefix+".question");
this.category = this.loadFromPref(STR_CATEGORY, "");
this.categoryID = this.loadFromPref(STR_CATEGORY_ID, -1);
this.token = this.loadFromPref(STR_TOKEN, "");
this.question = this.loadFromPref(STR_QUESTION, "");
this.answer0 = this.loadFromPref(STR_ANSWER0, "");
this.answer1 = this.loadFromPref(STR_ANSWER1, "");
this.answer2 = this.loadFromPref(STR_ANSWER2, "");
this.answer3 = this.loadFromPref(STR_ANSWER3, "");
}
private void set(de.hwr_berlin.it14.postgrachelor.Types.Question question) {
this.category = question.getCategory();
this.saveToPref(STR_CATEGORY, this.category);
this.categoryID = question.getCategoryID();
this.saveToPref(STR_CATEGORY_ID, this.categoryID);
this.token = question.getToken();
this.saveToPref(STR_TOKEN, this.token);
this.question = question.getQuestion();
this.saveToPref(STR_QUESTION, this.question);
this.answer0 = question.getAnswers()[0];
this.saveToPref(STR_ANSWER0, this.answer0);
this.answer1 = question.getAnswers()[1];
this.saveToPref(STR_ANSWER1, this.answer1);
this.answer2 = question.getAnswers()[2];
this.saveToPref(STR_ANSWER2, this.answer2);
this.answer3 = question.getAnswers()[3];
this.saveToPref(STR_ANSWER3, this.answer3);
this.unremove();
}
public de.hwr_berlin.it14.postgrachelor.Types.Question get() {
if (!this.isLoaded())
return null;
return new de.hwr_berlin.it14.postgrachelor.Types.Question(this.category, this.categoryID, this.token, this.question, new String[]{
this.answer0,
this.answer1,
this.answer2,
this.answer3
});
}
protected void remove() {
super.remove();
}
}
private class Answer extends StoredObject {
private static final String STR_ANSWER = "answer";
private int answer;
private Answer(SharedPreferences prefs, String prefix) {
super(prefs, prefix+".answer");
this.answer = this.loadFromPref(STR_ANSWER, -1);
}
private void set(int answer) {
if (this.isLoaded())
return;
this.answer = answer;
this.saveToPref(STR_ANSWER, this.answer);
this.unremove();
}
private int get() {
if (this.isLoaded())
return this.answer;
else return -1;
}
@Override
public void remove() {
super.remove();
}
}
private class Result extends StoredObject {
private static final String STR_SCORE = "score";
private int score;
private static final String STR_CORRECT_POS = "correctPos";
private int correctPos;
private static final String STR_TOTAL = "total";
private int total;
private static final String STR_IS_CORRECT = "isCorrect";
private boolean isCorrect;
private Result(SharedPreferences prefs, String prefix) {
super(prefs, prefix + ".result");
this.score = this.loadFromPref(STR_SCORE, 0);
this.correctPos = this.loadFromPref(STR_CORRECT_POS, 0);
this.total = this.loadFromPref(STR_TOTAL, 0);
this.isCorrect = this.loadFromPref(STR_IS_CORRECT, false);
}
private void set(de.hwr_berlin.it14.postgrachelor.Types.Result result) {
this.isCorrect = result.isCorrect();
this.saveToPref(STR_IS_CORRECT, this.isCorrect);
this.score = result.getScore();
this.saveToPref(STR_SCORE, this.score);
this.correctPos = result.getCorrectPos();
this.saveToPref(STR_CORRECT_POS, this.correctPos);
this.total = result.getTotal();
this.saveToPref(STR_TOTAL, this.total);
this.unremove();
}
private de.hwr_berlin.it14.postgrachelor.Types.Result get() {
if (!this.isLoaded())
return null;
return new de.hwr_berlin.it14.postgrachelor.Types.Result(
this.isCorrect,
this.score,
this.correctPos,
this.total
);
}
}
private final Question question;
private final Answer answer;
private final Result result;
public StoredQuestion(SharedPreferences prefs, String prefix) {
super(prefs, prefix+".question");
this.question = new Question(prefs, prefix+".question");
this.answer = new Answer(prefs, prefix+".question");
this.result = new Result(prefs, prefix+".question");
if (this.question.isLoaded())
this.unremove();
else
this.remove();
}
public void setQuestion(de.hwr_berlin.it14.postgrachelor.Types.Question question) {
this.question.set(question);
this.answer.remove();
this.result.remove();
this.unremove();
}
public void setAnswer(int answer) {
this.answer.set(answer);
}
public void setResult(de.hwr_berlin.it14.postgrachelor.Types.Result result) {
this.result.set(result);
}
public de.hwr_berlin.it14.postgrachelor.Types.Question getQuestion() {
return this.question.get();
}
public int getAnswer() {
return this.answer.get();
}
public de.hwr_berlin.it14.postgrachelor.Types.Result getResult() {
return this.result.get();
}
public boolean isQuestion() {
return this.question.isLoaded();
}
public boolean isAnswer() {
return this.answer.isLoaded();
}
public boolean isResult() {
return this.result.isLoaded();
}
public void remove() {
super.remove();
}
}

View File

@@ -0,0 +1,40 @@
package de.hwr_berlin.it14.postgrachelor.Types;
import android.content.SharedPreferences;
/**
* Created by Sebastian on 30.03.2017.
* Games scores on game end
*/
public class StoredScores extends StoredObject {
private int score;
private static final String STR_SCORE = "score";
private int time;
private static final String STR_TIME = "time";
public StoredScores(SharedPreferences prefs, String prefix) {
super(prefs, prefix+".scores");
this.score = this.loadFromPref(STR_SCORE, -1);
this.time = this.loadFromPref(STR_TIME, -1);
}
public void remove() {
super.remove();
}
public void set(Scores scores) {
this.score = scores.getScore();
this.saveToPref(STR_SCORE, this.score);
this.time = scores.getTime();
this.saveToPref(STR_TIME, this.time);
this.unremove();
}
public Scores get() {
if (!this.isLoaded())
return null;
return new Scores(this.score, this.time);
}
}

View File

@@ -0,0 +1,48 @@
package de.hwr_berlin.it14.postgrachelor.Types;
import android.content.SharedPreferences;
import java.util.NoSuchElementException;
/**
* Created by Sebastian on 21.04.2017.
* Stored game state object
*/
public final class StoredStates extends StoredObject {
public static final int UNINITIALIZED = 1;
public static final int END = 2;
public static final int ON_HOLD_LOADING = 4;
public static final int ON_HOLD_RESULT = 8;
public static final int RUNNING = 16;
public static final int PAUSED = 32;
private int state;
private static final String STR_STATE = "state";
private int previous;
private static final String STR_PREVIOUS = "previous";
public StoredStates(SharedPreferences prefs, String prefix) {
super(prefs, prefix+".states");
this.previous = this.loadFromPref(STR_PREVIOUS, END);
this.state = this.loadFromPref(STR_STATE, END);
this.unremove();
}
@Override
public boolean isLoaded() {
return true;
}
public States get() {
return new States(this.previous, this.state);
}
public void nextState(int state) {
this.previous = this.state;
this.state = state;
this.saveToPref(STR_PREVIOUS, this.previous);
this.saveToPref(STR_STATE, this.state);
}
}

View File

@@ -0,0 +1,71 @@
package de.hwr_berlin.it14.postgrachelor.Types;
import android.content.SharedPreferences;
import android.provider.Settings;
/**
* Created by Sebastian on 06.04.2017.
* Timing results on each tick, when game is running
*/
public class StoredTimings extends StoredObject {
private static final int M = 1000;
private static final int N = 100;
private static final int R = 30;
private static final int S = 0;
private static final int T = 100;
private long startTime;
private static final String STR_STARTTIME = "startTime";
private long endTime;
private static final String STR_ENDTIME = "endTime";
public StoredTimings(SharedPreferences prefs, String prefix) {
super(prefs, prefix+".timings");
this.startTime = this.loadFromPref(STR_STARTTIME, (long) -1);
this.endTime = this.loadFromPref(STR_ENDTIME, (long) -1);
if (this.startTime == -1)
this.remove();
else
this.unremove();
}
public void start() {
this.startTime = System.currentTimeMillis();
this.saveToPref(STR_STARTTIME, this.startTime);
this.endTime = -1;
this.saveToPref(STR_ENDTIME, this.endTime);
this.unremove();
}
public void stop() {
if (this.endTime != -1)
return;
this.endTime = System.currentTimeMillis();
this.saveToPref(STR_ENDTIME, this.endTime);
}
public void reset() {
this.remove();
}
public Timings get() {
return new Timings(this.getTimeDiff(), this.getScore());
}
private long getTimeDiff() {
if (!this.isLoaded())
return 0;
else if (this.endTime != -1)
return this.endTime-this.startTime;
else
return System.currentTimeMillis()-this.startTime;
}
private int getScore() {
//this.score = Math.max((int) Math.floor(1025 - 5*Math.sqrt(2*timeDiff-3975)), 10);
//this.score = (int) Math.max(N, M-(R*Math.sqrt(T*(-8*S+T+8*this.timeDiff))-T)/2/T);
// simplified
return (int) Math.max(N, M-(R*Math.sqrt(T*(T + 8 * this.getTimeDiff()))-T)/2/T);
}
}

View File

@@ -1,29 +1,17 @@
package de.hwr_berlin.it14.postgrachelor.Types;
/**
* Created by Sebastian on 06.04.2017.
* Timing results on each tick, when game is running
* Created by Sebastian on 23.04.2017.
* Time and score measurement
*/
public class Timings {
private static final int M = 1000;
private static final int N = 100;
private static final int R = 30;
private static final int S = 0;
private static final int T = 100;
private final long timeDiff;
private final int score;
public Timings(long timeDiff) {
Timings(long timeDiff, int score) {
this.timeDiff = timeDiff;
if (timeDiff <= S)
this.score = M;
else {
//this.score = Math.max((int) Math.floor(1025 - 5*Math.sqrt(2*timeDiff-3975)), 10);
//this.score = (int) Math.max(N, M-(R*Math.sqrt(T*(-8*S+T+8*this.timeDiff))-T)/2/T);
// simplified
this.score = (int) Math.max(N, M-(R*Math.sqrt(T*(T + 8 * this.timeDiff))-T)/2/T);
}
this.score = score;
}
public long getTimeDiff() {

View File

@@ -14,6 +14,7 @@ import de.hwr_berlin.it14.postgrachelor.Types.HighscoresUser;
/**
* Created by Sebastian on 11.04.2017.
* Async highscore adapter for list view
*/
public class HighscoreAdapter extends AsyncAdapter<HighscoresUser> {
@@ -81,6 +82,6 @@ public class HighscoreAdapter extends AsyncAdapter<HighscoresUser> {
@Override
protected int getRequestedLength(int position) {
return Math.min(1, getCount()-position);
return Math.min(10, getCount()-position);
}
}

View File

@@ -3,6 +3,7 @@ package de.hwr_berlin.it14.postgrachelor.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
@@ -96,16 +97,17 @@ public class JsonRequestPG extends JsonRequest {
int status = meta.optInt("status", -3);
String message = meta.optString("message", "");
if (status != 0) {
new AlertDialog.Builder(this.activity)
.setTitle("Error "+status)
.setMessage(message)
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
if (this.activity != null)
new AlertDialog.Builder(this.activity)
.setTitle("Error "+status)
.setMessage(message)
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
this.processError(status, message);
return;
}

View File

@@ -5,6 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/fragment_main"
android:clickable="true"
android:padding="5dp">
<View
@@ -42,6 +43,7 @@
android:paddingStart="50dp"
android:layout_alignParentRight="true"
android:paddingRight="50dp"
android:paddingLeft="50dp" />
android:paddingLeft="50dp"
android:clickable="false" />
</RelativeLayout>