This commit is contained in:
John Charron 2011-03-16 10:13:36 +01:00
commit 57b5c0611a
9 changed files with 122 additions and 117 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.DS_Store
code.zip
vp.log

View File

@ -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>

View File

@ -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>

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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 ",";

View File

@ -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();
?>
?>