Commit d58b93ce authored by rakam's avatar rakam
Browse files

New API (Back)

parent 6556e273
......@@ -6,7 +6,6 @@ public class API_URLS {
public static final String PING_ADD_NUMBER = BASE_URL + "/test/nombres/ajouterNombre";
public static final String SCORE_UPDATE = BASE_URL + "/score/update";
public static final String SET_SCORE_UPDATE = BASE_URL + "/score/majSet";
public static final String SCORE_BACK = BASE_URL + "/score/supprPoint";
public static final String MATCH_WINNER = BASE_URL + "/match/finMatch";
public static final String MATCHS = BASE_URL + "/matches";
public static final String INFO_MATCH = BASE_URL + "/match/info/"; // + idMatch
......@@ -23,4 +22,8 @@ public class API_URLS {
return BASE_URL + "/matches/" + matchId + "/score/" + pointId;
}
public static String DELETE_SCORE(int matchId, int pointId) {
return BASE_URL + "/matches/" + matchId + "/score/" + pointId;
}
}
......@@ -34,7 +34,7 @@ public class ScoreSyncManager {
private static final String TAG = "SCORE_SYNC_MANAGER";
private Context context;
private Automate automate; // todo
private Automate automate;
private int idMatch;
private TreeMap<Float, Object> syncQueue = new TreeMap<>();
private boolean isSyncing = false;
......@@ -267,57 +267,38 @@ public class ScoreSyncManager {
* @param backToSync
*/
private void syncBack(final float key, BackToSync backToSync) {
JsonObject param = new JsonObject();
param.addProperty("idMatch", idMatch);
param.addProperty("idPoint", backToSync.getId());
Log.d("syncBackJSON", param.toString());
Ion.with(context)
.load("POST", API_URLS.SCORE_BACK)
.load("DELETE", API_URLS.DELETE_SCORE(idMatch, backToSync.getId()))
.setTimeout(TIMEOUT_MS)
.setJsonObjectBody(param)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (e != null) { // Exception
if (e instanceof CancellationException || e instanceof TimeoutException) {
// Toast.makeText(context, "Back Cancelled", Toast.LENGTH_SHORT).show();
// After a timeout, retry in 5 seconds
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
syncNextElement();
}
}, RETRY_DELAY);
} else {
// Toast.makeText(context, "Back Exception:" + e, Toast.LENGTH_SHORT).show();
Log.d(TAG, "BackException:" + e);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
syncNextElement();
}
}, RETRY_DELAY);
}
} else { // JSON
if (result != null && result.has("code")) {
int code = result.getAsJsonPrimitive("code").getAsInt();
if (code == 0) { // MAJ OK
syncQueue.remove(key);
syncNextElement();
// Toast.makeText(context, "Back OK", Toast.LENGTH_SHORT).show();
} else {
Log.d(TAG, "Back:" + result);
// Toast.makeText(context, "Back Error code:" + code, Toast.LENGTH_SHORT).show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
syncNextElement();
}
}, RETRY_DELAY);
switch (code) {
case 0:
// Sync OK
Log.d(TAG, "Back " + code);
syncQueue.remove(key);
break;
case -2:
Log.d(TAG, "Back " + code);
break;
}
syncNextElement();
} else {
if (e != null)
Log.d(TAG, "Back ", e);
else
Log.d(TAG, "Back error");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
syncNextElement();
}
}, RETRY_DELAY);
}
}
});
......@@ -327,6 +308,7 @@ public class ScoreSyncManager {
* @param key
* @param winnerToSync
*/
private void syncWinner(final float key, WinnerToSync winnerToSync) {
JsonObject param = new JsonObject();
param.addProperty("action", winnerToSync.isAbandon() ? "ABANDON" : "END");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment