Add ability for additional delivery dates

This commit is contained in:
2021-03-20 15:07:45 +01:00
parent 14b45f638d
commit d54a7089c8
4 changed files with 114 additions and 61 deletions

View File

@@ -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;

View File

@@ -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<String> deliveryDates = getDeliveryDates(matchdayConfigJson, matchdayNumber);
JSONArray matchesConfig = (JSONArray) matchdayConfigJson.get("matchesConfig");
JSONObject matchesConfigPlaceholder = (JSONObject) matchesConfig.get(matchesConfig.size() - 1);
ArrayList<TLWMatch> 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<TLWMatch> 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<TLWMatch> getMatchesForMatchday(ArrayList<TLWMatch> matches, int matchday) {
ArrayList<TLWMatch> 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<String> getDeliveryDates(JSONObject matchdayConfigJson, int matchdayNumber) {
ArrayList<String> deliveryDates = getDefaultDeliveryDates(matchdayConfigJson, matchdayNumber);
deliveryDates.addAll(getAdditionalDeliveryDates(matchdayConfigJson, matchdayNumber));
deliveryDates = removeDuplicates(deliveryDates);
while(deliveryDates.size() < 3) {
deliveryDates.add("");
}
return deliveryDates;
}
private ArrayList<String> getDefaultDeliveryDates(JSONObject matchdayConfigJson, int matchdayNumber) {
ArrayList<String> deliveryDates = new ArrayList<>();
JSONArray matchesConfig = (JSONArray) matchdayConfigJson.get("matchesConfig");
JSONObject matchesConfigPlaceholder = (JSONObject) matchesConfig.get(matchesConfig.size() - 1);
ArrayList<TLWMatch> 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<TLWMatch> remainingMatches = TLWMatchesManagerBase.getMatchesStartingFromSecondDay(matchesOfMatchday);
if(remainingMatches.size() > 0) {
firstMatchDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(remainingMatches);
deliveryDates.add(firstMatchDate.format(formatter));
}
}
return deliveryDates;
}
private ArrayList<String> getAdditionalDeliveryDates(JSONObject matchdayConfigJson, int matchdayNumber) {
ArrayList<String> 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<APIFootballMatch> 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<TLWMatch> getMatchesForMatchday(ArrayList<TLWMatch> matches, int matchday) {
ArrayList<TLWMatch> matchesOfMatchday = new ArrayList<>();
for(TLWMatch match : matches) {
if(match.getMatchday() == matchday) {
matchesOfMatchday.add(match);
public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)
{
ArrayList<T> newList = new ArrayList<T>();
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;
}
}

View File

@@ -93,19 +93,25 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase {
ArrayList<APIFootballMatch> 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<APIFootballMatch> getAPIFootballMatchesForConfig(APIFootballConnector conn, JSONObject singleConfig) {
ArrayList<APIFootballMatch> 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;
}

View File

@@ -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<TLWMatchday> 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<TLWMatchday> 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();