www

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

commit 57b5c0611a2df7f9960dcb31e103ee92ce193933
parent 5d6933cac4bc3d69e73694e6ec0151e91ec58668
Author: John Charron <rm_rf_windows@yahoo.fr>
Date:   Wed, 16 Mar 2011 10:13:36 +0100

Merge branch 'master' of https://github.com/jsmaniac/2011-m1s2-ter

Diffstat:
M.gitignore | 1+
Mcode/PtiClic/AndroidManifest.xml | 2+-
Mcode/PtiClic/res/layout/score.xml | 4++++
Acode/PtiClic/src/org/pticlic/BaseScore.java | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dcode/PtiClic/src/org/pticlic/Score.java | 107-------------------------------------------------------------------------------
Mcode/PtiClic/src/org/pticlic/games/BaseGame.java | 4++--
Mcode/PtiClic/src/org/pticlic/model/DownloadedBaseGame.java | 15++++++++++++++-
Mcode/PtiClic/src/org/pticlic/model/Network.java | 157+++++++++++++++++++++++++++++++++----------------------------------------------
Mcode/serveur/php/pticlic.php | 16+++++++++-------
Mcode/serveur/php/server.php | 5++---
10 files changed, 216 insertions(+), 212 deletions(-)

diff --git 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 @@ -13,7 +13,7 @@ <activity android:name=".Preference" android:label="Préférence" android:screenOrientation="portrait"></activity> <activity android:name=".games.BaseGame" android:screenOrientation="portrait"></activity> <activity android:label="Information" android:name=".Information" android:screenOrientation="portrait"></activity> - <activity android:name=".Score" android:label="Score" android:screenOrientation="portrait"></activity> + <activity android:name="BaseScore" android:label="Score" android:screenOrientation="portrait"></activity> <activity android:name="FrontPage" android:screenOrientation="portrait"></activity> </application> diff --git 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_ <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> + <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/scoreRel1" android:text="@+id/scoreRel1"></TextView> + <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/scoreRel2" android:text="@+id/scoreRel2"></TextView> + <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/scoreRel3" android:text="@+id/scoreRel3"></TextView> + <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/scoreRel4" android:text="@+id/scoreRel4"></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> diff --git a/code/PtiClic/src/org/pticlic/BaseScore.java b/code/PtiClic/src/org/pticlic/BaseScore.java @@ -0,0 +1,117 @@ +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 android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.TextView; + +/** + * @author John CHARRON + * + * Permet l'affichage du score obtenu par le joueur lors de sa partie. + */ +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); + + + // 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); + builder.setTitle(getString(R.string.app_name)) + .setIcon(android.R.drawable.ic_dialog_alert) + .setMessage(e.getMessage()) + .setCancelable(false) + .setNegativeButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + finish(); + } + }); + AlertDialog alert = builder.create(); + alert.show(); + } catch (Exception e) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(getString(R.string.app_name)) + .setIcon(android.R.drawable.ic_dialog_alert) + .setMessage(R.string.server_down) + .setCancelable(false) + .setNegativeButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + finish(); + } + }); + AlertDialog alert = builder.create(); + alert.show(); + } + + ((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 + public void onClick(View v) { + if (v.getId()==R.id.saw) { + finish(); + } + } +} diff --git a/code/PtiClic/src/org/pticlic/Score.java b/code/PtiClic/src/org/pticlic/Score.java @@ -1,107 +0,0 @@ -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 android.app.Activity; -import android.app.AlertDialog; -import android.content.DialogInterface; -import android.content.SharedPreferences; -import android.os.Bundle; -import android.preference.PreferenceManager; -import android.view.View; -import android.view.View.OnClickListener; -import android.widget.Button; -import android.widget.TextView; - -/** - * @author John CHARRON - * - * Permet l'affichage du score obtenu par le joueur lors de sa partie. - */ -public class Score extends Activity implements OnClickListener{ - - private Match gamePlayed; - - @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))); - 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)) - .setIcon(android.R.drawable.ic_dialog_alert) - .setMessage(e.getMessage()) - .setCancelable(false) - .setNegativeButton("Ok", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - finish(); - } - }); - AlertDialog alert = builder.create(); - alert.show(); - } catch (Exception e) { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(getString(R.string.app_name)) - .setIcon(android.R.drawable.ic_dialog_alert) - .setMessage(R.string.server_down) - .setCancelable(false) - .setNegativeButton("Ok", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - finish(); - } - }); - AlertDialog alert = builder.create(); - alert.show(); - } - - ((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 - public void onClick(View v) { - if (v.getId()==R.id.saw) { - finish(); - } - } -} diff --git 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 @@ -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 @@ -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 : 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 : 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(); } - 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 @@ -360,9 +360,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; @@ -370,14 +371,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. @@ -465,7 +467,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 @@ -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 +?>