349 lines
16 KiB
Java
349 lines
16 KiB
Java
package de.jeyp91.tippliga;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.Objects;
|
|
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.JSONObject;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import de.jeyp91.StatusHolder;
|
|
import de.jeyp91.apifootball.APIFootballMatch;
|
|
import de.jeyp91.apifootball.APIFootballMatchesProvider;
|
|
import de.jeyp91.teamidmatcher.TeamIDMatcher;
|
|
import de.jeyp91.tippligaforum.TippligaSQLConnector;
|
|
|
|
public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
|
|
private static final Logger logger = LoggerFactory.getLogger(TLWMatchesUpdaterFootball.class);
|
|
|
|
ArrayList<TLWMatch> tlwMatchesOriginal;
|
|
ArrayList<TLWMatch> 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<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() > 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<TLWMatch> matchesOriginal, ArrayList<TLWMatch> 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<TLWMatch> tlwMatches,
|
|
ArrayList<APIFootballMatch> apiFootballMatches) {
|
|
|
|
ArrayList<APIFootballMatch> missingMatches = new ArrayList<>();
|
|
if(!validateApiFootballTeamIds(apiFootballMatches)) {
|
|
return;
|
|
}
|
|
|
|
for(APIFootballMatch apiFootballMatch : apiFootballMatches) {
|
|
if(getMatchingMatch(apiFootballMatch, tlwMatches) == null) {
|
|
missingMatches.add(apiFootballMatch);
|
|
}
|
|
}
|
|
|
|
for (APIFootballMatch missingMatch : missingMatches) {
|
|
Integer missingTeamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(missingMatch, TeamIDMatcher.HOME);
|
|
Integer missingTeamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(missingMatch, TeamIDMatcher.GUEST);
|
|
boolean done = false;
|
|
// update placeholder match based on matching match datetime
|
|
for (TLWMatch tlwMatch : tlwMatches) {
|
|
Boolean homeMatches = tlwMatch.getTeamIdHome() == 0 || tlwMatch.getTeamIdHome().equals(missingTeamIdHome);
|
|
Boolean guestMatches =tlwMatch.getTeamIdGuest() == 0 || tlwMatch.getTeamIdGuest().equals(missingTeamIdGuest);
|
|
Boolean timeMatches = Objects.equals(tlwMatch.getMatchDateTime(), missingMatch.getMatchDateTime().replace("T", " ").substring(0, 19));
|
|
Boolean formulaHomeMatches = !Objects.equals(tlwMatch.getFormulaHome(), "");
|
|
Boolean formulaGuestMatches = !Objects.equals(tlwMatch.getFormulaGuest(), "");
|
|
if (
|
|
homeMatches
|
|
&& guestMatches
|
|
&& timeMatches
|
|
&& formulaHomeMatches
|
|
&& formulaGuestMatches
|
|
) {
|
|
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<TLWMatch> tlwMatches,
|
|
JSONObject matchdayConfig) {
|
|
Integer numberOfMatches = this.matchesPerMatchday;
|
|
try { numberOfMatches = ((Long) matchdayConfig.get("numberOfMatches")).intValue();} catch (Exception ignored) {}
|
|
ArrayList<TLWMatch> placeholderMatches = new ArrayList<>();
|
|
ArrayList<TLWMatch> 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<TLWMatch> placeholderIterator = placeholderMatches.iterator();
|
|
while (placeholderIterator.hasNext()) {
|
|
TLWMatch placeholderMatch = placeholderIterator.next();
|
|
Iterator<TLWMatch> 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<TLWMatch> tlwMatches,
|
|
ArrayList<APIFootballMatch> apiFootballMatches)
|
|
{
|
|
ArrayList<TLWMatch> 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<TLWMatch> 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<TLWMatch> tlwMatches,
|
|
ArrayList<APIFootballMatch> apiFootballMatches)
|
|
{
|
|
ArrayList<TLWMatch> tlwMatchesCopy = new ArrayList<>(tlwMatches);
|
|
for(APIFootballMatch apiFootballMatch : apiFootballMatches) {
|
|
try {
|
|
TLWMatch tlwMatch = getMatchingMatch(apiFootballMatch, tlwMatchesCopy);
|
|
if(tlwMatch.getTeamIdHome() == 4) {
|
|
int debug = 1;
|
|
}
|
|
tlwMatchesCopy.remove(tlwMatch);
|
|
tlwMatch.setShowTable(apiFootballMatch.getShowTable() == 0);
|
|
} catch (NullPointerException ignored) {
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private void updateKo(
|
|
ArrayList<TLWMatch> tlwMatches,
|
|
ArrayList<APIFootballMatch> apiFootballMatches)
|
|
{
|
|
ArrayList<TLWMatch> 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<TLWMatch> getTLWMatchesUpdated() {
|
|
return this.tlwMatchesUpdated;
|
|
}
|
|
|
|
public String getBeautifulInfo() {
|
|
return this.beautifulInfo;
|
|
}
|
|
|
|
private boolean validateApiFootballTeamIds(ArrayList<APIFootballMatch> apiFootballMatches) {
|
|
|
|
int missing = 0;
|
|
|
|
for(APIFootballMatch apiFootballMatch : apiFootballMatches) {
|
|
if(TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.HOME) == null) {
|
|
missing++;
|
|
}
|
|
if(TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch, TeamIDMatcher.GUEST) == null) {
|
|
missing++;
|
|
}
|
|
}
|
|
|
|
return missing == 0;
|
|
}
|
|
}
|