diff --git a/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/controllers/MatchController.java b/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/controllers/MatchController.java index e676783..59422e7 100644 --- a/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/controllers/MatchController.java +++ b/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/controllers/MatchController.java @@ -2,15 +2,18 @@ package com.resultprediction.polishekstraklasa.Predictions.controllers; import com.resultprediction.polishekstraklasa.Predictions.model.match.Match; import com.resultprediction.polishekstraklasa.Predictions.model.match.MatchRepository; +import com.resultprediction.polishekstraklasa.Predictions.model.match.PredictService; import com.resultprediction.polishekstraklasa.Predictions.model.team.Team; import com.resultprediction.polishekstraklasa.Predictions.model.team.TeamRepository; import com.resultprediction.polishekstraklasa.Predictions.model.teamsMatchStatistics.TeamsMatchStatisticsRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -27,24 +30,33 @@ public class MatchController { @Autowired private TeamsMatchStatisticsRepository teamsMatchStatisticsRepository; + @Autowired + private PredictService predictService; + @PostMapping("") - public String create(@RequestBody List matches) { - for (Match match : matches){ - Optional team1 = teamRepository.findByName(match.getTeamsMatchStatistics1().getTeam().getName()); - if (team1.isPresent()) - match.getTeamsMatchStatistics1().setTeam(team1.get()); - else match.getTeamsMatchStatistics1().setTeam(teamRepository.save(new Team(null, match.getTeamsMatchStatistics1().getTeam().getName()))); + public String create(@RequestBody Match match) { + Optional team1 = teamRepository.findByName(match.getTeamsMatchStatistics1().getTeam().getName()); + if (team1.isPresent()) + match.getTeamsMatchStatistics1().setTeam(team1.get()); + else match.getTeamsMatchStatistics1().setTeam(teamRepository.save(new Team(null, match.getTeamsMatchStatistics1().getTeam().getName()))); - Optional team2 = teamRepository.findByName(match.getTeamsMatchStatistics2().getTeam().getName()); - if (team2.isPresent()) + Optional team2 = teamRepository.findByName(match.getTeamsMatchStatistics2().getTeam().getName()); + if (team2.isPresent()) match.getTeamsMatchStatistics2().setTeam(team2.get()); - else match.getTeamsMatchStatistics2().setTeam(teamRepository.save(new Team(null, match.getTeamsMatchStatistics2().getTeam().getName()))); - - match.setTeamsMatchStatistics1(teamsMatchStatisticsRepository.save(match.getTeamsMatchStatistics1())); - match.setTeamsMatchStatistics2(teamsMatchStatisticsRepository.save(match.getTeamsMatchStatistics2())); + else match.getTeamsMatchStatistics2().setTeam(teamRepository.save(new Team(null, match.getTeamsMatchStatistics2().getTeam().getName()))); + match.setTeamsMatchStatistics1(teamsMatchStatisticsRepository.save(match.getTeamsMatchStatistics1())); + match.setTeamsMatchStatistics2(teamsMatchStatisticsRepository.save(match.getTeamsMatchStatistics2())); + if (match.getMatchDate().getMonth() < 7){ + match.setSeason(match.getMatchDate().getYear() - 1 + 1900); + } else { + match.setSeason(match.getMatchDate().getYear() + 1900); } - matchRepository.saveAll(matches); + matchRepository.save(match); return "done"; } + @GetMapping("/predict") + public String predict() { + return predictService.predictMatches(); + } } diff --git a/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/Match.java b/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/Match.java index eaf48f8..40c0610 100644 --- a/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/Match.java +++ b/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/Match.java @@ -1,5 +1,6 @@ package com.resultprediction.polishekstraklasa.Predictions.model.match; +import com.fasterxml.jackson.annotation.JsonFormat; import com.resultprediction.polishekstraklasa.Predictions.model.teamsMatchStatistics.TeamsMatchStatistics; import lombok.AllArgsConstructor; import lombok.Getter; @@ -8,7 +9,7 @@ import lombok.Setter; import org.springframework.format.annotation.DateTimeFormat; import javax.persistence.*; -import java.sql.Date; +import java.util.Date; @NoArgsConstructor @@ -40,17 +41,17 @@ public class Match { @Getter @Setter - @DateTimeFormat(pattern = "dd-MM-yyyy") + @JsonFormat(pattern="dd-MM-yyyy") private Date matchDate; @Getter @Setter - private String season; + private Integer season; @Getter @Setter @Column(columnDefinition = "TINYINT(1)") - private int predicted_result;//{1,0,2} + private Integer predicted_result;//{1,0,2} @Getter @Setter diff --git a/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/MatchRepository.java b/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/MatchRepository.java index 7a6bcc4..724e8e3 100644 --- a/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/MatchRepository.java +++ b/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/MatchRepository.java @@ -2,5 +2,8 @@ package com.resultprediction.polishekstraklasa.Predictions.model.match; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface MatchRepository extends JpaRepository { + List findAllByOrderByMatchDateAsc(); } diff --git a/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/PredictService.java b/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/PredictService.java new file mode 100644 index 0000000..2116f02 --- /dev/null +++ b/Back/src/main/java/com/resultprediction/polishekstraklasa/Predictions/model/match/PredictService.java @@ -0,0 +1,23 @@ +package com.resultprediction.polishekstraklasa.Predictions.model.match; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class PredictService { + + + + @Autowired + private MatchRepository matchRepository; + + public String predictMatches(){ + + List matches = matchRepository.findAllByOrderByMatchDateAsc(); + + return "done"; + } + +} diff --git a/Data/Ekstraklasa_19_20/jsonsMatchesEndpoint/Team Stats Raków Częstochowa/Team Stats Raków Częstochowa AGAINST Zagłębie Lubin.json b/Data/Ekstraklasa_19_20/jsonsMatchesEndpoint/Team Stats Raków Częstochowa/Team Stats Raków Częstochowa AGAINST Zagłębie Lubin.json index 72d90fb..1bfae45 100644 --- a/Data/Ekstraklasa_19_20/jsonsMatchesEndpoint/Team Stats Raków Częstochowa/Team Stats Raków Częstochowa AGAINST Zagłębie Lubin.json +++ b/Data/Ekstraklasa_19_20/jsonsMatchesEndpoint/Team Stats Raków Częstochowa/Team Stats Raków Częstochowa AGAINST Zagłębie Lubin.json @@ -1 +1 @@ -{"result": 0, "matchDate": "10-11-2019", "teamsMatchStatistics1": {"team": {"name": "Raków Częstochowa"}, "formation": "5-3-2", "shootsOnTarget": 7, "possession": 31.42, "goals": 2, "red_cards": 0, "penaltyAreaEntries": 2, "penalties": 0, "goalkeeperSavesPercent": 66.66666666666667, "goals_lost": 2}, "teamsMatchStatistics2": {"team": {"name": "Zagłębie Lubin"}, "formation": "4-3-3", "shootsOnTarget": 6, "possession": 68.58, "goals": 2, "red_cards": 0, "penaltyAreaEntries": 1, "penalties": 0, "goalkeeperSavesPercent": 75.0, "goals_lost": 2}} \ No newline at end of file +{"result": 0, "matchDate": "10-11-2019", "teamsMatchStatistics1": {"team": {"name": "Raków Częstochowa"}, "formation": "5-3-2", "shootsOnTarget": 7, "possession": 31.42, "goals": 2, "red_cards": 0, "penaltyAreaEntries": 2, "penalties": 0, "goalkeeperSavesPercent": 66.66666666666667, "goals_lost": 2}, "teamsMatchStatistics2": {"team": {"name": "Zagłębie Lubin"}, "formation": "4-3-3", "shootsOnTarget": 6, "possession": 68.58, "goals": 2, "red_cards": 0, "penaltyAreaEntries": 1, "penalties": 0, "goalkeeperSavesPercent": 66.66666666666667, "goals_lost": 2}} \ No newline at end of file diff --git a/Data/Ekstraklasa_19_20/jsonsMatchesEndpoint/Team Stats Zagłębie Lubin/Team Stats Zagłębie Lubin AGAINST Raków Częstochowa.json b/Data/Ekstraklasa_19_20/jsonsMatchesEndpoint/Team Stats Zagłębie Lubin/Team Stats Zagłębie Lubin AGAINST Raków Częstochowa.json index 7a1e96b..6c46aa9 100644 --- a/Data/Ekstraklasa_19_20/jsonsMatchesEndpoint/Team Stats Zagłębie Lubin/Team Stats Zagłębie Lubin AGAINST Raków Częstochowa.json +++ b/Data/Ekstraklasa_19_20/jsonsMatchesEndpoint/Team Stats Zagłębie Lubin/Team Stats Zagłębie Lubin AGAINST Raków Częstochowa.json @@ -1 +1 @@ -{"result": 0, "matchDate": "10-11-2019", "teamsMatchStatistics1": {"team": {"name": "Zagłębie Lubin"}, "formation": "4-3-3", "shootsOnTarget": 6, "possession": 68.58, "goals": 2, "red_cards": 0, "penaltyAreaEntries": 1, "penalties": 0, "goalkeeperSavesPercent": 75.0, "goals_lost": 2}, "teamsMatchStatistics2": {"team": {"name": "Raków Częstochowa"}, "formation": "5-3-2", "shootsOnTarget": 7, "possession": 31.42, "goals": 2, "red_cards": 0, "penaltyAreaEntries": 2, "penalties": 0, "goalkeeperSavesPercent": 66.66666666666667, "goals_lost": 2}} \ No newline at end of file +{"result": 0, "matchDate": "10-11-2019", "teamsMatchStatistics1": {"team": {"name": "Zagłębie Lubin"}, "formation": "4-3-3", "shootsOnTarget": 6, "possession": 68.58, "goals": 2, "red_cards": 0, "penaltyAreaEntries": 1, "penalties": 0, "goalkeeperSavesPercent": 66.66666666666667, "goals_lost": 2}, "teamsMatchStatistics2": {"team": {"name": "Raków Częstochowa"}, "formation": "5-3-2", "shootsOnTarget": 7, "possession": 31.42, "goals": 2, "red_cards": 0, "penaltyAreaEntries": 2, "penalties": 0, "goalkeeperSavesPercent": 66.66666666666667, "goals_lost": 2}} \ No newline at end of file diff --git a/Data/Ekstraklasa_19_20/prepareMatchEndpointData.py b/Data/Ekstraklasa_19_20/prepareMatchEndpointData.py index 681c25d..3d72545 100644 --- a/Data/Ekstraklasa_19_20/prepareMatchEndpointData.py +++ b/Data/Ekstraklasa_19_20/prepareMatchEndpointData.py @@ -1,156 +1,159 @@ import os import re import json +import requests def main(): - jsonsDirectory = "./jsonsMatchesEndpoint" - if not os.path.exists(jsonsDirectory): - os.makedirs(jsonsDirectory) + jsonsDirectory = "./jsonsMatchesEndpoint" + if not os.path.exists(jsonsDirectory): + os.makedirs(jsonsDirectory) - path = './STATYSTYKI DRUŻYNOWE 2019_20/' + path = './STATYSTYKI DRUŻYNOWE 2019_20/' - files = [] + files = [] - for r, d, f in os.walk(path): - for file in f: - if '.txt' in file: - files.append(os.path.join(r, file)) + for r, d, f in os.walk(path): + for file in f: + if '.txt' in file: + files.append(os.path.join(r, file)) - for f in files: - ff = open(f, "r") - name = f.replace(".txt", "") - directory = jsonsDirectory + "/" + name.replace(path, "") - if not os.path.exists(directory): - os.makedirs(directory) - - i = -1 - j = -1 - matchJSON = {} - for line in ff.readlines(): - i = i + 1 + for f in files: + ff = open(f, "r", encoding = 'utf-8') + name = f.replace(".txt", "") + directory = jsonsDirectory + "/" + name.replace(path, "") + if not os.path.exists(directory): + os.makedirs(directory) - if i < 3: - continue + i = -1 + j = -1 + matchJSON = {} + for line in ff.readlines(): + i = i + 1 - splittedLine = line.split(";") - if len(splittedLine) == 0: - continue - - if i % 2 == 1: - j = j + 1 - matchJSON = {} - opponentName = opponentNameValue(splittedLine[1], f.replace(".txt", "").replace(path, "").replace("Team Stats", "").lstrip()) - print(opponentName) - out = open(directory + "/" + f.replace(path, "").replace(".txt", " AGAINST " + opponentName + ".json"), "w") + if i < 3: + continue - matchJSON["result"] = resultFieldValue(splittedLine[1]) - matchJSON['matchDate'] = matchDateFieldValue(splittedLine[0]) - matchJSON['teamsMatchStatistics1'] = teamsMatchStatisticsValue(splittedLine) - else: - matchJSON['teamsMatchStatistics2'] = teamsMatchStatisticsValue(splittedLine) - matchJSON['teamsMatchStatistics1']['goals_lost'] = matchJSON['teamsMatchStatistics2']['goals'] - matchJSON['teamsMatchStatistics2']['goals_lost'] = matchJSON['teamsMatchStatistics1']['goals'] - b = json.dumps(matchJSON, ensure_ascii=False).encode('utf8') - out.write(b.decode()) - out.close() + splittedLine = line.split(";") + if len(splittedLine) == 0: + continue + + if i % 2 == 1: + j = j + 1 + matchJSON = {} + opponentName = opponentNameValue(splittedLine[1], f.replace(".txt", "").replace(path, "").replace("Team Stats", "").lstrip()) + print(opponentName) + out = open(directory + "/" + f.replace(path, "").replace(".txt", " AGAINST " + opponentName + ".json"), "w", encoding = 'utf-8') + + matchJSON["result"] = resultFieldValue(splittedLine[1]) + matchJSON['matchDate'] = matchDateFieldValue(splittedLine[0]) + matchJSON['teamsMatchStatistics1'] = teamsMatchStatisticsValue(splittedLine) + else: + matchJSON['teamsMatchStatistics2'] = teamsMatchStatisticsValue(splittedLine) + matchJSON['teamsMatchStatistics1']['goals_lost'] = matchJSON['teamsMatchStatistics2']['goals'] + matchJSON['teamsMatchStatistics2']['goals_lost'] = matchJSON['teamsMatchStatistics1']['goals'] + b = json.dumps(matchJSON, ensure_ascii=False).encode('utf8') + headers = {'Content-type': 'application/json; charset=utf-8'} + requests.post('http://localhost:8080/matches', data = b, headers = headers) + out.write(b.decode()) + out.close() - - ff.close() + + ff.close() def opponentNameValue(s, teamName): - s = s.replace("\"", "") - s = re.sub(r'[0-9]+:[0-9]+', "", s) - s = s.rstrip() - s = s.replace(teamName, "") - s = s.replace("-", "") - s = s.strip() - return s + s = s.replace("\"", "") + s = re.sub(r'[0-9]+:[0-9]+', "", s) + s = s.rstrip() + s = s.replace(teamName, "") + s = s.replace("-", "") + s = s.strip() + return s def resultFieldValue(s): - search = re.search(r'[0-9]+:[0-9]+', s) - res = search.group(0) - resList = res.split(":") - resList1 = int(resList[0]) - resList2 = int(resList[1]) - if resList1 < resList2: - return 1 - elif resList1 == resList2: - return 0 - else: - return 2 + search = re.search(r'[0-9]+:[0-9]+', s) + res = search.group(0) + resList = res.split(":") + resList1 = int(resList[0]) + resList2 = int(resList[1]) + if resList1 < resList2: + return 1 + elif resList1 == resList2: + return 0 + else: + return 2 def matchDateFieldValue(s): - yyyy = s[0:4] - MM = s[5:7] - dd = s[8:10] - return dd + "-" + MM + "-" + yyyy + yyyy = s[0:4] + MM = s[5:7] + dd = s[8:10] + return dd + "-" + MM + "-" + yyyy def teamsMatchStatisticsValue(s): - result = {'team': {'name': s[4].replace("\"", "")}} - result['formation'] = s[5].replace("\"", "").replace("(", "").replace(")", "") - result['formation'] = re.sub(r'[0-9]+\.[0-9]+%', "",result['formation']) - result['formation'] = result['formation'].rstrip() - result['shootsOnTarget'] = int(s[9]) - result['possession'] = float(s[14].replace(",", ".").replace("\"", "")) - result['goals'] = int(s[6]) - result['red_cards'] = int(s[77]) - result['penaltyAreaEntries'] = int(s[53]) + int(s[54]) - result['penalties'] = int(s[40]) - result['goalkeeperSavesPercent'] = goalkeeperSavesPercentValue(s, s[4].replace("\"", "")) + result = {'team': {'name': s[4].replace("\"", "")}} + result['formation'] = s[5].replace("\"", "").replace("(", "").replace(")", "") + result['formation'] = re.sub(r'[0-9]+\.[0-9]+%', "",result['formation']) + result['formation'] = result['formation'].rstrip() + result['shootsOnTarget'] = int(s[9]) + result['possession'] = float(s[14].replace(",", ".").replace("\"", "")) + result['goals'] = int(s[6]) + result['red_cards'] = int(s[77]) + result['penaltyAreaEntries'] = int(s[53]) + int(s[54]) + result['penalties'] = int(s[40]) + result['goalkeeperSavesPercent'] = goalkeeperSavesPercentValue(s, s[4].replace("\"", "")) - print(result) - return result + print(result) + return result def goalkeeperSavesPercentValue(s, n): - goalkeeperSavesPercent = 100 - matchName = s[1] - matchDate = s[0] - teamDirName = n.replace("./STATYSTYKI DRUŻYNOWE 2019_20/Team Stats ", "").rstrip().upper() - if teamDirName == "LECH POZNAŃ": - teamDirName = teamDirName + " I" - path = "./ZAWODNICY/" + teamDirName - files = [] + goalkeeperSavesPercent = 100 + matchName = s[1] + matchDate = s[0] + teamDirName = n.replace("./STATYSTYKI DRUŻYNOWE 2019_20/Team Stats ", "").rstrip().upper() + if teamDirName == "LECH POZNAŃ": + teamDirName = teamDirName + " I" + path = "./ZAWODNICY/" + teamDirName + files = [] - for r, d, f in os.walk(path): - for file in f: - if '.txt' in file: - files.append(os.path.join(r, file)) - - if (teamDirName == "LECH POZNAŃ I"): - teamDirName = "LECH POZNAN II" - path = "./ZAWODNICY/" + teamDirName - for r, d, f in os.walk(path): - for file in f: - if '.txt' in file: - files.append(os.path.join(r, file)) + for r, d, f in os.walk(path): + for file in f: + if '.txt' in file: + files.append(os.path.join(r, file)) + + if (teamDirName == "LECH POZNAŃ I"): + teamDirName = "LECH POZNAN II" + path = "./ZAWODNICY/" + teamDirName + for r, d, f in os.walk(path): + for file in f: + if '.txt' in file: + files.append(os.path.join(r, file)) - for f in files: - ff = open(f, "r") - i = -1 - for line in ff.readlines(): - i = i + 1 - if (i < 1): - continue - - spl = line.split(";") - if matchName == spl[0] and matchDate == spl[2] and "GK" in spl[3]: - shots_against = int(spl[62]) - saves = int(spl[63]) - if shots_against != 0: - goalkeeperSavesPercent = 100 * saves / shots_against - break + for f in files: + ff = open(f, "r", encoding = 'utf-8') + i = -1 + for line in ff.readlines(): + i = i + 1 + if (i < 1): + continue + + spl = line.split(";") + if matchName == spl[0] and matchDate == spl[2] and "GK" in spl[3]: + shots_against = int(spl[62]) + saves = int(spl[63]) + if shots_against != 0: + goalkeeperSavesPercent = 100 * saves / shots_against + break - ff.close() - - return goalkeeperSavesPercent - + ff.close() + + return goalkeeperSavesPercent + main()