Commit acd04a69 authored by rakam's avatar rakam
Browse files

Improves ScoreSyncManager#synchronizeScore()

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