From 5cb6e64e53c44011d3e25d6ffbe0e69e558858ae Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Fri, 3 Jan 2025 17:56:04 +0100 Subject: [PATCH 1/3] Add John Wick Award badge Fix #26 --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/kosmotive/cs2pb?shareId=XXXX-XXXX-XXXX-XXXX). --- django/stats/models.py | 17 +++++++++++++++++ django/stats/templates/stats/squads.html | 6 ++++++ django/stats/tests.py | 24 ++++++++++++++++++++++++ django/stats/views.py | 1 + 4 files changed, 48 insertions(+) diff --git a/django/stats/models.py b/django/stats/models.py index 2a8c624..b7f63f0 100644 --- a/django/stats/models.py +++ b/django/stats/models.py @@ -1050,6 +1050,7 @@ def award(participation, **kwargs): MatchBadge.award_margin_badge( participation, 'peach', order = 'adr', margin = 0.67, emoji = '🍑', max_adr = 50, max_kd = 0.5, **kwargs, ) + MatchBadge.award_john_wick_award(participation, **kwargs) @staticmethod def award_with_history(participation, old_participations): @@ -1142,6 +1143,22 @@ def award_margin_badge(participation, badge_type_slug, order, margin, emoji, mut for m in participation.player.squad_memberships.all(): m.squad.notify_on_discord(text) + @staticmethod + def award_john_wick_award(participation, mute_discord = False): + badge_type = MatchBadgeType.objects.get(slug = 'john-wick-award') + if MatchBadge.objects.filter(badge_type=badge_type, participation=participation).exists(): + return + if participation.kd > 2 * participation.adr / 100: + log.info(f'{participation.player.name} received the {badge_type.name}') + MatchBadge.objects.create(badge_type = badge_type, participation = participation) + text = ( + f'🏅 <{participation.player.steamid}> has qualified for the **{badge_type.name}** ' + f'on *{participation.pmatch.map_name}*!' + ) + if not mute_discord: + for m in participation.player.squad_memberships.all(): + m.squad.notify_on_discord(text) + class Meta: verbose_name = 'Match-based badge' verbose_name_plural = 'Match-based badges' diff --git a/django/stats/templates/stats/squads.html b/django/stats/templates/stats/squads.html index 0203927..d3dca46 100644 --- a/django/stats/templates/stats/squads.html +++ b/django/stats/templates/stats/squads.html @@ -83,6 +83,12 @@
Be the red lantern of your team with K/D ≤0.5, ADR ≤50, and a substantial deficit in ADR.
+
+
+
John Wick Award
+
Achieve a kills per round ratio much higher than your average damage per round (ADR).
+
+ diff --git a/django/stats/tests.py b/django/stats/tests.py index 8bc2ae6..0654023 100644 --- a/django/stats/tests.py +++ b/django/stats/tests.py @@ -426,6 +426,30 @@ def test_peach_price__kd_and_adr_too_high(self): models.MatchBadge.award(self.mp5) self.assertEqual(len(models.MatchBadge.objects.filter(badge_type = 'peach', participation = self.mp5)), 0) + def test_john_wick_award(self): + """ + Test the 🏅 John Wick Award when the constraint ✅ "K/D > 2 * ADR / 100" is met. + """ + mp1 = self.pmatch.get_participation('76561197967680028') + mp1.kills = 10 + mp1.deaths = 1 + mp1.adr = 400 + mp1.save() + models.MatchBadge.award(mp1) + self.assertEqual(len(models.MatchBadge.objects.filter(badge_type = 'john-wick-award', participation = mp1)), 1) + + def test_john_wick_award_not_met(self): + """ + Test the 🏅 John Wick Award when the constraint ❌ "K/D > 2 * ADR / 100" is not met. + """ + mp1 = self.pmatch.get_participation('76561197967680028') + mp1.kills = 10 + mp1.deaths = 1 + mp1.adr = 600 + mp1.save() + models.MatchBadge.award(mp1) + self.assertEqual(len(models.MatchBadge.objects.filter(badge_type = 'john-wick-award', participation = mp1)), 0) + class MatchBadge__award_with_history(TestCase): diff --git a/django/stats/views.py b/django/stats/views.py index 3fed59d..3171efa 100644 --- a/django/stats/views.py +++ b/django/stats/views.py @@ -60,6 +60,7 @@ 'rising-star', 'ace', 'quad-kill', + 'john-wick-award', ] From c237e35ec429ef859c772ad5711d2e666b495ded Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Fri, 3 Jan 2025 19:14:06 +0100 Subject: [PATCH 2/3] Add "John Wick Award" badge type and logic * **Models**: Add "John Wick Award" badge type to `MatchBadgeType` and implement logic to award the badge in `MatchBadge`. * **Views**: Update badge display logic in `get_badges` to include "John Wick Award". * **Templates**: Update badge display templates in `player.html`, `squads.html`, `profile-card.html`, `sessions.html`, and `sessions-list.html` to include "John Wick Award". * **Tests**: Add tests for "John Wick Award" badge in `MatchBadge__award`. * **Migrations**: Create migration file to add "John Wick Award" badge type to `MatchBadgeType`. --- .../migrations/0021_add_john_wick_award.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 django/stats/migrations/0021_add_john_wick_award.py diff --git a/django/stats/migrations/0021_add_john_wick_award.py b/django/stats/migrations/0021_add_john_wick_award.py new file mode 100644 index 0000000..196ca8f --- /dev/null +++ b/django/stats/migrations/0021_add_john_wick_award.py @@ -0,0 +1,25 @@ +from django.db import migrations + + +def forwards(apps, schema_editor): + MatchBadgeType = apps.get_model('stats', 'MatchBadgeType') + MatchBadgeType.objects.using(schema_editor.connection.alias).bulk_create( + [ + MatchBadgeType(pk='john-wick-award', name='John Wick Award'), + ] + ) + + +def backwards(apps, schema_editor): + MatchBadgeType = apps.get_model('stats', 'MatchBadgeType') + MatchBadgeType.objects.using(schema_editor.connection.alias).filter(pk='john-wick-award').delete() + + +class Migration(migrations.Migration): + dependencies = [ + ('stats', '0020_rename_completed_timestamp_updatetask_completion_timestamp'), + ] + + operations = [ + migrations.RunPython(forwards, backwards), + ] From 4eb55e1786ebeacf0900357372d18b7456d05b1c Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Fri, 3 Jan 2025 19:54:33 +0100 Subject: [PATCH 3/3] Apply suggestions from code review --- django/stats/models.py | 2 +- django/stats/templates/stats/squads.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/django/stats/models.py b/django/stats/models.py index b7f63f0..f41ce5e 100644 --- a/django/stats/models.py +++ b/django/stats/models.py @@ -1152,7 +1152,7 @@ def award_john_wick_award(participation, mute_discord = False): log.info(f'{participation.player.name} received the {badge_type.name}') MatchBadge.objects.create(badge_type = badge_type, participation = participation) text = ( - f'🏅 <{participation.player.steamid}> has qualified for the **{badge_type.name}** ' + f'☠️ <{participation.player.steamid}> bagged the **{badge_type.name}** ' f'on *{participation.pmatch.map_name}*!' ) if not mute_discord: diff --git a/django/stats/templates/stats/squads.html b/django/stats/templates/stats/squads.html index d3dca46..90c3243 100644 --- a/django/stats/templates/stats/squads.html +++ b/django/stats/templates/stats/squads.html @@ -86,7 +86,7 @@
John Wick Award
-
Achieve a kills per round ratio much higher than your average damage per round (ADR).
+
Gain a surprisingly high K/D ratio, compared to the ADR.