package de.jeyp91.apifootball; import de.jeyp91.BaseMatch; import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class APIFootballMatch extends BaseMatch { private final int matchId; private final int leagueId; private final String teamNameHome; private final String teamNameGuest; private Boolean showTable = null; public APIFootballMatch(JSONObject json, int season) throws Exception { this.matchId = Integer.parseInt(json.get("fixture_id").toString()); // TODO this.leagueId = Integer.parseInt(json.get("league_id").toString()); this.teamIdHome = Integer.parseInt(((JSONObject) json.get("homeTeam")).get("team_id").toString()); this.teamIdGuest = Integer.parseInt(((JSONObject) json.get("awayTeam")).get("team_id").toString()); this.teamNameHome = ((JSONObject) json.get("homeTeam")).get("team_name").toString(); this.teamNameGuest = ((JSONObject) json.get("awayTeam")).get("team_name").toString(); this.goalsHome = getNumberOrNull(json.get("goalsHomeTeam")); this.goalsGuest = getNumberOrNull(json.get("goalsAwayTeam")); try { this.matchday = getMatchdayFromRoundString(season, json.get("round").toString(), this.leagueId); } catch (Exception e) { throw new Exception("Did not find matchday for league '" + this.leagueId + "': '" + json.get("round").toString() + "'"); } this.matchDatetime = (String) json.get("event_date"); } public int getAPIFootBallMatchID() { return this.matchId; } public static int getMatchdayFromRoundString(int season, String round, int leagueId) { round = round.replace(" ", "_"); Integer matchday = null; APIFootballConnector con = APIFootballConnector.getAPIFootballConnectorInstance(season); JSONObject roundsObject = con.getMatchdays(leagueId); JSONArray roundsArray = (JSONArray)(((JSONObject) roundsObject.get("api")).get("fixtures")); for (int i = 0; i < roundsArray.size(); i++) { if(roundsArray.get(i).toString().equals(round)) { matchday = i + 1; break; } } return matchday; } private Integer getNumberOrNull(Object object) { return object != null ? Integer.parseInt(object.toString()) : null; } public String getTeamNameHome() { return this.teamNameHome; } public String getTeamNameGuest() { return this.teamNameGuest; } public void setShowTable(boolean showTable) { this.showTable = showTable; } public Boolean getShowTable() { return this.showTable; } }