Commit 5e67feac authored by rakam's avatar rakam
Browse files

Sync Point

parent af57d15d
......@@ -18,12 +18,12 @@ import java.util.List;
import fr.istic.masteru.android.R;
import fr.istic.masteru.android.automate.Automate;
import fr.istic.masteru.android.automate.FileManager;
import fr.istic.masteru.android.automate.Point;
import fr.istic.masteru.android.automate.Score;
import fr.istic.masteru.android.automate.SimplePoint;
import fr.istic.masteru.android.automate.Stats;
import fr.istic.masteru.android.listeners.OnSyncFinishListener;
import fr.istic.masteru.android.model.Match;
import fr.istic.masteru.android.model.Point;
import fr.istic.masteru.android.model.Stats;
import fr.istic.masteru.android.utils.ScoreSyncManager;
import fr.istic.masteru.android.utils.Utils;
import fr.istic.masteru.android.views.MatchScoreView;
......@@ -284,7 +284,7 @@ public abstract class MatchActivity extends Activity {
matchScoreView.setScore(score.getGamesA(), score.getGamesB(), score.getNumSet(), score.getScoreA(), score.getScoreB());
}
syncManager.addScore(score);
syncManager.addPoint(point);
}
/**
......@@ -336,57 +336,37 @@ public abstract class MatchActivity extends Activity {
}
protected void setStatsButtonsForPlayerALeft() {
leftPointButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateUI(new Point(automate.point(0), Stats.NONE));
}
});
leftAceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateUI(new Point(automate.point(0), Stats.ACE));
}
});
rightPointButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateUI(new Point(automate.point(1), Stats.NONE));
}
});
rightAceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateUI(new Point(automate.point(1), Stats.ACE));
}
});
setStatsButtons(0, 1);
}
protected void setStatsButtonsForPlayerARight() {
setStatsButtons(1, 0);
}
private void setStatsButtons(final int leftPlayer, final int rightPlayer) {
leftPointButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateUI(new Point(automate.point(1), Stats.NONE));
updateUI(new Point(automate.point(1), new Stats(Stats.Type.NONE, false, leftPlayer)));
}
});
leftAceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateUI(new Point(automate.point(1), Stats.ACE));
updateUI(new Point(automate.point(1), new Stats(Stats.Type.ACE, false, leftPlayer)));
}
});
rightPointButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateUI(new Point(automate.point(0), Stats.NONE));
updateUI(new Point(automate.point(0), new Stats(Stats.Type.NONE, false, rightPlayer)));
}
});
rightAceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateUI(new Point(automate.point(0), Stats.ACE));
updateUI(new Point(automate.point(0), new Stats(Stats.Type.ACE, false, rightPlayer)));
}
});
}
......
package fr.istic.masteru.android.model;
import fr.istic.masteru.android.automate.Score;
package fr.istic.masteru.android.automate;
public class Point {
private final Score score;
private final Stats stats;
private Score score;
private Stats stats;
public Point(Score score, Stats stats) {
this.score = score;
......@@ -16,7 +14,15 @@ public class Point {
return score;
}
public void setScore(Score score) {
this.score = score;
}
public Stats getStats() {
return stats;
}
public void setStats(Stats stats) {
this.stats = stats;
}
}
package fr.istic.masteru.android.automate;
public class Stats {
public static enum Type {NONE, ACE, DF}
private Type type;
private boolean fsf;
private int winner;
public Stats(Type type, boolean fsf, int winner) {
this.type = type;
this.fsf = fsf;
this.winner = winner;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public boolean isFsf() {
return fsf;
}
public void setFsf(boolean fsf) {
this.fsf = fsf;
}
public int getWinner() {
return winner;
}
public void setWinner(int winner) {
this.winner = winner;
}
}
package fr.istic.masteru.android.model;
public enum Stats {
NONE, ACE, DF
}
......@@ -12,6 +12,7 @@ import java.util.Map;
import java.util.TreeMap;
import fr.istic.masteru.android.automate.Automate;
import fr.istic.masteru.android.automate.Point;
import fr.istic.masteru.android.automate.Score;
import fr.istic.masteru.android.listeners.OnSyncFinishListener;
......@@ -50,24 +51,25 @@ public class ScoreSyncManager {
}
/**
* Add a score to synchronize.
* Add a point to synchronize.
* Starts the synchronizations if needed.
*
* @param score The score to synchronize
* @param point The point to synchronize
*/
public void addScore(Score score) {
addScore(score, true);
public void addPoint(Point point) {
addPoint(point, true);
}
/**
* Add a score to synchronize
* Add a point to synchronize
* If canBeBack is set to false, the score will not be considered as a step backward (but as a
* another try to synchronize an old score).
*
* @param score
* @param point
* @param canBeBack true if the score can be a step backward, false otherwise.
*/
private void addScore(Score score, boolean canBeBack) {
private void addPoint(Point point, boolean canBeBack) {
Score score = point.getScore();
if (score.getId() < scoreIdMax && canBeBack) {
back(score.getId());
} else if (score.getId() > 0) {
......@@ -112,7 +114,7 @@ public class ScoreSyncManager {
}
/**
* Gets the next score to synchronize (with the lowest Id) and calls #synchronizeScore(fr.istic.masteru.android.automate.Score)
* Gets the next score to synchronize (with the lowest Id) and calls #synchronizePoint(fr.istic.masteru.android.automate.Score)
* or do nothing if #scores is empty.
*/
private void syncNextElement() {
......@@ -121,9 +123,9 @@ public class ScoreSyncManager {
setSyncing(false);
return;
}
if (firstEntry.getValue() instanceof Score) {
if (firstEntry.getValue() instanceof Point) {
setSyncing(true);
synchronizeScore(firstEntry.getKey(), (Score) firstEntry.getValue());
synchronizePoint(firstEntry.getKey(), (Point) firstEntry.getValue());
} else if (firstEntry.getValue() instanceof PreviousSetToSync) {
setSyncing(true);
synchronizePreviousSet(firstEntry.getKey(), (PreviousSetToSync) firstEntry.getValue());
......@@ -141,9 +143,10 @@ public class ScoreSyncManager {
* If the synchronization is OK, calls syncNextElement automatically.
* In case of timeout(#TIMEOUT_MS), calls syncNextElement after #RETRY_DELAY ms.
*
* @param score
* @param point
*/
private void synchronizeScore(final float key, final Score score) {
private void synchronizePoint(final float key, final Point point) {
final Score score = point.getScore();
JsonObject param = new JsonObject();
param.addProperty("token", PrefsUtils.getToken(context));
param.addProperty("scoreA", score.getScoreA());
......@@ -154,6 +157,9 @@ public class ScoreSyncManager {
param.addProperty("gameA", score.getGamesA());
param.addProperty("gameB", score.getGamesB());
param.addProperty("server", score.getService());
param.addProperty("pointWinner", point.getStats().getWinner() == 0 ? "A" : "B");
param.addProperty("stats", point.getStats().getType().name());
param.addProperty("fsf", point.getStats().isFsf() ? "0" : "1");
Ion.with(context)
.load("POST", API_URLS.UPDATE_SCORE(idMatch, score.getId()))
......@@ -181,7 +187,7 @@ public class ScoreSyncManager {
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);
addPoint(new Point(automate.getPoint(i), null), false);
}
break;
case -4: // Duplicate entry
......
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