From 5c00bac49c9ae833b7bb1ab3a5a4cd093aac1213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Wed, 16 Mar 2011 09:01:52 +0100 Subject: [PATCH] Travail non fini sur l'affichage des scores. --- .gitignore | 1 + code/PtiClic/AndroidManifest.xml | 2 +- code/PtiClic/res/layout/score.xml | 4 + .../pticlic/{Score.java => BaseScore.java} | 38 +++-- .../src/org/pticlic/games/BaseGame.java | 4 +- .../org/pticlic/model/DownloadedBaseGame.java | 15 +- .../src/org/pticlic/model/Network.java | 155 ++++++++---------- code/serveur/php/pticlic.php | 16 +- code/serveur/php/server.php | 4 +- 9 files changed, 122 insertions(+), 117 deletions(-) rename code/PtiClic/src/org/pticlic/{Score.java => BaseScore.java} (85%) diff --git a/.gitignore b/.gitignore index 748e5b0..44b99da 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store code.zip +vp.log diff --git a/code/PtiClic/AndroidManifest.xml b/code/PtiClic/AndroidManifest.xml index 30b6d44..33fc440 100644 --- a/code/PtiClic/AndroidManifest.xml +++ b/code/PtiClic/AndroidManifest.xml @@ -13,7 +13,7 @@ - + diff --git a/code/PtiClic/res/layout/score.xml b/code/PtiClic/res/layout/score.xml index 39520d9..b2a9a88 100644 --- a/code/PtiClic/res/layout/score.xml +++ b/code/PtiClic/res/layout/score.xml @@ -7,6 +7,10 @@ android:id="@+id/LinearLayout01" android:layout_gravity="center" android:layout_ + + + + diff --git a/code/PtiClic/src/org/pticlic/Score.java b/code/PtiClic/src/org/pticlic/BaseScore.java similarity index 85% rename from code/PtiClic/src/org/pticlic/Score.java rename to code/PtiClic/src/org/pticlic/BaseScore.java index aca5ef6..60a49c3 100644 --- a/code/PtiClic/src/org/pticlic/Score.java +++ b/code/PtiClic/src/org/pticlic/BaseScore.java @@ -24,33 +24,43 @@ import android.widget.TextView; * * Permet l'affichage du score obtenu par le joueur lors de sa partie. */ -public class Score extends Activity implements OnClickListener{ +public class BaseScore extends Activity implements OnClickListener{ private Match gamePlayed; + private void networkStuff() { + String id = sp.getString(Constant.USER_ID, "joueur"); + String passwd = sp.getString(Constant.USER_PASSWD, ""); + 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); + Network network = new Network(serverURL, mode, id, passwd); + try { + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.score); - SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); - String serverURL = sp.getString(Constant.SERVER_URL, Constant.SERVER); - String id = sp.getString(Constant.USER_ID, "joueur"); - String passwd = sp.getString(Constant.USER_PASSWD, ""); - Mode mode = null; - if (getIntent().getExtras() != null) { - // Pour JC : 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); - } - - Network network = new Network(serverURL, mode, id, passwd); - try { // 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(); } catch (PtiClicException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this); diff --git a/code/PtiClic/src/org/pticlic/games/BaseGame.java b/code/PtiClic/src/org/pticlic/games/BaseGame.java index ea300c3..7059ee9 100644 --- a/code/PtiClic/src/org/pticlic/games/BaseGame.java +++ b/code/PtiClic/src/org/pticlic/games/BaseGame.java @@ -1,7 +1,7 @@ package org.pticlic.games; import org.pticlic.R; -import org.pticlic.Score; +import org.pticlic.BaseScore; import org.pticlic.exception.PtiClicException; import org.pticlic.model.Constant; import org.pticlic.model.DownloadedBaseGame; @@ -283,7 +283,7 @@ public class BaseGame extends Activity implements OnClickListener { leaveView(); start(); } else { - Intent intent = new Intent(this, Score.class); + Intent intent = new Intent(this, BaseScore.class); intent.putExtra(Constant.SCORE_GAMEPLAYED, match); intent.putExtra(Constant.SCORE_MODE, Mode.SIMPLE_GAME); diff --git a/code/PtiClic/src/org/pticlic/model/DownloadedBaseGame.java b/code/PtiClic/src/org/pticlic/model/DownloadedBaseGame.java index 1cc9f88..981ae0c 100644 --- a/code/PtiClic/src/org/pticlic/model/DownloadedBaseGame.java +++ b/code/PtiClic/src/org/pticlic/model/DownloadedBaseGame.java @@ -69,10 +69,23 @@ public class DownloadedBaseGame extends DownloadedGame { return word.getName(); } + public int getCat(int numCat) { + switch (numCat) { + case 1: return getCat1(); + case 2: return getCat2(); + case 3: return getCat3(); + default: return getCat4(); + } + } + + public String getCatString(int numCat) { + return Relation.getInstance().getRelationName(this.getCat(numCat)); + } + public int getCat1() { return cat1; } - + public void setCat1(int cat1) { this.cat1 = cat1; } diff --git a/code/PtiClic/src/org/pticlic/model/Network.java b/code/PtiClic/src/org/pticlic/model/Network.java index 0755812..5ea9e37 100644 --- a/code/PtiClic/src/org/pticlic/model/Network.java +++ b/code/PtiClic/src/org/pticlic/model/Network.java @@ -28,6 +28,18 @@ import com.google.gson.stream.JsonReader; */ public class Network { + public static class ScoreResponse { + private int score; + private String newGame; + public ScoreResponse() {} + public int getScore() { + return score; + } + public String getNewGame() { + return newGame; + } + } + public static class Check implements Serializable { private static final long serialVersionUID = 1L; private boolean login_ok = false; @@ -124,16 +136,16 @@ public class Network { if (auth) { return auth; } - + Gson gson = null; String json = null; boolean res = false; - + String urlS = serverURL - + "?action=" + Action.CHECK_LOGIN.value() - + "&user=" + id - + "&passwd=" + passwd; - + + "?action=" + Action.CHECK_LOGIN.value() + + "&user=" + id + + "&passwd=" + passwd; + gson = new Gson(); json = HttpClient.SendHttpPost(urlS); @@ -143,7 +155,7 @@ public class Network { SharedPreferences.Editor editor = sp.edit(); editor.putBoolean(Constant.SERVER_AUTH, res); editor.commit(); - + return res; } @@ -174,12 +186,12 @@ public class Network { // connection.addRequestProperty("mode", mode.value()); String urlS = this.serverURL - + "?action=" + Action.GET_GAMES.value() - + "&user=" + this.id - + "&passwd=" + this.passwd - + "&nb=" + String.valueOf(nbGames) - + "&mode="+mode.value(); - + + "?action=" + Action.GET_GAMES.value() + + "&user=" + this.id + + "&passwd=" + this.passwd + + "&nb=" + String.valueOf(nbGames) + + "&mode="+mode.value(); + gson = new Gson(); json = HttpClient.SendHttpPost(urlS); @@ -261,99 +273,62 @@ public class Network { * @param game La partie jouee par l'utilisateur * @return Le score sous forme JSON. */ - public double sendGame(Match game) throws PtiClicException, Exception { + public ScoreResponse sendGame(Match game) throws PtiClicException, Exception { switch (mode) { case SIMPLE_GAME: return sendBaseGame(game); default: - return -1; + return null; } } - - public double sendBaseGame(Match game) throws PtiClicException, Exception { + public ScoreResponse sendBaseGame(Match game) throws PtiClicException, Exception { double score = -1; Gson gson = null; String json = null; - try { - // TODO : ne restera le temps que les requete du serveur passe du GET au POST - String urlS = this.serverURL - + "?action=" + Action.SEND_GAME.value() - + "&user=" + this.id - + "&passwd=" + this.passwd - + "&pgid=" + game.getGame().getPgid() - + "&gid=" + game.getGame().getGid() - + "&mode="+mode.value(); + // TODO : ne restera le temps que les requete du serveur passe du GET au POST + String urlS = this.serverURL + + "?action=" + Action.SEND_GAME.value() + + "&user=" + this.id + + "&passwd=" + this.passwd + + "&pgid=" + game.getGame().getPgid() + + "&gid=" + game.getGame().getGid() + + "&mode="+mode.value(); - // TODO : faut gere le mode - for (Integer i : game.getRelation1()) { - urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat1() ; - } - for (Integer i : game.getRelation2()) { - urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat2(); - } - for (Integer i : game.getRelation3()) { - urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat3(); - } - for (Integer i : game.getRelation4()) { - urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat4(); - } - - // URL url = new URL(this.serverURL); // Attention ! this.serverURL contient "/server.php" - // URLConnection connection = url.openConnection(); - // connection.addRequestProperty("action", Action.SEND_GAME.value()); - // connection.addRequestProperty("user", this.id); - // connection.addRequestProperty("passwd", this.passwd); - // connection.addRequestProperty("mode", mode.value()); - // connection.addRequestProperty("pgid", String.valueOf(game.getGame().getId())); - - gson = new Gson(); - json = HttpClient.SendHttpPost(urlS); - - //JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); - InputStream in = new ByteArrayInputStream(json.getBytes("UTF-8")); - JsonReader jsonReader = new JsonReader(new InputStreamReader(in)); - - // 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) { - score = getScore(jsonReader, gson); - } else { - throw new PtiClicException(error); - } - - } catch (UnsupportedEncodingException e1) { - throw new PtiClicException(0, "Impossible de recuperer l'erreur, nous avons pris note de cette erreur.\n Merci"); - } catch (IOException e1) { - throw new PtiClicException(0, "Impossible de recuperer l'erreur, nous avons pris note de cette erreur.\n Merci"); + // TODO : faut gere le mode + for (Integer i : game.getRelation1()) { + urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat1() ; + } + for (Integer i : game.getRelation2()) { + urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat2(); + } + for (Integer i : game.getRelation3()) { + urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat3(); + } + for (Integer i : game.getRelation4()) { + urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat4(); } - return score; - } + // URL url = new URL(this.serverURL); // Attention ! this.serverURL contient "/server.php" + // URLConnection connection = url.openConnection(); + // connection.addRequestProperty("action", Action.SEND_GAME.value()); + // connection.addRequestProperty("user", this.id); + // connection.addRequestProperty("passwd", this.passwd); + // connection.addRequestProperty("mode", mode.value()); + // connection.addRequestProperty("pgid", String.valueOf(game.getGame().getId())); - private double getScore(JsonReader reader, Gson gson) throws IOException { - double score = -1; + gson = new Gson(); + json = HttpClient.SendHttpPost(urlS); - 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(); - } + // 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); + } else { + throw new PtiClicException(error); } - reader.endObject(); - return score; - } - - public String getNewGame() { - return this.newGameJson; } } diff --git a/code/serveur/php/pticlic.php b/code/serveur/php/pticlic.php index 44ad9da..b649e7f 100644 --- a/code/serveur/php/pticlic.php +++ b/code/serveur/php/pticlic.php @@ -335,9 +335,10 @@ function game2json($user, $gameId) $game = $db->query("select gid, (select name from node where eid = eid_central_word) as name_central_word, eid_central_word, relation_1, relation_2 from game where gid = ".$gameId.";"); $game = $game->fetchArray(); - echo '{"gid":'.$gameId.',"pgid":'.$pgid.',"cat1":'.$game['relation_1'].',"cat2":'.$game['relation_2'].',"cat3":0,"cat4":-1,'; - echo '"center":{"id":'.$game['eid_central_word'].',"name":'.json_encode(''.formatWord($game['name_central_word'])).'},'; - echo '"cloudsize":10,"cloud":['; // TODO ! compter dynamiquement. + $retstr = ""; + $retstr .= '{"gid":'.$gameId.',"pgid":'.$pgid.',"cat1":'.$game['relation_1'].',"cat2":'.$game['relation_2'].',"cat3":0,"cat4":-1,'; + $retstr .= '"center":{"id":'.$game['eid_central_word'].',"name":'.json_encode(''.formatWord($game['name_central_word'])).'},'; + $retstr .= '"cloudsize":10,"cloud":['; // TODO ! compter dynamiquement. $res = $db->query("select eid_word,(select name from node where eid=eid_word) as name_word from game_cloud where gid = ".$gameId.";"); $notfirst = false; @@ -345,14 +346,15 @@ function game2json($user, $gameId) while ($x = $res->fetchArray()) { if ($notfirst) - echo ","; + $retstr .= ","; else $notfirst=true; - echo '{"id":'.$x['eid_word'].',"name":'.json_encode("".formatWord($x['name_word'])).'}'; + $retstr .= '{"id":'.$x['eid_word'].',"name":'.json_encode("".formatWord($x['name_word'])).'}'; } - echo "]}"; + $retstr .= "]}"; + return $retstr; } /** Récupère une partie sous forme de tableau. @@ -440,7 +442,7 @@ function getGame($user, $nbGames, $mode) for ($i=0; $i < $nbGames; $i) { - game2json($user, randomGame()); + echo game2json($user, randomGame()); if ((++$i) < $nbGames) echo ","; diff --git a/code/serveur/php/server.php b/code/serveur/php/server.php index 57fbc4a..f86f8be 100644 --- a/code/serveur/php/server.php +++ b/code/serveur/php/server.php @@ -73,7 +73,7 @@ function main() $scores = setGame($user, intval($_GET['pgid']), intval($_GET['gid']), $_GET); // On renvoie une nouvelle partie pour garder le client toujours bien alimenté. echo "{\"score\":".$scores['total'].",\"newGame\":"; - game2json($user, randomGame()); + json_encode("".game2json($user, randomGame())); echo "}"; } else { throw new Exception("Commande inconnue", 2); @@ -96,4 +96,4 @@ function server() { server(); -?> \ No newline at end of file +?>