commit 8ac70703a3db413307bd59752736b19555aab384
parent 310325521a33711db1c9e65385d73a9f93c1ad9a
Author: Bertrand BRUN <bertrand0brun@gmail.com>
Date: Tue, 8 Feb 2011 23:52:34 +0100
Connexion du jeu avec le serveur
Diffstat:
3 files changed, 106 insertions(+), 18 deletions(-)
diff --git a/code/PtiClic/src/org/pticlic/games/BaseGame.java b/code/PtiClic/src/org/pticlic/games/BaseGame.java
@@ -63,7 +63,7 @@ public class BaseGame extends Activity implements OnClickListener {
String passwd = sp.getString(Constant.USER_PASSWD, "");
// On initialise la classe permettant la communication avec le serveur.
- Network network = new Network(serverURL, Mode.SIMPLE_GAME, id, passwd);
+ network = new Network(serverURL, Mode.SIMPLE_GAME, id, passwd);
game = network.getGames(1);
int nbrel = game.getNbRelation();
@@ -104,31 +104,51 @@ public class BaseGame extends Activity implements OnClickListener {
trash.setImageResource(android.R.drawable.ic_menu_delete);
// Écoute des clics sur les relations
- if (nbrel > 0) {
+ if (nbrel > 0) {
+ // TODO : A enlever lorsque l'on aura toutes les images des relations.
+ try {
r1.setOnClickListener(this);
- r1.setImageResource(r.getRelationImage(game.getCat1()));
rn1.setText(r.getRelationName(game.getCat1()));
+ r1.setImageResource(r.getRelationImage(game.getCat1()));
+ } catch (Exception e) {
+ r1.setImageResource(R.drawable.icon);
+ }
} else {
rl1.setVisibility(View.GONE);
}
- if (nbrel > 1) {
+ if (nbrel > 1) {
+ // TODO : A enlever lorsque l'on aura toutes les images des relations.
+ try {
r2.setOnClickListener(this);
- r2.setImageResource(r.getRelationImage(game.getCat2()));
rn2.setText(r.getRelationName(game.getCat2()));
+ r2.setImageResource(r.getRelationImage(game.getCat2()));
+ } catch (Exception e) {
+ r2.setImageResource(R.drawable.icon);
+ }
} else {
rl2.setVisibility(View.GONE);
}
- if (nbrel > 2) {
+ if (nbrel > 2) {
+ // TODO : A enlever lorsque l'on aura toutes les images des relations.
+ try {
r3.setOnClickListener(this);
- r3.setImageResource(r.getRelationImage(game.getCat3()));
rn3.setText(r.getRelationName(game.getCat3()));
+ r3.setImageResource(r.getRelationImage(game.getCat3()));
+ } catch (Exception e) {
+ r3.setImageResource(R.drawable.icon);
+ }
} else {
rl3.setVisibility(View.GONE);
}
if (nbrel > 3) {
+ // TODO : A enlever lorsque l'on aura toutes les images des relations.
+ try {
r4.setOnClickListener(this);
- r4.setImageResource(r.getRelationImage(game.getCat4()));
rn4.setText(r.getRelationName(game.getCat4()));
+ r4.setImageResource(r.getRelationImage(game.getCat4()));
+ } catch (Exception e) {
+ r4.setImageResource(R.drawable.icon);
+ }
} else {
rl4.setVisibility(View.GONE);
}
diff --git a/code/PtiClic/src/org/pticlic/model/DownloadedGame.java b/code/PtiClic/src/org/pticlic/model/DownloadedGame.java
@@ -1,6 +1,7 @@
package org.pticlic.model;
import java.io.Serializable;
+import java.util.Arrays;
/**
@@ -38,6 +39,8 @@ public class DownloadedGame implements Serializable {
}
}
+ private int gid;
+ private int pgid;
private int id;
private int cat1;
private int cat2;
@@ -47,11 +50,14 @@ public class DownloadedGame implements Serializable {
private Word[] cloud;
+ public DownloadedGame() {}
- public DownloadedGame(int id, int cat1, int cat2, int cat3, int cat4, Word center,
+ public DownloadedGame(int id, int gid, int pgid, int cat1, int cat2, int cat3, int cat4, Word center,
Word[] cloud) {
super();
this.id = id;
+ this.gid = gid;
+ this.pgid = pgid;
this.cat1 = cat1;
this.cat2 = cat2;
this.cat3 = cat3;
@@ -88,6 +94,24 @@ public class DownloadedGame implements Serializable {
return word.getName();
}
+
+
+ public int getGid() {
+ return gid;
+ }
+
+ public void setGid(int gid) {
+ this.gid = gid;
+ }
+
+ public int getPgid() {
+ return pgid;
+ }
+
+ public void setPgid(int pgid) {
+ this.pgid = pgid;
+ }
+
public int getId() {
return id;
}
@@ -146,8 +170,10 @@ public class DownloadedGame implements Serializable {
@Override
public String toString() {
- return "Game [id=" + id + ", cat1=" + cat1 + ", cat2=" + cat2
- + ", cat3=" + cat3 + ", cat4=" + cat4 + ", center=" + center
- + ", cloud=" + cloud + "]";
- }
+ return "DownloadedGame [gid=" + gid + ", pgid=" + pgid + ", id=" + id
+ + ", cat1=" + cat1 + ", cat2=" + cat2 + ", cat3=" + cat3
+ + ", cat4=" + cat4 + ", center=" + center + ", cloud="
+ + Arrays.toString(cloud) + "]";
+ }
+
}
diff --git a/code/PtiClic/src/org/pticlic/model/Network.java b/code/PtiClic/src/org/pticlic/model/Network.java
@@ -128,7 +128,17 @@ public class Network {
public DownloadedGame getGames(int nbGames) {
DownloadedGame game = null;
try {
- URL url = new URL(this.serverURL);
+ //URL url = new URL(this.serverURL);
+
+
+ URL url = new URL("http://dumbs.fr/~bbrun/pticlic/pticlic.php?"
+ + "action=" + Action.GET_GAMES.value()
+ + "&user=" + this.id
+ + "&passwd=" + this.passwd
+ + "&nb=" + String.valueOf(nbGames)
+ + "&mode="+mode.value());
+
+
URLConnection connection = url.openConnection();
connection.addRequestProperty("action", Action.GET_GAMES.value());
connection.addRequestProperty("user", this.id);
@@ -137,8 +147,34 @@ public class Network {
connection.addRequestProperty("mode", mode.value());
Gson gson = new Gson();
- JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
-
+ //JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
+ JsonReader reader = new JsonReader(new InputStreamReader(url.openStream(), "UTF-8"));
+
+// //[
+// {
+// gid:2,
+// pgid:117,
+// cat1:10,
+// cat2:5,
+// cat3:0,
+// cat4:-1,
+// center:{id:173311, name:"nordafricain"},
+// cloudsize:10,
+// cloud:[
+// {id:9894,name:"g\u00e9ographie"},
+// {id:110821,name:"cannibale"},
+// {id:131053,name:"motricit\u00e9"},
+// {id:110821,name:"cannibale"},
+// {id:110821,name:"cannibale"},
+// {id:151567,name:"_FL:3"},
+// {id:9894,name:"g\u00e9ographie"},
+// {id:9894,name:"g\u00e9ographie"},
+// {id:9894,name:"g\u00e9ographie"},
+// {id:131053,name:"motricit\u00e9"}
+// ]
+// }
+// ]
+
// FIXME : Attention lorsque l'on pourra vraiment recupere plusieur partie, il faudra changer ce qui suit.
reader.beginArray();
while (reader.hasNext()) {
@@ -164,6 +200,8 @@ public class Network {
* @throws IOException
*/
private DownloadedGame makeGame(JsonReader reader, Gson gson) throws IOException {
+ int gid = -1;
+ int pgid = -1;
int id = -1;
int cat1 = -1;
int cat2 = -1;
@@ -173,10 +211,14 @@ public class Network {
DownloadedGame.Word[] cloud = null;
reader.beginObject();
- while (reader != null && reader.hasNext()) {
+ while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
id = reader.nextInt();
+ } else if (name.equals("gid")) {
+ gid = reader.nextInt();
+ } else if (name.equals("pgid")) {
+ pgid = reader.nextInt();
} else if (name.equals("cat1")) {
cat1 = reader.nextInt();
} else if (name.equals("cat2")) {
@@ -194,7 +236,7 @@ public class Network {
}
}
reader.endObject();
- return new DownloadedGame(id, cat1, cat2, cat3, cat4, center, cloud);
+ return new DownloadedGame(id, gid, pgid, cat1, cat2, cat3, cat4, center, cloud);
}