Parse overtime goals

This commit is contained in:
2023-04-10 13:39:45 +02:00
parent d078a182a1
commit 12164c03ae
5 changed files with 67 additions and 1 deletions

View File

@@ -13,6 +13,8 @@ public abstract class BaseMatch {
protected String teamNameGuest = null;
protected Integer goalsHome = null;
protected Integer goalsGuest = null;
protected Integer goalsOvertimeHome = null;
protected Integer goalsOvertimeGuest = null;
protected Integer matchday = null;
protected String matchDatetime = null;
@@ -40,6 +42,14 @@ public abstract class BaseMatch {
return this.goalsGuest;
}
public Integer getGoalsOvertimeHome() {
return this.goalsOvertimeHome;
}
public Integer getGoalsOvertimeGuest() {
return this.goalsOvertimeGuest;
}
public String getTeamNameHome() {
return this.teamNameHome;
}

View File

@@ -94,7 +94,7 @@ public class APIFootballConnector {
return stringToJSONObject(content);
}
private JSONObject stringToJSONObject(String rawData) {
public static JSONObject stringToJSONObject(String rawData) {
JSONParser parser = new JSONParser();
JSONObject data = null;

View File

@@ -30,6 +30,7 @@ public class APIFootballMatch extends BaseMatch {
throw new Exception("Did not find matchday for league '" + this.leagueId + "': '" + json.get("round").toString() + "'");
}
this.matchDatetime = (String) json.get("event_date");
this.parseResult(json);
}
public int getAPIFootBallMatchID() {
@@ -74,4 +75,16 @@ public class APIFootballMatch extends BaseMatch {
public Boolean getShowTable() {
return this.showTable;
}
private void parseResult(JSONObject json) {
String resultFulltime = ((JSONObject) json.get("score")).get("fulltime").toString();
this.goalsHome = Integer.parseInt(resultFulltime.substring(0, 1));
this.goalsGuest = Integer.parseInt(resultFulltime.substring(2, 3));
Object resultExtratime = ((JSONObject) json.get("score")).get("extratime");
if(resultExtratime != null) {
this.goalsOvertimeHome = this.goalsHome + Integer.parseInt(resultExtratime.toString().substring(0, 1));
this.goalsOvertimeGuest = this.goalsGuest + Integer.parseInt(resultExtratime.toString().substring(2, 3));
}
}
}

View File

@@ -98,6 +98,10 @@ public class TLWMatch extends BaseMatch{
this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.HOME);
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.GUEST);
this.goalsHome = apiFootballMatch.getGoalsHome();
this.goalsOvertimeHome = apiFootballMatch.getGoalsOvertimeHome();
this.goalsOvertimeGuest = apiFootballMatch.getGoalsOvertimeGuest();
this.goalsGuest = apiFootballMatch.getGoalsGuest();
this.goalsHome = apiFootballMatch.getGoalsHome();
this.goalsGuest = apiFootballMatch.getGoalsGuest();
this.matchDatetime = apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);
this.groupId = this.getGroupFromMatchdayString(apiFootballMatch.getRound());
@@ -248,6 +252,22 @@ public class TLWMatch extends BaseMatch{
return string != null ? "'"+string+"'" : "''";
}
public void setGoalsHome(int goals) {
this.goalsHome = goals;
}
public void setGoalsGuest(int goals) {
this.goalsGuest = goals;
}
public void setGoalsOvertimeHome(int goals) {
this.goalsOvertimeHome = goals;
}
public void setGoalsOvertimeGuest(int goals) {
this.goalsOvertimeGuest = goals;
}
public void setStatus(int status) {
this.status = status;
}

View File

@@ -0,0 +1,23 @@
package de.jeyp91.apifootball;
import org.json.simple.JSONObject;
import org.junit.Test;
import static de.jeyp91.apifootball.APIFootballConnector.stringToJSONObject;
public class APIFootballMatchTest {
@Test
public void withExtratime() throws Exception {
String matchString = "{\"fixture_id\":958530,\"league_id\":4303,\"league\":{\"name\":\"DFB Pokal\",\"country\":\"Germany\",\"logo\":\"https:\\/\\/media-3.api-sports.io\\/football\\/leagues\\/81.png\",\"flag\":\"https:\\/\\/media-1.api-sports.io\\/flags\\/de.svg\"},\"event_date\":\"2022-10-19T18:00:00+02:00\",\"event_timestamp\":1666195200,\"firstHalfStart\":1666195200,\"secondHalfStart\":1666198800,\"round\":\"2nd Round\",\"status\":\"Match Finished\",\"statusShort\":\"PEN\",\"elapsed\":120,\"venue\":\"Home Deluxe Arena\",\"referee\":\"Frank Willenborg, Germany\",\"homeTeam\":{\"team_id\":185,\"team_name\":\"SC Paderborn 07\",\"logo\":\"https:\\/\\/media-2.api-sports.io\\/football\\/teams\\/185.png\"},\"awayTeam\":{\"team_id\":162,\"team_name\":\"Werder Bremen\",\"logo\":\"https:\\/\\/media-3.api-sports.io\\/football\\/teams\\/162.png\"},\"goalsHomeTeam\":2,\"goalsAwayTeam\":2,\"score\":{\"halftime\":\"2-0\",\"fulltime\":\"2-2\",\"extratime\":\"0-0\",\"penalty\":\"5-4\"}}";
JSONObject matchJson = stringToJSONObject(matchString);
APIFootballMatch match = new APIFootballMatch(matchJson, 2023);
}
@Test
public void withoutExtratime() throws Exception {
String matchString = "{\"fixture_id\":958530,\"league_id\":4303,\"league\":{\"name\":\"DFB Pokal\",\"country\":\"Germany\",\"logo\":\"https:\\/\\/media-3.api-sports.io\\/football\\/leagues\\/81.png\",\"flag\":\"https:\\/\\/media-1.api-sports.io\\/flags\\/de.svg\"},\"event_date\":\"2022-10-19T18:00:00+02:00\",\"event_timestamp\":1666195200,\"firstHalfStart\":1666195200,\"secondHalfStart\":1666198800,\"round\":\"2nd Round\",\"status\":\"Match Finished\",\"statusShort\":\"PEN\",\"elapsed\":120,\"venue\":\"Home Deluxe Arena\",\"referee\":\"Frank Willenborg, Germany\",\"homeTeam\":{\"team_id\":185,\"team_name\":\"SC Paderborn 07\",\"logo\":\"https:\\/\\/media-2.api-sports.io\\/football\\/teams\\/185.png\"},\"awayTeam\":{\"team_id\":162,\"team_name\":\"Werder Bremen\",\"logo\":\"https:\\/\\/media-3.api-sports.io\\/football\\/teams\\/162.png\"},\"goalsHomeTeam\":2,\"goalsAwayTeam\":2,\"score\":{\"halftime\":\"2-0\",\"fulltime\":\"2-2\",\"extratime\":null,\"penalty\":null}}";
JSONObject matchJson = stringToJSONObject(matchString);
APIFootballMatch match = new APIFootballMatch(matchJson, 2023);
}
}