Commit 9c8ed921 authored by rakam's avatar rakam
Browse files

New API (SelectPlayersFragment)

parent 16966801
...@@ -6,16 +6,9 @@ import android.os.AsyncTask; ...@@ -6,16 +6,9 @@ import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import fr.istic.masteru.android.R; import fr.istic.masteru.android.R;
import fr.istic.masteru.android.automate.Automate; import fr.istic.masteru.android.automate.Automate;
import fr.istic.masteru.android.automate.FileManager; import fr.istic.masteru.android.automate.FileManager;
import fr.istic.masteru.android.model.Match;
import fr.istic.masteru.android.model.PlayerInfo;
import fr.istic.masteru.android.utils.API_URLS;
public class HomeActivity extends Activity { public class HomeActivity extends Activity {
...@@ -46,71 +39,72 @@ public class HomeActivity extends Activity { ...@@ -46,71 +39,72 @@ public class HomeActivity extends Activity {
@Override @Override
protected void onPostExecute(final Automate automate) { protected void onPostExecute(final Automate automate) {
super.onPostExecute(automate); super.onPostExecute(automate);
Ion.with(HomeActivity.this) //todo New API
.load("GET", API_URLS.INFO_MATCH + automate.getIdMatch()) // Ion.with(HomeActivity.this)
.asJsonObject() // .load("GET", API_URLS.INFO_MATCH + automate.getIdMatch())
.setCallback(new FutureCallback<JsonObject>() { // .asJsonObject()
@Override // .setCallback(new FutureCallback<JsonObject>() {
public void onCompleted(Exception e, JsonObject result) { // @Override
if (e != null || result == null) { // public void onCompleted(Exception e, JsonObject result) {
checkForSavedAutomate(); // if (e != null || result == null) {
return; // checkForSavedAutomate();
} // return;
String countryA = result.getAsJsonPrimitive("Nation_A").getAsString(); // }
String countryB = result.getAsJsonPrimitive("Nation_B").getAsString(); // String countryA = result.getAsJsonPrimitive("Nation_A").getAsString();
final String categorie = result.getAsJsonPrimitive("Categorie").getAsString(); // String countryB = result.getAsJsonPrimitive("Nation_B").getAsString();
String tableau = result.getAsJsonPrimitive("Tableau").getAsString(); // final String categorie = result.getAsJsonPrimitive("Categorie").getAsString();
// String tableau = result.getAsJsonPrimitive("Tableau").getAsString();
final PlayerInfo playerA1, playerA2, playerB1, playerB2; //
String nameA = result.getAsJsonPrimitive("Nom_CompletA").getAsString(); // final PlayerInfo playerA1, playerA2, playerB1, playerB2;
String nameB = result.getAsJsonPrimitive("Nom_CompletB").getAsString(); // String nameA = result.getAsJsonPrimitive("Nom_CompletA").getAsString();
if (categorie.contains("D")) { // String nameB = result.getAsJsonPrimitive("Nom_CompletB").getAsString();
int idPlayerA1 = result.getAsJsonPrimitive("IdJoueurA_1").getAsInt(); // if (categorie.contains("D")) {
int idPlayerA2 = result.getAsJsonPrimitive("IdJoueurA_2").getAsInt(); // int idPlayerA1 = result.getAsJsonPrimitive("IdJoueurA_1").getAsInt();
int idPlayerB1 = result.getAsJsonPrimitive("IdJoueurB_1").getAsInt(); // int idPlayerA2 = result.getAsJsonPrimitive("IdJoueurA_2").getAsInt();
int idPlayerB2 = result.getAsJsonPrimitive("IdJoueurB_2").getAsInt(); // int idPlayerB1 = result.getAsJsonPrimitive("IdJoueurB_1").getAsInt();
String nameA1 = nameA.split("/")[0]; // int idPlayerB2 = result.getAsJsonPrimitive("IdJoueurB_2").getAsInt();
String nameA2 = nameA.split("/")[1]; // String nameA1 = nameA.split("/")[0];
String nameB1 = nameB.split("/")[0]; // String nameA2 = nameA.split("/")[1];
String nameB2 = nameB.split("/")[1]; // String nameB1 = nameB.split("/")[0];
playerA1 = new PlayerInfo(idPlayerA1, PlayerInfo.MAN, nameA1, countryA); // String nameB2 = nameB.split("/")[1];
playerA2 = new PlayerInfo(idPlayerA2, PlayerInfo.MAN, nameA2, countryA); // playerA1 = new PlayerInfo(idPlayerA1, PlayerInfo.MAN, nameA1, countryA);
playerB1 = new PlayerInfo(idPlayerB1, PlayerInfo.MAN, nameB1, countryB); // playerA2 = new PlayerInfo(idPlayerA2, PlayerInfo.MAN, nameA2, countryA);
playerB2 = new PlayerInfo(idPlayerB2, PlayerInfo.MAN, nameB2, countryB); // playerB1 = new PlayerInfo(idPlayerB1, PlayerInfo.MAN, nameB1, countryB);
} else { // playerB2 = new PlayerInfo(idPlayerB2, PlayerInfo.MAN, nameB2, countryB);
int idPlayerA = result.getAsJsonPrimitive("IdJoueurA").getAsInt(); // } else {
int idPlayerB = result.getAsJsonPrimitive("IdJoueurB").getAsInt(); // int idPlayerA = result.getAsJsonPrimitive("IdJoueurA").getAsInt();
playerA1 = new PlayerInfo(idPlayerA, PlayerInfo.MAN, nameA, countryA); // int idPlayerB = result.getAsJsonPrimitive("IdJoueurB").getAsInt();
playerB1 = new PlayerInfo(idPlayerB, PlayerInfo.MAN, nameB, countryB); // playerA1 = new PlayerInfo(idPlayerA, PlayerInfo.MAN, nameA, countryA);
playerA2 = null; // playerB1 = new PlayerInfo(idPlayerB, PlayerInfo.MAN, nameB, countryB);
playerB2 = null; // playerA2 = null;
} // playerB2 = null;
final Match match = new Match(automate.getIdMatch(), countryA, countryB, categorie, tableau); // }
findViewById(R.id.resumeMatchButton).setOnClickListener(new View.OnClickListener() { // final Match match = new Match(automate.getIdMatch(), countryA, countryB, categorie, tableau);
@Override // findViewById(R.id.resumeMatchButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) { // @Override
if (categorie.contains("D")) { // public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, DoubleMatchActivity.class); // if (categorie.contains("D")) {
intent.putExtra(MatchActivity.RESUME_MATCH, true); // Intent intent = new Intent(HomeActivity.this, DoubleMatchActivity.class);
intent.putExtra(MatchActivity.MATCH, match); // intent.putExtra(MatchActivity.RESUME_MATCH, true);
intent.putExtra(MatchActivity.PLAYER_A1, playerA1); // intent.putExtra(MatchActivity.MATCH, match);
intent.putExtra(MatchActivity.PLAYER_A2, playerA2); // intent.putExtra(MatchActivity.PLAYER_A1, playerA1);
intent.putExtra(MatchActivity.PLAYER_B1, playerB1); // intent.putExtra(MatchActivity.PLAYER_A2, playerA2);
intent.putExtra(MatchActivity.PLAYER_B2, playerB2); // intent.putExtra(MatchActivity.PLAYER_B1, playerB1);
startActivity(intent); // intent.putExtra(MatchActivity.PLAYER_B2, playerB2);
} else { // startActivity(intent);
Intent intent = new Intent(HomeActivity.this, SingleMatchActivity.class); // } else {
intent.putExtra(MatchActivity.RESUME_MATCH, true); // Intent intent = new Intent(HomeActivity.this, SingleMatchActivity.class);
intent.putExtra(MatchActivity.MATCH, match); // intent.putExtra(MatchActivity.RESUME_MATCH, true);
intent.putExtra(MatchActivity.PLAYER_A1, playerA1); // intent.putExtra(MatchActivity.MATCH, match);
intent.putExtra(MatchActivity.PLAYER_B1, playerB1); // intent.putExtra(MatchActivity.PLAYER_A1, playerA1);
startActivity(intent); // intent.putExtra(MatchActivity.PLAYER_B1, playerB1);
} // startActivity(intent);
} // }
}); // }
findViewById(R.id.resumeMatchButton).setEnabled(true); // });
} // findViewById(R.id.resumeMatchButton).setEnabled(true);
}); // }
// });
} }
}.execute(null, null, null); }.execute(null, null, null);
} }
......
...@@ -84,12 +84,9 @@ public class SelectMatchFragment extends SetupFragment implements OnMatchSelecte ...@@ -84,12 +84,9 @@ public class SelectMatchFragment extends SetupFragment implements OnMatchSelecte
if (tableau == null) if (tableau == null)
tableau = "1/4"; tableau = "1/4";
final JsonObject param = new JsonObject();
param.addProperty("tableau", tableau);
Ion.with(matchSetupActivity) Ion.with(matchSetupActivity)
.load("GET", API_URLS.MATCHS + "?round=1/4") .load("GET", API_URLS.MATCHS + "?round=1/4")
.setTimeout(1000 * 10) .setTimeout(1000 * 10)
.setJsonObjectBody(param)
.asJsonObject() .asJsonObject()
.setCallback(new FutureCallback<JsonObject>() { .setCallback(new FutureCallback<JsonObject>() {
@Override @Override
...@@ -98,8 +95,8 @@ public class SelectMatchFragment extends SetupFragment implements OnMatchSelecte ...@@ -98,8 +95,8 @@ public class SelectMatchFragment extends SetupFragment implements OnMatchSelecte
loadMatches(); loadMatches();
return; return;
} }
JsonArray matchesArray = result.getAsJsonArray("matches");
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
JsonArray matchesArray = result.getAsJsonArray("matches");
for (int i = 0; i < matchesArray.size(); i++) { for (int i = 0; i < matchesArray.size(); i++) {
JsonObject o = matchesArray.get(i).getAsJsonObject(); JsonObject o = matchesArray.get(i).getAsJsonObject();
String status = o.get("Statut").getAsString(); String status = o.get("Statut").getAsString();
......
...@@ -3,6 +3,7 @@ package fr.istic.masteru.android.fragments; ...@@ -3,6 +3,7 @@ package fr.istic.masteru.android.fragments;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -131,29 +132,31 @@ public class SelectPlayersFragment extends SetupFragment { ...@@ -131,29 +132,31 @@ public class SelectPlayersFragment extends SetupFragment {
} }
private void loadPlayers(final int team) { private void loadPlayers(final int team) {
final JsonObject param = new JsonObject();
final String countryCode = team == TEAM_A ? match.getTeamA() : match.getTeamB(); final String countryCode = team == TEAM_A ? match.getTeamA() : match.getTeamB();
String gender = match.getCategory().contains("M") ? "M" : match.getCategory().contains("W") ? "W" : "X"; String gender = match.getCategory().contains("M") ? "M" : match.getCategory().contains("W") ? "W" : "X";
String url = API_URLS.TEAM_PLAYERS(countryCode, gender);
Log.d("URL", url);
Ion.with(matchSetupActivity) Ion.with(matchSetupActivity)
.load(API_URLS.TEAM_PLAYERS(countryCode, gender)) .load("GET", url)
.setTimeout(1000 * 10) .setTimeout(1000 * 10)
.setJsonObjectBody(param) .asJsonObject()
.asJsonArray() .setCallback(new FutureCallback<JsonObject>() {
.setCallback(new FutureCallback<JsonArray>() {
@Override @Override
public void onCompleted(Exception e, JsonArray result) { public void onCompleted(Exception e, JsonObject result) {
if (e != null) { if (e != null || result != null && result.get("code").getAsInt() != 0) {
loadPlayers(team); loadPlayers(team);
return; return;
} }
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
for (int i = 0; i < result.size(); i++) { JsonArray playerArray = result.getAsJsonArray("players");
JsonObject o = result.get(i).getAsJsonObject(); for (int i = 0; i < playerArray.size(); i++) {
int playerId = o.get("IdJoueur").getAsInt(); JsonObject o = playerArray.get(i).getAsJsonObject();
String name = o.get("Nom_Complet").getAsString(); int playerId = o.get("IdPlayer").getAsInt();
int gender = o.get("Sexe").getAsString().equals("H") ? PlayerInfo.MAN : PlayerInfo.WOMAN; String name = o.get("Name").getAsString();
PlayerInfo playerInfo = new PlayerInfo(playerId, gender, name, countryCode); String firstName = o.get("FirstName").getAsString();
int gender = o.get("Sex").getAsString().equals("M") ? PlayerInfo.MAN : PlayerInfo.WOMAN;
PlayerInfo playerInfo = new PlayerInfo(playerId, gender, name, firstName, countryCode);
if (team == TEAM_A) if (team == TEAM_A)
playersA.add(playerInfo); playersA.add(playerInfo);
else else
...@@ -170,7 +173,7 @@ public class SelectPlayersFragment extends SetupFragment { ...@@ -170,7 +173,7 @@ public class SelectPlayersFragment extends SetupFragment {
public void setMatch(Match match) { public void setMatch(Match match) {
this.match = match; this.match = match;
isDouble = match.getCategory().contains("D"); isDouble = match.getCategory().contains("D");
isMixed = match.getCategory().equals("DM"); isMixed = match.getCategory().equals("DX");
} }
@Override @Override
......
...@@ -12,12 +12,14 @@ public class PlayerInfo implements Serializable { ...@@ -12,12 +12,14 @@ public class PlayerInfo implements Serializable {
private int id; private int id;
private int gender; private int gender;
private String name; private String name;
private String firstName;
private String nationality; private String nationality;
public PlayerInfo(int id, int gender, String name, String nationality) { public PlayerInfo(int id, int gender, String name, String firstName, String nationality) {
this.id = id; this.id = id;
this.gender = gender; this.gender = gender;
this.name = name; this.name = name;
this.firstName = firstName;
this.nationality = nationality; this.nationality = nationality;
} }
......
...@@ -13,7 +13,7 @@ public class API_URLS { ...@@ -13,7 +13,7 @@ public class API_URLS {
public static final String INFO_MATCH = BASE_URL + "/match/info/"; // + idMatch public static final String INFO_MATCH = BASE_URL + "/match/info/"; // + idMatch
public static String TEAM_PLAYERS(String teamId, String gender) { public static String TEAM_PLAYERS(String teamId, String gender) {
return BASE_URL + "/teams/" + teamId + "/players?gender=" + gender == null ? "X" : gender; return BASE_URL + "/teams/" + teamId + "/players?gender=" + (gender == null ? "X" : gender);
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment