Clean up shit

This commit is contained in:
2020-10-04 12:23:43 +02:00
parent e57f79c18b
commit 557cd59c8f
14 changed files with 300 additions and 68 deletions

View File

@@ -37,7 +37,7 @@ public class TeamIDMatcher {
}
}
public static Integer getOpenLigaDbIdFromTippligaId(int id) {
public static Integer getApiFootballIdFromTippligaId(int id) {
if(ids == null) {
initBiMap();
}
@@ -50,7 +50,7 @@ public class TeamIDMatcher {
}
}
public static Integer getTippligaIdFromOpenLigaDbId(Integer id) {
public static Integer getTippligaIdFromApiFootballId(Integer id) {
if(id == null) return null;
if(ids == null) {
initBiMap();

View File

@@ -11,7 +11,7 @@ import de.jeyp91.openligadb.OpenLigaDBMatch;
/**
*
*/
public class TLWMatch extends BaseMatch {
public class TLWMatch extends BaseMatch{
public final Integer STATUS_NOTSTARTED = 0;
public final Integer STATUS_STARTED = 1;
@@ -94,8 +94,8 @@ public class TLWMatch extends BaseMatch {
this.league = league;
this.matchday = matchday;
this.matchNo = matchNo;
this.teamIdHome = TeamIDMatcher.getTippligaIdFromOpenLigaDbId(oldbmatch.getTeamIdHome());
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromOpenLigaDbId(oldbmatch.getTeamIdGuest());
this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(oldbmatch.getTeamIdHome());
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(oldbmatch.getTeamIdGuest());
this.goalsHome = oldbmatch.getGoalsHome();
this.goalsGuest = oldbmatch.getGoalsGuest();
this.matchDatetime = oldbmatch.getMatchDateTime().replace("T", " ");
@@ -105,16 +105,16 @@ public class TLWMatch extends BaseMatch {
this.status = 0;
}
public TLWMatch(APIFootballMatch APIFootballMatch, int season, int league, int matchday, int matchNo, int status, int koMatch) {
public TLWMatch(APIFootballMatch apiFootballMatch, int season, int league, int matchday, int matchNo, int status, int koMatch) {
this.season = season;
this.league = league;
this.matchday = matchday;
this.matchNo = matchNo;
this.teamIdHome = TeamIDMatcher.getTippligaIdFromOpenLigaDbId(APIFootballMatch.getTeamIdHome());
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromOpenLigaDbId(APIFootballMatch.getTeamIdGuest());
this.goalsHome = APIFootballMatch.getGoalsHome();
this.goalsGuest = APIFootballMatch.getGoalsGuest();
this.matchDatetime = APIFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);
this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdHome());
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdGuest());
this.goalsHome = apiFootballMatch.getGoalsHome();
this.goalsGuest = apiFootballMatch.getGoalsGuest();
this.matchDatetime = apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);
this.groupId = "";
this.formulaHome = "";
this.formulaGuest = "";
@@ -143,6 +143,31 @@ public class TLWMatch extends BaseMatch {
this.status = 0;
}
public TLWMatch(TLWMatch referenceMatch) {
this.season = referenceMatch.season;
this.league = referenceMatch.league;
this.matchday = referenceMatch.matchday;
this.teamIdHome = referenceMatch.teamIdHome;
this.teamIdGuest = referenceMatch.teamIdGuest;
this.goalsHome = referenceMatch.goalsHome;
this.goalsGuest = referenceMatch.goalsGuest;
this.matchDatetime = referenceMatch.matchDatetime;
this.matchNo = referenceMatch.matchNo;
this.groupId = referenceMatch.groupId;
this.formulaHome = referenceMatch.formulaHome;
this.formulaGuest = referenceMatch.formulaGuest;
this.status = referenceMatch.status;
this.koMatch = referenceMatch.koMatch;
this.goalsOvertimeHome = referenceMatch.goalsOvertimeHome;
this.goalsOvertimeGuest = referenceMatch.goalsOvertimeGuest;
this.showTable = referenceMatch.showTable;
this.trend = referenceMatch.trend;
this.odd1 = referenceMatch.odd1;
this.oddX = referenceMatch.oddX;
this.odd2 = referenceMatch.odd2;
this.rating = referenceMatch.rating;
}
public Integer getSeason() {
return this.season;
}
@@ -229,4 +254,20 @@ public class TLWMatch extends BaseMatch {
private String nullToSqlEmptyString(String string) {
return string != null ? "'"+string+"'" : "''";
}
public void setStatus(int status) {
this.status = status;
}
public void setMatchDateTime(String matchDateTime) {
this.matchDatetime = matchDateTime;
}
public void updateMatch(APIFootballMatch apiFootballMatch) {
this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdHome());
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdGuest());
this.goalsHome = apiFootballMatch.getGoalsHome();
this.goalsGuest = apiFootballMatch.getGoalsGuest();
this.matchDatetime = apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);
}
}

View File

@@ -1,4 +1,3 @@
<<<<<<< HEAD
package de.jeyp91.tippliga;
import com.google.common.io.Resources;
@@ -59,7 +58,4 @@ public class TLWMatchesManagerBase {
}
return matchDateTime;
}
=======
package de.jeyp91.tippliga;public class TLWMatchesManagerBase {
>>>>>>> origin/master
}

View File

@@ -1,11 +1,185 @@
package de.jeyp91.tippliga;
public class TLWFootballMatchesUpdater {
public TLWFootballMatchesUpdater() {
import de.jeyp91.TeamIDMatcher;
import de.jeyp91.apifootball.APIFootballMatch;
import de.jeyp91.apifootball.APIFootballMatchesProvider;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
import java.util.Date;
public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
ArrayList<TLWMatch> tlwMatchesOriginal;
ArrayList<TLWMatch> tlwMatchesUpdated;
public TLWMatchesUpdaterFootball(int season, int league, String configFileName) throws Exception {
this.season = season;
this.league = league;
tlwMatchesOriginal = new ArrayList<>();
tlwMatchesUpdated = new ArrayList<>();
super.initConfigParamsFromFile(configFileName);
this.initUpdates();
}
private void initUpdates() throws Exception {
APIFootballMatchesProvider apiFootballMatchesProvider = new APIFootballMatchesProvider(this.season);
TippligaSQLConnector tippligaSQLConnector = new TippligaSQLConnector();
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()) {
throw new Exception("Not matching config!");
}
addMissingMatches(tlwMatchesUpdatedMatchday, apiFootballMatches);
updateMatchDateTimes(tlwMatchesUpdatedMatchday, apiFootballMatches);
updateStatus(tlwMatchesUpdatedMatchday);
this.tlwMatchesOriginal.addAll(tlwMatchesOriginalMatchday);
this.tlwMatchesUpdated.addAll(tlwMatchesUpdatedMatchday);
}
}
public String getUpdateSQL() {
return "";
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";
if(matchOriginal.getTeamIdHome() != matchUpdated.getTeamIdHome()) {
updateString += updateStart +
"team_id_home = " + matchUpdated.getTeamIdHome() + " " +
condition;
}
if(matchOriginal.getTeamIdGuest() != matchUpdated.getTeamIdGuest()) {
updateString += updateStart +
"team_id_guest = " + matchUpdated.getTeamIdGuest() + " " +
condition;
}
if (!matchOriginal.getMatchDateTime().equals(matchUpdated.getMatchDateTime())) {
updateString += updateStart +
"match_datetime = '" + matchUpdated.getMatchDateTime() + "' " +
condition;
}
if (!matchOriginal.getStatus().equals(matchUpdated.getStatus())) {
updateString += updateStart +
"status = '" + matchUpdated.getStatus() + "' " +
condition;
}
return updateString;
}
private void addMissingMatches(ArrayList<TLWMatch> tlwMatches,
ArrayList<APIFootballMatch> apiFootballMatches) throws Exception {
ArrayList<APIFootballMatch> missingMatches = new ArrayList<>();
for(APIFootballMatch apiFootballMatch : apiFootballMatches) {
if(getMatchingMatch(apiFootballMatch, tlwMatches) == null) {
missingMatches.add(apiFootballMatch);
}
}
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) {
throw new Exception("Could not add missing match");
}
}
}
private void updateMatchDateTimes(
ArrayList<TLWMatch> tlwMatches,
ArrayList<APIFootballMatch> apiFootballMatches) throws Exception
{
for(APIFootballMatch apiFootballMatch : apiFootballMatches) {
TLWMatch tlwMatch = getMatchingMatch(apiFootballMatch, tlwMatches);
tlwMatch.setMatchDateTime(apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19));
}
}
private void updateStatus(ArrayList<TLWMatch> matches) {
Date earliestDate = getEarliestMatchDateTime(matches);
Date now = new Date(System.currentTimeMillis());
if(earliestDate.after(now)) {
for(TLWMatch match : matches) {
Date date = this.getDateObject(match.getMatchDateTime());
if (this.getDaysDifference(earliestDate, date) == 0) {
match.setStatus(0);
} else {
match.setStatus(-1);
}
}
}
}
private Date getEarliestMatchDateTime(ArrayList<TLWMatch> matches) {
Date earliestDate = this.getDateObject(matches.get(0).getMatchDateTime());
for(TLWMatch match : matches) {
Date date = this.getDateObject(match.getMatchDateTime());
if(date.before(earliestDate)) {
earliestDate = date;
}
}
return earliestDate;
}
private TLWMatch getMatchingMatch(APIFootballMatch apiFootballMatch, ArrayList<TLWMatch> tlwMatches) throws Exception {
int foundMatches = 0;
TLWMatch matchingMatch = null;
for(TLWMatch match : tlwMatches) {
int apiTeamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdHome());
int apiTeamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdGuest());
if(
apiTeamIdHome == match.getTeamIdHome()
&& apiTeamIdGuest == match.getTeamIdGuest()
) {
foundMatches++;
matchingMatch = match;
}
}
if(foundMatches > 1) {
throw new Exception("Found more than one matching matches");
}
return matchingMatch;
}
}

View File

@@ -42,6 +42,21 @@ public class TLWTeam {
}
}
public TLWTeam(
int season, int league, int teamId,
String teamName, String teamNameShort, String teamSymbol, String groupId,
int matchday
) {
this.season = season;
this.league = league;
this.teamId = teamId;
this.teamName = teamName;
this.teamNameShort = teamNameShort;
this.teamSymbol = teamSymbol;
this.groupId = groupId;
this.matchday = matchday;
}
public int getSeason() {
return this.season;
}

View File

@@ -33,7 +33,7 @@ public class TLWTeamsCreator {
Set<Integer> teamIds = getTeamIds();
String sql = "";
for (Integer id : teamIds) {
ArrayList<TLWTeam> teams = connector.getTeamsById(String.valueOf(id));
ArrayList<TLWTeam> teams = connector.getTeams(String.valueOf(id));
String teamName = teams.get(0).getTeamName();
String teamNameShort = teams.get(0).getTeamNameShort();
String teamSymbol = teams.get(0).getTeamSymbol();

View File

@@ -1,4 +1,3 @@
<<<<<<< HEAD
package de.jeyp91.tippliga;
import java.util.ArrayList;
@@ -87,7 +86,4 @@ public class TLWTeamsUpdater {
return updateSql;
}
=======
package de.jeyp91.tippliga;public class TLWTeamsUpdater {
>>>>>>> origin/master
}

View File

@@ -33,24 +33,34 @@ public class TippligaSQLConnector {
}
}
public ArrayList<TLWTeam> getTeamsBySeasonAndLeague(String season, String league) {
public ArrayList<TLWTeam> getTeams(String season, String league) {
String queryString = "SELECT * FROM `phpbb_footb_teams` WHERE `season` = " + season + " AND `league` = " + league + ";";
ArrayList<TLWTeam> teams = new ArrayList<>();
for (ResultSet rset : executeQuery(queryString)) {
ResultSet rset = executeQuery(queryString);
try {
while (rset.next()) {
teams.add(new TLWTeam(rset));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return teams;
}
public ArrayList<TLWTeam> getTeamsById(String id) {
String queryString = "SELECT * FROM `phpbb_footb_teams` WHERE `team_id` = " + id + " ORDER BY `season` DESC;";
public ArrayList<TLWTeam> getTeams(String id) {
String queryString = "SELECT * FROM `phpbb_footb_teams` WHERE `team_id` = " + id + " ORDER BY `season` DESC, `league` ASC;";
ArrayList<TLWTeam> teams = new ArrayList<>();
for (ResultSet rset : executeQuery(queryString)) {
ResultSet rset = executeQuery(queryString);
try {
while (rset.next()) {
teams.add(new TLWTeam(rset));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return teams;
}
@@ -58,9 +68,14 @@ public class TippligaSQLConnector {
private ArrayList<TLWMatch> getMatches(String queryString) {
ArrayList<TLWMatch> matches = new ArrayList<>();
for (ResultSet rset : executeQuery(queryString)) {
ResultSet rset = executeQuery(queryString);
try {
while (rset.next()) {
matches.add(new TLWMatch(rset));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return matches;
}
@@ -70,7 +85,7 @@ public class TippligaSQLConnector {
}
public ArrayList<TLWMatch> getMatches(String season, String league, String matchday) {
String queryString = "SELECT * FROM `phpbb_footb_matches` WHERE `season` = " + season + " AND `league` = " + league + "AND `matchday` = " + matchday + ";";
String queryString = "SELECT * FROM `phpbb_footb_matches` WHERE `season` = " + season + " AND `league` = " + league + " AND `matchday` = " + matchday + ";";
return getMatches(queryString);
}
@@ -83,19 +98,15 @@ public class TippligaSQLConnector {
executeQuery(queryString);
}
private ArrayList<ResultSet> executeQuery (String queryString){
private ResultSet executeQuery (String queryString){
Statement stmt;
ResultSet rset;
ArrayList<ResultSet> results = new ArrayList<>();
ResultSet rset = null;
try {
stmt = con.createStatement();
rset = stmt.executeQuery(queryString);
while (rset.next()) {
results.add(rset);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return results;
return rset;
}
}

View File

@@ -408,5 +408,15 @@
"teamname": "1. FC Düren",
"tippligaID": 182,
"apiFootballID": 12754
},
{
"teamname": "Olymp. Lyon",
"tippligaID": 505,
"apiFootballID": 80
},
{
"teamname": "AS Monaco",
"tippligaID": 160,
"apiFootballID": 91
}
]

View File

@@ -7,25 +7,25 @@ public class TeamIDMatcherTest {
@Test
public void getOpenLigaDbIdFromTippligaIdTest() {
int openligaDBID;
int apiFootballId;
openligaDBID = TeamIDMatcher.getOpenLigaDbIdFromTippligaId(1);
assertEquals(40, openligaDBID);
openligaDBID = TeamIDMatcher.getOpenLigaDbIdFromTippligaId(16);
assertEquals(54, openligaDBID);
openligaDBID = TeamIDMatcher.getOpenLigaDbIdFromTippligaId(12);
assertEquals(7, openligaDBID);
apiFootballId = TeamIDMatcher.getApiFootballIdFromTippligaId(505);
assertEquals(80, apiFootballId);
apiFootballId = TeamIDMatcher.getApiFootballIdFromTippligaId(160);
assertEquals(91, apiFootballId);
apiFootballId = TeamIDMatcher.getApiFootballIdFromTippligaId(182);
assertEquals(12754, apiFootballId);
}
@Test
public void getTippligaIdFromOpenLigaDbIdTest() {
int openligaDBID;
int tlwId;
openligaDBID = TeamIDMatcher.getTippligaIdFromOpenLigaDbId(40);
assertEquals(1, openligaDBID);
openligaDBID = TeamIDMatcher.getTippligaIdFromOpenLigaDbId(54);
assertEquals(16, openligaDBID);
openligaDBID = TeamIDMatcher.getTippligaIdFromOpenLigaDbId(7);
assertEquals(12, openligaDBID);
tlwId = TeamIDMatcher.getTippligaIdFromApiFootballId(80);
assertEquals(505, tlwId);
tlwId = TeamIDMatcher.getTippligaIdFromApiFootballId(91);
assertEquals(160, tlwId);
tlwId = TeamIDMatcher.getTippligaIdFromApiFootballId(12754);
assertEquals(182, tlwId);
}
}

View File

@@ -1,4 +1,3 @@
<<<<<<< HEAD
package de.jeyp91.tippliga;
import org.junit.Test;
@@ -12,7 +11,4 @@ public class TLWMatchesUpdaterFootballTest {
System.out.println(sql);
}
=======
package de.jeyp91.tippliga;public class TLWMatchesUpdaterFootball {
>>>>>>> origin/master
}

View File

@@ -1,4 +1,3 @@
<<<<<<< HEAD
package de.jeyp91.tippliga;
import org.junit.Test;
@@ -14,8 +13,4 @@ public class TLWTeamsUpdaterTest {
System.out.println(sql);
}
=======
package de.jeyp91.tippliga;public class TLWTeamsUpdaterTest {
>>>>>>> origin/master
}

View File

@@ -18,7 +18,7 @@ public class TippligaSQLConnectorTest {
@Test
public void getTeamsBySeasonAndLeagueTest() {
ArrayList<TLWTeam> teams = tl.getTeamsBySeasonAndLeague("2020", "1");
ArrayList<TLWTeam> teams = tl.getTeams("2020", "1");
assertEquals(84, teams.size());
@@ -34,11 +34,9 @@ public class TippligaSQLConnectorTest {
@Test
public void getTeamsByIdTest() {
ArrayList<TLWTeam> teams = tl.getTeamsById("1");
ArrayList<TLWTeam> teams = tl.getTeams("1");
assertEquals(39, teams.size());
assertEquals(2011, teams.get(0).getSeason());
assertEquals(2021, teams.get(0).getSeason());
assertEquals(1, teams.get(0).getLeague());
assertEquals(1, teams.get(0).getTeamId());
assertEquals("Bayern München", teams.get(0).getTeamName());

Binary file not shown.