Files
tlw-database-tool/src/main/java/de/jeyp91/tippliga/TLWMatchesResultsUpdater.java
T
2025-01-26 23:01:35 +01:00

175 lines
9.0 KiB
Java

package de.jeyp91.tippliga;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
public class TLWMatchesResultsUpdater extends TLWMatchesManagerBase {
private static final Logger logger = LoggerFactory.getLogger(TLWMatchesResultsUpdater.class);
ArrayList<ArrayList<TLWMatch>> tlwMatchesUpdated = new ArrayList<>();
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((JSONObject) singleMatchdayConfig);
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(apiFootballMatch.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() &&
this.betKOType == 2 &&
(!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);
}
if(tlwMatch.getKoMatch() &&
(this.betKOType == 1 || this.betKOType == 3) &&
(apiFootballMatch.getGoalsPenaltyHome() != null) &&
(!Objects.equals(tlwMatch.getGoalsOvertimeHome(), apiFootballMatch.getGoalsPenaltyHome())
|| !Objects.equals(tlwMatch.getGoalsOvertimeGuest(), apiFootballMatch.getGoalsPenaltyGuest()))
) {
tlwMatch.setGoalsOvertimeHome(apiFootballMatch.getGoalsPenaltyHome());
tlwMatch.setGoalsOvertimeGuest(apiFootballMatch.getGoalsPenaltyGuest());
tlwMatch.setUpdateStatus(TLWMatch.COMPARISON.DIFFERENT_RESULT);
}
if(tlwMatch.getKoMatch() &&
(this.betKOType == 1 || this.betKOType == 3) &&
(apiFootballMatch.getGoalsPenaltyHome() == null) &&
(!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);
}
if(Objects.equals(tlwMatch.getStatus(), TLWMatch.STATUS_PROVISIONAL_RESULT_AVAILABLE)) {
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() && (!Objects.equals(matchOriginal.getGoalsOvertimeHome(), matchUpdated.getGoalsOvertimeHome())
|| !Objects.equals(matchOriginal.getGoalsOvertimeGuest(), matchUpdated.getGoalsOvertimeGuest()))) {
this.beautifulInfo += beautifulInfoStart +
"Ergebnis Nachspielzeit zu '" + matchUpdated.getGoalsOvertimeHome() + ":" + matchUpdated.getGoalsOvertimeGuest() + "'.\n";
}
if(matchOriginal.getStatus().equals(TLWMatch.STATUS_PROVISIONAL_RESULT_AVAILABLE)
&& Objects.equals(matchOriginal.getGoalsHome(), matchUpdated.getGoalsHome())
&& Objects.equals(matchOriginal.getGoalsGuest(), matchUpdated.getGoalsGuest())
&& (!matchOriginal.getKoMatch() || (
matchOriginal.getKoMatch()
&& Objects.equals(matchOriginal.getGoalsOvertimeHome(), matchUpdated.getGoalsOvertimeHome())
&& Objects.equals(matchOriginal.getGoalsOvertimeGuest(), matchUpdated.getGoalsOvertimeGuest())
))
) {
this.beautifulInfo += beautifulInfoStart +
"Ergebnis gespeichert.\n";
}
}
public String getBeautifulInfo() {
return this.beautifulInfo;
}
}