sort_card_list moved to utils and refactored

This commit is contained in:
Jakub Senderák
2020-04-25 18:30:43 +02:00
parent 834e03c172
commit 618e632ce4
3 changed files with 18 additions and 14 deletions
+2 -2
View File
@@ -3,7 +3,7 @@ from bridzik import Bridzik, Card, Card_colors, Card_values, BridzikException
import json import json
from flask import render_template, url_for, flash, redirect from flask import render_template, url_for, flash, redirect
from api.forms import GuessForm, PlayForm, AdminForm 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() b = Bridzik()
@@ -24,7 +24,7 @@ def status(player):
game_status = b.get_status(player) game_status = b.get_status(player)
action = None action = None
form = 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] game_status['player_cards'] = [str(c) for c in player_cards]
points_sums = get_points_sums(game_status['standings']) points_sums = get_points_sums(game_status['standings'])
if b.is_completed() or b.series[-1].get_last_round().get_active_player() != player: if b.is_completed() or b.series[-1].get_last_round().get_active_player() != player:
+16
View File
@@ -1,3 +1,5 @@
from bridzik import Card_colors
def get_points_sums(standings: []): def get_points_sums(standings: []):
sums = [0] * 4 sums = [0] * 4
for series in standings: for series in standings:
@@ -5,3 +7,17 @@ def get_points_sums(standings: []):
for player, points in enumerate(round): for player, points in enumerate(round):
sums[player] += points sums[player] += points
return sums 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
-12
View File
@@ -69,18 +69,6 @@ class Card():
def __repr__(self): def __repr__(self):
return '<Card {}_{}>'.format(self.color.name, self.value.name) return '<Card {}_{}>'.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): class JSONEncoder(JSONEncoder):
def default(self, obj): def default(self, obj):
if isinstance(obj, Card): if isinstance(obj, Card):