11package com .mayadem .battlearena .api .controller ;
2+
23import java .util .List ;
34
45import org .springframework .http .HttpStatus ;
1516import org .springframework .web .bind .annotation .RestController ;
1617import com .mayadem .battlearena .api .dto .BattleHistorySummaryDto ;
1718import com .mayadem .battlearena .api .dto .BattleRoomDto ;
19+ import com .mayadem .battlearena .api .dto .LeaderboardEntryDto ;
20+ import com .mayadem .battlearena .api .dto .LeaderboardStatsDto ;
1821import com .mayadem .battlearena .api .dto .StartBattleRequestDto ;
1922import com .mayadem .battlearena .api .dto .SubmitBattleResultRequestDto ;
2023import com .mayadem .battlearena .api .entity .Warrior ;
2124import com .mayadem .battlearena .api .exception .ResourceNotFoundException ;
2225import com .mayadem .battlearena .api .service .BattleCompletionService ;
2326import com .mayadem .battlearena .api .service .BattleRoomService ;
24- import com .mayadem .battlearena .api .dto .BattleHistoryPageDto ;
27+ import com .mayadem .battlearena .api .service .LeaderboardService ;
28+ import com .mayadem .battlearena .api .dto .BattleHistoryPageDto ;
2529import com .mayadem .battlearena .api .entity .enums .BattleType ;
2630import com .mayadem .battlearena .api .service .BattleHistoryService ;
27- import org .springframework .data .domain .PageRequest ;
28- import org .springframework .data .domain .Pageable ;
31+ import org .springframework .data .domain .PageRequest ;
32+ import org .springframework .data .domain .Pageable ;
2933import java .util .Optional ;
3034import jakarta .validation .Valid ;
35+
36+ import com .mayadem .battlearena .api .dto .ArenaLeaderboardDto ;
3137import com .mayadem .battlearena .api .dto .BattleHistoryDto ;
3238
3339@ RestController
@@ -37,12 +43,15 @@ public class BattleController {
3743 private final BattleRoomService battleRoomService ;
3844 private final BattleCompletionService battleCompletionService ;
3945 private final BattleHistoryService battleHistoryService ;
46+ private final LeaderboardService leaderboardService ;
4047 private static final int MAX_PAGE_SIZE = 100 ;
4148
42- public BattleController (BattleRoomService battleRoomService , BattleCompletionService battleCompletionService , BattleHistoryService battleHistoryService ) {
49+ public BattleController (BattleRoomService battleRoomService , BattleCompletionService battleCompletionService ,
50+ BattleHistoryService battleHistoryService , LeaderboardService leaderboardService ) {
4351 this .battleRoomService = battleRoomService ;
4452 this .battleCompletionService = battleCompletionService ;
4553 this .battleHistoryService = battleHistoryService ;
54+ this .leaderboardService = leaderboardService ;
4655 }
4756
4857 @ PostMapping
@@ -77,7 +86,7 @@ public ResponseEntity<List<BattleRoomDto>> getAvailableBattles() {
7786
7887 @ PostMapping ("/submit-result" )
7988 public ResponseEntity <Object > submitBattleResult (@ Valid @ RequestBody SubmitBattleResultRequestDto request ,
80- @ AuthenticationPrincipal Warrior requester ){
89+ @ AuthenticationPrincipal Warrior requester ) {
8190 try {
8291 Object result = battleCompletionService .submitAndProcessScore (request , requester );
8392
@@ -99,25 +108,25 @@ public ResponseEntity<String> getBattleStatus(@PathVariable Long battleRoomId) {
99108 .map (ResponseEntity ::ok )
100109 .orElse (ResponseEntity .badRequest ().body ("BattleRoom not found." ));
101110 }
111+
102112 @ GetMapping ("/history" )
103113 public ResponseEntity <BattleHistoryPageDto > getBattleHistory (
104- @ RequestParam (defaultValue = "0" ) int page ,
105- @ RequestParam (defaultValue = "10" ) int size ,
106- @ RequestParam (required = false ) BattleType battleType ,
107- @ AuthenticationPrincipal Warrior warrior ) {
114+ @ RequestParam (defaultValue = "0" ) int page ,
115+ @ RequestParam (defaultValue = "10" ) int size ,
116+ @ RequestParam (required = false ) BattleType battleType ,
117+ @ AuthenticationPrincipal Warrior warrior ) {
108118
109- validatePageableParameters (page , size );
119+ validatePageableParameters (page , size );
110120
111- Pageable pageable = PageRequest .of (page , size );
121+ Pageable pageable = PageRequest .of (page , size );
112122
113- BattleHistoryPageDto historyPage = battleHistoryService .getWarriorBattleHistory (
114- warrior ,
115- Optional .ofNullable (battleType ),
116- pageable
117- );
123+ BattleHistoryPageDto historyPage = battleHistoryService .getWarriorBattleHistory (
124+ warrior ,
125+ Optional .ofNullable (battleType ),
126+ pageable );
118127
119- return ResponseEntity .ok (historyPage );
120- }
128+ return ResponseEntity .ok (historyPage );
129+ }
121130
122131 private void validatePageableParameters (int page , int size ) {
123132 if (page < 0 ) {
@@ -130,17 +139,41 @@ private void validatePageableParameters(int page, int size) {
130139 throw new IllegalArgumentException ("Page size must not be greater than " + MAX_PAGE_SIZE );
131140 }
132141 }
133- @ GetMapping ("/history/{battleRoomId}" )
142+
143+ @ GetMapping ("/history/{battleRoomId}" )
134144 public ResponseEntity <BattleHistoryDto > getBattleDetails (
135145 @ PathVariable Long battleRoomId ,
136146 @ AuthenticationPrincipal Warrior warrior ) {
137147
138148 BattleHistoryDto battleDetails = battleHistoryService .getBattleDetails (warrior , battleRoomId );
139149 return ResponseEntity .ok (battleDetails );
140150 }
151+
141152 @ GetMapping ("/history/stats" )
142153 public ResponseEntity <BattleHistorySummaryDto > getBattleStats (@ AuthenticationPrincipal Warrior warrior ) {
143154 BattleHistorySummaryDto summary = battleHistoryService .getBattleHistorySummary (warrior );
144155 return ResponseEntity .ok (summary );
145156 }
157+
158+ // Ana leaderboard
159+ @ GetMapping ("/leaderboard" )
160+ public ResponseEntity <ArenaLeaderboardDto > getArenaLeaderboard (@ AuthenticationPrincipal Warrior currentWarrior ) {
161+ ArenaLeaderboardDto dto = leaderboardService .getArenaLeaderboard (currentWarrior .getId ());
162+ return ResponseEntity .ok (dto );
163+ }
164+
165+ @ GetMapping ("/leaderboard/around-me" )
166+ public ResponseEntity <List <LeaderboardEntryDto >> getLeaderboardAroundMe (
167+ @ RequestParam (defaultValue = "5" ) int range ,
168+ @ AuthenticationPrincipal Warrior currentWarrior ) {
169+ List <LeaderboardEntryDto > around = leaderboardService .getWarriorsAroundMe (currentWarrior .getId (), range );
170+ return ResponseEntity .ok (around );
171+ }
172+
173+ // Global stats
174+ @ GetMapping ("/leaderboard/stats" )
175+ public ResponseEntity <LeaderboardStatsDto > getLeaderboardStats () {
176+ LeaderboardStatsDto stats = leaderboardService .getGlobalStats ();
177+ return ResponseEntity .ok (stats );
178+ }
146179}
0 commit comments