Skip to content

Commit

Permalink
Merge pull request #224 from Arquisoft/game-final-bugs
Browse files Browse the repository at this point in the history
Game final bugs
  • Loading branch information
Pelayori authored Apr 27, 2024
2 parents de42162 + 2cfe550 commit 51efecb
Show file tree
Hide file tree
Showing 19 changed files with 167 additions and 167 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/uniovi/configuration/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers("/api/**").permitAll()
.requestMatchers("/game/**").authenticated()
.requestMatchers("/multiplayerGame/**").authenticated()
.requestMatchers("/startMultiplayerGame/**", "/endGameList/**").authenticated()
.requestMatchers("/lobby/**").authenticated()
.requestMatchers("/ranking/playerRanking").authenticated()
.requestMatchers("/player/admin/**").hasAuthority("ROLE_ADMIN")
Expand Down
96 changes: 55 additions & 41 deletions src/main/java/com/uniovi/controllers/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@
@Controller
public class GameController {
private static final String GAMESESSION_STR = "gameSession";

private final QuestionService questionService;
private final GameSessionService gameSessionService;
private final PlayerService playerService;

private final MultiplayerSessionService multiplayerSessionService;

private boolean isMultiPlayer;

public GameController(QuestionService questionService, GameSessionService gameSessionService,
PlayerService playerService, MultiplayerSessionService multiplayerSessionService) {
this.questionService = questionService;
Expand All @@ -50,16 +47,14 @@ public GameController(QuestionService questionService, GameSessionService gameSe
@GetMapping("/game")
public String getGame(HttpSession session, Model model, Principal principal) {
GameSession gameSession = (GameSession) session.getAttribute(GAMESESSION_STR);
if (gameSession != null) {
if (gameSession != null && !gameSession.isFinished() && !gameSession.isMultiplayer()) {
if (checkUpdateGameSession(gameSession, session)) {
return "game/fragments/gameFinished";
}
} 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());
Expand All @@ -81,7 +76,6 @@ public String joinMultiplayerGame(@PathVariable String code, HttpSession session

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);
Expand All @@ -96,26 +90,39 @@ public String joinMultiplayerGame(@PathVariable String code, HttpSession session
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());
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 (! gameSession.isMultiplayer()) {
session.removeAttribute("gameSession");
return "redirect:/startMultiplayerGame";
}

if (gameSession.isFinished()) {
model.addAttribute("code", session.getAttribute("multiplayerCode"));
return "game/multiplayerFinished";
}

if (checkUpdateGameSession(gameSession, session)) {
return "game/fragments/gameFinished";
}
} else {
Optional<Player> player = playerService.getUserByUsername(principal.getName());
if (!player.isPresent()) {
return "redirect:/";
}
gameSession = gameSessionService.startNewMultiplayerGame(getLoggedInPlayer(principal),
playerService.getUserByUsername(principal.getName()).get().getMultiplayerCode());
player.get().getMultiplayerCode());
if (gameSession == null)
return "redirect:/multiplayerGame";
session.setAttribute("gameSession", gameSession);
}

Expand All @@ -129,6 +136,7 @@ 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) {
Expand All @@ -137,10 +145,10 @@ public Map<String, String> endMultiplayerGameTable(@PathVariable String code) {
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();
if (player.getValue() == -1) {
playerScoreValue = "N/A";
} else {
playerScoreValue = "" + player.getValue();
}
playersNameWithScore.put(playerName, playerScoreValue);
}
Expand All @@ -162,17 +170,12 @@ public List<String> updatePlayerList(@PathVariable String code) {
@GetMapping("/game/lobby")
public String createLobby( HttpSession session, Model model) {
int code = Integer.parseInt((String)session.getAttribute("multiplayerCode"));
List<Player> players=playerService.getUsersByMultiplayerCode(code);
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
* @param idQuestion The id of the question.
Expand Down Expand Up @@ -205,7 +208,6 @@ else if(questionService.checkAnswer(idQuestion, idAnswer)) {
gameSession.addQuestion(true, getRemainingTime(gameSession));
gameSession.addAnsweredQuestion(gameSession.getCurrentQuestion());
}

} else {
gameSession.addAnsweredQuestion(gameSession.getCurrentQuestion());
gameSession.addQuestion(false, 0);
Expand All @@ -220,26 +222,38 @@ else if(questionService.checkAnswer(idQuestion, idAnswer)) {
public String updateGame(Model model, HttpSession session, Principal principal) {
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);
if (nextQuestion == null && gameSession.isMultiplayer()) {
int code = Integer.parseInt((String) session.getAttribute("multiplayerCode"));
List<Player> players = playerService.getUsersByMultiplayerCode(code);

if (!gameSession.isFinished()) {
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");
model.addAttribute("players", players);
model.addAttribute("code", session.getAttribute("multiplayerCode"));
gameSession.setFinished(true);

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";
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());
} else {
model.addAttribute("players", players);

}

model.addAttribute("code", session.getAttribute("multiplayerCode"));
return "ranking/multiplayerRanking";
}

if (nextQuestion == null) {
gameSessionService.endGame(gameSession);
session.removeAttribute(GAMESESSION_STR);
model.addAttribute("score", gameSession.getScore());
if (!gameSession.isFinished()) {
gameSessionService.endGame(gameSession);
gameSession.setFinished(true);
} else {
session.removeAttribute(GAMESESSION_STR);
model.addAttribute("score", gameSession.getScore());
}
return "game/fragments/gameFinished";
}

Expand Down Expand Up @@ -268,7 +282,7 @@ public String getPoints(HttpSession session) {
public String getCurrentQuestion(HttpSession session) {
GameSession gameSession = (GameSession) session.getAttribute(GAMESESSION_STR);
if (gameSession != null)
return String.valueOf(gameSession.getAnsweredQuestions().size()+1);
return String.valueOf(Math.min(gameSession.getAnsweredQuestions().size()+1, GameSessionService.NORMAL_GAME_QUESTION_NUM));
else
return "0";
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/uniovi/entities/GameSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public class GameSession implements JsonEntity, Serializable {
@Transient
private Question currentQuestion;

@Transient
private boolean isMultiplayer = false;

@Transient
private boolean isFinished = false;

public GameSession(Player player, List<Question> questions) {
this.player = player;
this.questionsToAnswer = questions;
Expand Down Expand Up @@ -75,7 +81,7 @@ public boolean isAnswered(Question question) {
}

public Question getNextQuestion() {
if(questionsToAnswer.isEmpty()) {
if (questionsToAnswer.isEmpty()) {
currentQuestion = null;
return null;
}
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/uniovi/entities/MultiplayerSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import lombok.Setter;
import org.springframework.stereotype.Service;

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

@Getter // getters para todas las propiedades
@Setter // setters para todas las propiedades
Expand All @@ -29,7 +26,6 @@ public MultiplayerSession() {}
public MultiplayerSession(String code, Player p) {
this.multiplayerCode=code;
playerScores.put(p,-1);

}

public void addPlayer(Player p){
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/uniovi/entities/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class Question implements JsonEntity {
public static final String SPANISH = "es";
public static final String FRENCH = "fr";


@Id
@GeneratedValue
private Long id;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/uniovi/services/GameSessionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;

public interface GameSessionService {
Integer NORMAL_GAME_QUESTION_NUM = 4;

/**
* Return the list of GameSessions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.uniovi.entities.GameSession;
import com.uniovi.entities.MultiplayerSession;
import com.uniovi.entities.Player;
import com.uniovi.entities.Question;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -21,4 +22,6 @@ public interface MultiplayerSessionService {
void changeScore(String code,Long id,int score);

boolean existsCode(String code);

List<Question> getQuestions(String code);
}
2 changes: 0 additions & 2 deletions src/main/java/com/uniovi/services/QuestionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ 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
20 changes: 13 additions & 7 deletions src/main/java/com/uniovi/services/impl/GameSessionImpl.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.uniovi.services.impl;

import com.uniovi.entities.Associations;
import com.uniovi.entities.Player;
import com.uniovi.entities.*;
import com.uniovi.repositories.GameSessionRepository;
import com.uniovi.entities.GameSession;
import com.uniovi.services.GameSessionService;
import com.uniovi.services.MultiplayerSessionService;
import com.uniovi.services.QuestionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -15,14 +14,16 @@

@Service
public class GameSessionImpl implements GameSessionService {
public static final Integer NORMAL_GAME_QUESTION_NUM = 4;

private final GameSessionRepository gameSessionRepository;
private final QuestionService questionService;
private final MultiplayerSessionService multiplayerSessionService;

public GameSessionImpl(GameSessionRepository gameSessionRepository, QuestionService questionService) {
public GameSessionImpl(GameSessionRepository gameSessionRepository, QuestionService questionService,
MultiplayerSessionService multiplayerSessionService) {
this.gameSessionRepository = gameSessionRepository;
this.questionService = questionService;
this.multiplayerSessionService = multiplayerSessionService;
}

@Override
Expand All @@ -32,7 +33,6 @@ public List<GameSession> getGameSessions() {

@Override
public List<GameSession> getGameSessionsByPlayer(Player player) {

return gameSessionRepository.findAllByPlayer(player);
}

Expand All @@ -52,7 +52,13 @@ public GameSession startNewGame(Player player) {

@Override
public GameSession startNewMultiplayerGame(Player player, int code) {
return new GameSession(player, questionService.getRandomMultiplayerQuestions(NORMAL_GAME_QUESTION_NUM,code));
List<Question> qs = multiplayerSessionService.getQuestions(String.valueOf(code));
if (qs == null)
return null;

GameSession sess = new GameSession(player, qs);
sess.setMultiplayer(true);
return sess;
}

@Override
Expand Down
Loading

0 comments on commit 51efecb

Please sign in to comment.