package de.jeyp91.tippliga; import de.jeyp91.StatusHolder; import de.jeyp91.teamidmatcher.TeamIDMatcher; import de.jeyp91.apifootball.APIFootballMatch; import de.jeyp91.apifootball.APIFootballMatchesProvider; import de.jeyp91.tippligaforum.TippligaSQLConnector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import java.time.*; import java.util.ArrayList; import java.util.Iterator; import java.util.Objects; public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase { private static final Logger logger = LoggerFactory.getLogger(TLWMatchesUpdaterFootball.class); ArrayList tlwMatchesOriginal; ArrayList tlwMatchesUpdated; String beautifulInfo = ""; public TLWMatchesUpdaterFootball(int season, int league, String configFileName) { this.season = season; this.league = league; tlwMatchesOriginal = new ArrayList<>(); tlwMatchesUpdated = new ArrayList<>(); super.initConfigParamsFromFile(configFileName); this.initUpdates(); } private void initUpdates() { APIFootballMatchesProvider apiFootballMatchesProvider = new APIFootballMatchesProvider(this.season); TippligaSQLConnector tippligaSQLConnector = TippligaSQLConnector.getInstance(); for (Object singleMatchdayConfig : this.matchdayConfig) { int tlwMatchday = ((Long) ((JSONObject) singleMatchdayConfig).get("TLWMatchday")).intValue(); ArrayList apiFootballMatches = apiFootballMatchesProvider.getAPIFootballMatchesFromConfig((JSONObject) singleMatchdayConfig); ArrayList tlwMatchesOriginalMatchday = tippligaSQLConnector.getMatches(String.valueOf(this.season), String.valueOf(this.league), String.valueOf(tlwMatchday)); ArrayList tlwMatchesUpdatedMatchday = new ArrayList<>(); for (TLWMatch match : tlwMatchesOriginalMatchday) { tlwMatchesUpdatedMatchday.add(new TLWMatch(match)); } if (apiFootballMatches.size() > tlwMatchesUpdatedMatchday.size()) { logger.error("Not matching config!"); } LocalDateTime now = LocalDateTime.now(); LocalDateTime firstMatchOriginal = TLWMatchesManagerBase.getFirstMatchtimeTLW(tlwMatchesOriginalMatchday); LocalDateTime firstMatchUpdated = TLWMatchesManagerBase.getFirstMatchtimeAPIFootball(apiFootballMatches); if(apiFootballMatches.size() > 0 && now.isBefore(firstMatchOriginal) && now.isBefore(firstMatchUpdated)) { addMissingMatches(tlwMatchesUpdatedMatchday, apiFootballMatches); updateMatchDateTimes(tlwMatchesUpdatedMatchday, apiFootballMatches); updateShowTable(tlwMatchesUpdatedMatchday, apiFootballMatches); updateStatus(tlwMatchesUpdatedMatchday); } if(now.isBefore(firstMatchOriginal)) { updateStatus(tlwMatchesUpdatedMatchday); addPlaceholderMatches(tlwMatchesUpdatedMatchday, (JSONObject) singleMatchdayConfig); } this.tlwMatchesOriginal.addAll(tlwMatchesOriginalMatchday); this.tlwMatchesUpdated.addAll(tlwMatchesUpdatedMatchday); } } public String getUpdateSQL() { return this.getUpdateString(tlwMatchesOriginal, tlwMatchesUpdated); } private String getUpdateString(ArrayList matchesOriginal, ArrayList matchesUpdated) { String updateString = ""; for (int i = 0; i < matchesOriginal.size(); i++) { updateString += getUpdateString(matchesOriginal.get(i), matchesUpdated.get(i)); } return updateString; } private String getUpdateString(TLWMatch matchOriginal, TLWMatch matchUpdated) { String updateString = ""; String updateStart = "UPDATE phpbb_footb_matches SET "; String condition = "WHERE season = " + matchOriginal.getSeason() + " " + "AND league = " + matchOriginal.getLeague() + " " + "AND matchday = " + matchOriginal.getMatchday() + " " + "AND match_no = " + matchOriginal.getMatchNo() + ";\n"; String beautifulInfoStart = "Aktualisiere Saison " + matchOriginal.getSeason() + ", " + "Liga " + matchOriginal.getLeague() + ", " + "Spieltag " + matchOriginal.getMatchday() + ", " + "Spielnummer " + matchOriginal.getMatchNo() + ", " + "Spiel '" + TeamIDMatcher.getTeamNameFromTippligaId(matchOriginal.getTeamIdHome()) + "' - '" + TeamIDMatcher.getTeamNameFromTippligaId(matchOriginal.getTeamIdGuest()) + "', "; if(!matchOriginal.getTeamIdHome().equals(matchUpdated.getTeamIdHome())) { updateString += updateStart + "team_id_home = " + matchUpdated.getTeamIdHome() + " " + condition; this.beautifulInfo += beautifulInfoStart + "Heimteam zu '" + matchUpdated.getTeamNameHome() + "'.\n"; } if(!matchOriginal.getTeamIdGuest().equals(matchUpdated.getTeamIdGuest())) { updateString += updateStart + "team_id_guest = " + matchUpdated.getTeamIdGuest() + " " + condition; this.beautifulInfo += beautifulInfoStart + "Gastteam zu '" + matchUpdated.getTeamNameGuest() + "'.\n"; } if (!matchOriginal.getMatchDateTime().equals(matchUpdated.getMatchDateTime())) { updateString += updateStart + "match_datetime = '" + matchUpdated.getMatchDateTime() + "' " + condition; this.beautifulInfo += beautifulInfoStart + "Spielbeginn zu '" + matchUpdated.getMatchDateTime() + "'.\n"; } if (!matchOriginal.getStatus().equals(matchUpdated.getStatus())) { updateString += updateStart + "status = '" + matchUpdated.getStatus() + "' " + condition; this.beautifulInfo += beautifulInfoStart + "Status zu '" + matchUpdated.getStatus() + "'.\n"; } if (!matchOriginal.getShowTable().equals(matchUpdated.getShowTable())) { updateString += updateStart + "show_table = '" + matchUpdated.getShowTable() + "' " + condition; this.beautifulInfo += beautifulInfoStart + "Spiel in Tabelle einberechnen zu '" + (matchUpdated.getShowTable() == 1 ? "falsch" : "wahr") + "'.\n"; } if (!matchOriginal.getKoMatch().equals(matchUpdated.getKoMatch())) { updateString += updateStart + "ko_match = '" + (matchUpdated.getKoMatch() ? "1" : "0") + "' " + condition; this.beautifulInfo += beautifulInfoStart + "KO zu '" + (matchUpdated.getKoMatch() ? "wahr" : "falsch") + "'.\n"; } return updateString; } private void addMissingMatches(ArrayList tlwMatches, ArrayList apiFootballMatches) { ArrayList missingMatches = new ArrayList<>(); for(APIFootballMatch apiFootballMatch : apiFootballMatches) { if(getMatchingMatch(apiFootballMatch, tlwMatches) == null) { missingMatches.add(apiFootballMatch); } } for (APIFootballMatch missingMatch : missingMatches) { boolean done = false; // update placeholder match based on matching match datetime for (TLWMatch tlwMatch : tlwMatches) { if ( tlwMatch.getTeamIdHome() == 0 && tlwMatch.getTeamIdGuest() == 0 && Objects.equals(tlwMatch.getMatchDateTime(), missingMatch.getMatchDateTime().replace("T", " ").substring(0, 19)) && !Objects.equals(tlwMatch.getFormulaHome(), "") && !Objects.equals(tlwMatch.getFormulaGuest(), "")) { tlwMatch.updateMatch(missingMatch); done = true; break; } } // update empty match if(!done) { for (TLWMatch tlwMatch : tlwMatches) { if (tlwMatch.getTeamIdHome() == 0 && tlwMatch.getTeamIdGuest() == 0 && (Objects.equals(tlwMatch.getFormulaHome(), "") || Objects.equals(tlwMatch.getFormulaHome(), "D")) && (Objects.equals(tlwMatch.getFormulaGuest(), "") || Objects.equals(tlwMatch.getFormulaGuest(), "D")) ) { tlwMatch.updateMatch(missingMatch); done = true; break; } } } if(!done) { logger.error("Could not add missing match: " + this.season + ", " + this.league + ", " + tlwMatches.get(0).getMatchday() + ", " + missingMatch.getTeamNameHome() + " - " + missingMatch.getTeamNameGuest()); } } } private void addPlaceholderMatches(ArrayList tlwMatches, JSONObject matchdayConfig) { Integer numberOfMatches = this.matchesPerMatchday; try { numberOfMatches = ((Long) matchdayConfig.get("numberOfMatches")).intValue();} catch (Exception ignored) {} ArrayList placeholderMatches = new ArrayList<>(); ArrayList tlwMatchesCopy = new ArrayList<>(tlwMatches); JSONArray matchesConfig = (JSONArray) matchdayConfig.get("matchesConfig"); for (Object singleMatchConfig : matchesConfig) { // check if type placeholderSingleMatch if(((JSONObject) singleMatchConfig).get("type").equals("PlaceholderSingleMatch")) { String formulaHome = (String) ((JSONObject) singleMatchConfig).get("formulaHome"); String formulaGuest = (String) ((JSONObject) singleMatchConfig).get("formulaGuest"); String matchDateTime = (String) ((JSONObject) singleMatchConfig).get("placeholderDatetime"); TLWMatch placeholderMatch = new TLWMatch(formulaHome, formulaGuest, matchDateTime); placeholderMatches.add(placeholderMatch); } } // filter placeholderMatches by checking if they are already in tlwMatches Iterator placeholderIterator = placeholderMatches.iterator(); while (placeholderIterator.hasNext()) { TLWMatch placeholderMatch = placeholderIterator.next(); Iterator tlwMatchIterator = tlwMatchesCopy.iterator(); while (tlwMatchIterator.hasNext()) { TLWMatch tlwMatch = tlwMatchIterator.next(); if (TLWMatch.isSameMatchPlaceholder(tlwMatch, placeholderMatch)) { tlwMatchIterator.remove(); placeholderIterator.remove(); break; } } } // check if total number of matches is not too high if(tlwMatches.size() + placeholderMatches.size() > (numberOfMatches)) { logger.error("Too many matches in config: " + this.season + ", " + this.league + ", " + tlwMatches.get(0).getMatchday()); } else { // add placeholderMatches to tlwMatches tlwMatches.addAll(placeholderMatches); } } private void updateMatchDateTimes( ArrayList tlwMatches, ArrayList apiFootballMatches) { ArrayList tlwMatchesCopy = new ArrayList<>(tlwMatches); for(APIFootballMatch apiFootballMatch : apiFootballMatches) { try { TLWMatch tlwMatch = getMatchingMatch(apiFootballMatch, tlwMatchesCopy); tlwMatchesCopy.remove(tlwMatch); if(tlwMatch == null) { StatusHolder.setError(); logger.error("Did not find match to update: " + this.season + ", " + this.league + ", " + tlwMatches.get(0).getMatchday() + ", " + apiFootballMatch.getTeamNameHome() + " - " + apiFootballMatch.getTeamNameGuest()); } tlwMatch.setMatchDateTime(apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19)); } catch(NullPointerException ignored) { } } } private void updateStatus(ArrayList matches) { LocalDateTime earliestDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(matches); LocalDateTime now = LocalDateTime.now(); if(earliestDate.isAfter(now)) { for(TLWMatch match : matches) { LocalDateTime date = TLWMatchesManagerBase.getDate(match); int daysDifference = TLWMatchesManagerBase.getDaysDifference(earliestDate, date); if (daysDifference == 0) { match.setStatus(0); } else if (daysDifference > 0 && daysDifference < 7) { match.setStatus(-1); } else { match.setStatus(-2); } } } } private void updateShowTable( ArrayList tlwMatches, ArrayList apiFootballMatches) { ArrayList tlwMatchesCopy = new ArrayList<>(tlwMatches); for(APIFootballMatch apiFootballMatch : apiFootballMatches) { try { TLWMatch tlwMatch = getMatchingMatch(apiFootballMatch, tlwMatchesCopy); tlwMatchesCopy.remove(tlwMatch); tlwMatch.setShowTable(apiFootballMatch.getShowTable() == 0); } catch (NullPointerException ignored) { } } } private void updateKo( ArrayList tlwMatches, ArrayList apiFootballMatches) { ArrayList tlwMatchesCopy = new ArrayList<>(tlwMatches); for(APIFootballMatch apiFootballMatch : apiFootballMatches) { try { TLWMatch tlwMatch = getMatchingMatch(apiFootballMatch, tlwMatchesCopy); tlwMatchesCopy.remove(tlwMatch); tlwMatch.setKoMatch(apiFootballMatch.getKoMatch()); } catch (NullPointerException ignored) { } } } public ArrayList getTLWMatchesUpdated() { return this.tlwMatchesUpdated; } public String getBeautifulInfo() { return this.beautifulInfo; } }