From 6d6f188af3b593712a163c8cf48b77a9d20627f9 Mon Sep 17 00:00:00 2001 From: Dakota Date: Mon, 29 Apr 2024 17:02:49 +0200 Subject: [PATCH] close #6 --- pyZUnivers/loot_infos.py | 10 +++--- pyZUnivers/user.py | 8 ++++- tests/test_user_loot_infos.py | 68 +++++++++++++++++++++++++++++------ 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/pyZUnivers/loot_infos.py b/pyZUnivers/loot_infos.py index 80c3d74..523c927 100644 --- a/pyZUnivers/loot_infos.py +++ b/pyZUnivers/loot_infos.py @@ -1,5 +1,5 @@ import urllib.parse -from datetime import datetime, timedelta +from datetime import datetime, timedelta, date import pytz from .api_responses import LootInfos @@ -16,10 +16,13 @@ def __init__(self, username: str) -> None: datas: LootInfos = get_datas(f"{API_BASE_URL}/loot/{self.__parsed_name}") self.loot_infos = datas['lootInfos'] self.__last_loot_count = self.loot_infos[-1]['count'] + self.__last_date_looted = None def __get_journa_count(self) -> int: last_loots = self.loot_infos[::-1] for index, loot in enumerate(last_loots): + if loot['count'] > 0 and not self.__last_date_looted: + self.__last_date_looted = datetime.strptime(loot['date'], "%Y-%m-%d").date() if loot['count'] >= 2000: bonus_index = index break @@ -37,11 +40,10 @@ def bonus(self) -> bool: return not self.__get_journa_count() >= 7 @property - def bonus_when(self) -> '_Date': - now = datetime.now(pytz.timezone('Europe/Paris')).date() + def bonus_when(self) -> date: when_days = timedelta(days=7-self.__get_journa_count()) - return now + when_days + return self.__last_date_looted + when_days @property def journa(self) -> bool: diff --git a/pyZUnivers/user.py b/pyZUnivers/user.py index 565a50c..3270fb5 100644 --- a/pyZUnivers/user.py +++ b/pyZUnivers/user.py @@ -203,8 +203,14 @@ def get_checker(username: str) -> Checker: if is_advent_calendar(): advent = User.get_advent_calendar(username) else: advent = None + + now = datetime.now(pytz.timezone('Europe/Paris')).strftime('%Y-%m-%d') + + bonus = loot_infos.bonus + if not loot_infos.journa and loot_infos.bonus_when.strftime('%Y-%m-%d') == now: + bonus = False - return {"journa": loot_infos.journa, "bonus": loot_infos.bonus, "advent": advent} + return {"journa": loot_infos.journa, "bonus": bonus, "advent": advent} @staticmethod def get_insomniaque(username: str) -> Insomniaque: diff --git a/tests/test_user_loot_infos.py b/tests/test_user_loot_infos.py index 6a11aa6..2a5bc54 100644 --- a/tests/test_user_loot_infos.py +++ b/tests/test_user_loot_infos.py @@ -1,6 +1,8 @@ import unittest +from unittest.mock import patch, MagicMock import pyZUnivers -from datetime import date +from datetime import date, datetime +import pytz class UserLootInfosTest(unittest.TestCase): @@ -19,15 +21,59 @@ def test_bonus_when(self) -> None: def test_bonus_true_missing_journa(self) -> None: self.loot_infos_powaza.loot_infos = [ - {'count': 2000}, - {'count': 0}, - {'count': 0}, - {'count': 0}, - {'count': 0}, - {'count': 0}, - {'count': 0}, - {'count': 0}, - {'count': 1000}, + {'date': '2024-04-21', 'count': 2000}, + {'date': '2024-04-22', 'count': 0}, + {'date': '2024-04-23', 'count': 0}, + {'date': '2024-04-24', 'count': 0}, + {'date': '2024-04-25', 'count': 0}, + {'date': '2024-04-26', 'count': 0}, + {'date': '2024-04-27', 'count': 0}, + {'date': '2024-04-28', 'count': 0}, + {'date': '2024-04-29', 'count': 1000}, ] - self.assertTrue(self.loot_infos_powaza.bonus) \ No newline at end of file + self.assertTrue(self.loot_infos_powaza.bonus) + self.assertEqual('2024-05-05', self.loot_infos_powaza.bonus_when.strftime('%Y-%m-%d')) + + def test_bonus_true_when_journa_not_done_yet(self) -> None: + self.loot_infos_powaza.__last_loot_count = 0 + self.loot_infos_powaza.loot_infos = [ + {'date': '2024-04-22', 'count': 2000}, + {'date': '2024-04-23', 'count': 1000}, + {'date': '2024-04-24', 'count': 1000}, + {'date': '2024-04-25', 'count': 1000}, + {'date': '2024-04-26', 'count': 1000}, + {'date': '2024-04-27', 'count': 1000}, + {'date': '2024-04-28', 'count': 1000}, + {'date': '2024-04-29', 'count': 0}, + ] + + self.assertTrue(self.loot_infos_powaza.bonus) + self.assertEqual('2024-04-29', self.loot_infos_powaza.bonus_when.strftime('%Y-%m-%d')) + + def test_bonus_false_when_journa_is_done(self) -> None: + self.loot_infos_powaza.loot_infos = [ + {'date': '2024-04-22', 'count': 2000}, + {'date': '2024-04-23', 'count': 1000}, + {'date': '2024-04-24', 'count': 1000}, + {'date': '2024-04-25', 'count': 1000}, + {'date': '2024-04-26', 'count': 1000}, + {'date': '2024-04-27', 'count': 1000}, + {'date': '2024-04-28', 'count': 1000}, + {'date': '2024-04-29', 'count': 1000}, + ] + + self.assertFalse(self.loot_infos_powaza.bonus) + self.assertEqual('2024-04-29', self.loot_infos_powaza.bonus_when.strftime('%Y-%m-%d')) + + def test_journa_not_done_bonus_needed(self) -> None: + loot_infos_stub = MagicMock() + loot_infos_stub.journa = False + loot_infos_stub.bonus = True + now = datetime.now(pytz.timezone('Europe/Paris')).strftime('%Y-%m-%d') + loot_infos_stub.bonus_when.strftime.return_value = now + + with patch('pyZUnivers.user.UserLootInfos', return_value=loot_infos_stub): + result = pyZUnivers.User.get_checker('powaza') + + self.assertEqual(result, {'journa': False, 'bonus': False, 'advent': None}) \ No newline at end of file