Skip to content

Commit

Permalink
Merge branch 'develop' into code-smells
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelayori authored Apr 26, 2024
2 parents ce95f53 + f68cfeb commit 8ed53d5
Show file tree
Hide file tree
Showing 27 changed files with 1,075 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ jobs:
kill $(cat spring-boot-app.pid)
- name: Collect Jacoco report and send to Sonar
run: |
./mvnw org.jacoco:jacoco-maven-plugin:report sonar:sonar -Dsonar.projectKey=Arquisoft_wiq_es04b -Dsonar.organization=arquisoft -Dsonar.branch.name=${{ github.ref }} -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=${{ secrets.SONAR_TOKEN }} -Dspring.profiles.active=test
./mvnw org.jacoco:jacoco-maven-plugin:report sonar:sonar -Dsonar.projectKey=Arquisoft_wiq_es04b -Dsonar.organization=arquisoft -Dsonar.branch.name=${{ github.head_ref || github.ref_name }} -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=${{ secrets.SONAR_TOKEN }} -Dspring.profiles.active=test
2 changes: 2 additions & 0 deletions src/main/java/com/uniovi/configuration/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers("/signup/**").permitAll()
.requestMatchers("/api/**").permitAll()
.requestMatchers("/game/**").authenticated()
.requestMatchers("/multiplayerGame/**").authenticated()
.requestMatchers("/lobby/**").authenticated()
.requestMatchers("/ranking/playerRanking").authenticated()
.requestMatchers("/player/admin/**").hasAuthority("ROLE_ADMIN")
.requestMatchers("/**").permitAll()
Expand Down
134 changes: 130 additions & 4 deletions src/main/java/com/uniovi/controllers/GameController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.uniovi.controllers;

import com.uniovi.entities.GameSession;
import com.uniovi.entities.MultiplayerSession;
import com.uniovi.entities.Player;
import com.uniovi.entities.Question;
import com.uniovi.services.GameSessionService;
import com.uniovi.services.MultiplayerSessionService;
import com.uniovi.services.PlayerService;
import com.uniovi.services.QuestionService;
import jakarta.servlet.http.HttpSession;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -16,7 +20,7 @@
import java.security.Principal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.*;

@Controller
public class GameController {
Expand All @@ -26,11 +30,16 @@ public class GameController {
private final GameSessionService gameSessionService;
private final PlayerService playerService;

private final MultiplayerSessionService multiplayerSessionService;

private boolean isMultiPlayer;

public GameController(QuestionService questionService, GameSessionService gameSessionService,
PlayerService playerService) {
PlayerService playerService, MultiplayerSessionService multiplayerSessionService) {
this.questionService = questionService;
this.gameSessionService = gameSessionService;
this.playerService = playerService;
this.multiplayerSessionService = multiplayerSessionService;
}

/**
Expand All @@ -48,13 +57,115 @@ public String getGame(HttpSession session, Model model, Principal principal) {
} else {
gameSession = gameSessionService.startNewGame(getLoggedInPlayer(principal));
session.setAttribute(GAMESESSION_STR, gameSession);
playerService.deleteMultiplayerCode(gameSession.getPlayer().getId());
session.setAttribute("gameSession", gameSession);

}

model.addAttribute("question", gameSession.getCurrentQuestion());
model.addAttribute("questionDuration", getRemainingTime(gameSession));
return "game/basicGame";
}

@GetMapping("/multiplayerGame")
public String getMultiplayerGame() {
return "game/multiplayerGame";
}

@GetMapping("/multiplayerGame/{code}")
public String joinMultiplayerGame(@PathVariable String code, HttpSession session, Principal principal, Model model) {
Optional<Player> player = playerService.getUserByUsername(principal.getName());
Player p = player.orElse(null);
isMultiPlayer=true;
if(playerService.changeMultiplayerCode(p.getId(),code)){
multiplayerSessionService.addToLobby(code,p.getId());
model.addAttribute("multiplayerGameCode",code);
session.setAttribute("multiplayerCode",code);
return "redirect:/game/lobby";
} else {
return "redirect:/multiplayerGame";
}
}

@GetMapping("/multiplayerGame/createGame")
public String createMultiplayerGame(HttpSession session, Principal principal, Model model) {
Optional<Player> player = playerService.getUserByUsername(principal.getName());
Player p = player.orElse(null);
String code=""+playerService.createMultiplayerGame(p.getId());
multiplayerSessionService.multiCreate(code,p.getId());
session.setAttribute("multiplayerCode",code);
isMultiPlayer=true;
return "redirect:/game/lobby";
}

@GetMapping("/startMultiplayerGame")
public String startMultiplayerGame(HttpSession session, Model model, Principal principal) {
GameSession gameSession = (GameSession) session.getAttribute("gameSession");
if(! isMultiPlayer){
return "index";
}
if (gameSession != null) {
if (checkUpdateGameSession(gameSession, session)) {
return "game/fragments/gameFinished";
}
} else {
gameSession = gameSessionService.startNewMultiplayerGame(getLoggedInPlayer(principal),
playerService.getUserByUsername(principal.getName()).get().getMultiplayerCode());
session.setAttribute("gameSession", gameSession);
}

model.addAttribute("question", gameSession.getCurrentQuestion());
model.addAttribute("questionDuration", getRemainingTime(gameSession));
return "game/basicGame";
}

@GetMapping("/multiplayerGame/endGame/{code}")
public String endMultiplayerGame(Model model,@PathVariable String code) {
model.addAttribute("code",code);
return "ranking/multiplayerRanking";
}
@GetMapping("/endGameList/{code}")
@ResponseBody
public Map<String, String> endMultiplayerGameTable(@PathVariable String code) {
Map<Player, Integer> playerScores = multiplayerSessionService.getPlayersWithScores(Integer.parseInt(code));
Map<String, String> playersNameWithScore=new HashMap<>();
for (Map.Entry<Player, Integer> player : playerScores.entrySet()) {
String playerName = player.getKey().getUsername();
String playerScoreValue;
if(player.getValue()==-1){
playerScoreValue="N/A";
}else{
playerScoreValue=""+player.getValue();
}
playersNameWithScore.put(playerName, playerScoreValue);
}
return playersNameWithScore;
}

@GetMapping("/game/lobby/{code}")
@ResponseBody
public List<String> updatePlayerList(@PathVariable String code) {
Map<Player,Integer> players= multiplayerSessionService.getPlayersWithScores(Integer.parseInt(code));
List<String> playerNames = new ArrayList<>();
for (Map.Entry<Player, Integer> player : players.entrySet()) {
playerNames.add(player.getKey().getUsername());
}
Collections.sort(playerNames);
return playerNames;
}
@GetMapping("/game/lobby")
public String createLobby( HttpSession session, Model model) {
int code = Integer.parseInt((String)session.getAttribute("multiplayerCode"));
List<Player> players=playerService.getUsersByMultiplayerCode(code);
model.addAttribute("players",players);
model.addAttribute("code",session.getAttribute("multiplayerCode"));
return "/game/lobby";
}

@GetMapping("/game/startMultiplayerGame")
public String startMultiplayerGame( HttpSession session, Model model) {
return "/game/lobby";
}

/**
* This method is used to check the answer for a specific question
Expand Down Expand Up @@ -96,14 +207,29 @@ else if(questionService.checkAnswer(idQuestion, idAnswer)) {

session.setAttribute("hasJustAnswered", true);
gameSession.getNextQuestion();

return updateGame(model, session);
return updateGame(model, session, principal);
}

@GetMapping("/game/update")
public String updateGame(Model model, HttpSession session) {
GameSession gameSession = (GameSession) session.getAttribute(GAMESESSION_STR);
Question nextQuestion = gameSession.getCurrentQuestion();
if(nextQuestion == null && isMultiPlayer/*gameSession.getPlayer().getMultiplayerCode()!=null session.getAttribute("multiplayerCode") !=null*/){
gameSessionService.endGame(gameSession);

int code = Integer.parseInt((String)session.getAttribute("multiplayerCode"));
List<Player> players=playerService.getUsersByMultiplayerCode(code);
model.addAttribute("players",players);
model.addAttribute("code",session.getAttribute("multiplayerCode"));
session.removeAttribute("gameSession");

Optional<Player> player = playerService.getUserByUsername(principal.getName());
Player p = player.orElse(null);
playerService.setScoreMultiplayerCode(p.getId(),""+gameSession.getScore());
multiplayerSessionService.changeScore(p.getMultiplayerCode()+"",p.getId(),gameSession.getScore());
isMultiPlayer=false;
return "game/multiFinished";
}
if (nextQuestion == null) {
gameSessionService.endGame(gameSession);
session.removeAttribute(GAMESESSION_STR);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/uniovi/dto/PlayerDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class PlayerDto {
@Schema(hidden = true)
private String passwordConfirm;

//@Schema(description = "code of group of the player", example = "5565")
//private Integer multiplayerCode;

@Schema(description = "Roles of the player", example = "[\"ROLE_USER\"]")
private String[] roles;
}
38 changes: 38 additions & 0 deletions src/main/java/com/uniovi/entities/MultiplayerSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.uniovi.entities;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Getter // getters para todas las propiedades
@Setter // setters para todas las propiedades
@Entity
public class MultiplayerSession {
@Id
@GeneratedValue
private Long id;
@Column
private String multiplayerCode;

@ElementCollection
@Column
private Map<Player, Integer> playerScores = new HashMap<>();

public MultiplayerSession() {}

public MultiplayerSession(String code, Player p) {
this.multiplayerCode=code;
playerScores.put(p,-1);

}

public void addPlayer(Player p){
playerScores.put(p,-1);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/uniovi/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public class Player implements JsonEntity {
@NotEmpty
private String password;

@Column
private Integer multiplayerCode;

@Column
private String scoreMultiplayerCode;


@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
private Set<Role> roles = new HashSet<>();

Expand All @@ -42,6 +49,8 @@ public class Player implements JsonEntity {
@OneToOne(cascade = CascadeType.ALL, mappedBy = "player")
private ApiKey apiKey;



// Transient: no se almacena en la base de datos
@Transient
private String passwordConfirm;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.uniovi.repositories;

import com.uniovi.entities.GameSession;
import com.uniovi.entities.MultiplayerSession;
import com.uniovi.entities.Player;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface MultiplayerSessionRepository extends CrudRepository<MultiplayerSession, Long> {
MultiplayerSession findByMultiplayerCode(String code);
}

3 changes: 3 additions & 0 deletions src/main/java/com/uniovi/repositories/PlayerRepository.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.uniovi.repositories;

import com.uniovi.entities.Player;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;

public interface PlayerRepository extends CrudRepository<Player, Long> {
Player findByEmail(String email);
Player findByUsername(String nickname);
@Query("SELECT player FROM Player player WHERE player.multiplayerCode=:multiplayerCode")
Iterable<Player> findAllByMultiplayerCode(int multiplayerCode);

Page<Player> findAll(Pageable pageable);
}
3 changes: 3 additions & 0 deletions src/main/java/com/uniovi/services/GameSessionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ public interface GameSessionService {
Page<GameSession> getPlayerRanking(Pageable pageable, Player player);

GameSession startNewGame(Player player);

GameSession startNewMultiplayerGame(Player player, int code);

void endGame(GameSession gameSession);
}
22 changes: 22 additions & 0 deletions src/main/java/com/uniovi/services/MultiplayerSessionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.uniovi.services;

import com.uniovi.entities.GameSession;
import com.uniovi.entities.MultiplayerSession;
import com.uniovi.entities.Player;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public interface MultiplayerSessionService {

Map<Player, Integer> getPlayersWithScores(int multiplayerCode);
void multiCreate(String code, Long id);

void addToLobby(String code, Long id);

void changeScore(String code,Long id,int score);
}
22 changes: 22 additions & 0 deletions src/main/java/com/uniovi/services/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public interface PlayerService {
*/
List<Player> getUsers();


/**
* Get all the players in the database with same multiplayerCode
* @return A list with the players
*/
List<Player> getUsersByMultiplayerCode(int multiplayerCode);

/**
* Get a player by its id
* @param id The id of the player
Expand Down Expand Up @@ -66,6 +73,21 @@ public interface PlayerService {
*/
void updatePlayer(Long id, PlayerDto playerDto);

/**
* Update the multiplayerCode of a player
* @param id The id of the player to update
* @param code The new multiplayerCode of the player
*/
boolean changeMultiplayerCode(Long id, String code);

String getScoreMultiplayerCode(Long id);

void setScoreMultiplayerCode(Long id, String score);

int createMultiplayerGame(Long id);

void deleteMultiplayerCode(Long id);

/**
* Delete a player from the database
* @param id The id of the player to delete
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/uniovi/services/QuestionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public interface QuestionService {
*/
List<Question> getRandomQuestions(int num);

List<Question> getRandomMultiplayerQuestions(int num, int code);

/**
* Check if the answer is correct
* @param idquestion The id of the question
Expand Down
Loading

0 comments on commit 8ed53d5

Please sign in to comment.