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

This commit is contained in:
Georges Dupéron 2011-02-12 15:29:04 +01:00
commit d1a1fe7c19
5 changed files with 70 additions and 26 deletions

View File

@ -14,7 +14,7 @@
<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=".FrontPage" android:screenOrientation="portrait"></activity>
<activity android:name="FrontPage" android:screenOrientation="portrait"></activity>
</application>

View File

@ -1,11 +1,16 @@
package org.pticlic;
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.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
@ -42,7 +47,23 @@ public class Score extends Activity implements OnClickListener{
Network network = new Network(serverURL, mode, id, passwd);
// FIXME : Pour l'instant ne marche pas, attend de savoir comment est formater le score que l'on recois.
//DownloadedScore score = network.sendGame(gamePlayed);
try {
network.sendGame(gamePlayed);
} 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();
}
((Button)findViewById(R.id.saw)).setOnClickListener(this);

View File

@ -1,4 +1,4 @@
package exception;
package org.pticlic.exception;
import java.io.Serializable;
@ -9,7 +9,7 @@ public class PtiClicException extends Exception {
private static final long serialVersionUID = 1L;
private Error error;
private static class Error implements Serializable {
public static class Error implements Serializable {
private static final long serialVersionUID = 1L;
private int num;
@ -31,6 +31,10 @@ public class PtiClicException extends Exception {
}
}
public PtiClicException(Error error) {
this.error = error;
}
public PtiClicException(int num, String msg) {
this.error = new Error(num, msg);
}

View File

@ -2,6 +2,7 @@ package org.pticlic.games;
import org.pticlic.R;
import org.pticlic.Score;
import org.pticlic.exception.PtiClicException;
import org.pticlic.model.Constant;
import org.pticlic.model.DownloadedBaseGame;
import org.pticlic.model.Match;
@ -9,7 +10,6 @@ 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;
@ -234,7 +234,6 @@ 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);

View File

@ -8,13 +8,14 @@ import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import org.pticlic.exception.PtiClicException;
import android.content.Context;
import android.net.ConnectivityManager;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import exception.PtiClicException;
/**
* @author Bertrand BRUN
@ -236,7 +237,13 @@ public class Network {
return new DownloadedBaseGame(id, gid, pgid, cat1, cat2, cat3, cat4, center, cloud);
}
public TotalScore sendGame(Match game) {
/**
* Cette méthode permet d'envoyer les parties au serveur pour qu'il puisse les
* rajouter à la base de données, et calculer le score.
* @param game La partie jouee par l'utilisateur
* @return Le score sous forme JSON.
*/
public TotalScore sendGame(Match game) throws PtiClicException {
switch (mode) {
case SIMPLE_GAME:
return sendBaseGame(game);
@ -245,14 +252,13 @@ public class Network {
}
}
/**
* Cette méthode permet d'envoyer les parties au serveur pour qu'il puisse les
* rajouter à la base de données, et calculer le score.
* @param game La partie jouee par l'utilisateur
* @return Le score sous forme JSON.
*/
public TotalScore sendBaseGame(Match game) {
public TotalScore sendBaseGame(Match game) throws PtiClicException {
TotalScore score = null;
URL url = null;
Gson gson = null;
BufferedReader reader = null;
String json = null;
try {
// TODO : ne restera le temps que les requete du serveur passe du GET au POST
@ -278,7 +284,7 @@ public class Network {
urlS += "&" + i + "=" + ((DownloadedBaseGame)game.getGame()).getCat4();
}
URL url = new URL(urlS);
url = new URL(urlS);
// URL url = new URL(this.serverURL);
// URLConnection connection = url.openConnection();
@ -288,16 +294,30 @@ public class Network {
// connection.addRequestProperty("mode", mode.value());
// connection.addRequestProperty("pgid", String.valueOf(game.getGame().getId()));
Gson gson = new Gson();
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
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));
score = gson.fromJson(reader, TotalScore.class);
} catch (IOException e) {
return score;
// 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 = gson.fromJson(jsonReader, TotalScore.class);
} 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");
}
return score;
}
}