Skip to content

Commit

Permalink
close #6
Browse files Browse the repository at this point in the history
  • Loading branch information
MalikAza committed Apr 29, 2024
1 parent a74a196 commit 6d6f188
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
10 changes: 6 additions & 4 deletions pyZUnivers/loot_infos.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion pyZUnivers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
68 changes: 57 additions & 11 deletions tests/test_user_loot_infos.py
Original file line number Diff line number Diff line change
@@ -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):

Expand All @@ -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)
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})

0 comments on commit 6d6f188

Please sign in to comment.