TestsController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package controllers;
  2. import com.google.common.base.Strings;
  3. import com.google.common.collect.Lists;
  4. import data.GuiMysql;
  5. import javafx.application.Platform;
  6. import javafx.collections.FXCollections;
  7. import javafx.collections.ObservableList;
  8. import javafx.event.ActionEvent;
  9. import javafx.fxml.FXML;
  10. import javafx.fxml.Initializable;
  11. import javafx.scene.control.*;
  12. import javafx.scene.layout.AnchorPane;
  13. import javafx.scene.layout.Priority;
  14. import javafx.scene.layout.VBox;
  15. import objects.League;
  16. import org.eclipse.jetty.util.log.Log;
  17. import parser.OddsPortal;
  18. import tests.*;
  19. import java.net.URL;
  20. import java.time.LocalDate;
  21. import java.util.AbstractMap.SimpleEntry;
  22. import java.util.List;
  23. import java.util.Optional;
  24. import java.util.ResourceBundle;
  25. public class TestsController implements Initializable {
  26. @FXML
  27. AnchorPane basePane;
  28. @FXML
  29. AnchorPane testSettingsPanel;
  30. @FXML
  31. AnchorPane testResultsPanel;
  32. @FXML
  33. Button getResults;
  34. @FXML
  35. ScrollPane resultsPanel;
  36. @FXML
  37. DatePicker date;
  38. @FXML
  39. ComboBox<String> countrySelector;
  40. @FXML
  41. ComboBox<String> leagueSelector;
  42. @FXML
  43. TextField bettingLevel;
  44. @FXML
  45. TextField startingBank;
  46. @FXML
  47. TextField lookBack;
  48. @FXML
  49. TextField betMargin;
  50. @FXML
  51. Button calcBestValues;
  52. @FXML
  53. Button getLeagueInfoButton;
  54. @FXML
  55. Button getMoreLeagueInfoButton;
  56. @FXML
  57. Button topLeaguesTest;
  58. @FXML
  59. Button homeTest;
  60. @FXML
  61. Button drawTest;
  62. @FXML
  63. Button awayTest;
  64. @FXML
  65. Button homeDrawAwayTest;
  66. @FXML
  67. Button homeTeamTest;
  68. @FXML
  69. Button awayTeamTest;
  70. @FXML
  71. Button addedScoreTest;
  72. @FXML
  73. Button recalcTest;
  74. @FXML
  75. Button fibonacciDrawTest;
  76. @FXML
  77. VBox testPanelVBox;
  78. ObservableList<String> countries = FXCollections.observableArrayList();
  79. ObservableList<String> leagues = FXCollections.observableArrayList();
  80. private final List<SimpleEntry<Integer, String>> countriesList = Lists.newArrayList();
  81. private List<SimpleEntry<Integer, String>> leaguesList = Lists.newArrayList();
  82. private int mSportId;
  83. @Override
  84. public void initialize(URL arg0, ResourceBundle arg1) {
  85. getResults.setDisable(true);
  86. basePane.setPrefWidth(0.0);
  87. basePane.setPrefHeight(0.0);
  88. VBox.setVgrow(testPanelVBox, Priority.ALWAYS);
  89. date.valueProperty()
  90. .addListener((ov, oldValue, newValue) -> getResults.setDisable(newValue.isAfter(LocalDate.now())));
  91. countrySelector.setItems(countries);
  92. leagueSelector.setItems(leagues);
  93. testSettingsPanel.setVisible(false);
  94. }
  95. @FXML
  96. void getLeagueInfo() {
  97. final String country = countrySelector.getValue().trim();
  98. final String league = leagueSelector.getValue().trim();
  99. final OddsPortal op = new OddsPortal();
  100. op.getHistoricMatches("soccer", country, league, String.valueOf(LocalDate.now().getYear()));
  101. }
  102. @FXML
  103. private void addedScoreTestAction() {
  104. final AddedScoringTest test = new AddedScoringTest();
  105. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()),
  106. Integer.valueOf(getLookback()),
  107. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  108. test.runTest();
  109. }
  110. @FXML
  111. private void analysisDrawTests() {
  112. final TestClass test = new AnalysisBettDrawTest();
  113. test.setup(date.getValue().toString(), Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), -1.0f,
  114. -1, GuiMysql.getInstance().getSportId("soccer"),
  115. getCountryIdFromSelector(), getLeagueIdFromSelector());
  116. test.runTest();
  117. }
  118. @FXML
  119. private void analysisTets() {
  120. final TestClass test = new AnalysisBettTest();
  121. test.setup(date.getValue().toString(), Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), -1.0f,
  122. -1, GuiMysql.getInstance().getSportId("soccer"),
  123. getCountryIdFromSelector(), getLeagueIdFromSelector());
  124. test.runTest();
  125. }
  126. @FXML
  127. private void awayTeamTestAction() {
  128. final AwayTeamWinTest test = new AwayTeamWinTest();
  129. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()),
  130. Integer.valueOf(getLookback()),
  131. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  132. test.runTest();
  133. }
  134. @FXML
  135. private void awayTestAction() {
  136. final Away2Test test = new Away2Test();
  137. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()),
  138. Integer.valueOf(getLookback()),
  139. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  140. test.runTest();
  141. }
  142. @FXML
  143. private void calcBestResultsAction() {
  144. final LastResultsTest test = new LastResultsTest();
  145. calcBestValues.setDisable(true);
  146. final String buttonText = calcBestValues.getText();
  147. Platform.runLater(() -> {
  148. calcBestValues.setText("RUNNING...");
  149. test.calcBestResults(mSportId, getCountryIdFromSelector(), getLeagueIdFromSelector());
  150. calcBestValues.setDisable(false);
  151. calcBestValues.setText(buttonText);
  152. });
  153. }
  154. @FXML
  155. private void countrySelected(ActionEvent event) {
  156. if (countrySelector.getValue() != null && !countrySelector.getValue().equals("Country")) {
  157. leagueSelector.setDisable(false);
  158. final int countryId = getCountryIdFromSelector();
  159. final int sportId = getSoccerId();
  160. leaguesList = GuiMysql.getInstance().getLeaguesByDate(sportId, countryId, date.getValue().toString());
  161. leagues.clear();
  162. leagues.add("League");
  163. leaguesList.forEach(l -> leagues.add(l.getValue()));
  164. }
  165. }
  166. @FXML
  167. private void drawTestAction() {
  168. final Draw2Tests test = new Draw2Tests();
  169. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()),
  170. Integer.valueOf(getLookback()),
  171. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  172. test.runTest();
  173. }
  174. @FXML
  175. private void fibonacciDrawTestAction() {
  176. final TestClass test = new FibonacciDrawTest();
  177. test.setup(date.getValue().toString(), Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), -1.0f,
  178. -1, GuiMysql.getInstance().getSportId("soccer"),
  179. getCountryIdFromSelector(), getLeagueIdFromSelector());
  180. test.runTest();
  181. }
  182. private String getBetLevel() {
  183. String betLevel = Strings.isNullOrEmpty(bettingLevel.getText()) ? bettingLevel.getPromptText()
  184. : bettingLevel.getText();
  185. betLevel = betLevel.replace("%", "").trim();
  186. return betLevel;
  187. }
  188. private String getBetMargin() {
  189. String betM = Strings.isNullOrEmpty(betMargin.getText()) ? betMargin.getPromptText() : betMargin.getText();
  190. betM = betM.replace("%", "").trim();
  191. return betM;
  192. }
  193. private Integer getCountryIdFromSelector() {
  194. Optional<SimpleEntry<Integer, String>> o = countriesList.stream()
  195. .filter(p -> p.getValue().equals(countrySelector.getValue())).findFirst();
  196. return o.isPresent() ? o.get().getKey() : null;
  197. }
  198. private Integer getLeagueIdFromSelector() {
  199. Optional<SimpleEntry<Integer, String>> o = leaguesList.stream()
  200. .filter(p -> p.getValue().equals(leagueSelector.getValue())).findFirst();
  201. return o.isPresent() ? o.get().getKey() : null;
  202. }
  203. private String getLookback() {
  204. return Strings.isNullOrEmpty(lookBack.getText()) ? lookBack.getPromptText() : lookBack.getText();
  205. }
  206. @FXML
  207. private void getMoreLeagueInfo() {
  208. final String lastParsedYear = GuiMysql.getInstance().getLastParsedYear(leagueSelector.getValue().trim(),
  209. getCountryIdFromSelector());
  210. final String nextParseYear;
  211. if (lastParsedYear.contains("-")) {
  212. final String[] years = lastParsedYear.split("-");
  213. int firstYear = Integer.parseInt(years[0]);
  214. int lastYear = Integer.parseInt(years[1]);
  215. firstYear--;
  216. lastYear--;
  217. nextParseYear = firstYear + "-" + lastYear;
  218. } else if (lastParsedYear.length() == 4) {
  219. Integer val = Integer.valueOf(lastParsedYear);
  220. val--;
  221. nextParseYear = String.valueOf(val);
  222. } else {
  223. Log.getLog().info("Fail to get More league info for %s", lastParsedYear);
  224. return;
  225. }
  226. final String country = countrySelector.getValue().trim();
  227. final String league = leagueSelector.getValue().trim();
  228. final OddsPortal op = new OddsPortal();
  229. op.getHistoricMatches("soccer", country, league, nextParseYear);
  230. }
  231. @FXML
  232. private void getResults() {
  233. countriesList.clear();
  234. leaguesList.clear();
  235. initCountries();
  236. }
  237. private int getSoccerId() {
  238. if (mSportId <= 0) {
  239. mSportId = GuiMysql.getInstance().getSportId(MainController.SPORT);
  240. }
  241. return mSportId;
  242. }
  243. private String getStartingBank() {
  244. return Strings.isNullOrEmpty(startingBank.getText()) ? startingBank.getPromptText() : startingBank.getText();
  245. }
  246. @FXML
  247. private void goalDiffTest() {
  248. final TestClass test = new BetOnDifferenceTest();
  249. test.setup(date.getValue().toString(), Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), -1.0f,
  250. -1, GuiMysql.getInstance().getSportId("soccer"),
  251. getCountryIdFromSelector(), getLeagueIdFromSelector());
  252. test.runTest();
  253. }
  254. @FXML
  255. private void homeDrawAwayTestAction() {
  256. final HomeDrawAwayTest test = new HomeDrawAwayTest();
  257. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()),
  258. Integer.valueOf(getLookback()),
  259. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  260. test.runTest();
  261. }
  262. @FXML
  263. private void homeTeamTestAction() {
  264. final HomeTeamWinTest test = new HomeTeamWinTest();
  265. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()),
  266. Integer.valueOf(getLookback()),
  267. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  268. test.runTest();
  269. }
  270. @FXML
  271. private void homeTestAction() {
  272. final Home2Test test = new Home2Test();
  273. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()),
  274. Integer.valueOf(getLookback()),
  275. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  276. test.runTest();
  277. }
  278. private void initCountries() {
  279. countrySelector.setDisable(false);
  280. final int sportId = getSoccerId();
  281. countriesList.clear();
  282. countriesList.addAll(GuiMysql.getInstance().getCountriesBySport(sportId, date.getValue().toString()));
  283. countriesList.forEach(c -> countries.add(c.getValue()));
  284. }
  285. @FXML
  286. private void leagueSelected(ActionEvent event) {
  287. if (GuiMysql.getInstance().getParsingStarted(getCountryIdFromSelector(), getLeagueIdFromSelector())) {
  288. getLeagueInfoButton.setDisable(true);
  289. getMoreLeagueInfoButton.setDisable(false);
  290. } else {
  291. getLeagueInfoButton.setDisable(false);
  292. getMoreLeagueInfoButton.setDisable(true);
  293. }
  294. testSettingsPanel.setVisible(true);
  295. }
  296. @FXML
  297. private void recalcTestAction() {
  298. final recalcTest test = new recalcTest();
  299. test.setup("", Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), Float.valueOf(getBetMargin()),
  300. Integer.valueOf(getLookback()),
  301. 1, getCountryIdFromSelector(), getLeagueIdFromSelector());
  302. test.runTest();
  303. }
  304. @FXML
  305. private void relevanceTest() {
  306. final TestClass test = new RelevanceTest();
  307. test.setup(date.getValue().toString(), Float.valueOf(getStartingBank()), Float.valueOf(getBetLevel()), -1.0f,
  308. -1, GuiMysql.getInstance().getSportId("soccer"),
  309. getCountryIdFromSelector(), getLeagueIdFromSelector());
  310. test.runTest();
  311. }
  312. @FXML
  313. private void runTestAction() {
  314. final String betLevel = getBetLevel();
  315. final String betM = getBetMargin();
  316. final String startBank = getStartingBank();
  317. final String lookBackVal = getLookback();
  318. final TestClass test = new LastResultsTest();
  319. test.setup(date.getValue().toString(), Float.valueOf(startBank), Float.valueOf(betLevel), Float.valueOf(betM),
  320. Integer.valueOf(lookBackVal),
  321. mSportId, getCountryIdFromSelector(), getLeagueIdFromSelector());
  322. test.runTest();
  323. }
  324. @FXML
  325. private void standingsTest() {
  326. final TestClass test = new LeagueTablePositionTest();
  327. test.runTest();
  328. }
  329. @FXML
  330. private void topLeaguesTestAction() {
  331. final PrioCountriesAllTest prioCountriesAll = new PrioCountriesAllTest();
  332. prioCountriesAll.runTest();
  333. }
  334. @FXML
  335. private void updatePrioLeagues() {
  336. List<League> leaguesWithPrio = GuiMysql.getInstance().getLeaguesWithPrio();
  337. for (League league : leaguesWithPrio) {
  338. long startTime = System.currentTimeMillis();
  339. System.out.println("Starting with " + league.getLeagueName() + " " + new java.util.Date());
  340. RelevanceTest test = new RelevanceTest();
  341. test.setup("", -1, -1, 0.0f, -1, -1, -1, league.getLeagueId());
  342. test.runTest();
  343. long endTime = System.currentTimeMillis();
  344. System.out.println("Done with " + league.getLeagueName() + " " + new java.util.Date()
  345. + " took " + new java.util.Date(System.currentTimeMillis() + (endTime - startTime)));
  346. }
  347. }
  348. public void runXBestGamesTest(ActionEvent actionEvent) {
  349. XBestRatedGamesBettingTest xBestRatedGamesBettingTest = new XBestRatedGamesBettingTest();
  350. xBestRatedGamesBettingTest.runTest();
  351. }
  352. }