Files
tlw-database-tool/src/main/java/de/jeyp91/tippliga/TLWMatchesManagerBase.java
T
2024-07-28 22:16:22 +02:00

131 lines
5.1 KiB
Java

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 Integer matchesPerMatchday = null;
protected int ko;
protected int betKOType;
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();
try { this.matchesPerMatchday = ((Long) config.get("matchesPerMatchday")).intValue(); } catch (Exception ignored) {}
this.matchdayConfig = (JSONArray) config.get("matchdayConfig");
this.ko = ((Long) config.get("ko")).intValue();
Long parsedBetKOType = (Long) config.get("betKOType");
this.betKOType = parsedBetKOType == null ? 1 : parsedBetKOType.intValue();
}
public static ArrayList<TLWMatch> getMatchesStartingFromSecondDay(ArrayList<TLWMatch> matches) {
LocalDateTime firstMatchtime = getFirstMatchtimeTLW(matches);
ArrayList<TLWMatch> filteredMatches = new ArrayList<>();
for(TLWMatch match : matches) {
LocalDateTime matchDateTime = getDate(match);
if(getDaysDifference(matchDateTime, firstMatchtime) != 0) {
filteredMatches.add(match);
}
}
return filteredMatches;
}
public static ArrayList<TLWMatch> getMatchesStartingFromSecondWeek(ArrayList<TLWMatch> matches) {
LocalDateTime firstMatchtime = getFirstMatchtimeTLW(matches);
ArrayList<TLWMatch> 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<TLWMatch> 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<APIFootballMatch> 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<TLWMatch> tlwMatches) {
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)
) {
matchingMatch = match;
}
}
return matchingMatch;
}
}