First version with ability to create database for new season

This commit is contained in:
2020-09-27 19:05:20 +02:00
parent b97e15be7d
commit d18100eb2d
74 changed files with 106522 additions and 0 deletions
@@ -0,0 +1,137 @@
package de.jeyp91.tippliga;
import com.google.common.io.Resources;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class TLWFootballMatchdaysCreator {
int season;
int league;
ArrayList<TLWMatch> matches;
int matchesPerMatchday;
JSONObject configObject;
public TLWFootballMatchdaysCreator (int season, int league, String configPath){
this.season = season;
this.league = league;
TLWFootballMatchesCreator matchesCreator = new TLWFootballMatchesCreator(2021, 1, configPath);
this.matches = matchesCreator.getMatches();
JSONParser jsonParser = new JSONParser();
URL url = Resources.getResource(season + "\\" + configPath);
String jsonConfig = null;
try {
jsonConfig = Resources.toString(url, StandardCharsets.UTF_8);
this.configObject = (JSONObject) jsonParser.parse(jsonConfig);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
//Read JSON file
this.matchesPerMatchday = ((Long) this.configObject.get("matchesPerMatchday")).intValue();
}
public ArrayList<TLWMatchday> getMatchdays() {
ArrayList<TLWMatchday> matchdays = new ArrayList<>();
int matchdayCounter = 1;
while(getMatchesForMatchday(matches, matchdayCounter).size() > 0) {
ArrayList<TLWMatch> matchesOfMatchday = getMatchesForMatchday(matches, matchdayCounter);
String deliveryDate1 = null;
String deliveryDate2 = null;
Date firstMatchDate = null;
for(TLWMatch match : matchesOfMatchday) {
if(deliveryDate1 == null) {
deliveryDate1 = match.getMatchDateTime();
}
Date matchdate = null;
try {
matchdate = new SimpleDateFormat("yyyy-MM-dd").parse(match.getMatchDateTime().substring(0, 10));
} catch (java.text.ParseException e) {
e.printStackTrace();
}
if(firstMatchDate == null) {
firstMatchDate = matchdate;
}
if(deliveryDate2 == null && getDaysDifference(firstMatchDate, matchdate) >= 1) {
deliveryDate2 = match.getMatchDateTime();
}
}
int numberOfMatches = 0;
if(this.matchesPerMatchday == 0){
numberOfMatches = matchesOfMatchday.size();
}
String matchdayName = getMatchdayNameFromConfig(matchdayCounter);
TLWMatchday matchday = new TLWMatchday(this.season, this.league, matchdayCounter, 0, deliveryDate1, deliveryDate2, "", matchdayName, numberOfMatches);
matchdays.add(matchday);
matchdayCounter++;
}
return matchdays;
}
public String getMatchdaysSQL() {
String matchdaySql = "";
for (TLWMatchday matchday : getMatchdays()) {
matchdaySql += "REPLACE INTO phpbb_footb_matchdays VALUES('";
matchdaySql += matchday.getSeason().toString();
matchdaySql += "', '";
matchdaySql += matchday.getLeague().toString();
matchdaySql += "', '";
matchdaySql += matchday.getMatchday().toString();
matchdaySql += "', '";
matchdaySql += matchday.getStatus().toString();
matchdaySql += "', '";
matchdaySql += matchday.getDeliveryDate();
matchdaySql += "', '";
matchdaySql += matchday.getDeliveryDate2();
matchdaySql += "', '";
matchdaySql += matchday.getDeliveryDate3();
matchdaySql += "', '";
matchdaySql += matchday.getMatchdayName();
matchdaySql += "', '";
matchdaySql += matchday.getMatches().toString();
matchdaySql += "');\n";
}
return matchdaySql;
}
private int getDaysDifference(Date date1, Date date2) {
long startTime = date1.getTime();
long endTime = date2.getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);
return (int) diffDays;
}
private ArrayList<TLWMatch> getMatchesForMatchday(ArrayList<TLWMatch> matches, int matchday) {
ArrayList<TLWMatch> matchesOfMatchday = new ArrayList<>();
for(TLWMatch match : matches) {
if(match.getMatchday() == matchday) {
matchesOfMatchday.add(match);
}
}
return matchesOfMatchday;
}
private String getMatchdayNameFromConfig(int matchday) {
String matchdayName = "";
JSONArray matchdaysConfig = (JSONArray) this.configObject.get("matchdayConfig");
for (Object matchdayConfig : matchdaysConfig) {
if(((JSONObject) matchdayConfig).get("TLWMatchday").toString().equals(String.valueOf(matchday))
&& ((JSONObject) matchdayConfig).containsKey("matchdayName")) {
matchdayName = ((JSONObject) matchdayConfig).get("matchdayName").toString();
}
}
return matchdayName;
}
}
@@ -0,0 +1,191 @@
package de.jeyp91.tippliga;
import com.google.common.io.Resources;
import de.jeyp91.apifootball.APIFootballConnector;
import de.jeyp91.apifootball.APIFootballMatch;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class TLWFootballMatchesCreator {
int season;
int league;
int numberOfMatchdays;
int matchesPerMatchday;
int ko;
int nextMatchNo = 1;
JSONArray matchdayConfig;
APIFootballConnector conn;
ArrayList<TLWMatch> TLWMatches;
public TLWFootballMatchesCreator(int season, int league, String configFileName) {
this.season = season;
this.league = league;
conn = new APIFootballConnector(season - 1);
URL url = Resources.getResource(season + "\\" + configFileName);
String jsonConfig = null;
this.TLWMatches = new ArrayList<>();
try {
JSONParser jsonParser = new JSONParser();
jsonConfig = Resources.toString(url, StandardCharsets.UTF_8);
//Read JSON file
JSONObject config = (JSONObject) jsonParser.parse(jsonConfig);
this.numberOfMatchdays = ((Long) config.get("numberOfMatchdays")).intValue();
this.matchdayConfig = (JSONArray) config.get("matchdayConfig");
this.ko = ((Long) config.get("ko")).intValue();
} catch (IOException | ParseException e) {
e.printStackTrace();
}
this.publicateMatchObjects();
}
private void publicateMatchObjects() {
for(int i = 0; i < this.matchdayConfig.size(); i++) {
int TLWMatchday = ((Long) ((JSONObject) this.matchdayConfig.get(i)).get("TLWMatchday")).intValue();
JSONArray matchesConfig = (JSONArray)((JSONObject) this.matchdayConfig.get(i)).get("matchesConfig");
ArrayList<APIFootballMatch> APIFootballMatches = getMatchesForMatchday(matchesConfig);
int tempNumberOfMatchesBackup = this.matchesPerMatchday;
if(((JSONObject) this.matchdayConfig.get(i)).containsKey("numberOfMatches")) {
this.matchesPerMatchday = ((Long) ((JSONObject) this.matchdayConfig.get(i)).get("numberOfMatches")).intValue();
}
int matchdayMatchCounter = 0;
// Use first matchtime because API Football always returns matches sorted by date
Date firstDate = null;
for(APIFootballMatch match : APIFootballMatches) {
int matchNo = this.nextMatchNo;
this.nextMatchNo++;
int status = 0;
Date matchDateTime = null;
try {
matchDateTime = new SimpleDateFormat("yyyy-MM-dd").parse(match.getMatchDateTime().substring(0, 10));
} catch (java.text.ParseException e) {
e.printStackTrace();
}
if(firstDate != null && getDaysDifference(firstDate, matchDateTime) >= 1) {
status = -1;
}
this.TLWMatches.add(new TLWMatch(match, this.season, this.league, TLWMatchday, matchNo, status, this.ko));
matchdayMatchCounter++;
if(firstDate == null) {
firstDate = matchDateTime;
}
}
// Add empty missing matches
for(int j = matchdayMatchCounter; j < this.matchesPerMatchday; j++) {
String matchDatetime = "";
if(((JSONObject) matchesConfig.get(0)).get("type").toString().equals("ToBeDefined")) {
matchDatetime = ((JSONObject) matchesConfig.get(0)).get("placeholderDatetime").toString();
}
else if(APIFootballMatches.size() > 0) {
matchDatetime = this.TLWMatches.get(this.TLWMatches.size() - 1).getMatchDateTime();
}
int matchNo = this.nextMatchNo;
this.nextMatchNo++;
this.TLWMatches.add(new TLWMatch(this.season, this.league, TLWMatchday, matchNo, matchDatetime, 0, this.ko));
}
if(((JSONObject) this.matchdayConfig.get(i)).containsKey("numberOfMatches")) {
this.matchesPerMatchday = tempNumberOfMatchesBackup;
}
}
}
private ArrayList<APIFootballMatch> getMatchesForMatchday(JSONArray config) {
ArrayList<APIFootballMatch> apiFootballMatches = new ArrayList<>();
for (Object singleConfigObject : config) {
JSONObject singleConfig = (JSONObject) singleConfigObject;
String type = (String) singleConfig.get("type");
switch (type) {
case "AllMatchesOfMatchday":
int matchesLeague = ((Long) singleConfig.get("matchesLeague")).intValue();
int leagueMatchday = ((Long) singleConfig.get("leagueMatchday")).intValue();
apiFootballMatches.addAll(conn.getMatchDataByLeagueAndMatchday(matchesLeague, leagueMatchday));
break;
case "SingleMatch":
int matchLeague = ((Long) singleConfig.get("matchLeague")).intValue();
int matchId = ((Long) singleConfig.get("matchId")).intValue();
apiFootballMatches.add(conn.getMatchDataByLeagueAndMatchID(matchLeague, matchId));
break;
}
}
return apiFootballMatches;
}
private int getDaysDifference(Date date1, Date date2) {
long startTime = date1.getTime();
long endTime = date2.getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);
return (int) diffDays;
}
public ArrayList<TLWMatch> getMatches() {
return this.TLWMatches;
}
public String getSQLInsertString() {
String sql = "";
ArrayList<TLWMatch> tlwMatches = getMatches();
// Add matches from config
for(TLWMatch match : tlwMatches) {
String teamIdHome = match.getTeamIdHome() != null ? match.getTeamIdHome().toString() : "''";
String teamIdGuest = match.getTeamIdGuest() != null ? match.getTeamIdGuest().toString() : "''";
String matchSql = "REPLACE INTO phpbb_footb_matches VALUES(";
matchSql += match.getSeason().toString();
matchSql += ", ";
matchSql += match.getLeague().toString();
matchSql += ", ";
matchSql += match.getMatchNo().toString();
matchSql += ", ";
matchSql += teamIdHome;
matchSql += ", ";
matchSql += teamIdGuest;
// No goals while creating league
matchSql += ", '', '', ";
matchSql += match.getMatchday().toString();
// status 0 while creating
matchSql += ", ";
matchSql += match.getStatus().toString();
matchSql += ", '";
matchSql += match.getMatchDateTime();
// group_id, formula_home, formula_guest
matchSql += "', '', '', '', '";
// ko_match,
matchSql += match.getKoMatch();
// goals_overtime_home, goals_overtime_guest
matchSql += "', '', '', ";
// show_table
matchSql += "0";
// trend, odd_1, odd_x, odd_2, rating
matchSql += ", '', '0.00', '0.00', '0.00', '0.00');\n";
sql += matchSql;
}
return sql;
}
}
@@ -0,0 +1,79 @@
package de.jeyp91.tippliga;
public class TLWLeague {
public static String getLeagueName(int id) {
String leagueName = "";
switch (id) {
case 1:
leagueName = "1. Tippliga Würzburg";
break;
case 2:
leagueName = "2. Tippliga Würzburg";
break;
case 46:
leagueName = "Elfmeter";
break;
case 47:
leagueName = "Relegation";
break;
case 48:
leagueName = "WTL-Pokal";
break;
case 49:
leagueName = "Liga-Cup";
break;
default: break;
}
return leagueName;
}
public static String getLeagueNameShort(int id) {
String leagueName = "";
switch (id) {
case 1:
leagueName = "1. TLW";
break;
case 2:
leagueName = "2. TLW";
break;
case 46:
leagueName = "ELF";
break;
case 47:
leagueName = "REL";
break;
case 48:
leagueName = "WTL";
break;
case 49:
leagueName = "LC";
break;
default: break;
}
return leagueName;
}
public static String getLeagueNameCalendar(int id) {
String leagueName = "";
switch (id) {
case 1:
case 2:
leagueName = "TLW";
break;
case 46:
leagueName = "ELF";
break;
case 47:
leagueName = "REL";
break;
case 48:
leagueName = "WTL";
break;
case 49:
leagueName = "LC";
break;
default: break;
}
return leagueName;
}
}
@@ -0,0 +1,225 @@
package de.jeyp91.tippliga;
import java.sql.ResultSet;
import java.sql.SQLException;
import de.jeyp91.BaseMatch;
import de.jeyp91.TeamIDMatcher;
import de.jeyp91.apifootball.APIFootballMatch;
import de.jeyp91.openligadb.OpenLigaDBMatch;
/**
*
*/
public class TLWMatch extends BaseMatch {
public final Integer STATUS_NOTSTARTED = 0;
public final Integer STATUS_STARTED = 1;
public final Integer STATUS_PROVISIONAL_RESULT_AVAILABLE = 2;
public final Integer STATUS_FINISHED = 3;
private Integer season = null;
private Integer league = null;
private Integer matchNo = null;
private String groupId = null;
private String formulaHome = null;
private String formulaGuest = null;
private Integer status = null;
private Integer koMatch = 0;
private Integer goalsOvertimeHome = null;
private Integer goalsOvertimeGuest = null;
private Integer showTable = null;
private String trend = null;
private Float odd1 = null;
private Float oddX = null;
private Float odd2 = null;
private Float rating = null;
public TLWMatch(ResultSet rset) {
final int SEASON = 1;
final int LEAGUE = 2;
final int MATCH_NO = 3;
final int TEAM_ID_HOME = 4;
final int TEAM_ID_GUEST = 5;
final int GOALS_HOME = 6;
final int GOALS_GUEST = 7;
final int MATCHDAY = 8;
final int STATUS = 9;
final int MATCH_DATETIME = 10;
final int GROUP_ID = 11;
final int FORMULA_HOME = 12;
final int FORMULA_GUEST = 13;
final int KO_MATCH = 14;
final int GOALS_OVERTIME_HOME = 15;
final int GOALS_OVERTIME_GUEST = 16;
final int SHOW_TABLE = 17;
final int TREND = 18;
final int ODD1 = 19;
final int ODDX = 20;
final int ODD2 = 21;
final int RATING = 22;
try {
this.season = Integer.parseInt(rset.getString(SEASON));
this.league = Integer.parseInt(rset.getString(LEAGUE));
this.matchNo = Integer.parseInt(rset.getString(MATCH_NO));
this.teamIdHome = Integer.parseInt(rset.getString(TEAM_ID_HOME));
this.teamIdGuest = Integer.parseInt(rset.getString(TEAM_ID_GUEST));
this.goalsHome = rset.getString(GOALS_HOME).isEmpty()?null:Integer.parseInt(rset.getString(GOALS_HOME));
this.goalsGuest = rset.getString(GOALS_GUEST).isEmpty()?null:Integer.parseInt(rset.getString(GOALS_GUEST));
this.matchday = Integer.parseInt(rset.getString(MATCHDAY));
this.status = Integer.parseInt(rset.getString(STATUS));
this.matchDatetime = rset.getString(MATCH_DATETIME);
this.groupId = rset.getString(GROUP_ID);
this.formulaHome = rset.getString(FORMULA_HOME);
this.formulaGuest = rset.getString(FORMULA_GUEST);
this.koMatch = Integer.parseInt(rset.getString(KO_MATCH));
this.goalsOvertimeHome = rset.getString(GOALS_OVERTIME_HOME).isEmpty()?null:Integer.parseInt(rset.getString(GOALS_OVERTIME_HOME));
this.goalsOvertimeGuest = rset.getString(GOALS_OVERTIME_GUEST).isEmpty()?null:Integer.parseInt(rset.getString(GOALS_OVERTIME_GUEST));
this.showTable = Integer.parseInt(rset.getString(SHOW_TABLE));
this.trend = rset.getString(TREND);
this.odd1 = Float.parseFloat(rset.getString(ODD1));
this.oddX = Float.parseFloat(rset.getString(ODDX));
this.odd2 = Float.parseFloat(rset.getString(ODD2));
this.rating = Float.parseFloat(rset.getString(RATING));
} catch (SQLException e) {
/* TODO */
e.printStackTrace();
}
}
public TLWMatch(OpenLigaDBMatch oldbmatch, int season, int league, int matchday, int matchNo) {
this.season = season;
this.league = league;
this.matchday = matchday;
this.matchNo = matchNo;
this.teamIdHome = TeamIDMatcher.getTippligaIdFromOpenLigaDbId(oldbmatch.getTeamIdHome());
this.teamIdGuest = TeamIDMatcher.getTippligaIdFromOpenLigaDbId(oldbmatch.getTeamIdGuest());
this.goalsHome = oldbmatch.getGoalsHome();
this.goalsGuest = oldbmatch.getGoalsGuest();
this.matchDatetime = oldbmatch.getMatchDateTime().replace("T", " ");
this.groupId = "";
this.formulaHome = "";
this.formulaGuest = "";
this.status = 0;
}
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.groupId = "";
this.formulaHome = "";
this.formulaGuest = "";
this.status = status;
this.koMatch = koMatch;
}
public TLWMatch(int season, int league, int matchday, int matchNo, String matchDatetime, int status, int koMatch) {
this.season = season;
this.matchday = matchday;
this.league = league;
this.matchNo = matchNo;
this.formulaHome = "D";
this.formulaGuest = "D";
this.status = status;
this.koMatch = koMatch;
this.matchDatetime = matchDatetime;
}
public TLWMatch(int season, int league, int matchday, int matchNo, int teamIdHome, int teamIdGuest, String matchDatetime) {
this.season = season;
this.matchday = matchday;
this.league = league;
this.matchNo = matchNo;
this.teamIdHome = teamIdHome;
this.teamIdGuest = teamIdGuest;
this.matchDatetime = matchDatetime;
this.status = 0;
}
public Integer getSeason() {
return this.season;
}
public Integer getLeague() {
return this.league;
}
public Integer getMatchNo() {
return this.matchNo;
}
public Integer getStatus() {
return this.status;
}
public Integer getKoMatch() {
return this.koMatch;
}
public String getGroupId() {
return this.groupId;
}
public Integer isSameMatch(OpenLigaDBMatch compareMatch) {
if(this.getSeason() != compareMatch.getSeason()) {
return COMPARISON_DIFFERENT;
}
if(this.getMatchday() != compareMatch.getMatchday()) {
return COMPARISON_DIFFERENT;
}
if(this.getTeamIdHome() != compareMatch.getTeamIdHome()) {
return COMPARISON_DIFFERENT;
}
if(this.getTeamIdGuest() != compareMatch.getTeamIdGuest()) {
return COMPARISON_DIFFERENT;
}
String thisDateTime = this.getMatchDateTime().replace("T", " ");
String tempDateTime = compareMatch.getMatchDateTime().replace("T", " ");
if(!tempDateTime.equals(thisDateTime)) {
return COMPARISON_DIFFERENT_DATETIME;
}
if(this.goalsHome != compareMatch.getGoalsHome() ||
this.goalsGuest != compareMatch.getGoalsGuest()) {
return COMPARISON_DIFFERENT_RESULT;
}
return COMPARISON_IDENTICAL;
}
public String getSQLQueryReplace() {
String query = "REPLACE INTO phpbb_footb_matches VALUES (" +
this.season + ", " +
this.league + ", " +
this.matchNo + ", " +
nullToSqlEmptyString(this.teamIdHome) + ", " +
nullToSqlEmptyString(this.teamIdGuest) + ", " +
nullToSqlEmptyString(this.goalsHome) + ", " +
nullToSqlEmptyString(this.goalsGuest) + ", " +
this.matchday + ", " +
this.status + ", " +
"'" + this.matchDatetime + "', " +
"'" + this.groupId + "', " +
"'" + this.formulaHome + "', " +
"'" + this.formulaGuest + "', " +
nullToSqlEmptyString(this.koMatch) + ", " +
nullToSqlEmptyString(this.goalsOvertimeHome) + ", " +
nullToSqlEmptyString(this.goalsOvertimeGuest) + ", " +
nullToSqlEmptyString(this.showTable) + ", " +
"'0.00','0.00','0.00','0.00');";
return query;
}
private String nullToSqlEmptyString(Integer number) {
return number != null ? number.toString() : "''";
}
}
@@ -0,0 +1,99 @@
package de.jeyp91.tippliga;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TLWMatchday {
final Integer STATUS_NOTSTARTED = 0;
final Integer STATUS_STARTED = 1;
final Integer STATUS_PROVISIONAL_RESULT_AVAILABLE = 2;
final Integer STATUS_FINISHED = 3;
private Integer season = null;
private Integer league = null;
private Integer matchday = null;
private Integer status = null;
private String deliveryDate = null;
private String deliveryDate2 = null;
private String deliveryDate3 = null;
private String matchdayName = null;
private Integer matches = null;
public TLWMatchday(ResultSet rset) {
final int SEASON = 1;
final int LEAGUE = 2;
final int MATCHDAY = 3;
final int STATUS = 4;
final int DELIVERY_DATE = 5;
final int DELIVERY_DATE_2 = 6;
final int DELIVERY_DATE_3 = 7;
final int MATCHDAY_NAME = 8;
final int MATCHES = 9;
try {
this.season = Integer.parseInt(rset.getString(SEASON));
this.league = Integer.parseInt(rset.getString(LEAGUE));
this.matchday = Integer.parseInt(rset.getString(MATCHDAY));
this.status = Integer.parseInt(rset.getString(STATUS));
this.deliveryDate = rset.getString(DELIVERY_DATE);
this.deliveryDate2 = rset.getString(DELIVERY_DATE_2);
this.deliveryDate3 = rset.getString(DELIVERY_DATE_3);
this.matchdayName = rset.getString(MATCHDAY_NAME);
this.matches = Integer.parseInt(rset.getString(MATCHES));
} catch (SQLException e) {
/* TODO */
e.printStackTrace();
}
}
public TLWMatchday(int season, int league, int matchday, int status, String deliveryDate1, String deliveryDate2, String deliveryDate3, String matchdayName, int numberOfMatches) {
this.season = season;
this.league = league;
this.matchday = matchday;
this.status = status;
this.deliveryDate = deliveryDate1;
this.deliveryDate2 = deliveryDate2;
this.deliveryDate3 = deliveryDate3;
this.matchdayName = matchdayName;
this.matches = numberOfMatches;
}
public Integer getSeason() {
return this.season;
}
public Integer getLeague() {
return this.league;
}
public Integer getMatchday() {
return this.matchday;
}
public Integer getStatus() {
return this.status;
}
public String getDeliveryDate() {
return this.deliveryDate == null ? "" : this.deliveryDate;
}
public String getDeliveryDate2() {
return this.deliveryDate2 == null ? "" : this.deliveryDate2;
}
public String getDeliveryDate3() {
return this.deliveryDate3 == null ? "" : this.deliveryDate3;
}
public String getMatchdayName() {
return this.matchdayName;
}
public Integer getMatches() {
return this.matches;
}
}
@@ -0,0 +1,71 @@
package de.jeyp91.tippliga;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*
*/
public class TLWTeam {
private int season;
private int league;
private int teamId;
private String teamName;
private String teamNameShort;
private String teamSymbol;
private String groupId;
private int matchday;
public TLWTeam(ResultSet rset) throws SQLException {
final int SEASON = 1;
final int LEAGUE = 2;
final int TEAM_ID = 3;
final int TEAM_NAME = 4;
final int TEAM_NAME_SHORT = 5;
final int TEAM_SYMBOL = 6;
final int GROUP_ID = 7;
final int MATCHDAY = 8;
this.season = Integer.parseInt(rset.getString(SEASON));
this.league = Integer.parseInt(rset.getString(LEAGUE));
this.teamId = Integer.parseInt(rset.getString(TEAM_ID));
this.teamName = rset.getString(TEAM_NAME);
this.teamNameShort = rset.getString(TEAM_NAME_SHORT);
this.teamSymbol = rset.getString(TEAM_SYMBOL);
this.groupId = rset.getString(GROUP_ID);
this.matchday = Integer.parseInt(rset.getString(MATCHDAY));
}
public int getSeason() {
return this.season;
}
public int getLeague() {
return this.league;
}
public int getTeamId() {
return this.teamId;
}
public String getTeamName() {
return this.teamName;
}
public String getTeamNameShort() {
return this.teamNameShort;
}
public String getTeamSymbol() {
return this.teamSymbol;
}
public String getGroupId() {
return this.groupId;
}
public int getMatchday() {
return this.matchday;
}
}
@@ -0,0 +1,62 @@
package de.jeyp91.tippliga;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
public class TLWTeamsPokalCreator {
int season;
int league;
ArrayList<TLWMatch> matches;
TippligaSQLConnector connector = new TippligaSQLConnector();
public TLWTeamsPokalCreator(int season, int league, ArrayList<TLWMatch> matches) {
this.season = season;
this.league = league;
this.matches = matches;
}
public Set<Integer> getTeamIds() {
Set<Integer> teamIds = new LinkedHashSet<>();
for(TLWMatch match : matches) {
if(match.getTeamIdHome() != null) {
teamIds.add(match.getTeamIdHome());
}
if(match.getTeamIdGuest() != null) {
teamIds.add(match.getTeamIdGuest());
}
}
return teamIds;
}
public String getSql() {
Set<Integer> teamIds = getTeamIds();
String sql = "";
for (Integer id : teamIds) {
ArrayList<TLWTeam> teams = connector.getTeamsById(String.valueOf(id));
String teamName = teams.get(0).getTeamName();
String teamNameShort = teams.get(0).getTeamNameShort();
String teamSymbol = teams.get(0).getTeamSymbol();
String groupId = "";
int matchday = 0;
sql += "REPLACE INTO phpbb_footb_teams VALUES ('";
sql += this.season;
sql += "', '";
sql += this.league;
sql += "', '";
sql += id;
sql += "', '";
sql += teamName;
sql += "', '";
sql += teamNameShort;
sql += "', '";
sql += teamSymbol;
sql += "', '";
sql += groupId;
sql += "', '";
sql += matchday;
sql += "');\n";
}
return sql;
}
}
@@ -0,0 +1,143 @@
package de.jeyp91.tippliga;
import com.google.common.io.Resources;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class TLWTipperMatchesCreator {
int season;
int league;
ArrayList<TLWMatch> TLWMatches;
JSONArray matchPairingConfig;
JSONObject tipperList;
JSONArray tipperTeamConfig;
ArrayList<TLWMatchday> matchdays;
public TLWTipperMatchesCreator(int season, int league, String configFileName, ArrayList<TLWMatchday> matchdays) {
this.season = season;
this.league = league;
this.matchdays = matchdays;
URL matchPairConfigUrl = Resources.getResource("Tipper_Match_Pair_Config.json");
URL tipperListUrl = Resources.getResource(season + "\\" + configFileName);
URL tipperTeamConfigUrl = Resources.getResource("Tipper_Team_Config.json");
this.TLWMatches = new ArrayList<>();
try {
JSONParser jsonParser = new JSONParser();
String matchPairingConfigString = Resources.toString(matchPairConfigUrl, StandardCharsets.UTF_8);
this.matchPairingConfig = (JSONArray) jsonParser.parse(matchPairingConfigString);
String tipperListString = Resources.toString(tipperListUrl, StandardCharsets.UTF_8);
this.tipperList = (JSONObject) jsonParser.parse(tipperListString);
String tipperTeamConfigString = Resources.toString(tipperTeamConfigUrl, StandardCharsets.UTF_8);
this.tipperTeamConfig = (JSONArray) jsonParser.parse(tipperTeamConfigString);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
this.publicateMatchObjects();
}
private void publicateMatchObjects() {
for(Object matchdayConfig : this.matchPairingConfig) {
int matchday = ((Long) ((JSONObject) matchdayConfig).get("matchday")).intValue();
JSONArray matchesConfig = (JSONArray)((JSONObject) matchdayConfig).get("matches");
for(int i = 0; i < matchesConfig.size(); i++) {
int homeTipperNumber = ((Long) ((JSONObject) matchesConfig.get(i)).get("home")).intValue();
int guestTipperNumber = ((Long) ((JSONObject) matchesConfig.get(i)).get("guest")).intValue();
String homeName = this.tipperList.get(String.valueOf(homeTipperNumber)).toString();
String guestName = this.tipperList.get(String.valueOf(guestTipperNumber)).toString();
int teamIdHome = getTeamIdFromTipperName(homeName);
int teamIdGuest = getTeamIdFromTipperName(guestName);
int matchNo = (matchday - 1) * matchesConfig.size() + i + 1;
String matchDatetime = getDeliveryDateForMatchday(matchday);
TLWMatch tlwMatch = new TLWMatch(this.season, this.league, matchday, matchNo, teamIdHome, teamIdGuest, matchDatetime);
this.TLWMatches.add(tlwMatch);
}
}
}
private int getTeamIdFromTipperName(String name) {
int teamId = 0;
for(Object config : tipperTeamConfig) {
if (((JSONObject) config).get("team_name").toString().equals(name)) {
teamId = ((Long) (((JSONObject) config).get("team_id"))).intValue();
}
}
if(teamId == 0) {
System.out.println("Did not find Tipper ID for " + name);
}
return teamId;
}
public ArrayList<TLWMatch> getMatches() {
return this.TLWMatches;
}
public String getSQLInsertString() {
String sql = "";
ArrayList<TLWMatch> tlwMatches = getMatches();
// Add matches from config
for(TLWMatch match : tlwMatches) {
String matchSql = "REPLACE INTO phpbb_footb_matches VALUES(";
matchSql += match.getSeason().toString();
matchSql += ", ";
matchSql += match.getLeague().toString();
matchSql += ", ";
matchSql += match.getMatchNo().toString();
matchSql += ", ";
matchSql += match.getTeamIdHome().toString();
matchSql += ", ";
matchSql += match.getTeamIdGuest().toString();
// No goals while creating league
matchSql += ", '', '', ";
matchSql += match.getMatchday().toString();
// status 0 while creating
matchSql += ", 0, '";
matchSql += match.getMatchDateTime();
// group_id, formula_home, formula_guest, ko_match, goals_overtime_home, goals_overtime_guest
matchSql += "', '', '', '', 0, '', '', ";
// show_table
matchSql += "0";
// trend, odd_1, odd_x, odd_2, rating
matchSql += ", '', '0.00', '0.00', '0.00', '0.00');\n";
sql += matchSql;
}
return sql;
}
private String getDeliveryDateForMatchday(int matchday) {
String deliveryDate = "";
for (TLWMatchday matchdayObject : this.matchdays) {
if(matchdayObject.getMatchday() == matchday) {
deliveryDate = matchdayObject.getDeliveryDate();
}
}
return deliveryDate;
}
}
@@ -0,0 +1,132 @@
package de.jeyp91.tippliga;
import com.google.common.io.Resources;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class TLWTipperPokalMatchesCreator {
int season;
int league;
ArrayList<TLWMatch> TLWMatches;
JSONObject tipperList;
JSONArray tipperTeamConfig;
ArrayList<TLWMatchday> matchdays;
public TLWTipperPokalMatchesCreator(int season, int league, String configFileName, ArrayList<TLWMatchday> matchdays) {
this.season = season;
this.league = league;
this.matchdays = matchdays;
URL tipperListUrl = Resources.getResource(season + "\\" + configFileName);
URL tipperTeamConfigUrl = Resources.getResource("Tipper_Team_Config.json");
this.TLWMatches = new ArrayList<>();
try {
JSONParser jsonParser = new JSONParser();
String tipperListString = Resources.toString(tipperListUrl, StandardCharsets.UTF_8);
this.tipperList = (JSONObject) jsonParser.parse(tipperListString);
String tipperTeamConfigString = Resources.toString(tipperTeamConfigUrl, StandardCharsets.UTF_8);
this.tipperTeamConfig = (JSONArray) jsonParser.parse(tipperTeamConfigString);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
this.publicateMatchObjects();
}
private void publicateMatchObjects() {
int matchday = 1;
for(int i = 1; i < 13; i++) {
String homeName = this.tipperList.get(String.valueOf(2 * i - 1)).toString();
String guestName = this.tipperList.get(String.valueOf(2 * i)).toString();
int teamIdHome = getTeamIdFromTipperName(homeName);
int teamIdGuest = getTeamIdFromTipperName(guestName);
int matchNo = i;
String matchDatetime = getDeliveryDateForMatchday(matchday);
TLWMatch tlwMatch = new TLWMatch(this.season, this.league, matchday, matchNo, teamIdHome, teamIdGuest, matchDatetime);
this.TLWMatches.add(tlwMatch);
}
}
private int getTeamIdFromTipperName(String name) {
int teamId = 0;
for(Object config : tipperTeamConfig) {
if (((JSONObject) config).get("team_name").toString().equals(name)) {
teamId = ((Long) (((JSONObject) config).get("team_id"))).intValue();
}
}
if(teamId == 0) {
System.out.println("Did not find Tipper ID for " + name);
}
return teamId;
}
public ArrayList<TLWMatch> getMatches() {
return this.TLWMatches;
}
public String getSQLInsertString() {
String sql = "";
ArrayList<TLWMatch> tlwMatches = getMatches();
// Add matches from config
for(TLWMatch match : tlwMatches) {
String matchSql = "REPLACE INTO phpbb_footb_matches VALUES(";
matchSql += match.getSeason().toString();
matchSql += ", ";
matchSql += match.getLeague().toString();
matchSql += ", ";
matchSql += match.getMatchNo().toString();
matchSql += ", ";
matchSql += match.getTeamIdHome().toString();
matchSql += ", ";
matchSql += match.getTeamIdGuest().toString();
// No goals while creating league
matchSql += ", '', '', ";
matchSql += match.getMatchday().toString();
// status 0 while creating
matchSql += ", 0, '";
matchSql += match.getMatchDateTime();
// group_id, formula_home, formula_guest, ko_match, goals_overtime_home, goals_overtime_guest
matchSql += "', '', '', '', 0, '', '', ";
// show_table
matchSql += "0";
// trend, odd_1, odd_x, odd_2, rating
matchSql += ", '', '0.00', '0.00', '0.00', '0.00');\n";
sql += matchSql;
}
return sql;
}
private String getDeliveryDateForMatchday(int matchday) {
String deliveryDate = "";
for (TLWMatchday matchdayObject : this.matchdays) {
if(matchdayObject.getMatchday() == matchday) {
deliveryDate = matchdayObject.getDeliveryDate();
}
}
return deliveryDate;
}
}
@@ -0,0 +1,99 @@
package de.jeyp91.tippliga;
import de.jeyp91.tippliga.TLWMatch;
import de.jeyp91.tippliga.TLWTeam;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TippligaSQLConnector {
Connection con;
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.err.println("Unable to load MySQL Driver");
}
}
public TippligaSQLConnector() {
String jdbcUrl = "jdbc:mysql://localhost/d0144ddb?user=root&password=&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
try {
con = DriverManager.getConnection(jdbcUrl);
} catch (SQLException e) {
/* TODO */
e.printStackTrace();
}
}
public ArrayList<TLWTeam> getTeamsBySeasonAndLeague(String season, String league) {
String queryString = "SELECT * FROM `phpbb_footb_teams` WHERE `season` = " + season + " AND `league` = " + league + ";";
Statement stmt = null;
ResultSet rset = null;
ArrayList<TLWTeam> teams = new ArrayList<TLWTeam>();
try {
stmt = con.createStatement();
rset = stmt.executeQuery(queryString);
while ( rset.next()) {
teams.add(new TLWTeam(rset));
}
} catch (SQLException e) {
/* TODO */
e.printStackTrace();
}
teams.sort((t1, t2) -> t1.getTeamId() - t2.getTeamId());
return teams;
}
public ArrayList<TLWTeam> getTeamsById(String id) {
String queryString = "SELECT * FROM `phpbb_footb_teams` WHERE `team_id` = " + id + ";";
Statement stmt = null;
ResultSet rset = null;
ArrayList<TLWTeam> teams = new ArrayList<TLWTeam>();
try {
stmt = con.createStatement();
rset = stmt.executeQuery(queryString);
while ( rset.next()) {
teams.add(new TLWTeam(rset));
}
} catch (SQLException e) {
/* TODO */
e.printStackTrace();
}
return teams;
}
public ArrayList<TLWMatch> getMatchesBySeasonAndLeague(String season, String league) {
String queryString = "SELECT * FROM `phpbb_footb_matches` WHERE `season` = " + season + " AND `league` = " + league + ";";
Statement stmt = null;
ResultSet rset = null;
ArrayList<TLWMatch> matches = new ArrayList<TLWMatch>();
try {
stmt = con.createStatement();
rset = stmt.executeQuery(queryString);
while (rset.next()) {
matches.add(new TLWMatch(rset));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return matches;
}
public void updateMatchDateTime(String season, String league, String matchNo, String datetime) {
String queryString = "UPDATE `phpbb_footb_matches` "
+ "SET match_datetime = " + datetime
+ " WHERE `season` = " + season
+ " AND `league` = " + league
+ " AND match_no = " + matchNo + ";";
}
}