Przeglądaj źródła

Make sure league is included

Axel Nordh 3 lat temu
rodzic
commit
89ccaf8162

+ 33 - 18
Odds/src/mysql/Mysql.java

@@ -78,7 +78,7 @@ public class Mysql {
 
 			stat.executeUpdate();
 		}
-		return getId("League", leagueName, countryId);
+		return getId("League", leagueName, countryId, -1);
 	}
 
 	public int addCountry(String name) throws SQLException {
@@ -91,7 +91,7 @@ public class Mysql {
 			stat.executeUpdate();
 		}
 
-		return getId("Country", name, -1);
+		return getId("Country", name, -1, -1);
 	}
 
 	public int addSport(String sport) throws SQLException {
@@ -103,19 +103,27 @@ public class Mysql {
 			stat.setString(2, sport);
 			stat.executeUpdate();
 		}
-		return getId("Sport", sport, -1);
+		return getId("Sport", sport, -1, -1);
 	}
 
-	private int getId(String table, String name, int countryId) throws SQLException {
+	private int getId(String table, String name, int countryId, int leagueId) throws SQLException {
 		String sql = "SELECT id FROM " + table + " WHERE name = ?";
 		int id = -1;
 		if (countryId > -1) {
 			sql += " AND countryId = ?";
 		}
+		if (leagueId > -1) {
+			sql += " AND leagueId = ?";
+		}
 		try (PreparedStatement stat = conn.prepareStatement(sql)) {
 			stat.setString(1, name.trim());
-			if (countryId > -1) {
+			if (countryId > -1 && leagueId > -1) {
+				stat.setInt(2, countryId);
+				stat.setInt(3, leagueId);
+			} else if (countryId > -1) {
 				stat.setInt(2, countryId);
+			} else if (leagueId > -1) {
+				stat.setInt(2, leagueId);
 			}
 			final ResultSet insertRs = stat.executeQuery();
 			if (insertRs.next()) {
@@ -245,20 +253,27 @@ public class Mysql {
 
 	private int getOrInsertTeam(String teamName, int countryId, int leagueId, int sportId) throws SQLException {
 		teamName = teamName.replace('\u00A0', ' ').trim();
-		int teamId = getId("Team", teamName, countryId);
-		if (teamId <= 0) {
-			final String insertSql = "INSERT INTO Team (name, sportId, countryId, leagueId) VALUES (? ,? ,? ,?)";
-			try (PreparedStatement stat = conn.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS)) {
-				stat.setString(1, teamName.trim());
-				stat.setInt(2, sportId);
-				stat.setInt(3, countryId);
-				stat.setInt(4, leagueId);
-
-				stat.executeUpdate();
-				final ResultSet generatedKeys = stat.getGeneratedKeys();
-				generatedKeys.next();
-				teamId = generatedKeys.getInt(1);
+		int teamId;
+		if (leagueId > 0) {
+
+			teamId = getId("Team", teamName, countryId, leagueId);
+			if (teamId <= 0) {
+				final String insertSql = "INSERT INTO Team (name, sportId, countryId, leagueId) VALUES (? ,? ,? ,?)";
+				try (PreparedStatement stat = conn.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS)) {
+					stat.setString(1, teamName.trim());
+					stat.setInt(2, sportId);
+					stat.setInt(3, countryId);
+					stat.setInt(4, leagueId);
+
+					stat.executeUpdate();
+					final ResultSet generatedKeys = stat.getGeneratedKeys();
+					generatedKeys.next();
+					teamId = generatedKeys.getInt(1);
+				}
 			}
+		} else {
+			Log.getLog().info("No league Specified for " + teamName + " with countryId " + countryId);
+			teamId = -1;
 		}
 
 		return teamId;

+ 5 - 1
Odds/src/parser/OddsPortal.java

@@ -251,6 +251,7 @@ public class OddsPortal implements ParserJoinedFunctions {
 		} catch (FailingHttpStatusCodeException | IOException | SQLException e) {
 			e.printStackTrace();
 		} catch (final ClassCastException cce) {
+			Log.getLog().info("Class cast exception message: " + cce.getMessage() + " \ncause: " + cce.getCause());
 		} finally {
 			Mysql.getInstance().setParsingForLeague(leagueId, sportId, countryId, gameDate, currentParsePage, year);
 		}
@@ -310,7 +311,7 @@ public class OddsPortal implements ParserJoinedFunctions {
 							} else if (odds2 == 0F) {
 								odds2 = convertAmericanOddsToDecimal(Integer.valueOf(tc.asNormalizedText()));
 							}
-						} else if (tc.asNormalizedText().matches("[0-9].[0-9]+")) {
+						} else if (tc.asNormalizedText().matches("[0-9]+.[0-9]+")) {
 							if (odds1 == 0F) {
 								odds1 = Float.valueOf(tc.asNormalizedText());
 							} else if (oddsX == 0F) {
@@ -326,6 +327,9 @@ public class OddsPortal implements ParserJoinedFunctions {
 						&& !Strings.isNullOrEmpty(season)) { // All set.. update sql result table
 					Mysql.getInstance().addResult(new ResultDTO("SoccerResults", gameDate, homeTeam, awayTeam, homeScore, awayScore, overtime, odds1,
 							oddsX, odds2, countryId, season, leagueId, sportId));
+				} else {
+					Log.getLog().info(String.format("Failed to insert result %s %s %s odds %s %s %s season %s", gameDate, homeTeam, awayTeam, odds1,
+							oddsX, odds2, season));
 				}
 
 			} else if (tr.getAttribute(CLASS).contains("center nob-border")) { // Datum rader

+ 1 - 1
OddsJavaFx/bin/fxml/OddsFxBuilder.fxml

@@ -103,7 +103,7 @@
                     <fx:include fx:id="statTab" source="StatisticsTabBuilder.fxml" />
                   </content>
                 </Tab>
-                <Tab text="Match Statistics">
+                <Tab text="Standings Table">
                 	<content>
  						<fx:include fx:id="matchStatTab" source="MatchStatTabBuilder.fxml" />               	
                 	</content>

+ 18 - 23
OddsJavaFx/src/controllers/MainController.java

@@ -1,8 +1,10 @@
 package controllers;
 
 import java.net.URL;
+import java.text.SimpleDateFormat;
 import java.time.LocalDate;
 import java.util.AbstractMap.SimpleEntry;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -80,8 +82,8 @@ public class MainController implements Initializable {
 			}
 		}
 
-		matchTabController.getTable().getSelectionModel().selectedItemProperty().addListener(
-				(obs, oldSelection, newSelection) -> showMatchStatsButton.setDisable(newSelection == null));
+		matchTabController.getTable().getSelectionModel().selectedItemProperty()
+				.addListener((obs, oldSelection, newSelection) -> showMatchStatsButton.setDisable(newSelection == null));
 
 		sportSelector.setItems(sports);
 		countrySelector.setItems(countries);
@@ -100,10 +102,8 @@ public class MainController implements Initializable {
 				homeTeams.add(soccerMatch.getHomeTeam().getTeamName());
 				awayTeams.add(soccerMatch.getAwayTeam().getTeamName());
 
-				if (countriesList.stream()
-						.noneMatch(p -> p != null && p.getValue().equals(soccerMatch.getHomeTeam().getCountryName()))) {
-					countriesList.add(new SimpleEntry<>(soccerMatch.getHomeTeam().getCountryId(),
-							soccerMatch.getHomeTeam().getCountryName()));
+				if (countriesList.stream().noneMatch(p -> p != null && p.getValue().equals(soccerMatch.getHomeTeam().getCountryName()))) {
+					countriesList.add(new SimpleEntry<>(soccerMatch.getHomeTeam().getCountryId(), soccerMatch.getHomeTeam().getCountryName()));
 				}
 			}
 
@@ -118,8 +118,8 @@ public class MainController implements Initializable {
 		if (countrySelector.getValue() != null && !countrySelector.getValue().equals("Country")) {
 			leagueSelector.setDisable(false);
 			final int countryId = getCountryIdFromSelector();
-			Optional<SimpleEntry<Integer, String>> selectedSport = sportsList.stream()
-					.filter(p -> p.getValue().equals(sportSelector.getValue())).findFirst();
+			Optional<SimpleEntry<Integer, String>> selectedSport = sportsList.stream().filter(p -> p.getValue().equals(sportSelector.getValue()))
+					.findFirst();
 			if (selectedSport.isPresent()) {
 				final int sportId = selectedSport.get().getKey();
 				matchTabController.filterCountryMatches(countrySelector.getValue());
@@ -140,14 +140,12 @@ public class MainController implements Initializable {
 	}
 
 	private Integer getCountryIdFromSelector() {
-		Optional<SimpleEntry<Integer, String>> o = countriesList.stream()
-				.filter(p -> p.getValue().equals(countrySelector.getValue())).findFirst();
+		Optional<SimpleEntry<Integer, String>> o = countriesList.stream().filter(p -> p.getValue().equals(countrySelector.getValue())).findFirst();
 		return o.isPresent() ? o.get().getKey() : null;
 	}
 
 	private Integer getLeagueIdFromSelector() {
-		Optional<SimpleEntry<Integer, String>> o = leaguesList.stream()
-				.filter(p -> p.getValue().equals(leagueSelector.getValue())).findFirst();
+		Optional<SimpleEntry<Integer, String>> o = leaguesList.stream().filter(p -> p.getValue().equals(leagueSelector.getValue())).findFirst();
 		return o.isPresent() ? o.get().getKey() : null;
 	}
 
@@ -167,14 +165,13 @@ public class MainController implements Initializable {
 			}
 			matchTabController.filterLeagueMatches(leagueSelector.getValue(), getCountryIdFromSelector());
 			updateStatsTableButton.setDisable(false);
-			final List<Float> leagueAvareges = GuiMysql.getInstance().getLeagueAvarages(getLeagueIdFromSelector(),
-					getCountryIdFromSelector());
+			final List<Float> leagueAvareges = GuiMysql.getInstance().getLeagueAvarages(getLeagueIdFromSelector(), getCountryIdFromSelector());
 
 			avgLeagueHomeScoreTxt.setText(leagueAvareges.get(0).toString());
 			avgLeagueAwayScoreTxt.setText(leagueAvareges.get(1).toString());
 
-			Map<Integer, Integer> goalAvgTheSeason = GuiMysql.getInstance()
-					.getGoalAvgThisSeason(getLeagueIdFromSelector(), getCountryIdFromSelector());
+			Map<Integer, Integer> goalAvgTheSeason = GuiMysql.getInstance().getGoalAvgThisSeason(getLeagueIdFromSelector(),
+					getCountryIdFromSelector());
 
 			league0Goals.setText(goalAvgTheSeason.getOrDefault(0, 0).toString());
 			league1Goals.setText(goalAvgTheSeason.getOrDefault(1, 0).toString());
@@ -222,8 +219,7 @@ public class MainController implements Initializable {
 		glass.setVisible(true);
 		glass.setOpacity(0.8);
 		final OddsPortal op = new OddsPortal();
-		op.getHistoricMatches(sportSelector.getValue().trim(), country, league,
-				String.valueOf(LocalDate.now().getYear()));
+		op.getHistoricMatches(sportSelector.getValue().trim(), country, league, String.valueOf(LocalDate.now().getYear()));
 		glass.setOpacity(0.0);
 		glass.setVisible(false);
 		glass.setDisable(true);
@@ -234,8 +230,7 @@ public class MainController implements Initializable {
 	}
 
 	@FXML private void getMoreLeagueInfo() {
-		final String lastParsedYear = GuiMysql.getInstance().getLastParsedYear(leagueSelector.getValue().trim(),
-				getCountryIdFromSelector());
+		final String lastParsedYear = GuiMysql.getInstance().getLastParsedYear(leagueSelector.getValue().trim(), getCountryIdFromSelector());
 		final String nextParseYear;
 		if (lastParsedYear.contains("-")) {
 			final String[] years = lastParsedYear.split("-");
@@ -261,8 +256,8 @@ public class MainController implements Initializable {
 	}
 
 	@FXML private void setMatchStatInfo() {
-		final String season = GuiMysql.getInstance().getLastSeason(getCountryIdFromSelector(),
-				getLeagueIdFromSelector());
-		matchStatTabController.setLeagueStandingsTable(getLeagueIdFromSelector(), season, getCountryIdFromSelector());
+		final String season = GuiMysql.getInstance().getLastSeason(getCountryIdFromSelector(), getLeagueIdFromSelector());
+		matchStatTabController.setLeagueStandingsTable(getLeagueIdFromSelector(), season, getCountryIdFromSelector(),
+				new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
 	}
 }

+ 17 - 2
OddsJavaFx/src/controllers/MatchStatTabController.java

@@ -25,6 +25,8 @@ import objects.TeamStanding;
 
 	private static final String GAMES_PLAYED = "gamesPlayed";
 
+	private static MatchStatTabController instance;
+
 	@FXML TableView<Map<String, Object>> matchStatTable;
 
 	@FXML TableColumn<Map, String> team = new TableColumn<>("team");
@@ -37,6 +39,17 @@ import objects.TeamStanding;
 	@FXML TableColumn<Map, Float> goalsConceded = new TableColumn<>(GOALS_CONCEDED);
 	@FXML TableColumn<Map, Float> diff = new TableColumn<>("diff");
 
+	public static MatchStatTabController getInstance() {
+		if (instance == null) {
+			synchronized (MatchStatTabController.class) {
+				if (instance == null) {
+					instance = new MatchStatTabController();
+				}
+			}
+		}
+		return instance;
+	}
+
 	@Override public void initialize(URL arg0, ResourceBundle arg1) {
 		team.setCellValueFactory(new MapValueFactory<>("team"));
 		gamesPlayed.setCellValueFactory(new MapValueFactory<>(GAMES_PLAYED));
@@ -47,10 +60,12 @@ import objects.TeamStanding;
 		goalsScored.setCellValueFactory(new MapValueFactory<>(GOALS_SCORED));
 		goalsConceded.setCellValueFactory(new MapValueFactory<>(GOALS_CONCEDED));
 		diff.setCellValueFactory(new MapValueFactory<>("diff"));
+
+		instance = this;
 	}
 
-	public void setLeagueStandingsTable(int leagueId, String season, int countryId) {
-		final List<TeamStanding> res = GuiMysql.getInstance().getLeagueTable(leagueId, season, countryId);
+	public void setLeagueStandingsTable(int leagueId, String season, int countryId, String date) {
+		final List<TeamStanding> res = GuiMysql.getInstance().getLeagueTable(leagueId, season, countryId, date);
 		matchStatTable.getItems().clear();
 		for (final TeamStanding ts : res) {
 			final Map<String, Object> line = Maps.newHashMap();

+ 14 - 5
OddsJavaFx/src/controllers/PastResultsController.java

@@ -127,11 +127,13 @@ import objects.SoccerMatch;
 	}
 
 	private void updateMatchStats(Map<String, Object> newSelection) {
-		Map<String, String> previousMatches = db.getPreviousMatches(5, newSelection.get("homeTeam").toString(),
-				newSelection.get("awayTeam").toString(), date.getValue().toString(), getCountryIdFromSelector(), getLeagueIdFromSelector());
-		homeTeamPastResultsText.setText(previousMatches.get("PrevHomeTeam"));
-		awayTeamPastResultsText.setText(previousMatches.get("PrevAwayTeam"));
-		previousMeetingsText.setText(previousMatches.get("PrevCombined"));
+		if (newSelection != null) {
+			Map<String, String> previousMatches = db.getPreviousMatches(5, newSelection.get("homeTeam").toString(),
+					newSelection.get("awayTeam").toString(), date.getValue().toString(), getCountryIdFromSelector(), getLeagueIdFromSelector());
+			homeTeamPastResultsText.setText(previousMatches.get("PrevHomeTeam"));
+			awayTeamPastResultsText.setText(previousMatches.get("PrevAwayTeam"));
+			previousMeetingsText.setText(previousMatches.get("PrevCombined"));
+		}
 	}
 
 	private void clear() {
@@ -262,4 +264,11 @@ import objects.SoccerMatch;
 	@FXML private void updateStatsTable() {
 		StatisticsTabController.getInstance().setOverUnderTable(getLeagueIdFromSelector(), date.getValue().toString());
 	}
+
+	@FXML private void updateStandingsTable() {
+		String seasonFromDate = GuiMysql.getInstance().getSeasonFromDate(getCountryIdFromSelector(), getLeagueIdFromSelector(),
+				date.getValue().toString());
+		MatchStatTabController.getInstance().setLeagueStandingsTable(getLeagueIdFromSelector(), seasonFromDate, getCountryIdFromSelector(),
+				date.getValue().toString());
+	}
 }

+ 6 - 5
OddsJavaFx/src/data/GuiMysql.java

@@ -217,7 +217,7 @@ public class GuiMysql extends Mysql {
 		return returnValue;
 	}
 
-	private String getSeasonFromDate(int countryId, int leagueId, String gameDate) {
+	public String getSeasonFromDate(int countryId, int leagueId, String gameDate) {
 		String sql = "SELECT season FROM SoccerResults WHERE DATE(gameDate) = ? AND countryId = ? AND leagueId = ? LIMIT 1";
 		String returnValue = "";
 		try (PreparedStatement stat = conn.prepareStatement(sql)) {
@@ -429,7 +429,7 @@ public class GuiMysql extends Mysql {
 		return INCREMENT;
 	}
 
-	public List<TeamStanding> getLeagueTable(int leagueId, String season, int countryId) {
+	public List<TeamStanding> getLeagueTable(int leagueId, String season, int countryId, String date) {
 		final ArrayList<TeamStanding> result = Lists.newArrayList();
 
 		final String sql = "SELECT teamName.name as teamName, count(*) played, count(case when homeScore > awayScore then 1 end) wins, "
@@ -437,15 +437,16 @@ public class GuiMysql extends Mysql {
 				+ "sum(homeScore) homeScore, " + "sum(awayScore) awayScore, " + "sum(homeScore) - sum(awayScore) goal_diff, "
 				+ "sum(case when homeScore > awayScore then 3 else 0 end  + case " + "WHEN homeScore = awayScore then 1 else 0 end) score, "
 				+ "season FROM "
-				+ "(select hometeamId team, homeScore, awayScore, season, leagueId as league, countryId as country FROM SoccerResults "
-				+ "union all SELECT awayteamId, awayScore, homeScore, season, leagueId as league, countryId as country FROM SoccerResults) a "
+				+ "(select hometeamId team, homeScore, awayScore, season, gameDate, leagueId as league, countryId as country FROM SoccerResults "
+				+ "union all SELECT awayteamId, awayScore, homeScore, season, gameDate, leagueId as league, countryId as country FROM SoccerResults) a "
 				+ "INNER JOIN Team teamName ON team = teamName.id " + "WHERE season = ? " + "AND league = ? " + "AND country = ? "
-				+ "AND homeScore != -1 " + "AND awayScore != -1 " + "group by team " + "order by score desc, goal_diff desc";
+				+ "AND homeScore != -1 " + "AND awayScore != -1 AND DATE(gameDate) < DATE(?) group by team " + "order by score desc, goal_diff desc";
 		try (PreparedStatement stat = conn.prepareStatement(sql)) {
 
 			stat.setString(1, season);
 			stat.setInt(2, leagueId);
 			stat.setInt(3, countryId);
+			stat.setString(4, date);
 
 			final ResultSet rs = stat.executeQuery();
 			while (rs.next()) {

+ 1 - 1
OddsJavaFx/src/fxml/MatchTabBuilder.fxml

@@ -45,7 +45,7 @@
                   <HBox prefHeight="23.0" prefWidth="998.0">
                      <children>
                         <Button mnemonicParsing="false" onAction="#addRowAction" text="Add New Row" />
-                        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Över Score 5,8 och båda lagen har över 1 i score concede" wrappingWidth="303.486328125" />
+                        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Över 1.5 om score &gt; 5 och under 4.5 om score &lt; 3" wrappingWidth="303.486328125" />
                      </children>
                   </HBox>
                

+ 1 - 0
OddsJavaFx/src/fxml/PastResults.fxml

@@ -26,6 +26,7 @@
             		    <ComboBox id="CountryComboBox" fx:id="countrySelector" onAction="#countrySelected" prefHeight="25.0" prefWidth="200.0" promptText="Country" />
                         <ComboBox id="LeagueComboBox" fx:id="leagueSelector" disable="true" onAction="#leagueSelected" prefHeight="25.0" prefWidth="200.0" promptText="League" />
                         <Button mnemonicParsing="false" onAction="#updateStatsTable" text="Update Statistics" />
+                        <Button mnemonicParsing="false" onAction="#updateStandingsTable" text="Update standings table" />
                      </children>
                   </FlowPane>
                </children>