Commit acd04a69 authored by rakam's avatar rakam
Browse files

Improves ScoreSyncManager#synchronizeScore()

parent 79346b2e
......@@ -20,7 +20,6 @@ import java.util.concurrent.TimeoutException;
import fr.istic.masteru.android.R;
import fr.istic.masteru.android.utils.API_URLS;
import fr.istic.masteru.android.utils.ScoreSyncManager;
public class MainActivity extends Activity {
......@@ -70,8 +69,6 @@ public class MainActivity extends Activity {
Log.d("SecureDelayButton", "click");
}
});
ScoreSyncManager syncManager = new ScoreSyncManager(this, 22);
}
private void sendNumber() {
......
......@@ -57,7 +57,6 @@ public abstract class MatchActivity extends Activity {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
match = (Match) getIntent().getSerializableExtra(MATCH);
syncManager = new ScoreSyncManager(this, match.getId());
matchScoreView = (MatchScoreView) findViewById(R.id.matchScoreView);
pointLeftButton = (SecureDelayButton) findViewById(R.id.pointLeftButton);
pointRightButton = (SecureDelayButton) findViewById(R.id.pointRightButton);
......@@ -86,26 +85,19 @@ public abstract class MatchActivity extends Activity {
boolean resumeMatch = getIntent().getBooleanExtra(RESUME_MATCH, false);
if (resumeMatch) {
automate = FileManager.importAutomate(this);
automate.point(0);
Score initScore = automate.back(); // todo clean this dirty hack
if (initScore.isSideLeftA())
setPlayerALeft();
else
setPlayerARight();
updateScore(initScore);
fadeIn(null);
} else {
int service = getIntent().getIntExtra(SERVICE, 0);
boolean sideLeftA = getIntent().getBooleanExtra(SIDE_LEFT_A, true);
automate = getAutomateInstance(service, sideLeftA);
Score initScore = automate.getPoint(0);
if (initScore.isSideLeftA())
setPlayerALeft();
else
setPlayerARight();
updateScore(initScore);
fadeIn(null);
}
syncManager = new ScoreSyncManager(this, automate);
Score initScore = automate.last();
if (initScore.isSideLeftA())
setPlayerALeft();
else
setPlayerARight();
updateScore(initScore);
fadeIn(null);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
......
......@@ -13,6 +13,7 @@ import java.util.TreeMap;
import java.util.concurrent.CancellationException;
import java.util.concurrent.TimeoutException;
import fr.istic.masteru.android.automate.Automate;
import fr.istic.masteru.android.automate.Score;
import fr.istic.masteru.android.listeners.OnSyncFinishListener;
......@@ -29,12 +30,13 @@ public class ScoreSyncManager {
/**
* Delay before retry after a timeout.
*/
private static final int RETRY_DELAY = 6 * 1000;
private static final int RETRY_DELAY = 3 * 1000;
private static final String TAG = "SCORE_SYNC_MANAGER";
private Context context;
private Automate automate; // todo
private int idMatch;
private TreeMap<Float, Object> syncQueue = new TreeMap<Float, Object>();
private TreeMap<Float, Object> syncQueue = new TreeMap<>();
private boolean isSyncing = false;
private int scoreIdMax;
......@@ -43,9 +45,10 @@ public class ScoreSyncManager {
*/
private OnSyncFinishListener syncFinishListener;
public ScoreSyncManager(Context context, int idMatch) {
public ScoreSyncManager(Context context, Automate automate) {
this.context = context;
this.idMatch = idMatch;
this.idMatch = automate.getIdMatch();
this.automate = automate;
}
/**
......@@ -55,12 +58,17 @@ public class ScoreSyncManager {
* @param score The score to synchronize
*/
public void addScore(Score score) {
if (score.getId() < scoreIdMax) {
addScore(score, true);
}
private void addScore(Score score, boolean canBeBack) {
if (score.getId() < scoreIdMax && canBeBack) {
back(score.getId());
} else if (score.getId() > 0) {
if (score.getVainqueur() == -1) {
syncQueue.put((float) score.getId(), score);
scoreIdMax = score.getId();
if (canBeBack)
scoreIdMax = score.getId();
} else {
WinnerToSync winnerToSync = new WinnerToSync(score.getVainqueur() == 0 ? "A" : "B", false);
syncQueue.put(score.getId() + 0.2f, winnerToSync);
......@@ -129,7 +137,7 @@ public class ScoreSyncManager {
*
* @param score
*/
private void synchronizeScore(final float key, Score score) {
private void synchronizeScore(final float key, final Score score) {
JsonObject param = new JsonObject();
param.addProperty("pointId", score.getId());
param.addProperty("scoreA", score.getScoreA());
......@@ -149,51 +157,45 @@ public class ScoreSyncManager {
.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, "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, "Exception:" + e, Toast.LENGTH_SHORT).show();
Log.d(TAG, "ScoreException:" + 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);
// Toast.makeText(context, "MAJ OK", Toast.LENGTH_SHORT).show();
syncNextElement();
} else if (result.toString().contains("Duplicate entry")) {
syncQueue.remove(key);
syncNextElement();
// Toast.makeText(context, "DUPLICATE ENTRY WORKAROUND", Toast.LENGTH_SHORT).show();
Log.d(TAG, "DUPLICATE ENTRY WORKAROUND!!!");
} else {
Log.d(TAG, "" + result);
// todo handle the BAD_ID_POINT case
// Toast.makeText(context, "Error code:" + code + result, Toast.LENGTH_SHORT).show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
syncNextElement();
switch (code) {
case 0:
// Sync OK
Log.d(TAG, "SyncScore " + code);
syncQueue.remove(key);
break;
case -1:
Log.d(TAG, "SyncScore " + code);
break;
case -2:
Log.d(TAG, "SyncScore " + code);
break;
case -3:
Log.d(TAG, "SyncScore " + code);
int oldPointId = result.getAsJsonPrimitive("oldPointId").getAsInt();
for (int i = oldPointId + 1; i < score.getId(); i++) {
addScore(automate.getPoint(i), false);
}
}, RETRY_DELAY);
break;
case -4: // Duplicate entry
Log.d(TAG, "SyncScore " + code);
syncQueue.remove(key);
break;
}
syncNextElement();
} else {
if (e != null)
Log.d(TAG, "SyncScore ", e);
else
Log.d(TAG, "SyncScore error");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
syncNextElement();
}
}, RETRY_DELAY);
}
}
});
......
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