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

New API (SelectPlayersFragment)

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