Add auto update of results

This commit is contained in:
2023-06-06 15:48:06 +02:00
parent 7b721a5592
commit 4dcd994ac9
13 changed files with 275 additions and 74 deletions
@@ -14,10 +14,11 @@ import org.apache.logging.log4j.Logger;
*/
public class TLWMatch extends BaseMatch{
public final Integer STATUS_NOTSTARTED = 0;
public final Integer STATUS_STARTED = 1;
public final Integer STATUS_PROVISIONAL_RESULT_AVAILABLE = 2;
public final Integer STATUS_FINISHED = 3;
public static final Integer STATUS_NOTSTARTED = 0;
public static final Integer STATUS_STARTED = 1;
public static final Integer STATUS_PROVISIONAL_RESULT_AVAILABLE = 2;
public static final Integer STATUS_FINISHED = 3;
public static final Integer STATUS_NOT_EVALUATED = 4;
private Integer season = null;
private Integer league = null;
@@ -1,6 +1,7 @@
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;
@@ -100,4 +101,28 @@ public class TLWMatchesManagerBase {
date2 = date2.toLocalDate().atTime(0, 0, 0);
return (int) date1.until(date2, ChronoUnit.DAYS);
}
protected TLWMatch getMatchingMatch(APIFootballMatch apiFootballMatch, ArrayList<TLWMatch> 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;
}
}
@@ -0,0 +1,140 @@
package de.jeyp91.tippliga;
import de.jeyp91.StatusHolder;
import de.jeyp91.apifootball.APIFootballMatch;
import de.jeyp91.apifootball.APIFootballMatchesProvider;
import de.jeyp91.teamidmatcher.TeamIDMatcher;
import de.jeyp91.tippligaforum.TippligaSQLConnector;
import de.jeyp91.tippligaforum.TippligaWebsiteConnector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class TLWMatchesResultsUpdater extends TLWMatchesManagerBase {
private static final Logger logger = LogManager.getLogger(TLWMatchesResultsUpdater.class);
ArrayList<ArrayList<TLWMatch>> tlwMatchesUpdated = new ArrayList<>();
private String beautifulInfo = "";
public TLWMatchesResultsUpdater(int season, int league, String configFileName) {
this.season = season;
this.league = league;
super.initConfigParamsFromFile(configFileName);
initUpdates();
applyUpdates();
}
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<APIFootballMatch> apiFootballMatches = apiFootballMatchesProvider.getAPIFootballMatchesFromConfig(matchesConfig);
ArrayList<TLWMatch> tlwMatchesOriginalMatchday = tippligaSQLConnector.getMatches(String.valueOf(this.season), String.valueOf(this.league), String.valueOf(tlwMatchday));
ArrayList<TLWMatch> tlwMatchesUpdatedMatchday = new ArrayList<>();
for (TLWMatch match : tlwMatchesOriginalMatchday) {
tlwMatchesUpdatedMatchday.add(new TLWMatch(match));
}
if (apiFootballMatches.size() > tlwMatchesOriginalMatchday.size()) {
logger.error("Not matching config!");
}
if(apiFootballMatches.size() > 0) {
updateResults(tlwMatchesUpdatedMatchday, apiFootballMatches);
}
updateBeautifulInfo(tlwMatchesOriginalMatchday, tlwMatchesUpdatedMatchday);
tlwMatchesUpdatedMatchday.removeIf(tlwMatch -> tlwMatch.getUpdateStatus() != TLWMatch.COMPARISON_DIFFERENT_RESULT);
if(tlwMatchesUpdatedMatchday.size() > 0) this.tlwMatchesUpdated.add(tlwMatchesUpdatedMatchday);
}
}
public void applyUpdates() {
TippligaWebsiteConnector con;
try {
con = new TippligaWebsiteConnector();
} catch (IOException e) {
throw new RuntimeException(e);
}
this.tlwMatchesUpdated.forEach(tlwMatches -> {
con.updateResultsAdmin(this.season, this.league, tlwMatches.get(0).getMatchday(), tlwMatches);
});
}
private void updateResults(
ArrayList<TLWMatch> tlwMatchesUpdated,
ArrayList<APIFootballMatch> apiFootballMatches)
{
ArrayList<TLWMatch> tlwMatchesCopy = new ArrayList<>(tlwMatchesUpdated);
for(APIFootballMatch apiFootballMatch : apiFootballMatches) {
try {
TLWMatch tlwMatch = getMatchingMatch(apiFootballMatch, tlwMatchesCopy);
tlwMatchesCopy.remove(tlwMatch);
if(Objects.equals(tlwMatch.getStatus(), TLWMatch.STATUS_STARTED)
|| Objects.equals(tlwMatch.getStatus(), TLWMatch.STATUS_PROVISIONAL_RESULT_AVAILABLE)
|| Objects.equals(tlwMatch.getStatus(), TLWMatch.STATUS_FINISHED)) {
if(!Objects.equals(tlwMatch.getGoalsHome(), apiFootballMatch.getGoalsHome())
|| !Objects.equals(tlwMatch.getGoalsGuest(), apiFootballMatch.getGoalsGuest())
) {
tlwMatch.setGoalsHome(apiFootballMatch.getGoalsHome());
tlwMatch.setGoalsGuest(apiFootballMatch.getGoalsGuest());
tlwMatch.setUpdateStatus(TLWMatch.COMPARISON_DIFFERENT_RESULT);
}
if(tlwMatch.getKoMatch() == 1 &&
(!Objects.equals(tlwMatch.getGoalsOvertimeHome(), apiFootballMatch.getGoalsOvertimeHome())
|| !Objects.equals(tlwMatch.getGoalsOvertimeGuest(), apiFootballMatch.getGoalsOvertimeGuest()))
) {
tlwMatch.setGoalsOvertimeHome(apiFootballMatch.getGoalsOvertimeHome());
tlwMatch.setGoalsOvertimeGuest(apiFootballMatch.getGoalsOvertimeGuest());
tlwMatch.setUpdateStatus(TLWMatch.COMPARISON_DIFFERENT_RESULT);
}
}
} catch (NullPointerException ignored) {
}
}
}
private void updateBeautifulInfo(ArrayList<TLWMatch> matchesOriginal, ArrayList<TLWMatch> matchesUpdated) {
for (int i = 0; i < matchesOriginal.size(); i++) {
updateBeautifulInfo(matchesOriginal.get(i), matchesUpdated.get(i));
}
}
private void updateBeautifulInfo(TLWMatch matchOriginal, TLWMatch matchUpdated) {
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(!Objects.equals(matchOriginal.getGoalsHome(), matchUpdated.getGoalsHome()) || !Objects.equals(matchOriginal.getGoalsGuest(), matchUpdated.getGoalsGuest())) {
this.beautifulInfo += beautifulInfoStart +
"Ergebnis zu '" + matchUpdated.getGoalsHome() + ":" + matchUpdated.getGoalsGuest() + "'.\n";
}
if(matchOriginal.getKoMatch() == 1 && (!Objects.equals(matchOriginal.getGoalsOvertimeHome(), matchUpdated.getGoalsOvertimeHome()) || !Objects.equals(matchOriginal.getGoalsOvertimeGuest(), matchUpdated.getGoalsOvertimeGuest()))) {
this.beautifulInfo += beautifulInfoStart +
"Ergebnis Nachspielzeit zu '" + matchUpdated.getGoalsOvertimeHome() + ":" + matchUpdated.getGoalsOvertimeGuest() + "'.\n";
}
}
public String getBeautifulInfo() {
return this.beautifulInfo;
}
}
@@ -220,30 +220,6 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
}
}
private TLWMatch getMatchingMatch(APIFootballMatch apiFootballMatch, ArrayList<TLWMatch> 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<TLWMatch> getTLWMatchesUpdated() {
return this.tlwMatchesUpdated;
}