114 lines
4.2 KiB
Java
114 lines
4.2 KiB
Java
package de.jeyp91.apifootball;
|
|
|
|
import de.jeyp91.BaseMatch;
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.JSONObject;
|
|
|
|
public class APIFootballMatch extends BaseMatch {
|
|
|
|
private int matchId;
|
|
private int leagueId;
|
|
private String teamNameHome;
|
|
private String teamNameGuest;
|
|
private String round;
|
|
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"));
|
|
this.round = json.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) json.get("event_date");
|
|
this.status = parseStatus(json.get("statusShort").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 = 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 String getRound() {
|
|
return this.round;
|
|
}
|
|
|
|
public void setShowTable(boolean showTable) {
|
|
this.showTable = showTable;
|
|
}
|
|
|
|
public Boolean getShowTable() {
|
|
return this.showTable;
|
|
}
|
|
|
|
private int parseStatus(String statusShort) {
|
|
switch (statusShort) {
|
|
case "TBD":
|
|
case "NS": return 0;
|
|
case "FT":
|
|
case "AET":
|
|
case "PEN": return 3;
|
|
default: return 1;
|
|
}
|
|
}
|
|
|
|
private void parseResult(JSONObject json) {
|
|
Object resultFulltime = ((JSONObject) json.get("score")).get("fulltime");
|
|
if(resultFulltime != null) {
|
|
String[] result = resultFulltime.toString().split("-");
|
|
this.goalsHome = Integer.parseInt(result[0]);
|
|
this.goalsGuest = Integer.parseInt(result[1]);
|
|
}
|
|
|
|
Object resultExtratime = ((JSONObject) json.get("score")).get("extratime");
|
|
if(resultExtratime != null) {
|
|
String[] result = resultExtratime.toString().split("-");
|
|
this.goalsOvertimeHome = this.goalsHome + Integer.parseInt(result[0]);
|
|
this.goalsOvertimeGuest = this.goalsGuest + Integer.parseInt(result[1]);
|
|
}
|
|
|
|
Object resultPenalty = ((JSONObject) json.get("score")).get("penalty");
|
|
if(resultPenalty != null) {
|
|
String[] result = resultPenalty.toString().split("-");
|
|
this.goalsPenaltyHome = this.goalsOvertimeHome + Integer.parseInt(result[0]);
|
|
this.goalsPenaltyGuest = this.goalsOvertimeGuest + Integer.parseInt(result[1]);
|
|
}
|
|
}
|
|
}
|