package de.jeyp91.apifootball; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import de.jeyp91.BaseMatch; public class APIFootballMatch extends BaseMatch { private int matchId; private int leagueId; private String round; public APIFootballMatch(JSONObject json, int season) throws Exception { this.matchId = Integer.parseInt(((JSONObject) json.get("fixture")).get("id").toString()); this.leagueId = Integer.parseInt(((JSONObject) json.get("league")).get("id").toString()); this.teamIdHome = Integer.valueOf(((JSONObject) ((JSONObject) json.get("teams")).get("home")).get("id").toString()); this.teamIdGuest = Integer.valueOf(((JSONObject) ((JSONObject) json.get("teams")).get("away")).get("id").toString()); this.teamNameHome = ((JSONObject) ((JSONObject) json.get("teams")).get("home")).get("name").toString(); this.teamNameGuest = ((JSONObject) ((JSONObject) json.get("teams")).get("away")).get("name").toString(); this.round = ((JSONObject) json.get("league")).get("round").toString(); try { this.matchday = getMatchdayFromRoundString(season, this.round, this.leagueId); } catch (Exception e) { throw new Exception("Did not find matchday for league '" + this.leagueId + "': '" + json.get("round").toString() + "'"); } this.matchDatetime = (String) ((JSONObject) json.get("fixture")).get("date"); this.status = parseStatus(((JSONObject) ((JSONObject) json.get("fixture")).get("status")).get("short").toString()); this.parseResult(json); } public int getAPIFootBallMatchID() { return this.matchId; } public static int getMatchdayFromRoundString(int season, String round, int leagueId) { round = round.replace(" ", "_"); Integer matchday = -1; if (round.startsWith("Regular_Season_-_")) { matchday = Integer.valueOf(round.replace("Regular_Season_-_", "")); } else { APIFootballConnector con = APIFootballConnector.getAPIFootballConnectorInstance(season); JSONObject roundsObject = con.getMatchdays(leagueId); JSONArray roundsArray = (JSONArray)(roundsObject.get("response")); for (int i = 0; i < roundsArray.size(); i++) { if(roundsArray.get(i).toString().replace("_", " ").equals(round.replace("_", " "))) { matchday = i + 1; break; } } } return matchday; } private Integer getNumberOrNull(Object object) { return object != null ? Integer.valueOf(object.toString()) : null; } public String getRound() { return this.round; } private int parseStatus(String statusShort) { return switch (statusShort) { case "TBD", "NS" -> 0; case "FT", "AET", "PEN" -> 3; default -> 1; }; } private void parseResult(JSONObject json) { Object resultFulltime = ((JSONObject) json.get("score")).get("fulltime"); this.goalsHome = getNumberOrNull(((JSONObject) resultFulltime).get("home")); this.goalsGuest = getNumberOrNull(((JSONObject) resultFulltime).get("away")); Object resultExtratime = ((JSONObject) json.get("score")).get("extratime"); this.goalsOvertimeHome = getNumberOrNull(((JSONObject) resultExtratime).get("home")); this.goalsOvertimeGuest = getNumberOrNull(((JSONObject) resultExtratime).get("away")); Object resultPenalty = ((JSONObject) json.get("score")).get("penalty"); this.goalsPenaltyHome = getNumberOrNull(((JSONObject) resultPenalty).get("home")); this.goalsPenaltyGuest = getNumberOrNull(((JSONObject) resultPenalty).get("away")); } }