| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- package objects;
- import data.GuiMysql;
- import java.io.Serializable;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.DecimalFormat;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.List;
- import java.util.Optional;
- public class SoccerMatchAnalysis extends SoccerMatch implements Serializable {
- GuiMysql database;
- private List<TeamStanding> leagueTable;
- private LocalDate leagueTableUpdated;
- private int scoringDiffValue;
- private int scoringTotal;
- private int winCount;
- private int winLossRatio;
- private String percentages;
- Percentages calculatedPercentages;
- private int scoringTotalValue;
- public SoccerMatchAnalysis(SoccerMatch match) {
- setMatchData(match);
- database = GuiMysql.getInstance();
- }
- public int getWinLossRatio() {
- return winLossRatio;
- }
- public String getPercentages() {
- return percentages;
- }
- public void setPercentages(String value) {
- percentages = value;
- }
- public void setWinLossRatio(int winLossRatio) {
- this.winLossRatio = winLossRatio;
- }
- public int getWinCount() {
- return winCount;
- }
- public void setWinCount(int winCount) {
- this.winCount = winCount;
- }
- public int getScoringTotal() {
- return scoringTotal;
- }
- public void setScoringTotal(int scoringTotal) {
- this.scoringTotal = scoringTotal;
- }
- public int getScoringDiffValue() {
- return scoringDiffValue;
- }
- public void setScoringDiffValue(int scoringDiffValue) {
- this.scoringDiffValue = scoringDiffValue;
- }
- /**
- * Antalet mål som hemma laget gjort de senaste "gamesLookback" matcherna -
- * antalet mål för bortalaget under samma period
- *
- * @param gamesLookback - hur många matcher bakåt i tiden som ska kontrolleras
- * @return Float med skillnaden i mål mellan lagen hemma/borta
- */
- public int getScoringDiffLastGames(int gamesLookback) {
- int result = 0;
- String sql = "SELECT * FROM " +
- "(SELECT * FROM SoccerResults WHERE homeTeamId = ? AND leagueId = ? AND DATE" +
- "(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) ht " +
- "UNION " +
- "(SELECT * FROM SoccerResults WHERE " +
- "awayTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? " +
- "ORDER BY gameDate DESC " +
- "LIMIT ?);";
- int homeRes = 0;
- int awayRes = 0;
- try (PreparedStatement stat = database.getDbConnection().prepareStatement(sql)) {
- stat.setInt(1, getHomeTeam().getTeamId());
- stat.setInt(2, getHomeTeam().getTeamLeagueId());
- stat.setString(3, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- stat.setInt(4, gamesLookback);
- stat.setInt(5, getAwayTeam().getTeamId());
- stat.setInt(6, getAwayTeam().getTeamLeagueId());
- stat.setString(7, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- stat.setInt(8, gamesLookback);
- ResultSet rs = stat.executeQuery();
- while (rs.next()) {
- if (getHomeTeam().getTeamId() == rs.getInt("homeTeamId")) {
- homeRes += rs.getInt("homeScore");
- }
- if (getAwayTeam().getTeamId() == rs.getInt("awayTeamId")) {
- awayRes += rs.getInt("awayScore");
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- scoringDiffValue = homeRes - awayRes;
- return scoringDiffValue;
- }
- /**
- * Plocka fram antalet mål som ett specifikt lag gjort under de senaste
- * gamesLookback matcherna
- *
- * @param gamesLookback - hur många matcher bakåt i tiden som ska kontrolleras
- * @param homeTeam - är det hemma laget som ska kontrolleras i matchen
- * @return antalet mål som är gjorda av bestämt lag.
- */
- public int scoringTotal(int gamesLookback, boolean homeTeam) {
- int result = 0;
- /*
- * String sql = "SELECT * FROM " +
- * "(SELECT * FROM SoccerResults WHERE homeTeamId = ? AND leagueId = ? AND DATE"
- * +
- * "(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) ht " + "UNION DISTINCT " +
- * "(SELECT * FROM " +
- * "SoccerResults WHERE awayTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC "
- * +
- * "LIMIT ?)";
- */
- String sql = "SELECT * FROM ( " +
- "SELECT * FROM SoccerResults " +
- "WHERE (homeTeamId = ? OR awayTeamId = ?) AND leagueId = ? AND gameDate < ? " +
- ") AS CombinedResults " +
- "ORDER BY gameDate DESC " +
- "LIMIT ?";
- try (PreparedStatement stat = database.getDbConnection().prepareStatement(sql)) {
- final Team team;
- if (homeTeam) {
- team = getHomeTeam();
- } else {
- team = getAwayTeam();
- }
- stat.setInt(1, team.getTeamId());
- stat.setInt(2, team.getTeamId());
- stat.setInt(3, team.getTeamLeagueId());
- stat.setString(4, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- stat.setInt(5, gamesLookback);
- /*
- * stat.setInt(5, team.getTeamId());
- * stat.setInt(6, team.getTeamLeagueId());
- * stat.setString(7, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- * stat.setInt(8, gamesLookback);
- */
- ResultSet rs = stat.executeQuery();
- while (rs.next()) {
- if (rs.getInt("homeTeamId") == team.getTeamId()) {
- result += rs.getInt("homeScore");
- } else if (rs.getInt("awayTeamId") == team.getTeamId()) {
- result += rs.getInt("awayScore");
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- /**
- * Hämta tabell positionen för hemma eller borta laget
- *
- * @param homeTeam - är det hemma laget som ska kontrolleras för matchen?
- * @return position för specifierat lag
- */
- public int getTablePosition(boolean homeTeam) {
- int result = 0;
- updateLeagueTable();
- Optional<TeamStanding> standingOptional = leagueTable.stream().filter(
- p -> p.getTeamName().equals(homeTeam ? getHomeTeam().getTeamName() : getAwayTeam().getTeamName()))
- .findFirst();
- if (standingOptional.isPresent()) {
- TeamStanding standing = standingOptional.get();
- result = leagueTable.indexOf(standing);
- }
- return result;
- }
- private void updateLeagueTable() {
- if (!leagueTableUpdated.isEqual(getGameDate().toLocalDate())) {
- leagueTable = database.getLeagueTable(getHomeTeam().getTeamLeagueId(), getSeason(),
- getHomeTeam().getCountryId(), getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- leagueTableUpdated = getGameDate().toLocalDate();
- }
- }
- /**
- * @return Integer - hur många platser det är mellan hemma och borta laget
- */
- public int diffInStanding() {
- int result = 0;
- updateLeagueTable();
- Optional<TeamStanding> homeTeamStandingOptional = leagueTable.stream()
- .filter(p -> p.getTeamName().equals(getHomeTeam().getTeamName())).findFirst();
- Optional<TeamStanding> awayTeamStandingOptional = leagueTable.stream()
- .filter(p -> p.getTeamName().equals(getAwayTeam().getTeamName())).findFirst();
- if (homeTeamStandingOptional.isPresent() && awayTeamStandingOptional.isPresent()) {
- TeamStanding homeStanding = homeTeamStandingOptional.get();
- TeamStanding awayStanding = awayTeamStandingOptional.get();
- result = leagueTable.indexOf(homeStanding) - leagueTable.indexOf(awayStanding);
- }
- return result;
- }
- /**
- * Vinst förlust ratio för om man är enbart hemma eller bortalag.
- *
- * @param gamesLookback
- * @param homeTeam
- * @return Integer där vinst ger +1 lika ger 0 och förlust get -1
- */
- public int winLossRatio(int gamesLookback, boolean homeTeam) {
- int result = 0;
- Team team = homeTeam ? getHomeTeam() : getAwayTeam();
- String teamSql = homeTeam ? "homeTeamId = ? " : "awayTeamId = ? ";
- String sql = "SELECT * FROM SoccerResults WHERE " + teamSql + "AND leagueId = ? AND DATE(gameDate) < ? ORDER " +
- "BY gameDate DESC LIMIT ?";
- try (PreparedStatement stat = database.getDbConnection().prepareStatement(sql)) {
- stat.setInt(1, team.getTeamId());
- stat.setInt(2, team.getTeamLeagueId());
- stat.setString(3, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- stat.setInt(4, gamesLookback);
- ResultSet rs = stat.executeQuery();
- while (rs.next()) {
- int homeScore = rs.getInt("homeScore");
- int awayScore = rs.getInt("awayScore");
- if (homeTeam) {
- if (homeScore > awayScore) {
- result++;
- } else if (homeScore < awayScore) {
- result--;
- }
- } else {
- if (homeScore > awayScore) {
- result--;
- } else if (homeScore < awayScore) {
- result++;
- }
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- public int winLossRatioHomeAndAway(int gamesLookback, boolean homeTeam) {
- int result = 0;
- String sql = "SELECT * FROM SoccerResults " +
- "WHERE (homeTeamId = ? OR awayTeamId = ?) " +
- "AND leagueId = ? " +
- "AND DATE(gameDate) < ? " +
- "ORDER BY gameDate DESC " +
- "LIMIT ?";
- /*
- * String sql = "SELECT * FROM " +
- * "(SELECT * FROM SoccerResults WHERE homeTeamId = ? AND leagueId = ? AND DATE"
- * +
- * "(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) a " + "UNION DISTINCT " +
- * "(SELECT * FROM " +
- * "SoccerResults WHERE awayTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC "
- * +
- * "LIMIT ?) " + "ORDER BY gameDate DESC " + "LIMIT ?";
- */
- Team team = homeTeam ? getHomeTeam() : getAwayTeam();
- try (PreparedStatement stat = database.getDbConnection().prepareStatement(sql)) {
- stat.setInt(1, team.getTeamId());
- stat.setInt(2, team.getTeamId());
- stat.setInt(3, team.getTeamLeagueId());
- stat.setString(4, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- stat.setInt(5, gamesLookback);
- /*
- * stat.setInt(1, team.getTeamId());
- * stat.setInt(2, team.getTeamLeagueId());
- * stat.setString(3, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- * stat.setInt(4, gamesLookback);
- *
- * stat.setInt(5, team.getTeamId());
- * stat.setInt(6, team.getTeamLeagueId());
- * stat.setString(7, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- * stat.setInt(8, gamesLookback);
- *
- * stat.setInt(9, gamesLookback);
- */
- ResultSet rs = stat.executeQuery();
- while (rs.next()) {
- if (rs.getInt("homeTeamId") == team.getTeamId() && rs.getInt("homeScore") > rs.getInt("awayScore")) {
- result++;
- } else if (rs.getInt("awayTeamId") == team.getTeamId() && rs.getInt("awayScore") > rs.getInt(
- "homeScore")) {
- result++;
- } else {
- result--;
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- public float goalsScoredHomeAndAway(boolean homeTeam, int gameLookback) {
- int result = 0;
- Team team = homeTeam ? getHomeTeam() : getAwayTeam();
- String sql = "SELECT * FROM SoccerResults " +
- "WHERE (" + (homeTeam ? "home" : "away") + "TeamId = ?) " +
- " AND leagueId = ? " +
- " AND DATE(gameDate) < ? " +
- "ORDER BY gameDate DESC " +
- "LIMIT ?";
- /*
- * String sql = "SELECT * FROM " +
- * "(SELECT * FROM SoccerResults WHERE homeTeamId = ? AND leagueId = ? AND DATE"
- * +
- * "(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) a " + "UNION ALL " +
- * "(SELECT * FROM SoccerResults " +
- * "WHERE awayTeamId = ? AND leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ?) "
- * +
- * "ORDER BY gameDate DESC " + "LIMIT ?";
- */
- try (PreparedStatement stat = database.getDbConnection().prepareStatement(sql)) {
- stat.setInt(1, team.getTeamId());
- stat.setInt(2, team.getTeamLeagueId());
- stat.setString(3, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- stat.setInt(4, gameLookback);
- /*
- * stat.setInt(1, team.getTeamId());
- * stat.setInt(2, team.getTeamLeagueId());
- * stat.setString(3, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- * stat.setInt(4, gameLookback);
- * stat.setInt(5, team.getTeamId());
- * stat.setInt(6, team.getTeamLeagueId());
- * stat.setString(7, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- * stat.setInt(8, gameLookback);
- * stat.setInt(9, gameLookback);
- */
- ResultSet rs = stat.executeQuery();
- while (rs.next()) {
- if (rs.getInt("homeTeamId") == team.getTeamId()) {
- result += rs.getInt("homeScore");
- }
- if (rs.getInt("awayTeamId") == team.getTeamId()) {
- result += rs.getInt("awayScore");
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- // TODO Think Should the result be divided by lookback??
- // NEW TEST, devide by lookback (OR SIZE IF LESS THAN LOOKBACK?
- BigDecimal res = BigDecimal.valueOf((result / (float) gameLookback) * 2);
- res = res.setScale(1, RoundingMode.HALF_UP);
- DecimalFormat df = new DecimalFormat("#.00");
- return Float.valueOf(df.format(res.floatValue() / 2.0f).replaceAll(",", "."));
- }
- public int goalsScoredHomeOrAway(boolean homeTeam, int gameLookback) {
- int result = 0;
- Team team = homeTeam ? getHomeTeam() : getAwayTeam();
- String homeOrAway = homeTeam ? "home" : "away";
- String sql = "SELECT SUM(" + homeOrAway + "Score) FROM SoccerResults WHERE " + homeOrAway + "TeamId = ? AND " +
- "leagueId = ? AND DATE(gameDate) < ? ORDER BY gameDate DESC LIMIT ? " + "ORDER BY gameDate DESC";
- try (PreparedStatement stat = database.getDbConnection().prepareStatement(sql)) {
- stat.setInt(1, team.getTeamId());
- stat.setInt(2, team.getTeamLeagueId());
- stat.setString(3, getGameDate().format(DateTimeFormatter.ISO_LOCAL_DATE));
- stat.setInt(4, gameLookback);
- ResultSet rs = stat.executeQuery();
- while (rs.next()) {
- result += rs.getInt(homeOrAway + "Score");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- public int getScoringTotalValue() {
- return scoringTotalValue;
- }
- public void setScoringTotalValue(int value) {
- scoringTotalValue = value;
- }
- public Percentages calculateWinPercentages() {
- calculatedPercentages = new Percentages();
- League leagueInfo = database.getLeagueInfo(this.getHomeTeam().getTeamLeagueId());
- int homeScore = this.scoringTotal(leagueInfo.getScoringTotal(), true);
- int awayScore = this.scoringTotal(leagueInfo.getScoringTotal(), false);
- database.getTotalGoalStat(calculatedPercentages, leagueInfo.getLeagueId(), leagueInfo.getScoringTotal(),
- homeScore - awayScore);
- database.getScoringDiffLastGames(calculatedPercentages, leagueInfo.getLeagueId(),
- leagueInfo.getScoringDiffLastGame(),
- this.getScoringDiffLastGames(leagueInfo.getScoringDiffLastGame()));
- int winLossHAHome = this.winLossRatioHomeAndAway(leagueInfo.getWinLossRatioHomeAndAway(), true);
- int winLossHAAway = this.winLossRatioHomeAndAway(leagueInfo.getWinLossRatioHomeAndAway(), false);
- database.getWinLossRatioHomeAndAwayStatistics(calculatedPercentages, leagueInfo.getLeagueId(),
- leagueInfo.getWinLossRatioHomeAndAway(),
- winLossHAHome - winLossHAAway);
- int winLossHome = this.winLossRatio(leagueInfo.getWinLossRatio(), true);
- int winLossAway = this.winLossRatio(leagueInfo.getWinLossRatio(), false);
- this.winLossRatio(leagueInfo.getWinLossRatio(), false);
- database.getWinLossRatioStatisticsGames(calculatedPercentages, leagueInfo.getLeagueId(),
- leagueInfo.getWinLossRatio(), winLossHome - winLossAway);
- calculatedPercentages.calculatePercentages();
- return calculatedPercentages;
- }
- public Percentages getCalculatedPercentages() {
- if (calculatedPercentages == null) {
- calculatedPercentages = calculateWinPercentages();
- }
- return calculatedPercentages;
- }
- public void setCalculatedPercentages(Percentages calculatedPercentages) {
- this.calculatedPercentages = calculatedPercentages;
- }
- }
|