-
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.
migrate the logic for determining clear status to model
- Loading branch information
Showing
7 changed files
with
65 additions
and
123 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
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 |
---|---|---|
@@ -1,98 +1,46 @@ | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.http import HttpRequest, HttpResponse | ||
from django.shortcuts import get_object_or_404, render | ||
|
||
from main.models import Music, Medal, Bad_Count, Extra_Option | ||
from users.models import CustomUser | ||
from main.models import Bad_Count, Extra_Option, Medal, Music | ||
|
||
|
||
def ranking_detail(request: HttpRequest, music_id: int) -> HttpResponse: | ||
""" ランキング: 詳細 """ | ||
|
||
def bad_count_rank(bad_count_list_ordered: list[Bad_Count], user: CustomUser) -> int | None: | ||
""" | ||
指定されたユーザーの順位を返す | ||
@param bad_count_list_ordered: 曲で絞込済みのBAD数リスト(昇順) | ||
@param user: 指定されたユーザー | ||
@return rank: 順位 | ||
""" | ||
if not bad_count_list_ordered: | ||
return None | ||
|
||
bad_count_num = 0 # BAD数の個数 | ||
bad_count_now = -1 # 現在のBAD数 | ||
rank = -1 # ランク | ||
found = False # BAD数を登録済であればTrueを返す | ||
tmp_rank = 0 | ||
|
||
for bad_count in bad_count_list_ordered: | ||
bad_count_before = bad_count_now | ||
bad_count_now = bad_count.bad_count | ||
music = get_object_or_404(Music, pk=music_id) | ||
|
||
# BAD数が前後で重複した場合 | ||
if bad_count_now == bad_count_before: | ||
# 指定されたユーザーの記録が見つかれば rank にランクを格納 | ||
if bad_count.user.id == user.id: | ||
found = True | ||
rank = tmp_rank | ||
medals = Medal.objects.filter(music=music).select_related('user') | ||
extra_options = Extra_Option.objects.filter(music=music, hard=True).select_related('user') | ||
|
||
bad_count_num += 1 | ||
medals_dict = {medal.user.id: medal for medal in medals} | ||
extra_options_dict = {option.user.id: option for option in extra_options} | ||
|
||
# BAD数が重複しなかった場合 | ||
else: | ||
bad_count_num += 1 | ||
bad_count_list = list(Bad_Count.objects.filter(music=music).order_by('bad_count', 'updated_at')) | ||
rank, last_bad_count, results = 1, None, [] | ||
|
||
# 一時ランクを更新 | ||
tmp_rank = bad_count_num | ||
for i, bad_count in enumerate(bad_count_list, start=1): | ||
if bad_count.bad_count != last_bad_count: | ||
rank = i | ||
last_bad_count = bad_count.bad_count | ||
|
||
# 自分の記録が見つかれば rank にランクを格納 | ||
if bad_count.user.id == user.id: | ||
found = True | ||
rank = bad_count_num | ||
medal = medals_dict.get(bad_count.user.id) | ||
extra_option = extra_options_dict.get(bad_count.user.id) | ||
|
||
if found: | ||
return rank | ||
if medal is None: | ||
status = Medal.ClearStatus.NO_PLAY.value | ||
else: | ||
return None | ||
|
||
# 曲を取得 | ||
music = get_object_or_404(Music, pk=music_id) | ||
|
||
medal_list = Medal.objects.filter(music=music) | ||
bad_count_list = Bad_Count.objects.filter(music=music).order_by('bad_count', 'updated_at') | ||
extra_option_list = Extra_Option.objects.filter(music=music) | ||
|
||
# 対象曲を記録しているユーザーを取得 | ||
user_id_list = list(bad_count_list.values_list('user', flat=True)) | ||
users = CustomUser.objects.filter(pk__in=user_id_list, is_active=True) | ||
status = medal.get_clear_status(bad_count, extra_option).value | ||
|
||
# ランキングを生成 | ||
results = [] | ||
for bad_count in bad_count_list: | ||
selected_user = users.get(pk=bad_count.user.id) | ||
try: | ||
medal = medal_list.get(user=selected_user) | ||
if medal.medal == 12: | ||
medal = None | ||
except ObjectDoesNotExist: | ||
medal = None | ||
try: | ||
extra_option = extra_option_list.get(user=selected_user) | ||
except ObjectDoesNotExist: | ||
extra_option = None | ||
rank = bad_count_rank(bad_count_list, selected_user) | ||
results.append({ | ||
'rank': rank, | ||
'user': selected_user, | ||
'user': bad_count.user, | ||
'medal': medal, | ||
'bad_count': bad_count, | ||
'extra_option': extra_option | ||
'extra_option': extra_option, | ||
'status': status, | ||
}) | ||
|
||
context = { | ||
'music': music, | ||
'bad_count_list': bad_count_list, | ||
'results': results | ||
'results': results, | ||
} | ||
|
||
return render(request, 'main/ranking_detail.html', context) |
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