From d54a7089c82d4427f2f54f176803e3eec175afaf Mon Sep 17 00:00:00 2001 From: Julian Arndt Date: Sat, 20 Mar 2021 15:07:45 +0100 Subject: [PATCH] Add ability for additional delivery dates --- .../apifootball/APIFootballConnector.java | 1 - .../jeyp91/tippliga/TLWMatchdaysCreator.java | 126 ++++++++++++------ .../tippliga/TLWMatchesCreatorFootball.java | 32 +++-- .../tippliga/TLWMatchdaysCreatorTest.java | 16 +-- 4 files changed, 114 insertions(+), 61 deletions(-) diff --git a/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java b/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java index cb94526..cafa118 100644 --- a/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java +++ b/src/main/java/de/jeyp91/apifootball/APIFootballConnector.java @@ -1,7 +1,6 @@ package de.jeyp91.apifootball; import de.jeyp91.S3Provider; -import de.jeyp91.tippliga.TLWMatchesUpdaterFootball; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.json.simple.JSONArray; diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java index b9894b6..e22544f 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java @@ -1,5 +1,7 @@ package de.jeyp91.tippliga; +import de.jeyp91.apifootball.APIFootballConnector; +import de.jeyp91.apifootball.APIFootballMatch; import de.jeyp91.tippligaforum.TippligaConfigProvider; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -45,34 +47,91 @@ public class TLWMatchdaysCreator { numberOfMatches = Integer.parseInt(matchdayConfigJson.get("numberOfMatches").toString()); } - String deliveryDate1; - String deliveryDate2 = null; + ArrayList deliveryDates = getDeliveryDates(matchdayConfigJson, matchdayNumber); - JSONArray matchesConfig = (JSONArray) matchdayConfigJson.get("matchesConfig"); - JSONObject matchesConfigPlaceholder = (JSONObject) matchesConfig.get(matchesConfig.size() - 1); - ArrayList matchesOfMatchday = getMatchesForMatchday(matches, matchdayNumber); - if(matchesOfMatchday.size() == 0) { - deliveryDate1 = matchesConfigPlaceholder.get("placeholderDatetime").toString(); - } - else { - LocalDateTime firstMatchDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(matchesOfMatchday); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - deliveryDate1 = firstMatchDate.format(formatter); - - ArrayList remainingMatches = TLWMatchesManagerBase.getMatchesStartingFromSecondDay(matchesOfMatchday); - if(remainingMatches.size() > 0) { - firstMatchDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(remainingMatches); - deliveryDate2 = firstMatchDate.format(formatter); - } - } - - TLWMatchday matchday = new TLWMatchday(this.season, this.league, matchdayNumber, 0, deliveryDate1, deliveryDate2, "", matchdayName, numberOfMatches); + TLWMatchday matchday = new TLWMatchday(this.season, this.league, matchdayNumber, 0, deliveryDates.get(0), deliveryDates.get(1), deliveryDates.get(2), matchdayName, numberOfMatches); matchdays.add(matchday); } return matchdays; } + private ArrayList getMatchesForMatchday(ArrayList matches, int matchday) { + ArrayList matchesOfMatchday = new ArrayList<>(); + for(TLWMatch match : matches) { + if(match.getMatchday() == matchday) { + matchesOfMatchday.add(match); + } + } + return matchesOfMatchday; + } + + private String getMatchdayNameFromConfig(int matchday) { + String matchdayName = ""; + JSONArray matchdaysConfig = (JSONArray) this.configObject.get("matchdayConfig"); + for (Object matchdayConfig : matchdaysConfig) { + if(((JSONObject) matchdayConfig).get("TLWMatchday").toString().equals(String.valueOf(matchday)) + && ((JSONObject) matchdayConfig).containsKey("matchdayName")) { + matchdayName = ((JSONObject) matchdayConfig).get("matchdayName").toString(); + } + } + return matchdayName; + } + + private ArrayList getDeliveryDates(JSONObject matchdayConfigJson, int matchdayNumber) { + ArrayList deliveryDates = getDefaultDeliveryDates(matchdayConfigJson, matchdayNumber); + deliveryDates.addAll(getAdditionalDeliveryDates(matchdayConfigJson, matchdayNumber)); + deliveryDates = removeDuplicates(deliveryDates); + while(deliveryDates.size() < 3) { + deliveryDates.add(""); + } + return deliveryDates; + } + + private ArrayList getDefaultDeliveryDates(JSONObject matchdayConfigJson, int matchdayNumber) { + ArrayList deliveryDates = new ArrayList<>(); + + JSONArray matchesConfig = (JSONArray) matchdayConfigJson.get("matchesConfig"); + JSONObject matchesConfigPlaceholder = (JSONObject) matchesConfig.get(matchesConfig.size() - 1); + ArrayList matchesOfMatchday = getMatchesForMatchday(matches, matchdayNumber); + if(matchesOfMatchday.size() == 0) { + deliveryDates.add(matchesConfigPlaceholder.get("placeholderDatetime").toString()); + } + else { + LocalDateTime firstMatchDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(matchesOfMatchday); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + deliveryDates.add(firstMatchDate.format(formatter)); + + ArrayList remainingMatches = TLWMatchesManagerBase.getMatchesStartingFromSecondDay(matchesOfMatchday); + if(remainingMatches.size() > 0) { + firstMatchDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(remainingMatches); + deliveryDates.add(firstMatchDate.format(formatter)); + } + } + + return deliveryDates; + } + + private ArrayList getAdditionalDeliveryDates(JSONObject matchdayConfigJson, int matchdayNumber) { + ArrayList deliveryDates = new ArrayList<>(); + JSONArray matchesConfig = (JSONArray) matchdayConfigJson.get("matchesConfig"); + + APIFootballConnector conn = APIFootballConnector.getAPIFootballConnectorInstance(this.season); + for(Object config: matchesConfig) { + JSONObject jsonConfig = (JSONObject) config; + if(jsonConfig.containsKey("additionalDeliveryDate") && + ((Boolean) jsonConfig.get("additionalDeliveryDate"))) { + ArrayList matches = TLWMatchesCreatorFootball.getAPIFootballMatchesForConfig(conn, jsonConfig); + if(matches.size() > 0) { + LocalDateTime firstDate = TLWMatchesManagerBase.getFirstMatchtimeAPIFootball(matches); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + deliveryDates.add(firstDate.format(formatter)); + } + } + } + return deliveryDates; + } + public String getMatchdaysSQL() { String matchdaySql = ""; for (TLWMatchday matchday : getMatchdays()) { @@ -99,25 +158,14 @@ public class TLWMatchdaysCreator { return matchdaySql; } - private ArrayList getMatchesForMatchday(ArrayList matches, int matchday) { - ArrayList matchesOfMatchday = new ArrayList<>(); - for(TLWMatch match : matches) { - if(match.getMatchday() == matchday) { - matchesOfMatchday.add(match); + public static ArrayList removeDuplicates(ArrayList list) + { + ArrayList newList = new ArrayList(); + for (T element : list) { + if (!newList.contains(element)) { + newList.add(element); } } - return matchesOfMatchday; - } - - private String getMatchdayNameFromConfig(int matchday) { - String matchdayName = ""; - JSONArray matchdaysConfig = (JSONArray) this.configObject.get("matchdayConfig"); - for (Object matchdayConfig : matchdaysConfig) { - if(((JSONObject) matchdayConfig).get("TLWMatchday").toString().equals(String.valueOf(matchday)) - && ((JSONObject) matchdayConfig).containsKey("matchdayName")) { - matchdayName = ((JSONObject) matchdayConfig).get("matchdayName").toString(); - } - } - return matchdayName; + return newList; } } diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java index ab36bad..f5e55ea 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java @@ -93,19 +93,25 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase { ArrayList apiFootballMatches = new ArrayList<>(); for (Object singleConfigObject : config) { JSONObject singleConfig = (JSONObject) singleConfigObject; - String type = (String) singleConfig.get("type"); - switch (type) { - case "AllMatchesOfMatchday": - int matchesLeague = ((Long) singleConfig.get("matchesLeague")).intValue(); - int leagueMatchday = ((Long) singleConfig.get("leagueMatchday")).intValue(); - apiFootballMatches.addAll(conn.getMatchDataByLeagueAndMatchday(matchesLeague, leagueMatchday)); - break; - case "SingleMatch": - int matchLeague = ((Long) singleConfig.get("matchLeague")).intValue(); - int matchId = ((Long) singleConfig.get("matchId")).intValue(); - apiFootballMatches.add(conn.getMatchDataByLeagueAndMatchID(matchLeague, matchId)); - break; - } + apiFootballMatches.addAll(getAPIFootballMatchesForConfig(this.conn, singleConfig)); + } + return apiFootballMatches; + } + + public static ArrayList getAPIFootballMatchesForConfig(APIFootballConnector conn, JSONObject singleConfig) { + ArrayList apiFootballMatches = new ArrayList<>(); + String type = (String) singleConfig.get("type"); + switch (type) { + case "AllMatchesOfMatchday": + int matchesLeague = ((Long) singleConfig.get("matchesLeague")).intValue(); + int leagueMatchday = ((Long) singleConfig.get("leagueMatchday")).intValue(); + apiFootballMatches.addAll(conn.getMatchDataByLeagueAndMatchday(matchesLeague, leagueMatchday)); + break; + case "SingleMatch": + int matchLeague = ((Long) singleConfig.get("matchLeague")).intValue(); + int matchId = ((Long) singleConfig.get("matchId")).intValue(); + apiFootballMatches.add(conn.getMatchDataByLeagueAndMatchID(matchLeague, matchId)); + break; } return apiFootballMatches; } diff --git a/src/test/java/de/jeyp91/tippliga/TLWMatchdaysCreatorTest.java b/src/test/java/de/jeyp91/tippliga/TLWMatchdaysCreatorTest.java index 56966a6..de971c1 100644 --- a/src/test/java/de/jeyp91/tippliga/TLWMatchdaysCreatorTest.java +++ b/src/test/java/de/jeyp91/tippliga/TLWMatchdaysCreatorTest.java @@ -10,7 +10,7 @@ import static org.junit.Assert.assertEquals; public class TLWMatchdaysCreatorTest { @Test - public void getMatchdaysTippligaTest() throws ParseException { + public void getMatchdaysTippligaTest() { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 1, "Tippliga"); ArrayList matchdays = matchdaysCreator.getMatchdays(); @@ -19,7 +19,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysWTLPokalTest() throws ParseException { + public void getMatchdaysWTLPokalTest() { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 1, "WTL-Pokal"); ArrayList matchdays = matchdaysCreator.getMatchdays(); @@ -27,7 +27,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysTippliga1SqlTest() throws ParseException { + public void getMatchdaysTippliga1SqlTest() { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 1, "Tippliga"); String sql = matchdaysCreator.getMatchdaysSQL(); @@ -35,7 +35,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysTippliga2SqlTest() throws ParseException { + public void getMatchdaysTippliga2SqlTest() { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 2, "Tippliga"); String sql = matchdaysCreator.getMatchdaysSQL(); @@ -43,7 +43,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysTippliga51SqlTest() throws ParseException { + public void getMatchdaysTippliga51SqlTest() { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 51, "Tippliga"); String sql = matchdaysCreator.getMatchdaysSQL(); @@ -51,7 +51,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysTippliga52SqlTest() throws ParseException { + public void getMatchdaysTippliga52SqlTest() { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 52, "Tippliga"); String sql = matchdaysCreator.getMatchdaysSQL(); @@ -59,7 +59,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysWTLPokal48SqlTest() throws ParseException { + public void getMatchdaysWTLPokal48SqlTest() { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 48, "WTL-Pokal"); String sql = matchdaysCreator.getMatchdaysSQL(); @@ -67,7 +67,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysWTLPokal98SqlTest() throws ParseException { + public void getMatchdaysWTLPokal98SqlTest() { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 98, "WTL-Pokal"); String sql = matchdaysCreator.getMatchdaysSQL();