From 12164c03aed6cc238d20b37352b94236f58945c7 Mon Sep 17 00:00:00 2001 From: Julian Arndt Date: Mon, 10 Apr 2023 13:39:45 +0200 Subject: [PATCH] Parse overtime goals --- src/main/java/de/jeyp91/BaseMatch.java | 10 ++++++++ .../apifootball/APIFootballConnector.java | 2 +- .../jeyp91/apifootball/APIFootballMatch.java | 13 +++++++++++ .../java/de/jeyp91/tippliga/TLWMatch.java | 20 ++++++++++++++++ .../apifootball/APIFootballMatchTest.java | 23 +++++++++++++++++++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/test/java/de/jeyp91/apifootball/APIFootballMatchTest.java diff --git a/src/main/java/de/jeyp91/BaseMatch.java b/src/main/java/de/jeyp91/BaseMatch.java index 11d5b70..bbdef5c 100644 --- a/src/main/java/de/jeyp91/BaseMatch.java +++ b/src/main/java/de/jeyp91/BaseMatch.java @@ -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; } diff --git a/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java b/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java index cafa118..787da93 100644 --- a/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java +++ b/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java @@ -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; diff --git a/src/main/java/de/jeyp91/apifootball/APIFootballMatch.java b/src/main/java/de/jeyp91/apifootball/APIFootballMatch.java index c7c01c3..7c7f8d8 100644 --- a/src/main/java/de/jeyp91/apifootball/APIFootballMatch.java +++ b/src/main/java/de/jeyp91/apifootball/APIFootballMatch.java @@ -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)); + } + } } diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatch.java b/src/main/java/de/jeyp91/tippliga/TLWMatch.java index 3a58f2e..bb14d4e 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatch.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatch.java @@ -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; } diff --git a/src/test/java/de/jeyp91/apifootball/APIFootballMatchTest.java b/src/test/java/de/jeyp91/apifootball/APIFootballMatchTest.java new file mode 100644 index 0000000..dd8e841 --- /dev/null +++ b/src/test/java/de/jeyp91/apifootball/APIFootballMatchTest.java @@ -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); + } +}