Skip to content

Commit

Permalink
Merge pull request #128 from Arquisoft/fix-empty-answers
Browse files Browse the repository at this point in the history
Fixed empty answers
  • Loading branch information
Pelayori authored Mar 9, 2024
2 parents 1ea5f7e + 6dbcdd9 commit f51b68c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/uniovi/controllers/PlayersController.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public String home(Model model, Principal principal) {
return "player/home";
}


@GetMapping("/ranking/globalRanking")
public String showGlobalRanking(Pageable pageable, Model model) {
Page<Object[]> ranking = gameSessionService.getGlobalRanking(pageable);
Expand All @@ -114,5 +113,4 @@ public String showPlayerRanking(Pageable pageable, Model model, Principal princi

return "ranking/playerRanking";
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/uniovi/entities/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,13 @@ public JsonNode toJson() {
obj .put("options", optionsArray);
return obj;
}

public boolean hasEmptyOptions() {
for(Answer a : options) {
if(a.getText().isEmpty() || a.getText().isBlank() || a.getText() == null) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;


import java.util.List;

public interface GameSessionRepository extends CrudRepository<GameSession, Long> {
Expand All @@ -18,7 +17,6 @@ public interface GameSessionRepository extends CrudRepository<GameSession, Long>

List<GameSession> findAllByPlayer(Player player);


@Query("SELECT gs.player, SUM(gs.score) FROM GameSession gs GROUP BY gs.player ORDER BY SUM(gs.score) DESC")
Page<Object[]> findTotalScoresByPlayer(Pageable pageable);
Page<GameSession> findAllByPlayerOrderByScoreDesc(Pageable pageable, Player player);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/uniovi/services/GameSessionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public interface GameSessionService {
List<GameSession> getGameSessions();

/**
* Return the list of GameSessions by player
*
* @return the list of GameSessions by player
*/
List<GameSession> getGameSessionsByPlayer(Player player);

* Return the global ranking
*
* @param pageable the pageable
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/uniovi/services/impl/GameSessionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public List<GameSession> getGameSessions() {
}

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

return gameSessionRepository.findAllByPlayer(player);
}

public Page<Object[]> getGlobalRanking(Pageable pageable) {
return gameSessionRepository.findTotalScoresByPlayer(pageable);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,21 @@ public Optional<Question> getQuestion(Long id) {

@Override
public Optional<Question> getRandomQuestion() {
Long qty = questionRepository.count();
int idx = (int)(Math.random() * qty);
Page<Question> questionPage = questionRepository.findAll(PageRequest.of(idx, 1));
Question q = null;
if (questionPage.hasContent()) {
q = questionPage.getContent().get(0);
}

List<Question> allQuestions = questionRepository.findAll().stream()
.filter(question -> question.getLanguage().equals(LocaleContextHolder.getLocale().getLanguage())).toList();
int idx = (int) (Math.random() * allQuestions.size());
Question q = allQuestions.get(idx);
while (q.hasEmptyOptions()){
return getRandomQuestion();
}
return Optional.ofNullable(q);
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/resources/templates/fragments/playerRanking.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h2 th:text="#{ranking.title}">Ranking</h2>
<table class="ranking-table">
<thead>
<tr>
<th th:text="#{ranking.position}">Posición</th>
<th th:text="#{ranking.score}">Puntuación</th>
</tr>
</thead>
<tbody>
<tr th:each="game : ${ranking}">
<td th:text="${game.createdAt}"></td>
<td th:text="${game.score}"></td>
</tr>
</tbody>
</table>

0 comments on commit f51b68c

Please sign in to comment.