From 98b9321c125fdb6156364af571d089d53bf321d1 Mon Sep 17 00:00:00 2001 From: Julian Arndt Date: Tue, 6 Jun 2023 15:45:25 +0200 Subject: [PATCH] Add auto update of results --- .../tippliga/TLWMatchesResultsUpdater.java | 254 ++++++++++++++++++ .../TLWMatchesResultsUpdaterTest.java | 33 +++ 2 files changed, 287 insertions(+) create mode 100644 src/main/java/de/jeyp91/tippliga/TLWMatchesResultsUpdater.java create mode 100644 src/test/java/de/jeyp91/tippliga/TLWMatchesResultsUpdaterTest.java diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesResultsUpdater.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesResultsUpdater.java new file mode 100644 index 0000000..0773442 --- /dev/null +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesResultsUpdater.java @@ -0,0 +1,254 @@ +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) { + + } + } + } + + private 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; + } + + public ArrayList getTLWMatchesUpdated() { + return this.tlwMatchesUpdated; + } + + public String getBeautifulInfo() { + return this.beautifulInfo; + } +} diff --git a/src/test/java/de/jeyp91/tippliga/TLWMatchesResultsUpdaterTest.java b/src/test/java/de/jeyp91/tippliga/TLWMatchesResultsUpdaterTest.java new file mode 100644 index 0000000..45af32f --- /dev/null +++ b/src/test/java/de/jeyp91/tippliga/TLWMatchesResultsUpdaterTest.java @@ -0,0 +1,33 @@ +package de.jeyp91.tippliga; + +import org.junit.Test; + +public class TLWMatchesUpdaterFootballTest { + + @Test + public void getUpdateSqlTest1() { + TLWMatchesUpdaterFootball updater = new TLWMatchesUpdaterFootball(2022, 1, "Tippliga"); + String sql = updater.getUpdateSQL(); + +// System.out.println(sql); +// System.out.println(updater.getBeautifulInfo()); + } + + @Test + public void getUpdateSqlTest2() { + TLWMatchesUpdaterFootball updater = new TLWMatchesUpdaterFootball(2021, 2, "Tippliga"); + String sql = updater.getUpdateSQL(); + +// System.out.println(sql); +// System.out.println(updater.getBeautifulInfo()); + } + + @Test + public void getUpdateSqlTest48() { + TLWMatchesUpdaterFootball updater = new TLWMatchesUpdaterFootball(2021, 48, "WTL-Pokal"); + String sql = updater.getUpdateSQL(); + +// System.out.println(sql); +// System.out.println(updater.getBeautifulInfo()); + } +}