generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from Arquisoft/ranking_global-develop
Ranking global develop
- Loading branch information
Showing
15 changed files
with
317 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/uniovi/repositories/GameSessionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,25 @@ | ||
package com.uniovi.repositories; | ||
|
||
import com.uniovi.entities.Answer; | ||
import com.uniovi.entities.GameSession; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import com.uniovi.entities.Player; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
|
||
import java.util.List; | ||
|
||
public interface GameSessionRepository extends CrudRepository<GameSession, Long> { | ||
|
||
List<GameSession> findAll(); | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.uniovi.services; | ||
|
||
import com.uniovi.entities.GameSession; | ||
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.HashMap; | ||
import java.util.List; | ||
@Service | ||
public interface GameSessionService { | ||
|
||
/** | ||
* Return the list of GameSessions | ||
* | ||
* @return the list of GameSessions | ||
*/ | ||
List<GameSession> getGameSessions(); | ||
|
||
// /** | ||
// * Return the list of GameSessions by player | ||
// * | ||
// * @return the list of GameSessions by player | ||
// */ | ||
// List<GameSession> getGameSessionsByPlayer(Player player); | ||
// | ||
// HashMap<Player,Integer> getSortedPlayersScores(); | ||
|
||
|
||
public Page<Object[]> getGlobalRanking(Pageable pageable); | ||
public Page<GameSession> getPlayerRanking(Pageable pageable, Player player); | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/com/uniovi/services/impl/GameSessionImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.uniovi.services.impl; | ||
|
||
import com.uniovi.entities.Player; | ||
import com.uniovi.repositories.GameSessionRepository; | ||
import com.uniovi.entities.GameSession; | ||
import com.uniovi.services.GameSessionService; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.*; | ||
|
||
@Service | ||
public class GameSessionImpl implements GameSessionService { | ||
|
||
private final GameSessionRepository gameSessionRepository; | ||
|
||
public GameSessionImpl(GameSessionRepository gameSessionRepository) { | ||
this.gameSessionRepository = gameSessionRepository; | ||
} | ||
|
||
@Override | ||
public List<GameSession> getGameSessions() { | ||
return gameSessionRepository.findAll(); | ||
} | ||
|
||
// @Override | ||
// public List<GameSession> getGameSessionsByPlayer(Player player) { | ||
// return gameSessionRepository.findAllByPlayer(player); | ||
// } | ||
// | ||
// @Override | ||
// public HashMap<Player, Integer> getSortedPlayersScores() { | ||
// List<GameSession> gameSessions = gameSessionRepository.findAll(); | ||
// HashMap<Player, Integer> ranking = getRanking(gameSessions); | ||
// // Ordenar las entradas del ranking por puntuación | ||
// List<Map.Entry<Player, Integer>> sortedEntries = new ArrayList<>(ranking.entrySet()); | ||
// sortedEntries.sort(Map.Entry.comparingByValue(Comparator.reverseOrder())); | ||
// | ||
// // Crear un LinkedHashMap para mantener el orden de inserción | ||
// LinkedHashMap<Player, Integer> sortedRanking = new LinkedHashMap<>(); | ||
// for (Map.Entry<Player, Integer> entry : sortedEntries) { | ||
// sortedRanking.put(entry.getKey(), entry.getValue()); | ||
// } | ||
// | ||
// return sortedRanking; | ||
// } | ||
// | ||
// private static HashMap<Player, Integer> getRanking(List<GameSession> gameSessions) { | ||
// HashMap<Player, Integer> ranking = new HashMap<>(); | ||
// | ||
// // Iterar a través de las sesiones de juego | ||
// for (GameSession gameSession : gameSessions) { | ||
// Player player = gameSession.getPlayer(); | ||
// int score = gameSession.getScore(); | ||
// | ||
// // Si el jugador ya está en el ranking, sumar la puntuación, de lo contrario, agregarlo al ranking | ||
// if (ranking.containsKey(player)) { | ||
// int currentScore = ranking.get(player) + score; | ||
// ranking.put(player, currentScore); | ||
// } else { | ||
// ranking.put(player, score); | ||
// } | ||
// } | ||
// return ranking; | ||
// } | ||
|
||
|
||
@Override | ||
public Page<Object[]> getGlobalRanking(Pageable pageable) { | ||
return gameSessionRepository.findTotalScoresByPlayer(pageable); | ||
} | ||
|
||
@Override | ||
public Page<GameSession> getPlayerRanking(Pageable pageable, Player player) { | ||
return gameSessionRepository.findAllByPlayerOrderByScoreDesc(pageable, player); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<div class="text-center"> | ||
<ul class="pagination justify-content-center"> | ||
<!-- Primera --> | ||
<li class="page-item" > | ||
<a class="page-link" th:href="@{'?page=0'}">Primera</a> | ||
</li> | ||
<!-- Anterior (si la hay ) --> | ||
<li class="page-item" th:if='${page.getNumber()-1 >= 0}'> | ||
<a class="page-link" th:href="@{'?page='+${page.getNumber()-1} }" | ||
th:text="${page.getNumber()}"></a> | ||
</li> | ||
<!-- Actual --> | ||
<li class="page-item active" > | ||
<a class="page-link" th:href="@{'?page='+${page.getNumber()} }" | ||
th:text="${page.getNumber()+1}"></a> | ||
</li> | ||
<!-- Siguiente (si la hay) --> | ||
<li class="page-item" th:if='${page.getNumber()+1 <= page.getTotalPages()-1}'> | ||
<a class="page-link" th:href="@{'?page='+${page.getNumber()+1} }" | ||
th:text="${page.getNumber()+2}"></a> | ||
</li> | ||
<!-- Última --> | ||
<li class="page-item" > | ||
<a class="page-link" | ||
th:href="@{'?page='+${page.getTotalPages()-1}}"> Última</a> | ||
</li> | ||
</ul> | ||
</div> |
Oops, something went wrong.