Merge branch 'master' of https://github.com/jsmaniac/2011-m1s2-ter
This commit is contained in:
commit
57b5c0611a
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
.DS_Store
|
.DS_Store
|
||||||
code.zip
|
code.zip
|
||||||
|
vp.log
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
<activity android:name=".Preference" android:label="Préférence" android:screenOrientation="portrait"></activity>
|
<activity android:name=".Preference" android:label="Préférence" android:screenOrientation="portrait"></activity>
|
||||||
<activity android:name=".games.BaseGame" 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: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>
|
<activity android:name="FrontPage" android:screenOrientation="portrait"></activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
|
|
@ -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">
|
<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/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_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>
|
||||||
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_weight="1" android:layout_height="wrap_content">
|
<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>
|
<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>
|
||||||
|
|
|
@ -24,33 +24,43 @@ import android.widget.TextView;
|
||||||
*
|
*
|
||||||
* Permet l'affichage du score obtenu par le joueur lors de sa partie.
|
* 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 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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.score);
|
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
|
// Permet de regler la precision : rajoute/enlever des # pour modifier la precision
|
||||||
DecimalFormat dfrmtr = new DecimalFormat("#.##");
|
DecimalFormat dfrmtr = new DecimalFormat("#.##");
|
||||||
Double score = network.sendGame(gamePlayed);
|
Double score = network.sendGame(gamePlayed);
|
||||||
((TextView)findViewById(R.id.total)).setText(String.valueOf(dfrmtr.format(score)));
|
((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();
|
sp.edit().putString(Constant.NEW_BASE_GAME, network.getNewGame()).commit();
|
||||||
} catch (PtiClicException e) {
|
} catch (PtiClicException e) {
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
@ -1,7 +1,7 @@
|
||||||
package org.pticlic.games;
|
package org.pticlic.games;
|
||||||
|
|
||||||
import org.pticlic.R;
|
import org.pticlic.R;
|
||||||
import org.pticlic.Score;
|
import org.pticlic.BaseScore;
|
||||||
import org.pticlic.exception.PtiClicException;
|
import org.pticlic.exception.PtiClicException;
|
||||||
import org.pticlic.model.Constant;
|
import org.pticlic.model.Constant;
|
||||||
import org.pticlic.model.DownloadedBaseGame;
|
import org.pticlic.model.DownloadedBaseGame;
|
||||||
|
@ -283,7 +283,7 @@ public class BaseGame extends Activity implements OnClickListener {
|
||||||
leaveView();
|
leaveView();
|
||||||
start();
|
start();
|
||||||
} else {
|
} 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_GAMEPLAYED, match);
|
||||||
intent.putExtra(Constant.SCORE_MODE, Mode.SIMPLE_GAME);
|
intent.putExtra(Constant.SCORE_MODE, Mode.SIMPLE_GAME);
|
||||||
|
|
||||||
|
|
|
@ -69,6 +69,19 @@ public class DownloadedBaseGame extends DownloadedGame {
|
||||||
return word.getName();
|
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() {
|
public int getCat1() {
|
||||||
return cat1;
|
return cat1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,18 @@ import com.google.gson.stream.JsonReader;
|
||||||
*/
|
*/
|
||||||
public class Network {
|
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 {
|
public static class Check implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private boolean login_ok = false;
|
private boolean login_ok = false;
|
||||||
|
@ -261,21 +273,19 @@ public class Network {
|
||||||
* @param game La partie jouee par l'utilisateur
|
* @param game La partie jouee par l'utilisateur
|
||||||
* @return Le score sous forme JSON.
|
* @return Le score sous forme JSON.
|
||||||
*/
|
*/
|
||||||
public double sendGame(Match game) throws PtiClicException, Exception {
|
public ScoreResponse sendGame(Match game) throws PtiClicException, Exception {
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case SIMPLE_GAME:
|
case SIMPLE_GAME:
|
||||||
return sendBaseGame(game);
|
return sendBaseGame(game);
|
||||||
default:
|
default:
|
||||||
return -1;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ScoreResponse sendBaseGame(Match game) throws PtiClicException, Exception {
|
||||||
public double sendBaseGame(Match game) throws PtiClicException, Exception {
|
|
||||||
double score = -1;
|
double score = -1;
|
||||||
Gson gson = null;
|
Gson gson = null;
|
||||||
String json = null;
|
String json = null;
|
||||||
try {
|
|
||||||
|
|
||||||
// TODO : ne restera le temps que les requete du serveur passe du GET au POST
|
// TODO : ne restera le temps que les requete du serveur passe du GET au POST
|
||||||
String urlS = this.serverURL
|
String urlS = this.serverURL
|
||||||
|
@ -311,49 +321,14 @@ public class Network {
|
||||||
gson = new Gson();
|
gson = new Gson();
|
||||||
json = HttpClient.SendHttpPost(urlS);
|
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.
|
// 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
|
// On creer tout d'abord une objet error et si celui-ci est vide on creer l'objet score, sinon on lance
|
||||||
// une exception.
|
// une exception.
|
||||||
PtiClicException.Error error = gson.fromJson(json, PtiClicException.Error.class);
|
PtiClicException.Error error = gson.fromJson(json, PtiClicException.Error.class);
|
||||||
if (error.getMsg() == null) {
|
if (error.getMsg() == null) {
|
||||||
score = getScore(jsonReader, gson);
|
return gson.fromJson(json, ScoreResponse.class);
|
||||||
} else {
|
} else {
|
||||||
throw new PtiClicException(error);
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
private double getScore(JsonReader reader, Gson gson) throws IOException {
|
|
||||||
double score = -1;
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reader.endObject();
|
|
||||||
return score;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getNewGame() {
|
|
||||||
return this.newGameJson;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 = $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();
|
$game = $game->fetchArray();
|
||||||
|
|
||||||
echo '{"gid":'.$gameId.',"pgid":'.$pgid.',"cat1":'.$game['relation_1'].',"cat2":'.$game['relation_2'].',"cat3":0,"cat4":-1,';
|
$retstr = "";
|
||||||
echo '"center":{"id":'.$game['eid_central_word'].',"name":'.json_encode(''.formatWord($game['name_central_word'])).'},';
|
$retstr .= '{"gid":'.$gameId.',"pgid":'.$pgid.',"cat1":'.$game['relation_1'].',"cat2":'.$game['relation_2'].',"cat3":0,"cat4":-1,';
|
||||||
echo '"cloudsize":10,"cloud":['; // TODO ! compter dynamiquement.
|
$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.";");
|
$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;
|
$notfirst = false;
|
||||||
|
@ -370,14 +371,15 @@ function game2json($user, $gameId)
|
||||||
while ($x = $res->fetchArray())
|
while ($x = $res->fetchArray())
|
||||||
{
|
{
|
||||||
if ($notfirst)
|
if ($notfirst)
|
||||||
echo ",";
|
$retstr .= ",";
|
||||||
else
|
else
|
||||||
$notfirst=true;
|
$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.
|
/** Récupère une partie sous forme de tableau.
|
||||||
|
@ -465,7 +467,7 @@ function getGame($user, $nbGames, $mode)
|
||||||
|
|
||||||
for ($i=0; $i < $nbGames; $i)
|
for ($i=0; $i < $nbGames; $i)
|
||||||
{
|
{
|
||||||
game2json($user, randomGame());
|
echo game2json($user, randomGame());
|
||||||
|
|
||||||
if ((++$i) < $nbGames)
|
if ((++$i) < $nbGames)
|
||||||
echo ",";
|
echo ",";
|
||||||
|
|
|
@ -73,7 +73,7 @@ function main()
|
||||||
$scores = setGame($user, intval($_GET['pgid']), intval($_GET['gid']), $_GET);
|
$scores = setGame($user, intval($_GET['pgid']), intval($_GET['gid']), $_GET);
|
||||||
// On renvoie une nouvelle partie pour garder le client toujours bien alimenté.
|
// On renvoie une nouvelle partie pour garder le client toujours bien alimenté.
|
||||||
echo "{\"score\":".$scores['total'].",\"newGame\":";
|
echo "{\"score\":".$scores['total'].",\"newGame\":";
|
||||||
game2json($user, randomGame());
|
json_encode("".game2json($user, randomGame()));
|
||||||
echo "}";
|
echo "}";
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("Commande inconnue", 2);
|
throw new Exception("Commande inconnue", 2);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user