|
5 | 5 | from django.contrib.auth.decorators import login_required |
6 | 6 | from django.core.paginator import Paginator |
7 | 7 | from django.db import models |
8 | | -from django.http import FileResponse, HttpRequest, HttpResponse |
| 8 | +from django.http import FileResponse, HttpRequest, HttpResponse, JsonResponse |
9 | 9 | from django.shortcuts import get_object_or_404, redirect, render |
10 | 10 | from django.utils import timezone |
11 | 11 |
|
@@ -173,12 +173,59 @@ def request_match(request: HttpRequest) -> HttpResponse: |
173 | 173 |
|
174 | 174 |
|
175 | 175 | @login_required |
| 176 | +def queue_json(request: HttpRequest) -> HttpResponse: |
| 177 | + matches = Match.objects.all().order_by("-created_at") |
| 178 | + my_matches_only = request.GET.get("my_matches") == "1" |
| 179 | + if my_matches_only: |
| 180 | + matches = matches.filter( |
| 181 | + models.Q(player1__user=request.user) | models.Q(player2__user=request.user) |
| 182 | + ) |
| 183 | + paginator = Paginator(matches, 10) |
| 184 | + page_number = request.GET.get("page") |
| 185 | + page_obj = paginator.get_page(page_number) |
| 186 | + |
| 187 | + matches_data = [] |
| 188 | + for match in page_obj: |
| 189 | + matches_data.append( |
| 190 | + { |
| 191 | + "id": match.id, |
| 192 | + "player1_name": match.player1.get_game_name(), |
| 193 | + "player2_name": match.player2.get_game_name(), |
| 194 | + "score": f"{match.player1_wins}-{match.ties}-{match.player2_wins}" |
| 195 | + if match.status == "completed" |
| 196 | + else "-", |
| 197 | + "is_ranked": "Yes" if match.is_ranked else "No", |
| 198 | + "status": match.status, |
| 199 | + "created_at": match.created_at.isoformat(), |
| 200 | + "can_view_replay": request.user in [match.player1.user, match.player2.user], |
| 201 | + } |
| 202 | + ) |
| 203 | + |
| 204 | + return JsonResponse( |
| 205 | + { |
| 206 | + "matches": matches_data, |
| 207 | + "has_next": page_obj.has_next(), |
| 208 | + "has_previous": page_obj.has_previous(), |
| 209 | + "page_number": page_obj.number, |
| 210 | + } |
| 211 | + ) |
| 212 | + |
| 213 | + |
176 | 214 | def queue(request: HttpRequest) -> HttpResponse: |
177 | 215 | matches = Match.objects.all().order_by("-created_at") |
178 | | - paginator = Paginator(matches, 10) # 10 per page |
| 216 | + my_matches_only = request.GET.get("my_matches") == "1" |
| 217 | + if my_matches_only and request.user.is_authenticated: |
| 218 | + matches = matches.filter( |
| 219 | + models.Q(player1__user=request.user) | models.Q(player2__user=request.user) |
| 220 | + ) |
| 221 | + paginator = Paginator(matches, 10) |
179 | 222 | page_number = request.GET.get("page") |
180 | 223 | page_obj = paginator.get_page(page_number) |
181 | | - return render(request, "games/queue.html", {"page_obj": page_obj}) |
| 224 | + return render( |
| 225 | + request, |
| 226 | + "games/queue.html", |
| 227 | + {"page_obj": page_obj, "my_matches_only": my_matches_only, "request": request}, |
| 228 | + ) |
182 | 229 |
|
183 | 230 |
|
184 | 231 | def watch(request: HttpRequest, game_id: int | None = None) -> HttpResponse: |
|
0 commit comments