Modification du code pour qu'il puisse prendre en compte nos propres exception, et dans le jeux BaseGame la poubelle n'est plus une relation a part

This commit is contained in:
Bertrand BRUN 2011-02-10 13:46:28 +01:00
parent b944258a23
commit 65c00bfba6
6 changed files with 92 additions and 149 deletions

View File

@ -104,22 +104,6 @@
</TextView>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:orientation="horizontal"
android:id="@+id/trashLayout" android:layout_weight="1"
android:layout_gravity="center">
<ImageView android:id="@+id/trash" android:src="@android:drawable/ic_menu_delete"
android:layout_weight="1" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_gravity="center">
</ImageView>
<TextView android:id="@+id/trashName" android:layout_width="wrap_content"
android:text="poubelle" android:textStyle="bold" android:textSize="15dip"
android:layout_height="wrap_content" android:layout_weight="3"
android:layout_gravity="center_vertical" android:visibility="gone">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>

View File

@ -9,7 +9,11 @@ import org.pticlic.model.Network;
import org.pticlic.model.Network.Mode;
import org.pticlic.model.Relation;
import exception.PtiClicException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
@ -42,6 +46,7 @@ import android.widget.TextView;
*/
public class BaseGame extends Activity implements OnClickListener {
// TODO : mettre la poubelle comme categorie -1
private int currentWord = 0;
private TextView currentWordTextView;
private TextView wordRemaining;
@ -75,19 +80,34 @@ public class BaseGame extends Activity implements OnClickListener {
@Override
protected void onStart() {
super.onStart();
try {
game = (DownloadedBaseGame)network.getGames(1);
runMatch();
start();
} 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();
}
});
AlertDialog alert = builder.create();
alert.show();
finish();
}
game = (DownloadedBaseGame)network.getGames(1);
runMatch();
start();
}
private void runMatch() {
int nbrel = game.getNbRelation();
nbWord = game.getNbWord();
wordRemaining = (TextView)findViewById(R.id.wordRemaining);
wordRemaining.setText((currentWord + 1) + "/" + nbWord);
// On initialise la partie.
match = new Match();
match.setGame(game);
@ -105,75 +125,49 @@ public class BaseGame extends Activity implements OnClickListener {
TextView rn3 = ((TextView)findViewById(R.id.relation3Name));
TextView rn4 = ((TextView)findViewById(R.id.relation4Name));
// Layout des relations
LinearLayout rl1 = ((LinearLayout)findViewById(R.id.relation1Layout));
LinearLayout rl2 = ((LinearLayout)findViewById(R.id.relation2Layout));
LinearLayout rl3 = ((LinearLayout)findViewById(R.id.relation3Layout));
LinearLayout rl4 = ((LinearLayout)findViewById(R.id.relation4Layout));
// Bouton d'aide
ImageView aide = ((ImageView)findViewById(R.id.aideBaseGame));
aide.setOnClickListener(this);
Relation r = Relation.getInstance();
ImageView trash = ((ImageView)findViewById(R.id.trash));
trash.setOnClickListener(this);
trash.setImageResource(android.R.drawable.ic_menu_delete);
// Écoute des clics sur les relations
if (nbrel > 0) {
// TODO : A enlever lorsque l'on aura toutes les images des relations.
try {
r1.setOnClickListener(this);
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);
// TODO : A enlever lorsque l'on aura toutes les images des relations.
try {
r1.setOnClickListener(this);
rn1.setText(r.getRelationName(game.getCat1()));
r1.setImageResource(r.getRelationImage(game.getCat1()));
} catch (Exception e) {
r1.setImageResource(R.drawable.icon);
}
if (nbrel > 1) {
// TODO : A enlever lorsque l'on aura toutes les images des relations.
try {
r2.setOnClickListener(this);
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);
// TODO : A enlever lorsque l'on aura toutes les images des relations.
try {
r2.setOnClickListener(this);
rn2.setText(r.getRelationName(game.getCat2()));
r2.setImageResource(r.getRelationImage(game.getCat2()));
} catch (Exception e) {
r2.setImageResource(R.drawable.icon);
}
if (nbrel > 2) {
// TODO : A enlever lorsque l'on aura toutes les images des relations.
try {
r3.setOnClickListener(this);
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);
// TODO : A enlever lorsque l'on aura toutes les images des relations.
try {
r3.setOnClickListener(this);
rn3.setText(r.getRelationName(game.getCat3()));
r3.setImageResource(r.getRelationImage(game.getCat3()));
} catch (Exception e) {
r3.setImageResource(R.drawable.icon);
}
// TODO : A enlever lorsque l'on aura toutes les images des relations.
try {
r4.setOnClickListener(this);
rn4.setText(r.getRelationName(game.getCat4()));
r4.setImageResource(r.getRelationImage(game.getCat4()));
} catch (Exception e) {
r4.setImageResource(R.drawable.icon);
}
if (nbrel > 3) {
// TODO : A enlever lorsque l'on aura toutes les images des relations.
try {
r4.setOnClickListener(this);
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);
}
((TextView)findViewById(R.id.mainWord)).setText(DownloadedBaseGame.getName(game.getCentre()));
}
/* (non-Javadoc)
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
*/
@ -240,6 +234,7 @@ public class BaseGame extends Activity implements OnClickListener {
leaveView();
start();
} else {
network.sendGame(match);
Intent intent = new Intent(this, Score.class);
intent.putExtra(Constant.SCORE_GAMEPLAYED, match);
intent.putExtra(Constant.SCORE_MODE, Mode.SIMPLE_GAME);
@ -293,13 +288,6 @@ public class BaseGame extends Activity implements OnClickListener {
relationName = ((TextView)findViewById(R.id.relation4Name));
relationName.setVisibility(View.VISIBLE);
//trash
relationLayout = ((LinearLayout)findViewById(R.id.trashLayout));
relationLayout.setGravity(Gravity.LEFT);
relationName = ((TextView)findViewById(R.id.trashName));
relationName.setVisibility(View.VISIBLE);
// On met le mot courant au bon endroit dans la fenetre
// On recupere la largueur de l'ecran.
@ -355,13 +343,6 @@ public class BaseGame extends Activity implements OnClickListener {
relationName = ((TextView)findViewById(R.id.relation4Name));
relationName.setVisibility(View.GONE);
//trash
relationLayout = ((LinearLayout)findViewById(R.id.trashLayout));
relationLayout.setGravity(Gravity.CENTER);
relationName = ((TextView)findViewById(R.id.trashName));
relationName.setVisibility(View.GONE);
// On met le mot courant au bon endroit dans la fenetre
// On recupere la largueur de l'ecran.
Display display = getWindowManager().getDefaultDisplay();
@ -393,13 +374,11 @@ public class BaseGame extends Activity implements OnClickListener {
*/
@Override
public void onClick(View v) {
int currentWord = game.getWordInCloud(this.currentWord).getId();
switch (v.getId()) {
case (R.id.relation1) : match.add(1, currentWord); next(); break;
case (R.id.relation2) : match.add(2, currentWord); next(); break;
case (R.id.relation3) : match.add(3, currentWord); next(); break;
case (R.id.relation4) : match.add(4, currentWord); next(); break;
case (R.id.trash) : match.add(0, currentWord); next(); break;
case (R.id.aideBaseGame) : helpMode(); break;
}
}

View File

@ -64,30 +64,6 @@ public class DownloadedBaseGame extends DownloadedGame {
this.center = center;
this.cloud = cloud;
}
/**
* Permet de recupere le nombre de relation que l'on va utiliser.
*
* @return le nombre de relation
*/
public int getNbRelation() {
int res = 0;
if (cat1 != -1) {
res++;
}
if (cat2 != -1) {
res++;
}
if (cat3 != -1) {
res++;
}
if (cat4 != -1) {
res++;
}
return res;
}
public static String getName(Word word) {
return word.getName();

View File

@ -18,7 +18,6 @@ public class Match implements Serializable {
private ArrayList<Integer> relation2;
private ArrayList<Integer> relation3;
private ArrayList<Integer> relation4;
private ArrayList<Integer> trash;
private DownloadedGame game;
public Match() {
@ -26,7 +25,6 @@ public class Match implements Serializable {
relation2 = new ArrayList<Integer>();
relation3 = new ArrayList<Integer>();
relation4 = new ArrayList<Integer>();
trash = new ArrayList<Integer>();
}
public void setGame(DownloadedGame game) {
@ -43,7 +41,6 @@ public class Match implements Serializable {
case 2: relation2.add(word); break;
case 3: relation3.add(word); break;
case 4: relation4.add(word); break;
default: trash.add(word); break;
}
}
@ -74,12 +71,4 @@ public class Match implements Serializable {
public ArrayList<Integer> getRelation4() {
return relation4;
}
/**
* @return the trash
*/
public ArrayList<Integer> getTrash() {
return trash;
}
}

View File

@ -1,7 +1,11 @@
package org.pticlic.model;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import android.content.Context;
@ -10,6 +14,8 @@ import android.net.ConnectivityManager;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import exception.PtiClicException;
/**
* @author Bertrand BRUN
*
@ -124,7 +130,7 @@ public class Network {
* @param nbGames Le nombre de parties que l'on veut récupérer.
* @return
*/
public DownloadedGame getGames(int nbGames) {
public DownloadedGame getGames(int nbGames) throws PtiClicException {
switch (mode) {
case SIMPLE_GAME:
return DownloadBaseGame(nbGames);
@ -133,18 +139,21 @@ public class Network {
}
}
private DownloadedBaseGame DownloadBaseGame(int nbGames) {
private DownloadedBaseGame DownloadBaseGame(int nbGames) throws PtiClicException {
DownloadedBaseGame game = null;
URL url = null;
Gson gson = null;
BufferedReader reader = null;
try {
// TODO : ne restera le temps que les requete du serveur passe du GET au POST
String urlS = this.serverURL+"/pticlic.php?"
+ "action=" + Action.GET_GAMES.value()
+ "&user=" + this.id
+ "&passwd=" + this.passwd
+ "&nb=" + String.valueOf(nbGames)
//+ "&nb=" + String.valueOf(nbGames)
+ "&mode="+mode.value();
URL url = new URL(urlS);
url = new URL(urlS);
// URLConnection connection = url.openConnection();
// connection.addRequestProperty("action", Action.GET_GAMES.value());
@ -152,22 +161,29 @@ public class Network {
// connection.addRequestProperty("passwd", this.passwd);
// connection.addRequestProperty("nb", String.valueOf(nbGames));
// connection.addRequestProperty("mode", mode.value());
Gson gson = new Gson();
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
String json = reader.readLine();
gson = new Gson();
//JsonReader reader = new JsonReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
JsonReader reader = new JsonReader(new InputStreamReader(url.openStream(), "UTF-8"));
InputStream in = new ByteArrayInputStream(json.getBytes("UTF-8"));
JsonReader jsonReader = new JsonReader(new InputStreamReader(in));
// FIXME : Attention lorsque l'on pourra vraiment recupere plusieur partie, il faudra changer ce qui suit.
reader.beginArray();
while (reader.hasNext()) {
game = makeBaseGame(reader, gson);
jsonReader.beginArray();
while (jsonReader.hasNext()) {
game = makeBaseGame(jsonReader, gson);
}
reader.endArray();
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
jsonReader.endArray();
jsonReader.close();
} catch (Exception e) {
try {
throw new PtiClicException(reader.readLine());
} catch (UnsupportedEncodingException e1) {
throw new PtiClicException(0, "Impossible de recuperer l'erreur, nous avons pris note de cette erreur");
} catch (IOException e1) {
throw new PtiClicException(0, "Impossible de recuperer l'erreur, nous avons pris note de cette erreur");
}
}
return game;
@ -262,9 +278,7 @@ public class Network {
for (Integer i : game.getRelation4()) {
urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat4();
}
// for (Integer i : game.getRelation4()) {
// urlS += "&" + ((DownloadedBaseGame)game.getGame()).getCat4() + "=" + i;
// }
URL url = new URL(urlS);
// URL url = new URL(this.serverURL);

View File

@ -20,13 +20,14 @@ public class Relation {
private Relation() {
imageRelations = new HashMap<Integer, Integer>();
imageRelations.put(-1, android.R.drawable.ic_menu_delete);
imageRelations.put(5, R.drawable.synonyme);
imageRelations.put(7, R.drawable.contraire);
imageRelations.put(9, R.drawable.contenu);
imageRelations.put(10, R.drawable.contenant);
stringRelations = new HashMap<Integer, String>();
stringRelations.put(-1, "");
stringRelations.put(-1, "poubelle");
stringRelations.put(0, "idée");
stringRelations.put(1, "raffinement sémantique");
stringRelations.put(2, "raffinement morphologique");