TestsController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package controllers;
  2. import java.net.URL;
  3. import java.time.LocalDate;
  4. import java.util.AbstractMap.SimpleEntry;
  5. import java.util.List;
  6. import java.util.Optional;
  7. import java.util.ResourceBundle;
  8. import org.eclipse.jetty.util.log.Log;
  9. import com.google.common.base.Strings;
  10. import com.google.common.collect.Lists;
  11. import data.GuiMysql;
  12. import javafx.application.Platform;
  13. import javafx.collections.FXCollections;
  14. import javafx.collections.ObservableList;
  15. import javafx.event.ActionEvent;
  16. import javafx.fxml.FXML;
  17. import javafx.fxml.Initializable;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.ComboBox;
  20. import javafx.scene.control.DatePicker;
  21. import javafx.scene.control.ScrollPane;
  22. import javafx.scene.control.TextField;
  23. import javafx.scene.layout.AnchorPane;
  24. import parser.OddsPortal;
  25. import tests.AddedScoringTest;
  26. import tests.AwayTeamWinTest;
  27. import tests.AwayTests2;
  28. import tests.DrawTests2;
  29. import tests.HomeDrawAwayTest;
  30. import tests.HomeTeamWinTest;
  31. import tests.HomeTests2;
  32. import tests.LastResultsTest;
  33. import tests.LeagueTablePositionTest;
  34. import tests.PrioCountriesAll;
  35. import tests.TestClass;
  36. import tests.recalcTest;
  37. public class TestsController implements Initializable {
  38. @FXML AnchorPane basePane;
  39. @FXML AnchorPane testSettingsPanel;
  40. @FXML AnchorPane testResultsPanel;
  41. @FXML Button getResults;
  42. @FXML ScrollPane resultsPanel;
  43. @FXML DatePicker date;
  44. @FXML ComboBox<String> countrySelector;
  45. @FXML ComboBox<String> leagueSelector;
  46. @FXML TextField bettingLevel;
  47. @FXML TextField startingBank;
  48. @FXML TextField lookBack;
  49. @FXML TextField betMargin;
  50. @FXML Button calcBestValues;
  51. @FXML Button getLeagueInfoButton;
  52. @FXML Button getMoreLeagueInfoButton;
  53. @FXML Button topLeaguesTest;
  54. @FXML Button homeTest;
  55. @FXML Button drawTest;
  56. @FXML Button awayTest;
  57. @FXML Button homeDrawAwayTest;
  58. @FXML Button homeTeamTest;
  59. @FXML Button awayTeamTest;
  60. @FXML Button addedScoreTest;
  61. @FXML Button recalcTest;
  62. ObservableList<String> countries = FXCollections.observableArrayList();
  63. ObservableList<String> leagues = FXCollections.observableArrayList();
  64. private final List<SimpleEntry<Integer, String>> countriesList = Lists.newArrayList();
  65. private List<SimpleEntry<Integer, String>> leaguesList = Lists.newArrayList();
  66. private int mSportId;
  67. @Override public void initialize(URL arg0, ResourceBundle arg1) {
  68. getResults.setDisable(true);
  69. basePane.setPrefWidth(0.0);
  70. basePane.setPrefHeight(0.0);
  71. date.valueProperty().addListener((ov, oldValue, newValue) -> getResults.setDisable(newValue.isAfter(LocalDate.now())));
  72. countrySelector.setItems(countries);
  73. leagueSelector.setItems(leagues);
  74. testSettingsPanel.setVisible(false);
  75. }
  76. @FXML private void getResults() {
  77. countriesList.clear();
  78. leaguesList.clear();
  79. initCountries();
  80. }
  81. @FXML private void countrySelected(ActionEvent event) {
  82. if (countrySelector.getValue() != null && !countrySelector.getValue().equals("Country")) {
  83. leagueSelector.setDisable(false);
  84. final int countryId = getCountryIdFromSelector();
  85. final int sportId = getSoccerId();
  86. leaguesList = GuiMysql.getInstance().getLeaguesByDate(sportId, countryId, date.getValue().toString());
  87. leagues.clear();
  88. leagues.add("League");
  89. leaguesList.forEach(l -> leagues.add(l.getValue()));
  90. }
  91. }
  92. private Integer getCountryIdFromSelector() {
  93. Optional<SimpleEntry<Integer, String>> o = countriesList.stream().filter(p -> p.getValue().equals(countrySelector.getValue())).findFirst();
  94. return o.isPresent() ? o.get().getKey() : null;
  95. }
  96. private Integer getLeagueIdFromSelector() {
  97. Optional<SimpleEntry<Integer, String>> o = leaguesList.stream().filter(p -> p.getValue().equals(leagueSelector.getValue())).findFirst();
  98. return o.isPresent() ? o.get().getKey() : null;
  99. }
  100. private void initCountries() {
  101. countrySelector.setDisable(false);
  102. final int sportId = getSoccerId();
  103. countriesList.clear();
  104. countriesList.addAll(GuiMysql.getInstance().getCountriesBySport(sportId, date.getValue().toString()));
  105. countriesList.forEach(c -> countries.add(c.getValue()));
  106. }
  107. private int getSoccerId() {
  108. if (mSportId <= 0) {
  109. mSportId = GuiMysql.getInstance().getSportId(MainController.SPORT);
  110. }
  111. return mSportId;
  112. }
  113. @FXML private void leagueSelected(ActionEvent event) {
  114. if (GuiMysql.getInstance().getParsingStarted(getCountryIdFromSelector(), getLeagueIdFromSelector())) {
  115. getLeagueInfoButton.setDisable(true);
  116. getMoreLeagueInfoButton.setDisable(false);
  117. } else {
  118. getLeagueInfoButton.setDisable(false);
  119. getMoreLeagueInfoButton.setDisable(true);
  120. }
  121. testSettingsPanel.setVisible(true);
  122. }
  123. @FXML private void runTestAction() {
  124. final String betLevel = getBetLevel();
  125. final String betM = getBetMargin();
  126. final String startBank = getStartingBank();
  127. final String lookBackVal = getLookback();
  128. final TestClass test = new LastResultsTest();
  129. test.setup(date.getValue().toString(), Float.valueOf(startBank), Float.valueOf(betLevel), Float.valueOf(betM), Integer.valueOf(lookBackVal),
  130. mSportId, getCountryIdFromSelector(), getLeagueIdFromSelector());
  131. test.runTest();
  132. }
  133. private String getLookback() {
  134. return Strings.isNullOrEmpty(lookBack.getText()) ? lookBack.getPromptText() : lookBack.getText();
  135. }
  136. private String getStartingBank() {
  137. return Strings.isNullOrEmpty(startingBank.getText()) ? startingBank.getPromptText() : startingBank.getText();
  138. }
  139. private String getBetMargin() {
  140. String betM = Strings.isNullOrEmpty(betMargin.getText()) ? betMargin.getPromptText() : betMargin.getText();
  141. betM = betM.replace("%", "").trim();
  142. return betM;
  143. }
  144. private String getBetLevel() {
  145. String betLevel = Strings.isNullOrEmpty(bettingLevel.getText()) ? bettingLevel.getPromptText() : bettingLevel.getText();
  146. betLevel = betLevel.replace("%", "").trim();
  147. return betLevel;
  148. }
  149. @FXML private void calcBestResultsAction() {
  150. final LastResultsTest test = new LastResultsTest();
  151. calcBestValues.setDisable(true);
  152. final String buttonText = calcBestValues.getText();
  153. Platform.runLater(() -> {
  154. calcBestValues.setText("RUNNING...");
  155. test.calcBestResults(mSportId, getCountryIdFromSelector(), getLeagueIdFromSelector());
  156. calcBestValues.setDisable(false);
  157. calcBestValues.setText(buttonText);
  158. });
  159. }
  160. @FXML void getLeagueInfo() {
  161. final String country = countrySelector.getValue().trim();
  162. final String league = leagueSelector.getValue().trim();
  163. final OddsPortal op = new OddsPortal();
  164. op.getHistoricMatches("soccer", country, league, String.valueOf(LocalDate.now().getYear()));
  165. }
  166. @FXML private void getMoreLeagueInfo() {
  167. final String lastParsedYear = GuiMysql.getInstance().getLastParsedYear(leagueSelector.getValue().trim(), getCountryIdFromSelector());
  168. final String nextParseYear;
  169. if (lastParsedYear.contains("-")) {
  170. final String[] years = lastParsedYear.split("-");
  171. int firstYear = Integer.parseInt(years[0]);
  172. int lastYear = Integer.parseInt(years[1]);
  173. firstYear--;
  174. lastYear--;
  175. nextParseYear = firstYear + "-" + lastYear;
  176. } else if (lastParsedYear.length() == 4) {
  177. Integer val = Integer.valueOf(lastParsedYear);
  178. val--;
  179. nextParseYear = String.valueOf(val);
  180. } else {
  181. Log.getLog().info("Fail to get More league info for %s", lastParsedYear);
  182. return;
  183. }
  184. final String country = countrySelector.getValue().trim();
  185. final String league = leagueSelector.getValue().trim();
  186. final OddsPortal op = new OddsPortal();
  187. op.getHistoricMatches("soccer", country, league, nextParseYear);
  188. }
  189. @FXML private void topLeaguesTestAction() {
  190. final PrioCountriesAll prioCountriesAll = new PrioCountriesAll();
  191. prioCountriesAll.runTest();
  192. }
  193. @FXML private void homeTestAction() {
  194. final HomeTests2 test = new HomeTests2();
  195. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()),
  196. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  197. test.runTest();
  198. }
  199. @FXML private void drawTestAction() {
  200. final DrawTests2 test = new DrawTests2();
  201. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()),
  202. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  203. test.runTest();
  204. }
  205. @FXML private void awayTestAction() {
  206. final AwayTests2 test = new AwayTests2();
  207. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()),
  208. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  209. test.runTest();
  210. }
  211. @FXML private void homeDrawAwayTestAction() {
  212. final HomeDrawAwayTest test = new HomeDrawAwayTest();
  213. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()),
  214. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  215. test.runTest();
  216. }
  217. @FXML private void homeTeamTestAction() {
  218. final HomeTeamWinTest test = new HomeTeamWinTest();
  219. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()),
  220. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  221. test.runTest();
  222. }
  223. @FXML private void awayTeamTestAction() {
  224. final AwayTeamWinTest test = new AwayTeamWinTest();
  225. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()),
  226. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  227. test.runTest();
  228. }
  229. @FXML private void addedScoreTestAction() {
  230. final AddedScoringTest test = new AddedScoringTest();
  231. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()),
  232. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  233. test.runTest();
  234. }
  235. @FXML private void recalcTestAction() {
  236. final recalcTest test = new recalcTest();
  237. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()), Integer.valueOf(getLookback()),
  238. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  239. test.runTest();
  240. }
  241. @FXML private void standingsTest() {
  242. final LeagueTablePositionTest test = new LeagueTablePositionTest();
  243. test.runTest();
  244. }
  245. }