From a8b0e92a083f3c9cdde811c8f75387f4fcf8045f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Wed, 16 Mar 2011 10:31:49 +0100 Subject: [PATCH] Avancement sur l'affichage des scores. --- code/PtiClic/res/layout/score.xml | 18 +++--- code/PtiClic/src/org/pticlic/BaseScore.java | 61 +++++++++---------- .../src/org/pticlic/model/Network.java | 11 ++-- 3 files changed, 47 insertions(+), 43 deletions(-) diff --git a/code/PtiClic/res/layout/score.xml b/code/PtiClic/res/layout/score.xml index b2a9a88..608f1fa 100644 --- a/code/PtiClic/res/layout/score.xml +++ b/code/PtiClic/res/layout/score.xml @@ -3,17 +3,21 @@ +android:id="@+id/LinearLayout01" android:layout_gravity="center" android:layout_width="fill_parent"> - - - - + + + + + + + + - - + + diff --git a/code/PtiClic/src/org/pticlic/BaseScore.java b/code/PtiClic/src/org/pticlic/BaseScore.java index 60a49c3..53a4f2a 100644 --- a/code/PtiClic/src/org/pticlic/BaseScore.java +++ b/code/PtiClic/src/org/pticlic/BaseScore.java @@ -1,12 +1,11 @@ package org.pticlic; -import java.text.DecimalFormat; - import org.pticlic.exception.PtiClicException; import org.pticlic.model.Constant; import org.pticlic.model.Match; import org.pticlic.model.Network; import org.pticlic.model.Network.Mode; +import org.pticlic.model.Network.ScoreResponse; import android.app.Activity; import android.app.AlertDialog; @@ -25,43 +24,29 @@ import android.widget.TextView; * Permet l'affichage du score obtenu par le joueur lors de sa partie. */ public class BaseScore extends Activity implements OnClickListener{ - - private Match gamePlayed; + + private Match gamePlayed; + private ScoreResponse sr = null; private void networkStuff() { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String id = sp.getString(Constant.USER_ID, "joueur"); String passwd = sp.getString(Constant.USER_PASSWD, ""); + String serverURL = sp.getString(Constant.SERVER_URL, Constant.SERVER); Mode mode = null; - + if (getIntent().getExtras() != null) { // GamePlayed contient toutes les infos sur la partie jouee this.gamePlayed = (Match) getIntent().getExtras().get(Constant.SCORE_GAMEPLAYED); mode = (Mode) getIntent().getExtras().get(Constant.SCORE_MODE); } - + // TODO : factoriser le serverUrl dans Network - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); - String serverURL = sp.getString(Constant.SERVER_URL, Constant.SERVER); + sp.edit().remove(Constant.NEW_BASE_GAME).commit(); Network network = new Network(serverURL, mode, id, passwd); try { - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.score); - - - // Permet de regler la precision : rajoute/enlever des # pour modifier la precision - DecimalFormat dfrmtr = new DecimalFormat("#.##"); - Double score = network.sendGame(gamePlayed); - ((TextView)findViewById(R.id.total)).setText(String.valueOf(dfrmtr.format(score))); - // TODO : Attention, le cast en (BaseGame) n'est pas sûr ! - ((TextView)findViewById(R.id.scoreRel1)).setText("Foo1"); - ((TextView)findViewById(R.id.scoreRel2)).setText("Foo2"); - ((TextView)findViewById(R.id.scoreRel3)).setText("Foo3"); - ((TextView)findViewById(R.id.scoreRel4)).setText("Foo4"); - sp.edit().putString(Constant.NEW_BASE_GAME, network.getNewGame()).commit(); + sr = network.sendBaseGame(gamePlayed); + sp.edit().putString(Constant.NEW_BASE_GAME, sr.getNewGame()).commit(); } catch (PtiClicException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getString(R.string.app_name)) @@ -91,21 +76,35 @@ public class BaseScore extends Activity implements OnClickListener{ AlertDialog alert = builder.create(); alert.show(); } + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.score); + + this.networkStuff(); + + ((TextView)findViewById(R.id.total)).setText("42"); + // TODO : Attention, le cast en (BaseGame) n'est pas sûr ! + ((TextView)findViewById(R.id.scoreRel1)).setText("Foo1"); + ((TextView)findViewById(R.id.scoreRel2)).setText("Foo2"); + ((TextView)findViewById(R.id.scoreRel3)).setText("Foo3"); + ((TextView)findViewById(R.id.scoreRel4)).setText("Foo4"); ((Button)findViewById(R.id.saw)).setOnClickListener(this); - + } - + @Override public void onBackPressed() { super.onBackPressed(); - + finish(); } - + protected double calculateTotal(){ throw new UnsupportedOperationException(); - //return this.corrects - this.manquants - this.mauvais; } @Override diff --git a/code/PtiClic/src/org/pticlic/model/Network.java b/code/PtiClic/src/org/pticlic/model/Network.java index 5ea9e37..0ddf9e3 100644 --- a/code/PtiClic/src/org/pticlic/model/Network.java +++ b/code/PtiClic/src/org/pticlic/model/Network.java @@ -283,7 +283,6 @@ public class Network { } public ScoreResponse sendBaseGame(Match game) throws PtiClicException, Exception { - double score = -1; Gson gson = null; String json = null; @@ -324,11 +323,13 @@ public class Network { // Comme gson ne renvoie pas une erreur si l'objet qui recupere ne correspond pas a la classe qu'il attends. // On creer tout d'abord une objet error et si celui-ci est vide on creer l'objet score, sinon on lance // une exception. - PtiClicException.Error error = gson.fromJson(json, PtiClicException.Error.class); - if (error.getMsg() == null) { - return gson.fromJson(json, ScoreResponse.class); + int foo = 42; + ScoreResponse sr = gson.fromJson(json, ScoreResponse.class); + foo = foo + 1; + if (sr.getNewGame() == null) { + throw new PtiClicException(gson.fromJson(json, PtiClicException.Error.class)); } else { - throw new PtiClicException(error); + return sr; } } }