Add auto update of results
This commit is contained in:
254
src/main/java/de/jeyp91/tippliga/TLWMatchesResultsUpdater.java
Normal file
254
src/main/java/de/jeyp91/tippliga/TLWMatchesResultsUpdater.java
Normal file
@@ -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<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();
|
||||
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() > 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<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";
|
||||
}
|
||||
|
||||
|
||||
return updateString;
|
||||
}
|
||||
|
||||
private void addMissingMatches(ArrayList<TLWMatch> tlwMatches,
|
||||
ArrayList<APIFootballMatch> apiFootballMatches) {
|
||||
|
||||
ArrayList<APIFootballMatch> 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<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);
|
||||
if (getDaysDifference(earliestDate, date) == 0) {
|
||||
match.setStatus(0);
|
||||
} else {
|
||||
match.setStatus(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
tlwMatchesCopy.remove(tlwMatch);
|
||||
tlwMatch.setShowTable(apiFootballMatch.getShowTable());
|
||||
} catch (NullPointerException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getBeautifulInfo() {
|
||||
return this.beautifulInfo;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user