Add show table update, add matches list creator

This commit is contained in:
2020-10-31 20:26:47 +01:00
parent 57baee27bd
commit 3a5df3cae6
17 changed files with 195 additions and 32 deletions

View File

@@ -21,6 +21,7 @@ dependencies {
implementation 'mysql:mysql-connector-java:8.0.17'
implementation 'org.apache.httpcomponents:httpclient:4.5.9'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
implementation 'com.google.code.gson:gson:2.8.6'
testImplementation 'junit:junit:4.11'
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'

View File

@@ -55,7 +55,7 @@ public class App {
}
if(!StatusHolder.getError() && !sql.equals("")) {
TippligaSQLConnector con = new TippligaSQLConnector();
// con.executeUpdate(sql);
con.executeUpdate(sql);
logger.info(beautifulInfo);
}
if(StatusHolder.getError()) {

View File

@@ -17,8 +17,8 @@ public class APIFootballConnector {
private static APIFootballConnector conn = null;
private final int season;
private final String gistId;
private HashMap<String, JSONObject> rounds = new HashMap<>();
private HashMap<String, JSONObject> matches = new HashMap<>();
private final HashMap<String, JSONObject> rounds = new HashMap<>();
private final HashMap<String, JSONObject> matches = new HashMap<>();
private APIFootballConnector(int season) {
this.season = season;
@@ -94,7 +94,7 @@ public class APIFootballConnector {
JSONParser jsonParser = new JSONParser();
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getFileFromGist(this.gistId, filename);
String jsonConfig = prov.getFileFromGist(filename);
try {
object = (JSONObject) jsonParser.parse(jsonConfig);
@@ -111,7 +111,7 @@ public class APIFootballConnector {
JSONParser jsonParser = new JSONParser();
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getFileFromGist(this.gistId, filename);
String jsonConfig = prov.getFileFromGist(filename);
try {
object = (JSONArray) jsonParser.parse(jsonConfig);

View File

@@ -20,6 +20,7 @@ public class APIFootballMatch extends BaseMatch {
private String matchStatus;
private final String teamNameHome;
private final String teamNameGuest;
private Boolean showTable = null;
public APIFootballMatch(JSONObject json, int season) {
this.season = season;
@@ -67,4 +68,12 @@ public class APIFootballMatch extends BaseMatch {
public String getTeamNameGuest() {
return this.teamNameGuest;
}
public void setShowTable(boolean showTable) {
this.showTable = showTable;
}
public Boolean getShowTable() {
return this.showTable;
}
}

View File

@@ -17,16 +17,28 @@ public class APIFootballMatchesProvider {
for (Object singleConfigObject : config) {
JSONObject singleConfig = (JSONObject) singleConfigObject;
String type = (String) singleConfig.get("type");
boolean showTable = false;
switch (type) {
case "AllMatchesOfMatchday":
int matchesLeague = ((Long) singleConfig.get("matchesLeague")).intValue();
int leagueMatchday = ((Long) singleConfig.get("leagueMatchday")).intValue();
apiFootballMatches.addAll(conn.getMatchDataByLeagueAndMatchday(matchesLeague, leagueMatchday));
ArrayList<APIFootballMatch> matches = conn.getMatchDataByLeagueAndMatchday(matchesLeague, leagueMatchday);
try {
showTable = (boolean) singleConfig.get("showTable");
} catch (Exception e) {
e.printStackTrace();
}
boolean finalShowTable = showTable;
matches.forEach(apiFootballMatch -> apiFootballMatch.setShowTable(finalShowTable));
apiFootballMatches.addAll(matches);
break;
case "SingleMatch":
int matchLeague = ((Long) singleConfig.get("matchLeague")).intValue();
int matchId = ((Long) singleConfig.get("matchId")).intValue();
apiFootballMatches.add(conn.getMatchDataByLeagueAndMatchID(matchLeague, matchId));
APIFootballMatch match = conn.getMatchDataByLeagueAndMatchID(matchLeague, matchId);
showTable = (boolean) singleConfig.get("showTable");
match.setShowTable(showTable);
apiFootballMatches.add(match);
break;
}
}

View File

@@ -70,7 +70,7 @@ public class APIFootballUpdater {
JSONParser jsonParser = new JSONParser();
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getTippligaConfig(this.season, config);
String jsonConfig = prov.getTippligaConfig(config);
JSONObject configObject = null;
try {
configObject = (JSONObject) jsonParser.parse(jsonConfig);

View File

@@ -26,7 +26,7 @@ import java.util.Set;
public class GistProvider {
private static GistProvider prov = null;
private JSONArray gistList;
private final JSONArray gistList;
private String username;
private String password;
@@ -153,11 +153,10 @@ public class GistProvider {
}
public JSONArray getTeamIdMatcher() {
String id = getGistID("Team_ID_Matcher");
return stringToJSONArray(getFileFromGist(id, "Team_ID_Matcher.json"));
return stringToJSONArray(getFileFromGist("Team_ID_Matcher.json"));
}
public String getFileFromGist(String id, String filename) {
public String getFileFromGist(String filename) {
String fileurl = null;
for (Object o : this.gistList) {
JSONObject gist = (JSONObject) o;
@@ -237,13 +236,30 @@ public class GistProvider {
return data;
}
public String getTippligaConfig(int season, String filename) {
String id = getGistID("Tippliga_Config_" + season);
return getFileFromGist(id, filename);
private JSONObject stringToJSONObject(String rawData) {
JSONParser parser = new JSONParser();
JSONObject data = null;
try {
data = (JSONObject) parser.parse(rawData);
} catch (ParseException e) {
/* TODO */
e.printStackTrace();
}
return data;
}
public String getTippligaConfig(String filename) {
return getFileFromGist(filename);
}
public String getTippligaBaseConfig(String filename) {
String id = getGistID("Tippliga_Config_Base");
return getFileFromGist(id, filename);
return getFileFromGist(filename);
}
public JSONObject getAPIFootballLeagueConfig(int league) {
String filename = "matches_league_" + league + ".json";
String file = getFileFromGist(filename);
return stringToJSONObject(file);
}
}

View File

@@ -0,0 +1,58 @@
package de.jeyp91.gists;
import com.google.gson.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class MatchesListCreator {
private final JSONObject matches = new JSONObject();
public MatchesListCreator(int league) {
GistProvider provider = GistProvider.getInstance();
JSONObject leagueConfig = provider.getAPIFootballLeagueConfig(league);
JSONObject api = (JSONObject) leagueConfig.get("api");
JSONArray matchesAPIFootball = (JSONArray) api.get("fixtures");
JSONObject firstMatchAPIFootball = (JSONObject) matchesAPIFootball.get(0);
JSONObject leagueObject = (JSONObject) firstMatchAPIFootball.get("league");
String country = leagueObject.get("country").toString();
String leagueName = leagueObject.get("name").toString();
matches.put("country", country);
matches.put("leagueName", leagueName);
matches.put("leagueId", league);
populateMatches(matchesAPIFootball);
}
private void populateMatches(JSONArray matchesAPIFootball) {
JSONArray matches = new JSONArray();
for(Object matchAPIFootballObject : matchesAPIFootball) {
JSONObject matchAPIFootball = (JSONObject) matchAPIFootballObject;
JSONObject match = new JSONObject();
String matchday = matchAPIFootball.get("round").toString();
match.put("matchday", matchday);
JSONObject teamHome = (JSONObject) matchAPIFootball.get("homeTeam");
String teamNameHome = teamHome.get("team_name").toString();
JSONObject teamGuest = (JSONObject) matchAPIFootball.get("awayTeam");
String teamNameGuest = teamGuest.get("team_name").toString();
match.put("match", teamNameHome + " - " + teamNameGuest);
Long fixtureId = (Long) matchAPIFootball.get("fixture_id");
match.put("matchId", fixtureId);
matches.add(match);
}
this.matches.put("matches", matches);
}
public JSONObject getMatches() {
return this.matches;
}
public String getMatchesBeautful() {
JsonElement jelement = JsonParser.parseString(this.matches.toString());
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(jelement);
}
}

View File

@@ -199,6 +199,10 @@ public class TLWMatch extends BaseMatch{
return this.groupId;
}
public Integer getShowTable() {
return this.showTable;
}
public String getFormulaHome() {
String formula = "'D'";
if(this.teamIdHome != null) {
@@ -270,9 +274,15 @@ public class TLWMatch extends BaseMatch{
this.matchDatetime = matchDateTime;
}
public void setShowTable(Boolean showTable) {
this.showTable = showTable ? 0 : 1;
}
public void updateMatch(APIFootballMatch apiFootballMatch) {
this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdHome());
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdGuest());
this.teamNameHome = apiFootballMatch.getTeamNameHome();
this.teamNameGuest = apiFootballMatch.getTeamNameGuest();
this.goalsHome = apiFootballMatch.getGoalsHome();
this.goalsGuest = apiFootballMatch.getGoalsGuest();
this.matchDatetime = apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);

View File

@@ -28,7 +28,7 @@ public class TLWMatchdaysCreator {
this.matches = matchesCreator.getMatches();
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getTippligaConfig(season, configPath);
String jsonConfig = prov.getTippligaConfig(configPath);
JSONParser jsonParser = new JSONParser();
try {

View File

@@ -36,7 +36,7 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase {
this.conn = APIFootballConnector.getAPIFootballConnectorInstance(season - 1);
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getTippligaConfig(2021, configFileName);
String jsonConfig = prov.getTippligaConfig(configFileName);
try {
JSONParser jsonParser = new JSONParser();

View File

@@ -35,7 +35,7 @@ public class TLWMatchesCreatorTipperLeague extends TLWMatchesCreatorBase {
String matchPairingConfigString = prov.getTippligaBaseConfig("Tipper_Match_Pair_Config.json");
this.matchPairingConfig = (JSONArray) jsonParser.parse(matchPairingConfigString);
String tipperListString = prov.getTippligaConfig(season, configFileName);
String tipperListString = prov.getTippligaConfig(configFileName);
this.tipperList = (JSONObject) jsonParser.parse(tipperListString);
String tipperTeamConfigString = prov.getTippligaBaseConfig("Tipper_Team_Config.json");

View File

@@ -35,7 +35,7 @@ public class TLWMatchesCreatorTipperPokal extends TLWMatchesCreatorBase{
try {
JSONParser jsonParser = new JSONParser();
String tipperListString = prov.getTippligaConfig(season, configFileName);
String tipperListString = prov.getTippligaConfig(configFileName);
this.tipperList = (JSONObject) jsonParser.parse(tipperListString);
String tipperTeamConfigString = prov.getTippligaBaseConfig("Tipper_Team_Config.json");

View File

@@ -28,7 +28,7 @@ public class TLWMatchesManagerBase {
protected void initConfigParamsFromFile(String configFileName) {
GistProvider prov = GistProvider.getInstance();
String jsonConfig = prov.getTippligaConfig(season, configFileName);
String jsonConfig = prov.getTippligaConfig(configFileName);
try {
JSONParser jsonParser = new JSONParser();
//Read JSON file

View File

@@ -1,6 +1,5 @@
package de.jeyp91.tippliga;
import com.google.api.client.util.DateTime;
import de.jeyp91.App;
import de.jeyp91.TeamIDMatcher;
import de.jeyp91.apifootball.APIFootballMatch;
@@ -10,14 +9,11 @@ import org.apache.logging.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import javax.sound.midi.SysexMessage;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
private static Logger logger = LogManager.getLogger(App.class);
private static final Logger logger = LogManager.getLogger(App.class);
ArrayList<TLWMatch> tlwMatchesOriginal;
ArrayList<TLWMatch> tlwMatchesUpdated;
@@ -47,7 +43,9 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
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));
for (TLWMatch match : tlwMatchesOriginalMatchday) {
tlwMatchesUpdatedMatchday.add(new TLWMatch(match));
}
if (apiFootballMatches.size() > tlwMatchesUpdatedMatchday.size()) {
logger.error("Not matching config!");
@@ -55,6 +53,7 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
addMissingMatches(tlwMatchesUpdatedMatchday, apiFootballMatches);
updateMatchDateTimes(tlwMatchesUpdatedMatchday, apiFootballMatches);
updateShowTable(tlwMatchesUpdatedMatchday, apiFootballMatches);
updateStatus(tlwMatchesUpdatedMatchday);
this.tlwMatchesOriginal.addAll(tlwMatchesOriginalMatchday);
@@ -87,7 +86,7 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
"Spielnummer " + matchOriginal.getMatchNo() + ", " +
"Spiel '" + TeamIDMatcher.getTeamNameFromTippligaId(matchOriginal.getTeamIdHome()) + "' - '" + TeamIDMatcher.getTeamNameFromTippligaId(matchOriginal.getTeamIdGuest()) + "', ";
if(matchOriginal.getTeamIdHome() != matchUpdated.getTeamIdHome()) {
if(!matchOriginal.getTeamIdHome().equals(matchUpdated.getTeamIdHome())) {
updateString += updateStart +
"team_id_home = " + matchUpdated.getTeamIdHome() + " " +
condition;
@@ -95,7 +94,7 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
"Heimteam zu '" + matchUpdated.getTeamNameHome() + "'.\n";
}
if(matchOriginal.getTeamIdGuest() != matchUpdated.getTeamIdGuest()) {
if(!matchOriginal.getTeamIdGuest().equals(matchUpdated.getTeamIdGuest())) {
updateString += updateStart +
"team_id_guest = " + matchUpdated.getTeamIdGuest() + " " +
condition;
@@ -119,6 +118,14 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
"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;
}
@@ -174,6 +181,16 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase {
}
}
private void updateShowTable(
ArrayList<TLWMatch> tlwMatches,
ArrayList<APIFootballMatch> apiFootballMatches)
{
for(APIFootballMatch apiFootballMatch : apiFootballMatches) {
TLWMatch tlwMatch = getMatchingMatch(apiFootballMatch, tlwMatches);
tlwMatch.setShowTable(apiFootballMatch.getShowTable());
}
}
private TLWMatch getMatchingMatch(APIFootballMatch apiFootballMatch, ArrayList<TLWMatch> tlwMatches) {
int foundMatches = 0;
TLWMatch matchingMatch = null;

View File

@@ -19,8 +19,7 @@ public class GistProviderTest {
@Test
public void getFileTest() {
GistProvider prov = GistProvider.getInstance();
String id = prov.getGistID("Team_ID_Matcher");
String file = prov.getFileFromGist(id,"Team_ID_Matcher.json");
String file = prov.getFileFromGist("Team_ID_Matcher.json");
System.out.println(file);
}

View File

@@ -0,0 +1,41 @@
package de.jeyp91.gists;
import org.junit.Test;
public class MatchesListCreatorTest {
@Test
public void getMatchesTest2664() {
MatchesListCreator creator = new MatchesListCreator(2664);
String matches = creator.getMatchesBeautful();
System.out.println(matches);
}
@Test
public void getMatchesTest2743() {
MatchesListCreator creator = new MatchesListCreator(2743);
String matches = creator.getMatchesBeautful();
System.out.println(matches);
}
@Test
public void getMatchesTest2755() {
MatchesListCreator creator = new MatchesListCreator(2755);
String matches = creator.getMatchesBeautful();
System.out.println(matches);
}
@Test
public void getMatchesTest2790() {
MatchesListCreator creator = new MatchesListCreator(2790);
String matches = creator.getMatchesBeautful();
System.out.println(matches);
}
@Test
public void getMatchesTest2795() {
MatchesListCreator creator = new MatchesListCreator(2795);
String matches = creator.getMatchesBeautful();
System.out.println(matches);
}
}