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.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import java.time.*; import java.util.ArrayList; public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase { private static final Logger logger = LogManager.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(); JSONArray matchesConfig = (JSONArray) ((JSONObject) singleMatchdayConfig).get("matchesConfig"); ArrayList apiFootballMatches = apiFootballMatchesProvider.getAPIFootballMatchesFromConfig(matchesConfig); 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); } 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"; } return updateString; } private void addMissingMatches(ArrayList tlwMatches, ArrayList apiFootballMatches) { ArrayList missingMatches = new ArrayList<>(); for(APIFootballMatch apiFootballMatch : apiFootballMatches) { try { if(getMatchingMatch(apiFootballMatch, tlwMatches) == null) { missingMatches.add(apiFootballMatch); } } catch (NullPointerException ignored) { } } for (APIFootballMatch missingMatch : missingMatches) { boolean done = false; for(TLWMatch tlwMatch : tlwMatches) { if(tlwMatch.getTeamIdHome() == 0 && tlwMatch.getTeamIdGuest() == 0) { 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 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); if (getDaysDifference(earliestDate, date) == 0) { match.setStatus(0); } else { match.setStatus(-1); } } } } 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()); } catch (NullPointerException ignored) { } } } public ArrayList getTLWMatchesUpdated() { return this.tlwMatchesUpdated; } public String getBeautifulInfo() { return this.beautifulInfo; } }