Skip to content

Commit

Permalink
Merge pull request #25 from syargeau/feature/unranked-players
Browse files Browse the repository at this point in the history
Feature/unranked players
  • Loading branch information
syargeau authored Jun 15, 2018
2 parents c2d11b5 + c55de5b commit 82ff4ee
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
25 changes: 24 additions & 1 deletion functional_tests/test_home_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.contrib.sessions.backends.db import SessionStore
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

from leaderboard.models import Player, Match

Expand Down Expand Up @@ -286,7 +287,7 @@ def test_leaderboard(self):
losing_score=10
)

# Bob loads PongBoard and sees him ranked first, and Sue in second
# Bob loads PongBoard and sees him above Sue
self.browser.get(self.live_server_url)
player_rankings = self.browser.find_elements_by_id('player-ranking')
self.assertEqual(len(player_rankings), 2)
Expand All @@ -295,4 +296,26 @@ def test_leaderboard(self):
self.assertIn('Sue Hope', player_rankings[1].text)
self.assertIn('985', player_rankings[1].text)

# However, he realizes they are unranked
self.assertIn('N/A', player_rankings[0].text)
unranked_warning = self.browser.find_element_by_id('unranked-warning')
self.assertEqual(
unranked_warning.text,
'Note: you must play a minimum of 5 games before being ranked.'
)

# Submit 4 more matches, and reload
for _ in range(4):
Match.objects.create(
winner=bob,
loser=sue,
winning_score=21,
losing_score=19
)
self.browser.get(self.live_server_url)

# They are now ranked, and the warning is no longer there
with self.assertRaises(NoSuchElementException):
self.browser.find_element_by_id('unranked-warning')

# TODO: test each line in leaderboard for specific stats
35 changes: 26 additions & 9 deletions leaderboard/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,38 @@ <h1>PongBoard</h1>
<th>PPG</th>
<th>AvgDiff</th>
</tr>
{% for ranking in leaderboard %}
{% for ranked_player in ranked_players %}
<tr id='player-ranking'>
<td>{{ forloop.counter }}</td>
<td>{{ ranking.player.full_name }}</td>
<td>{{ ranking.rating }}</td>
<td>{{ ranking.games_played }}</td>
<td>{{ ranking.wins }}</td>
<td>{{ ranking.losses }}</td>
<td>{{ ranking.win_percent|percentage:1 }}</td>
<td>{{ ranking.points_per_game|floatformat }}</td>
<td>{{ ranking.avg_point_differential|stringformat:"+.1f" }}</td>
<td>{{ ranked_player.player.full_name }}</td>
<td>{{ ranked_player.rating }}</td>
<td>{{ ranked_player.games_played }}</td>
<td>{{ ranked_player.wins }}</td>
<td>{{ ranked_player.losses }}</td>
<td>{{ ranked_player.win_percent|percentage:1 }}</td>
<td>{{ ranked_player.points_per_game|floatformat }}</td>
<td>{{ ranked_player.avg_point_differential|stringformat:"+.1f" }}</td>
</tr>
{% endfor %}
{% for unranked_player in unranked_players %}
<tr id='player-ranking'>
<td>N/A</td>
<td>{{ unranked_player.player.full_name }}</td>
<td>{{ unranked_player.rating }}</td>
<td>{{ unranked_player.games_played }}</td>
<td>{{ unranked_player.wins }}</td>
<td>{{ unranked_player.losses }}</td>
<td>{{ unranked_player.win_percent|percentage:1 }}</td>
<td>{{ unranked_player.points_per_game|floatformat }}</td>
<td>{{ unranked_player.avg_point_differential|stringformat:"+.1f" }}</td>
</tr>
{% endfor %}
</table>

{% if unranked_players %}
<p id='unranked-warning'>Note: you must play a minimum of 5 games before being ranked.</p>
{% endif %}

<ul id="recent-matches" list-style-type="none">
{% for match in recent_matches %}
<li>{{ match.description }}</li>
Expand Down
7 changes: 5 additions & 2 deletions leaderboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
def home_page(request):
"""Render view for home page."""
recent_matches = Match.get_recent_matches(num_matches=20)
leaderboard = PlayerRating.objects.all().order_by('-rating')
rated_players = PlayerRating.objects.all().order_by('-rating')
ranked_players = [player for player in rated_players if player.games_played >= 5]
unranked_players = [player for player in rated_players if player.games_played < 5]
match_form = MatchForm()
player_form = PlayerForm()
if request.method == 'POST':
Expand All @@ -28,6 +30,7 @@ def home_page(request):
'recent_matches': recent_matches,
'match_form': match_form,
'player_form': player_form,
'leaderboard': leaderboard
'ranked_players': ranked_players,
'unranked_players': unranked_players
}
)

0 comments on commit 82ff4ee

Please sign in to comment.