diff --git a/practica/src/main/java/com/practica/demo/WebController.java b/practica/src/main/java/com/practica/demo/WebController.java index 6a40319d..aa568ffe 100644 --- a/practica/src/main/java/com/practica/demo/WebController.java +++ b/practica/src/main/java/com/practica/demo/WebController.java @@ -240,24 +240,35 @@ public String goTeam(Model model) { */ + /* + @RequestMapping("/info") public String goInfo(Model model) { model.addAttribute("noloaded", !userComponent.isLoggedUser()); model.addAttribute("user", userComponent.getLoggedUser()); return "infoPage"; } - + + */ + + /* + @RequestMapping("/bracket") public String goBrackets(Model model) { model.addAttribute("noloaded", !userComponent.isLoggedUser()); model.addAttribute("user", userComponent.getLoggedUser()); return "diamond"; } + + */ /* * @RequestMapping("/errorPage") public String errorPage(Model model) { return * "error"; } */ + + /* + @GetMapping("/{name}") public String tournaments(Model model, @PathVariable String name) { model.addAttribute("noloaded", !userComponent.isLoggedUser()); @@ -356,18 +367,6 @@ public String join(Model model, @RequestParam(required = false) String torunamen teamOnGameId.setGame_Id_Game(auxGame.getId_game()); teamOnGameId.setTeam_Id_Team(teamPlayer.getId()); - /* - Teams_On_Game teamOnGame = new Teams_On_Game(); - - - teamOnGame.setGameIdGame(auxGame.get().getId_game()); - teamOnGame.setTeamIdTeam(teamPlayer.getId()); - teamOnGame.setDate(String.valueOf(fecha)); - teamOnGame.setWinner(false); - teamOnGame.setResult(0); - teamOnGame.setRound("1"); - */ - repositoryTeamsOnGame.save(teamOnGame); } } @@ -442,5 +441,7 @@ public String goGameUpdate(Model model, @RequestParam String team1Name, @Request return "/index"; } - + + */ + } diff --git a/practica/src/main/java/com/practica/demo/data/bracket/BracketController.java b/practica/src/main/java/com/practica/demo/data/bracket/BracketController.java index 69332c96..4b58817d 100644 --- a/practica/src/main/java/com/practica/demo/data/bracket/BracketController.java +++ b/practica/src/main/java/com/practica/demo/data/bracket/BracketController.java @@ -1,13 +1,31 @@ package com.practica.demo.data.bracket; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; +import com.practica.demo.data.user.UserComponent; + + +@EnableAutoConfiguration +@Controller public class BracketController { + @Autowired + private UserComponent userComponent; + @RequestMapping("/bracketCreation") public String goBracketCreation(Model model) { return "bracketCreation"; } + @RequestMapping("/bracket") + public String goBrackets(Model model) { + model.addAttribute("noloaded", !userComponent.isLoggedUser()); + model.addAttribute("user", userComponent.getLoggedUser()); + return "diamond"; + } + } diff --git a/practica/src/main/java/com/practica/demo/data/teams/TeamController.java b/practica/src/main/java/com/practica/demo/data/teams/TeamController.java index 27bdb13d..5dd95cbe 100644 --- a/practica/src/main/java/com/practica/demo/data/teams/TeamController.java +++ b/practica/src/main/java/com/practica/demo/data/teams/TeamController.java @@ -1,9 +1,11 @@ package com.practica.demo.data.teams; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -14,6 +16,7 @@ import com.practica.demo.data.user.User; import com.practica.demo.data.user.UserComponent; +@Controller public class TeamController { @Autowired diff --git a/practica/src/main/java/com/practica/demo/data/tournament/TournamentController.java b/practica/src/main/java/com/practica/demo/data/tournament/TournamentController.java index 3cd2602c..2b3bfe78 100644 --- a/practica/src/main/java/com/practica/demo/data/tournament/TournamentController.java +++ b/practica/src/main/java/com/practica/demo/data/tournament/TournamentController.java @@ -1,18 +1,35 @@ package com.practica.demo.data.tournament; +import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import com.practica.demo.EloCalculator; +import com.practica.demo.data.bracket.Bracket; import com.practica.demo.data.game.Game; import com.practica.demo.data.game.GameRepository; +import com.practica.demo.data.play.Play; +import com.practica.demo.data.player.Player; +import com.practica.demo.data.player.PlayerRepository; import com.practica.demo.data.rol.Rol; +import com.practica.demo.data.teams.Team; +import com.practica.demo.data.teams.TeamRepository; +import com.practica.demo.data.teamsOnGame.TeamsOnGameIds; +import com.practica.demo.data.teamsOnGame.Teams_On_Game; +import com.practica.demo.data.teamsOnGame.Teams_On_GameRepository; import com.practica.demo.data.user.User; import com.practica.demo.data.user.UserComponent; +@EnableAutoConfiguration +@Controller public class TournamentController { @Autowired @@ -24,6 +41,15 @@ public class TournamentController { @Autowired public GameRepository gameRepository; + @Autowired + private TeamRepository repositoryTeam; + + @Autowired + private Teams_On_GameRepository repositoryTeamsOnGame; + + @Autowired + private PlayerRepository playerRepository; + @RequestMapping("/tournaments") public String goTournaments(Model model) { model.addAttribute("noloaded", !userComponent.isLoggedUser()); @@ -73,4 +99,196 @@ public String goNewTournament(Model model, @RequestParam String tournament_name, return "/index"; } + @RequestMapping("/info") + public String goInfo(Model model) { + model.addAttribute("noloaded", !userComponent.isLoggedUser()); + model.addAttribute("user", userComponent.getLoggedUser()); + return "infoPage"; + } + + @GetMapping("/{name}") + public String tournaments(Model model, @PathVariable String name) { + model.addAttribute("noloaded", !userComponent.isLoggedUser()); + model.addAttribute("user", userComponent.getLoggedUser()); + + if (userComponent.isLoggedUser()) { + User user = userComponent.getLoggedUser(); + + Rol rol = user.getRol(); + + if (rol.getIdRol() == 1) { + model.addAttribute("admin", true); + model.addAttribute("adminNO", false); + } else { + model.addAttribute("admin", false); + model.addAttribute("adminNO", true); + } + + } + + // find tornament + Tournament auxTour = repositoryTournament.findByname(name); + Game auxGame = (Game) gameRepository.findByTournament(auxTour); + + model.addAttribute("name", name); + List listaTeamDate = new ArrayList<>(); + listaTeamDate = repositoryTeamsOnGame.findGameIdGame(auxGame.getId_game()); + ArrayList listamatch = new ArrayList(); + List listateams = new ArrayList(); + ArrayList listaplays = new ArrayList(); + for (int i = 0; i < listaTeamDate.size(); i++) { + Team team = repositoryTeam.findByidTeam(listaTeamDate.get(i).getTeamIdTeam()); + listateams.add(team); + if (i % 2 != 0) { + listamatch.add(new Bracket(i, listateams)); + listateams = new ArrayList(); + } + } + // si la lista es impar se aƱade el suelto. + if (listaTeamDate.size() % 2 != 0) { + + Teams_On_Game teamEmpty = new Teams_On_Game(0, listaTeamDate.get(0).getGameIdGame(), 0, false, "", ""); + listaTeamDate.add(teamEmpty); + Team team = repositoryTeam.findByidTeam(0); + listateams.add(team); + listamatch.add(new Bracket(listaTeamDate.size() - 1, listateams)); + + } + model.addAttribute("brackets", listamatch); + for (int i = 0; i < listamatch.size(); i++) { + listaplays.add(new Play()); + listaplays.get(i).setRound(listaTeamDate.get(i * 2).getRound()); + listaplays.get(i).setName1(listamatch.get(i).getTeams().get(0).getName()); + listaplays.get(i).setElo1(listamatch.get(i).getTeams().get(0).getElo()); + listaplays.get(i).setName2(listamatch.get(i).getTeams().get(1).getName()); + listaplays.get(i).setElo2(listamatch.get(i).getTeams().get(1).getElo()); + listaplays.get(i).setDate(listaTeamDate.get(i * 2).getDate()); + if (listaTeamDate.get(i * 2).isWinner()) { + listaplays.get(i).setNameWinner(listamatch.get(i).getTeams().get(0).getName()); + } else { + listaplays.get(i).setNameWinner(listamatch.get(i).getTeams().get(1).getName()); + } + + } + + model.addAttribute("plays", listaplays); + + return "diamond"; + + } + + @GetMapping("/jointournament") + public String join(Model model, @RequestParam(required = false) String torunament) { + + User userJoin = userComponent.getLoggedUser(); + Player playerJoin = playerRepository.findByuser(userJoin); + + if (!playerJoin.getTeam().getName().equals("")) { + + + Tournament auxTour = repositoryTournament.findByname(torunament); + Game auxGame = gameRepository.findByTournament(auxTour); + List numGames = gameRepository.findBytournamentIdTournament(auxTour.getIdTournament()); + + if(auxTour.getNumTeams()>numGames.size()) { + java.util.Date fecha = new java.util.Date(); + + Team teamPlayer = repositoryTeam.findByname(playerJoin.getTeam().getName()); + + + Teams_On_Game teamOnGame = new Teams_On_Game(teamPlayer.getId(), auxGame.getId_game(), 0, false, "1", + String.valueOf(fecha)); + + TeamsOnGameIds teamOnGameId = new TeamsOnGameIds(); + + teamOnGameId.setGame_Id_Game(auxGame.getId_game()); + teamOnGameId.setTeam_Id_Team(teamPlayer.getId()); + + /* + Teams_On_Game teamOnGame = new Teams_On_Game(); + + + teamOnGame.setGameIdGame(auxGame.get().getId_game()); + teamOnGame.setTeamIdTeam(teamPlayer.getId()); + teamOnGame.setDate(String.valueOf(fecha)); + teamOnGame.setWinner(false); + teamOnGame.setResult(0); + teamOnGame.setRound("1"); + */ + + repositoryTeamsOnGame.save(teamOnGame); + } + } + + return "index"; + } + + @GetMapping("/gameData") + public String goPlay(Model model, @RequestParam String name1, @RequestParam String elo1, @RequestParam String name2, + @RequestParam String elo2) { + + model.addAttribute("name1", name1); + model.addAttribute("elo1", elo1); + + model.addAttribute("name2", name2); + model.addAttribute("elo2", elo2); + + if (userComponent.isLoggedUser()) { + User user = userComponent.getLoggedUser(); + + Rol rol = user.getRol(); + + if (rol.getIdRol() == 1) { + model.addAttribute("admin", true); + } else { + model.addAttribute("admin", false); + } + + } + + return "play"; + } + + @RequestMapping("/gameUpdate") + public String goGameUpdate(Model model, @RequestParam String team1Name, @RequestParam String team2Name, + @RequestParam int puntuation1, @RequestParam int puntuation2, @RequestParam String winner) { + + EloCalculator calculateElo = new EloCalculator(repositoryTeam); + + Team team1 = repositoryTeam.findByname(team1Name); + Team team2 = repositoryTeam.findByname(team2Name); + + Teams_On_Game teamOnGame1 = repositoryTeamsOnGame.findByteam_Id_Team(team1.getId()); + Teams_On_Game teamOnGame2 = repositoryTeamsOnGame.findByteam_Id_Team(team2.getId()); + + teamOnGame1.setResult(puntuation1); + teamOnGame2.setResult(puntuation2); + + int team1Id = team1.getId(); + int team2Id = team2.getId(); + + if (team1Name.equals(winner)) { + + teamOnGame1.setWinner(true); + teamOnGame2.setWinner(false); + + calculateElo.updateElo(team1Id, team2Id, 1); + + } else if (team2Name.equals(winner)) { + + teamOnGame1.setWinner(false); + teamOnGame2.setWinner(true); + + calculateElo.updateElo(team1Id, team2Id, 0); + + } else { + System.out.println("The winner dont match any team"); + } + + repositoryTeamsOnGame.save(teamOnGame1); + repositoryTeamsOnGame.save(teamOnGame2); + + return "/index"; + } + } diff --git a/practica/src/main/java/com/practica/demo/security/WebSecurityConfig.java b/practica/src/main/java/com/practica/demo/security/WebSecurityConfig.java index 71011628..9d883261 100644 --- a/practica/src/main/java/com/practica/demo/security/WebSecurityConfig.java +++ b/practica/src/main/java/com/practica/demo/security/WebSecurityConfig.java @@ -14,35 +14,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { - /* - * @EnableWebMvc class WebConfig extends WebMvcConfigurerAdapter { - * - * @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - * registry.addResourceHandler( "/core/**", "/imgs/**", "/css/**", "/js/**") - * .addResourceLocations( "classpath:/static/imgs/", "classpath:/static/css/", - * "classpath:/static/core/", "classpath:/static/js/"); } } - */ - /** - * - * @EnableWebMvc class SimpleCORSFilter implements Filter { - * - * public void doFilter(ServletRequest req, ServletResponse res, - * FilterChain chain) throws IOException, ServletException { - * HttpServletResponse response = (HttpServletResponse) res; - * response.setHeader("Access-Control-Allow-Origin", "*"); - * response.setHeader("Access-Control-Allow-Methods", "POST, GET, - * OPTIONS, DELETE, PUT"); - * response.setHeader("Access-Control-Max-Age", "3600"); - * response.setHeader("Access-Control-Allow-Headers", - * "x-requested-with, content-type"); chain.doFilter(req, res); } - * - * public void init(FilterConfig filterConfig) { } - * - * public void destroy() { } - * - * } - * - **/ http.authorizeRequests().antMatchers("/core/**").permitAll(); http.authorizeRequests().antMatchers("/imgs/**").permitAll(); diff --git a/practica/src/main/resources/application.properties b/practica/src/main/resources/application.properties index 1d290543..e42ade57 100644 --- a/practica/src/main/resources/application.properties +++ b/practica/src/main/resources/application.properties @@ -11,3 +11,5 @@ server.port = 8443 server.ssl.key-store = classpath:keystore.jks server.ssl.key-store-password = password server.ssl.key-password = secret + +spring.main.allow-bean-definition-overriding=true \ No newline at end of file diff --git a/practica/src/main/resources/templates/error.html b/practica/src/main/resources/error.html similarity index 100% rename from practica/src/main/resources/templates/error.html rename to practica/src/main/resources/error.html diff --git a/practica/src/main/resources/templates/index.html b/practica/src/main/resources/templates/index.html index 9a6eb04d..17c3af4f 100644 --- a/practica/src/main/resources/templates/index.html +++ b/practica/src/main/resources/templates/index.html @@ -40,20 +40,20 @@
{{/noloaded}} {{#adminNO}} {{^noloaded}} -
+
{{/noloaded}} {{/adminNO}} {{#admin}}