From 02531a434fb9a90e6b25477a4c3179fd1cfe3f5b Mon Sep 17 00:00:00 2001
From: Gonzague <gogaz@gogaz.org>
Date: Fri, 20 Mar 2020 19:37:59 +0100
Subject: [PATCH] Fixed blocking errors on official API

---
 backend/lib/official_api/clan.py              |  8 +++++---
 backend/lib/official_api/player.py            |  4 ++--
 backend/migrations/0015_auto_20200320_1834.py | 23 +++++++++++++++++++++++
 backend/models/clashroyale.py                 |  2 +-
 coach_royale/settings.example.py              |  1 +
 5 files changed, 32 insertions(+), 6 deletions(-)
 create mode 100644 backend/migrations/0015_auto_20200320_1834.py

diff --git a/backend/lib/official_api/clan.py b/backend/lib/official_api/clan.py
index ce3e88a..7d735fe 100644
--- a/backend/lib/official_api/clan.py
+++ b/backend/lib/official_api/clan.py
@@ -65,8 +65,10 @@ def refresh_clan_details(command, options, db_clan, api_client):
     if db_clan_history.highest_score is None or db_clan_history.highest_score < clan.clan_score:
         db_clan_history.highest_score = clan.score
 
-    if previous_history and previous_history.highest_score > db_clan_history.highest_score:
-        db_clan_history.highest_score = previous_history.highest_score
+    if previous_history:
+        db_clan_history.highest_score = clan.clan_score if clan.clan_score > previous_history.highest_score else previous_history.highest_score
+    else:
+        db_clan_history.highest_score = clan.clan_score
 
     db_clan_history.save()
     read_clan_members(api_client, clan, db_clan, command, now, options['verbose'], clan_created)
@@ -78,7 +80,7 @@ def refresh_clan_details(command, options, db_clan, api_client):
 def read_clan_members(api_client, clan, db_clan, command, now=timezone.now(), verbose=False, clan_created=False):
     # Read clan members
     read_players = []
-    for player in clan.members:
+    for player in clan.member_list:
         now = timezone.now()
         try:
             db_player = Player.objects.get(tag=player.tag[1:])
diff --git a/backend/lib/official_api/player.py b/backend/lib/official_api/player.py
index 210b428..1ec4049 100644
--- a/backend/lib/official_api/player.py
+++ b/backend/lib/official_api/player.py
@@ -90,12 +90,12 @@ def refresh_player_profile(command, options, db_player: Player, api_client):
     # Player cards
     for card in player.cards:
         db_card = Card.instance_from_data(card)
-        db_pcl, created = PlayerCardLevel.objects.get_or_create(player=db_player, card=db_card)
+        db_pcl, created = PlayerCardLevel.create_or_find(player=db_player, card=db_card)
         if db_pcl.count != card.count:
             db_pcl.count = card.count
         db_pcl.level = card.level
         db_pcl.count = card.count
-        db_pcl.star_level = card.star_level
+        db_pcl.star_level = card.star_level if 'starLevel' in card.keys() else 0
         db_pcl.save()
 
     # Player battles
diff --git a/backend/migrations/0015_auto_20200320_1834.py b/backend/migrations/0015_auto_20200320_1834.py
new file mode 100644
index 0000000..d7896aa
--- /dev/null
+++ b/backend/migrations/0015_auto_20200320_1834.py
@@ -0,0 +1,23 @@
+# Generated by Django 3.0.3 on 2020-03-20 18:34
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('backend', '0014_auto_20200315_1355'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='playercardlevel',
+            name='last_refresh',
+            field=models.DateTimeField(null=True),
+        ),
+        migrations.AddField(
+            model_name='playercardlevel',
+            name='timestamp',
+            field=models.DateTimeField(null=True),
+        ),
+    ]
diff --git a/backend/models/clashroyale.py b/backend/models/clashroyale.py
index db1af12..05fe20b 100644
--- a/backend/models/clashroyale.py
+++ b/backend/models/clashroyale.py
@@ -55,7 +55,7 @@ class Card(BaseModel):
         return card
 
 
-class PlayerCardLevel(BaseModel):
+class PlayerCardLevel(HistoryModel):
     player = models.ForeignKey("Player", related_name='card_level', on_delete=models.CASCADE)
     card = models.ForeignKey(Card, related_name='player', on_delete=models.CASCADE)
     count = models.IntegerField(null=True)
diff --git a/coach_royale/settings.example.py b/coach_royale/settings.example.py
index 551851b..3f8f0b5 100644
--- a/coach_royale/settings.example.py
+++ b/coach_royale/settings.example.py
@@ -76,6 +76,7 @@ INSTALLED_APPS = [
     'backend.apps.BackendConfig',
     'rest_framework',
     'command_log',
+    'backend.lib.clan_rule_matcher',
 ]
 
 MIDDLEWARE = [
-- 
2.11.0

