www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit de69ae7663bc2d32babde6b1376a6b3b279d868a
parent de174de72b437a02b053a86a884502fa7d74b8c7
Author: Bertrand BRUN <bertrand0brun@gmail.com>
Date:   Sat, 12 Feb 2011 18:22:52 +0100

Ajout de l'affichage du score, ainsi que de la mise en cache de la partie suivant renvoyer par le serveur et utilisation de cette partie en cache
Diffstat:
Mcode/PtiClic/res/layout/score.xml | 40++++++++--------------------------------
Mcode/PtiClic/src/org/pticlic/Score.java | 7++++---
Mcode/PtiClic/src/org/pticlic/games/BaseGame.java | 9+++++++--
Mcode/PtiClic/src/org/pticlic/model/Constant.java | 4+++-
Mcode/PtiClic/src/org/pticlic/model/Network.java | 35++++++++++++++++++++++++++++++-----
5 files changed, 52 insertions(+), 43 deletions(-)

diff --git a/code/PtiClic/res/layout/score.xml b/code/PtiClic/res/layout/score.xml @@ -3,38 +3,14 @@ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:orientation="vertical" -android:layout_width="fill_parent" android:id="@+id/LinearLayout01"> - - -<TextView android:text="@+id/corrects" android:id="@+id/correct" android:layout_width="fill_parent" -android:layout_height="wrap_content" android:textSize="20px"></TextView> - -<TextView android:text="@+id/manquants" android:id="@+id/missing" android:layout_width="fill_parent" -android:layout_height="wrap_content" android:textSize="20px"></TextView> - -<TextView android:text="@+id/mauvais" android:id="@+id/bad" android:layout_width="fill_parent" -android:layout_height="wrap_content" android:textSize="20px"></TextView> - -<TextView android:text="@+id/total" android:id="@+id/total" android:layout_width="fill_parent" -android:layout_height="wrap_content" android:textSize="25px"></TextView> - -<TextView android:text="@+id/joueravec" android:id="@+id/playedWith" android:layout_width="wrap_content" -android:layout_height="wrap_content" android:textSize="20px"></TextView> - -<Button android:text="J'ai vu !" android:id="@+id/saw" android:layout_width="100px" -android:layout_height="40px" android:layout_gravity="center_vertical|center_horizontal|center"></Button> - -<TextView android:text="@+id/category1" android:id="@+id/category1" android:layout_width="wrap_content" -android:layout_height="wrap_content" android:textSize="20px"></TextView> - -<TextView android:text="@+id/category1words" android:id="@+id/category1words" android:layout_width="wrap_content" -android:layout_height="wrap_content"></TextView> - -<TextView android:text="@+id/category2" android:id="@+id/category2" android:layout_width="wrap_content" -android:layout_height="wrap_content" android:textSize="20px"></TextView> - -<TextView android:text="@+id/category2words" android:id="@+id/category2words" android:layout_width="wrap_content" -android:layout_height="wrap_content"></TextView> +android:id="@+id/LinearLayout01" android:layout_gravity="center" android:layout_width="150dip"> + <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:orientation="vertical" android:layout_weight="2"> + <TextView android:layout_width="fill_parent" android:id="@+id/textView1" android:layout_height="wrap_content" android:text="Votre score est de :" android:gravity="center" android:layout_weight="1"></TextView> + <TextView android:layout_width="fill_parent" android:id="@+id/total" android:layout_height="wrap_content" android:text="@+id/total" android:textStyle="bold" android:textSize="25px" android:layout_weight="1" android:gravity="center|top"></TextView> + </LinearLayout> +<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_weight="1" android:layout_height="wrap_content"> + <Button android:id="@+id/saw" android:text="J'ai vu !" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_gravity="top"></Button> +</LinearLayout> </LinearLayout> diff --git a/code/PtiClic/src/org/pticlic/Score.java b/code/PtiClic/src/org/pticlic/Score.java @@ -6,10 +6,8 @@ import org.pticlic.model.Match; import org.pticlic.model.Network; import org.pticlic.model.Network.Mode; - import android.app.Activity; import android.app.AlertDialog; -import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; @@ -17,6 +15,7 @@ import android.preference.PreferenceManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; +import android.widget.TextView; /** * @author John CHARRON @@ -48,7 +47,9 @@ public class Score extends Activity implements OnClickListener{ // FIXME : Pour l'instant ne marche pas, attend de savoir comment est formater le score que l'on recois. try { - network.sendGame(gamePlayed); + Double score = network.sendGame(gamePlayed); + ((TextView)findViewById(R.id.total)).setText(String.valueOf(score.floatValue())); + sp.edit().putString(Constant.NEW_BASE_GAME, network.getNewGame()).commit(); } catch (PtiClicException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getString(R.string.app_name)) diff --git a/code/PtiClic/src/org/pticlic/games/BaseGame.java b/code/PtiClic/src/org/pticlic/games/BaseGame.java @@ -10,7 +10,6 @@ import org.pticlic.model.Network; import org.pticlic.model.Network.Mode; import org.pticlic.model.Relation; - import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; @@ -30,6 +29,8 @@ import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; +import com.google.gson.Gson; + /** * @author Bertrand BRUN et Georges DUPÉRON * @@ -54,6 +55,7 @@ public class BaseGame extends Activity implements OnClickListener { private Match match; private Network network; private boolean help = false; + private String gameJson; /** Called when the activity is first created. */ @Override @@ -66,6 +68,7 @@ public class BaseGame extends Activity implements OnClickListener { String serverURL = sp.getString(Constant.SERVER_URL, "http://dumbs.fr/~bbrun/pticlic.json"); // TODO : Mettre comme valeur par defaut l'adresse reel du serveur String id = sp.getString(Constant.USER_ID, "joueur"); String passwd = sp.getString(Constant.USER_PASSWD, ""); + gameJson = sp.getString(Constant.NEW_BASE_GAME, null); // On initialise la classe permettant la communication avec le serveur. network = new Network(serverURL, Mode.SIMPLE_GAME, id, passwd); @@ -80,7 +83,9 @@ public class BaseGame extends Activity implements OnClickListener { protected void onStart() { super.onStart(); try { - game = (DownloadedBaseGame)network.getGames(1); + Gson gson = new Gson(); + if (gameJson == null) game = (DownloadedBaseGame)network.getGames(1); + else game = gson.fromJson(gameJson, DownloadedBaseGame.class); runMatch(); start(); } catch (PtiClicException e) { diff --git a/code/PtiClic/src/org/pticlic/model/Constant.java b/code/PtiClic/src/org/pticlic/model/Constant.java @@ -2,9 +2,11 @@ package org.pticlic.model; public class Constant { public static final String SERVER_URL = "server"; - public static final String SERVER_AUTH = "SERVER_AUTH"; + // Constant pour les parties en cache + public static final String NEW_BASE_GAME = "NEW_BASE_GAME"; + // Constant pour les information de l'utilisateur. public static final String USER_ID = "login"; public static final String USER_PASSWD = "passwd"; diff --git a/code/PtiClic/src/org/pticlic/model/Network.java b/code/PtiClic/src/org/pticlic/model/Network.java @@ -27,6 +27,8 @@ import com.google.gson.stream.JsonReader; */ public class Network { + String newGameJson = null; + public enum Action { GET_GAMES(0), SEND_GAME(1); @@ -241,18 +243,18 @@ public class Network { * @param game La partie jouee par l'utilisateur * @return Le score sous forme JSON. */ - public TotalScore sendGame(Match game) throws PtiClicException { + public double sendGame(Match game) throws PtiClicException { switch (mode) { case SIMPLE_GAME: return sendBaseGame(game); default: - return null; + return -1; } } - public TotalScore sendBaseGame(Match game) throws PtiClicException { - TotalScore score = null; + public double sendBaseGame(Match game) throws PtiClicException { + double score = -1; URL url = null; Gson gson = null; BufferedReader reader = null; @@ -305,7 +307,7 @@ public class Network { // une exception. PtiClicException.Error error = gson.fromJson(json, PtiClicException.Error.class); if (error.getMsg() == null) { - score = gson.fromJson(jsonReader, TotalScore.class); + score = getScore(jsonReader, gson); } else { throw new PtiClicException(error); } @@ -318,4 +320,27 @@ public class Network { return score; } + + private double getScore(JsonReader reader, Gson gson) throws IOException { + double score = -1; + + reader.beginObject(); + while (reader.hasNext()) { + String name = reader.nextName(); + if (name.equals("score")) { + score = reader.nextDouble(); + } else if (name.equals("newGame")) { + DownloadedBaseGame newGame = gson.fromJson(reader, DownloadedBaseGame.class); + newGameJson = gson.toJson(newGame); + } else { + reader.skipValue(); + } + } + reader.endObject(); + return score; + } + + public String getNewGame() { + return this.newGameJson; + } }