Skip to content

Commit

Permalink
[#44] Smoothing out the scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
jrsmth-tier2 committed Apr 10, 2024
1 parent 7433f7e commit 8d72637
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/app/archbishop/archbishop.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def hello():
@archbishop.route("/scroll", methods=['POST'])
def unroll():
""" Handle slack command for scroll information """
group = redis.get_complex(request.form["team_id"], Group)
group_id = request.form["team_id"]
group = redis.get_complex(group_id, Group)
if group is None:
return Response("Cannot find group with id " + group_id, status=400)
formatted_scroll = ""
position = 0
for record in group.scroll:
Expand Down
21 changes: 12 additions & 9 deletions src/app/model/group/group.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from dataclasses import dataclass
from datetime import datetime

import shortuuid

from src.app.model.base import Base
from src.app.model.group.record import Record
from src.app.model.group.player import Player
Expand Down Expand Up @@ -42,6 +40,10 @@ def update_player(self, player):

def update_scroll(self, player):
""" Update scroll if new streak is worthy """
if self.__is_empty_scroll():
self.scroll.append(player.get_record())
return

if self.__is_unworthy(player.streak):
return

Expand All @@ -53,8 +55,7 @@ def update_scroll(self, player):
self.scroll = sorted(unsorted_scroll, key=lambda x: x.streak, reverse=True)

else:
new_record = Record(player.name, player.streak, player.streak_id, datetime.today().strftime('%d/%m/%Y'))
self.scroll.append(new_record)
self.scroll.append(player.get_record())
self.scroll = sorted(self.scroll, key=lambda x: x.streak, reverse=True)
if len(self.scroll) > 3:
self.scroll.pop()
Expand All @@ -73,11 +74,13 @@ def dethrone(self):
def __is_unworthy(self, new_streak):
""" Determine if streak is unworthy of scroll update by comparison to the lowest record """
sorted_scroll = sorted(self.scroll, key=lambda x: x.streak, reverse=False)
return len(sorted_scroll) == 0 or new_streak < sorted_scroll[0].streak
return len(self.scroll) == 3 and new_streak < sorted_scroll[0].streak

def __is_active(self, streak_id):
""" Determine if player streak is active in scroll by comparison with recorded streak ids """
if len(self.scroll) != 0:
matching_ids = [x for x in self.scroll if x.streak_id == streak_id]
return len(matching_ids) != 0
else: return False
matching_ids = [x for x in self.scroll if x.streak_id == streak_id]
return len(matching_ids) != 0

def __is_empty_scroll(self):
""" Determine if scroll is empty """
return len(self.scroll) == 0
15 changes: 15 additions & 0 deletions src/app/model/group/group_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ def should_not_update_scroll_if_new_streak_lower_than_the_lowest_record_streak(s
# Then : scroll is left untouched
assert subject.scroll is scroll

def should_update_scroll_if_new_streak_lower_than_the_lowest_record_streak_but_scroll_has_capacity(self):
""" Should update scroll if new streak lower than the lowest record streak but scroll has capacity"""
# Given
players = [Player('Adam', 4, '1', 1000), Player('Hayden', 3, '2', 1000)]
scroll = [Record('Adam', 4, '1', 'today'), Record('Hayden', 3, '2', 'today')]
subject = Group(self.group_name, players, Player('Adam', 4, '1', 1000), scroll)

test = Player('Maciej', 1, '4', 1000)

# When
subject.update_scroll(test)

# Then : test is added to the scroll
assert subject.scroll == [Record('Adam', 4, '1', 'today'), Record('Hayden', 3, '2', 'today'), test.get_record()]

def should_update_existing_record_if_player_streak_is_active_on_the_scroll(self):
""" Should update existing record if worthy player streak is active on the scroll """
# Given
Expand Down
7 changes: 6 additions & 1 deletion src/app/model/group/player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass

from datetime import datetime
from src.app.model.base import Base
from src.app.model.group.record import Record


@dataclass
Expand All @@ -19,3 +20,7 @@ def from_dict(cls, dic):
streak_id=dic["streak_id"],
score=dic["score"]
)

def get_record(self):
""" Convert a player into a record """
return Record(self.name, self.streak, self.streak_id, datetime.today().strftime('%d/%m/%Y'))

0 comments on commit 8d72637

Please sign in to comment.