Error handling and highscore bug fix

This commit is contained in:
Caesar2011
2017-04-07 14:27:11 +02:00
parent ef78bff9be
commit fcb1ca3aa0
4 changed files with 40 additions and 18 deletions

View File

@@ -108,7 +108,7 @@ public class MainFragment extends Fragment {
textView.setText(String.format(Locale.getDefault(), "%1$d", category.getScore()));
catProgress = (ProgressBar) categoryView.findViewById(R.id.progressBarCategory);
catProgress.setProgress(category.getPlace());
catProgress.setProgress(scores.getAll()-category.getPlace()+1);
catProgress.setMax(scores.getAll());
gridLayoutCategories.addView(categoryView);
@@ -119,7 +119,7 @@ public class MainFragment extends Fragment {
userStatusTextViewScore.setText(String.format(Locale.getDefault(), "%1$d", scores.getScore()));
userStatusProgressBar.setMax(scores.getAll());
userStatusProgressBar.setProgress(scores.getPlace());
userStatusProgressBar.setProgress(scores.getAll()-scores.getPlace()+1);
}
}
});

View File

@@ -396,10 +396,7 @@ public class GameService {
HashMap<String, String> params = new HashMap<>();
String uid;
if (LoginService.isLoggedIn())
uid = LoginService.getLoginUID();
else
throw new NotLoggedInException();
params.put("answer", Integer.toString(id));
params.put("uid", uid);
params.put("time", Long.toString(time));
@@ -457,10 +454,7 @@ public class GameService {
HashMap<String, String> params = new HashMap<>();
String uid;
if (LoginService.isLoggedIn())
uid = LoginService.getLoginUID();
else
throw new NotLoggedInException();
params.put("uid", uid);
params.put("length", Conversion.intToStr(GameService.QUESTION_COUNT));
JsonRequestPG requester = new JsonRequestPG("start.php", params, GameService.activity, new JsonRequestPG.AsyncResponse() {
@@ -481,6 +475,13 @@ public class GameService {
@Override
public void processError(int status, String message) {
GameService.setStateSave(States.END);
if (status==12104) { // user uid not found
try {
LoginService.setLogout(status, message);
} catch (NotInstantiatedException e) {
e.printStackTrace();
}
}
}
});
requester.execute();

View File

@@ -9,8 +9,10 @@ import org.json.JSONException;
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.NotLoggedInException;
import de.hwr_berlin.it14.postgrachelor.Types.GameScores;
import de.hwr_berlin.it14.postgrachelor.Types.Highscores;
import de.hwr_berlin.it14.postgrachelor.Utils.JsonRequestPG;
@@ -54,7 +56,7 @@ public class HighscoreService {
public void onLoginEvent(String name, String uid) {
try {
HighscoreService.updateHighscores();
} catch (NotInstantiatedException e) {
} catch (NotInstantiatedException | NotLoggedInException e) {
e.printStackTrace();
HighscoreService.setLatestScores(null);
}
@@ -71,7 +73,7 @@ public class HighscoreService {
public void onGameEndEvent(GameScores scores) {
try {
HighscoreService.updateHighscores();
} catch (NotInstantiatedException e) {
} catch (NotInstantiatedException | NotLoggedInException e) {
e.printStackTrace();
HighscoreService.setLatestScores(null);
}
@@ -92,10 +94,15 @@ public class HighscoreService {
emitHighscoreUpdateEvent();
}
private static void updateHighscores() throws NotInstantiatedException {
private static void updateHighscores() throws NotInstantiatedException, NotLoggedInException {
if (!instantiated)
throw new NotInstantiatedException();
JsonRequestPG requester = new JsonRequestPG("highscores.php", null, activity, new JsonRequestPG.AsyncResponse() {
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() {
@Override
public void processFinish(JSONObject output) {
Log.d("Activity fragment", "output");
@@ -130,6 +137,13 @@ public class HighscoreService {
@Override
public void processError(int status, String message) {
setLatestScores(null);
if (status==15102) { // user uid not found
try {
LoginService.setLogout(status, message);
} catch (NotInstantiatedException e) {
e.printStackTrace();
}
}
}
});
requester.execute();

View File

@@ -10,6 +10,7 @@ import org.json.JSONObject;
import java.util.HashMap;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInstantiatedException;
import de.hwr_berlin.it14.postgrachelor.Exceptions.NotLoggedInException;
import de.hwr_berlin.it14.postgrachelor.Utils.JsonRequestPG;
/**
@@ -35,7 +36,7 @@ public class LoginService {
if (isLoggedInSave()) {
try {
onLoginEventListener.onLoginEvent(getLoginName(), getLoginUID());
} catch (NotInstantiatedException e) {
} catch (NotInstantiatedException | NotLoggedInException e) {
e.printStackTrace();
}
} else {
@@ -63,7 +64,7 @@ public class LoginService {
String uid = null;
try {
name = getLoginName();
uid = getLoginUID();
uid = getLoginUIDSave();
} catch (NotInstantiatedException e) {
e.printStackTrace();
}
@@ -94,7 +95,13 @@ public class LoginService {
return settings.getString("loginName", "");
}
static String getLoginUID() throws NotInstantiatedException {
static String getLoginUID() throws NotInstantiatedException, NotLoggedInException {
if (!LoginService.isLoggedIn())
throw new NotLoggedInException();
return settings.getString("loginUID", "");
}
static String getLoginUIDSave() throws NotInstantiatedException {
if (!LoginService.isLoggedIn())
return "";
return settings.getString("loginUID", "");
@@ -111,7 +118,7 @@ public class LoginService {
emitLoginEvent(name, uid);
}
private static void setLogout(int status, String message) throws NotInstantiatedException {
static void setLogout(int status, String message) throws NotInstantiatedException {
if (!instantiated)
throw new NotInstantiatedException();
SharedPreferences.Editor editor = settings.edit();