From 618e632ce463fdfdf8c44ef073248f3ec5c49da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Sender=C3=A1k?= Date: Sat, 25 Apr 2020 18:30:43 +0200 Subject: [PATCH] sort_card_list moved to utils and refactored --- api/routes.py | 4 ++-- api/utils.py | 16 ++++++++++++++++ bridzik.py | 12 ------------ 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/api/routes.py b/api/routes.py index 6bd2812..d2cc7fc 100644 --- a/api/routes.py +++ b/api/routes.py @@ -3,7 +3,7 @@ from bridzik import Bridzik, Card, Card_colors, Card_values, BridzikException import json from flask import render_template, url_for, flash, redirect from api.forms import GuessForm, PlayForm, AdminForm -from api.utils import get_points_sums +from api.utils import get_points_sums, sort_card_list b = Bridzik() @@ -24,7 +24,7 @@ def status(player): game_status = b.get_status(player) action = None form = None - player_cards = Card.sort_card_list(b.series[-1].get_last_round().player_cards[player]) + player_cards = sort_card_list(b.series[-1].get_last_round().player_cards[player]) game_status['player_cards'] = [str(c) for c in player_cards] points_sums = get_points_sums(game_status['standings']) if b.is_completed() or b.series[-1].get_last_round().get_active_player() != player: diff --git a/api/utils.py b/api/utils.py index d4e156f..d56eb00 100644 --- a/api/utils.py +++ b/api/utils.py @@ -1,3 +1,5 @@ +from bridzik import Card_colors + def get_points_sums(standings: []): sums = [0] * 4 for series in standings: @@ -5,3 +7,17 @@ def get_points_sums(standings: []): for player, points in enumerate(round): sums[player] += points return sums + +def sort_card_list(input_card_set: []) -> []: + color_paritions = [ + [c for c in input_card_set if c.color == Card_colors['HEARTS']], + [c for c in input_card_set if c.color == Card_colors['LEAVES']], + [c for c in input_card_set if c.color == Card_colors['ACORNS']], + [c for c in input_card_set if c.color == Card_colors['BELLS']] + + ] + output_list = [] + for color_list in color_paritions: + color_list.sort(key=lambda a : a.value) + output_list.extend(color_list) + return output_list diff --git a/bridzik.py b/bridzik.py index b702a2d..8325608 100644 --- a/bridzik.py +++ b/bridzik.py @@ -69,18 +69,6 @@ class Card(): def __repr__(self): return ''.format(self.color.name, self.value.name) - @staticmethod - def sort_card_list(input_card_set: []) -> []: - hearts = [c for c in input_card_set if c.color == Card_colors['HEARTS']] - leaves = [c for c in input_card_set if c.color == Card_colors['LEAVES']] - acorns = [c for c in input_card_set if c.color == Card_colors['ACORNS']] - bells = [c for c in input_card_set if c.color == Card_colors['BELLS']] - hearts.sort(key=lambda a : a.value) - leaves.sort(key=lambda a : a.value) - acorns.sort(key=lambda a : a.value) - bells.sort(key=lambda a : a.value) - return hearts + leaves + acorns + bells - class JSONEncoder(JSONEncoder): def default(self, obj): if isinstance(obj, Card):