package de.jeyp91.tippliga; import de.jeyp91.apifootball.APIFootballMatch; import de.jeyp91.teamidmatcher.TeamIDMatcher; import de.jeyp91.tippligaforum.TippligaConfigProvider; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.ArrayList; public class TLWMatchesManagerBase { protected int season; protected int league; protected int numberOfMatchdays; protected int matchesPerMatchday; protected int ko; protected JSONArray matchdayConfig; protected void initConfigParamsFromFile(String configFileName) { TippligaConfigProvider prov = new TippligaConfigProvider(this.season); JSONObject config = prov.getTippligaConfig(configFileName); this.numberOfMatchdays = ((Long) config.get("numberOfMatchdays")).intValue(); this.matchdayConfig = (JSONArray) config.get("matchdayConfig"); this.ko = ((Long) config.get("ko")).intValue(); } public static ArrayList getMatchesStartingFromSecondDay(ArrayList matches) { LocalDateTime firstMatchtime = getFirstMatchtimeTLW(matches); ArrayList filteredMatches = new ArrayList<>(); for(TLWMatch match : matches) { LocalDateTime matchDateTime = getDate(match); if(getDaysDifference(matchDateTime, firstMatchtime) != 0) { filteredMatches.add(match); } } return filteredMatches; } public static ArrayList getMatchesStartingFromSecondWeek(ArrayList matches) { LocalDateTime firstMatchtime = getFirstMatchtimeTLW(matches); ArrayList filteredMatches = new ArrayList<>(); for(TLWMatch match : matches) { LocalDateTime matchDateTime = getDate(match); if(getDaysDifference(matchDateTime, firstMatchtime) > 6) { filteredMatches.add(match); } } return filteredMatches; } public static LocalDateTime getFirstMatchtimeTLW(ArrayList matches) { LocalDateTime matchtime = null; for(TLWMatch match : matches) { LocalDateTime matchtimeTemp = getDate(match); if(matchtime == null) { matchtime = matchtimeTemp; } else { if(matchtimeTemp.isBefore(matchtime)) { matchtime = matchtimeTemp; } } } return matchtime; } public static LocalDateTime getFirstMatchtimeAPIFootball(ArrayList matches) { LocalDateTime matchtime = null; for(APIFootballMatch match : matches) { LocalDateTime matchtimeTemp = getDate(match); if(matchtime == null) { matchtime = matchtimeTemp; } else { if(matchtimeTemp.isBefore(matchtime)) { matchtime = matchtimeTemp; } } } return matchtime; } public static LocalDateTime getDate(TLWMatch match) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dateTime = LocalDateTime.parse(match.getMatchDateTime(), formatter); return dateTime; } public static LocalDateTime getDate(APIFootballMatch match) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); LocalDateTime dateTime = LocalDateTime.parse(match.getMatchDateTime().substring(0, 19), formatter); return dateTime; } public static int getDaysDifference(LocalDateTime date1, LocalDateTime date2) { date1 = date1.toLocalDate().atTime(0, 0, 0); date2 = date2.toLocalDate().atTime(0, 0, 0); return (int) date1.until(date2, ChronoUnit.DAYS); } protected TLWMatch getMatchingMatch(APIFootballMatch apiFootballMatch, ArrayList tlwMatches) throws NullPointerException{ int foundMatches = 0; TLWMatch matchingMatch = null; for(TLWMatch match : tlwMatches) { Integer apiTeamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.HOME); Integer apiTeamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.GUEST); int tlwTeamIdHome = match.getTeamIdHome(); int tlwTeamIdGuest = match.getTeamIdGuest(); if(apiTeamIdHome == null || apiTeamIdGuest == null) { throw new NullPointerException(); } if( apiTeamIdHome.equals(tlwTeamIdHome) && apiTeamIdGuest.equals(tlwTeamIdGuest) ) { foundMatches++; matchingMatch = match; } } return matchingMatch; } }