From 25aa3834880b08f84eeadfdd3f6a36569a1d4008 Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 19:53:28 +0100 Subject: [PATCH 01/13] UPDATE: tests to allwo early start Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- tests/whist_core/session/test_matcher.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/whist_core/session/test_matcher.py b/tests/whist_core/session/test_matcher.py index 417f08ff..1a2a30eb 100644 --- a/tests/whist_core/session/test_matcher.py +++ b/tests/whist_core/session/test_matcher.py @@ -36,13 +36,26 @@ def test_round_robin_distribute(self): self.assertEqual(distribution[3].player_index, 3) self.assertEqual(distribution[3].team_id, 1) + def test_random_min_player_distribute(self): + user_list = UserList() + user_list.append(self.players[0]) + user_list.append(self.players[1]) + random_matcher = RandomMatcher(number_teams=2) + distribution = random_matcher.distribute(user_list) + self.assertEqual(distribution[0].player_index, 0) + self.assertEqual(distribution[0].team_id, 0) + self.assertEqual(distribution[1].player_index, 1) + def test_round_robin_min_player_distribute(self): user_list = UserList() user_list.append(self.players[0]) user_list.append(self.players[1]) - random_matcher = RandomMatcher(number_teams=2, team_size=2) - with self.assertRaises(NotEnoughPlayersError): - _ = random_matcher.distribute(user_list) + robin_matcher = RoundRobinMatcher(number_teams=2, team_size=2) + distribution = robin_matcher.distribute(user_list) + self.assertEqual(distribution[0].player_index, 0) + self.assertEqual(distribution[0].team_id, 0) + self.assertEqual(distribution[1].player_index, 1) + def test_second_round_robin(self): _ = self.robin_matcher.distribute(self.user_list) From edf3172606cd67a0b55f48431ec3e9e8864f6469 Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 19:53:45 +0100 Subject: [PATCH 02/13] REMOVE: team size Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- whist_core/session/matcher.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/whist_core/session/matcher.py b/whist_core/session/matcher.py index 0cd0e046..acbc35bb 100644 --- a/whist_core/session/matcher.py +++ b/whist_core/session/matcher.py @@ -22,7 +22,6 @@ class Matcher(abc.ABC, BaseModel): """ teams: list[Distribution] = [] number_teams: int - team_size: int def __init_subclass__(cls, **kwargs: Any) -> None: """ @@ -59,19 +58,18 @@ class RoundRobinMatcher(Matcher): iteration: int = 0 distributions: list[Distribution] = [] - def __init__(self, number_teams: int, team_size: int, **data): + def __init__(self, number_teams: int, **data): """ Constructor. See details in base class. :param number_teams: :param team_size: :param data: """ - super().__init__(number_teams=number_teams, team_size=team_size, **data) - number_players = self.team_size * self.number_teams + super().__init__(number_teams=number_teams, **data) if len(self.distributions) == 0: for distribution_int in sorted( - set(permutations((x % self.number_teams for x in range(number_players))))): + set(permutations((x % self.number_teams for x in range(4))))): distribution = Distribution() for player_index, team_id in enumerate(distribution_int): distribution.add(DistributionEntry(player_index=player_index, team_id=team_id)) @@ -103,9 +101,8 @@ def distribute(self, users: UserList) -> Distribution: :rtype: None """ players = users.players - if len(players) != self.number_teams * self.team_size: - raise NotEnoughPlayersError() - teams: list = list(range(0, self.team_size)) * self.number_teams + team_size:int = int(self.number_teams / len(players)) + teams: list = list(range(0, team_size)) * self.number_teams distribution: Distribution = Distribution() for player_index in range(len(players)): team_id = random.choice(teams) # nosec random From 0c0ba8d132357fc4e18bf511b08f506e983bb6cf Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 19:53:57 +0100 Subject: [PATCH 03/13] ADD: test for early table start Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- tests/whist_core/session/test_table.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/whist_core/session/test_table.py b/tests/whist_core/session/test_table.py index febe2e73..b60df39f 100644 --- a/tests/whist_core/session/test_table.py +++ b/tests/whist_core/session/test_table.py @@ -125,6 +125,12 @@ def test_start_robin(self): self.assertIsInstance(self.table.current_rubber, Rubber) self.assertTrue(isinstance(self.table.matcher, RoundRobinMatcher)) + def test_start_early(self): + self.table.join(self.player) + self.table.player_ready(self.player) + self.table.start() + self.assertTrue(self.table.started) + def test_not_ready_start(self): self.table.join(self.player) with self.assertRaises(TableNotReadyError): From 59899370ebcec575b34e8b17d68d7dd439fcd3a9 Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 19:59:36 +0100 Subject: [PATCH 04/13] FIX: dynamic adapt of players Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- whist_core/session/matcher.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/whist_core/session/matcher.py b/whist_core/session/matcher.py index acbc35bb..d6118d30 100644 --- a/whist_core/session/matcher.py +++ b/whist_core/session/matcher.py @@ -68,12 +68,7 @@ def __init__(self, number_teams: int, **data): super().__init__(number_teams=number_teams, **data) if len(self.distributions) == 0: - for distribution_int in sorted( - set(permutations((x % self.number_teams for x in range(4))))): - distribution = Distribution() - for player_index, team_id in enumerate(distribution_int): - distribution.add(DistributionEntry(player_index=player_index, team_id=team_id)) - self.distributions.append(distribution) + self._precalculate_distributions() def distribute(self, users: UserList) -> Distribution: """ @@ -82,12 +77,23 @@ def distribute(self, users: UserList) -> Distribution: :param users: the players to be distributed to teams :return: the teams in round robin distribution """ + if len(self.distributions) != len(users): + self.distributions = [] + self._precalculate_distributions(len(users)) distribution = self.distributions[self.iteration] self.iteration += 1 self._apply_distribution(distribution) return distribution + def _precalculate_distributions(self, number_players: int = 4): + for distribution_int in sorted( + set(permutations((x % self.number_teams for x in range(number_players))))): + distribution = Distribution() + for player_index, team_id in enumerate(distribution_int): + distribution.add(DistributionEntry(player_index=player_index, team_id=team_id)) + self.distributions.append(distribution) + class RandomMatcher(Matcher): """ @@ -101,7 +107,7 @@ def distribute(self, users: UserList) -> Distribution: :rtype: None """ players = users.players - team_size:int = int(self.number_teams / len(players)) + team_size: int = int(self.number_teams / len(players)) teams: list = list(range(0, team_size)) * self.number_teams distribution: Distribution = Distribution() for player_index in range(len(players)): From 0c6b305377cc881dee89d142bc767b02f73b98c8 Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 19:59:53 +0100 Subject: [PATCH 05/13] REMOVAL: of pre compile Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- whist_core/session/matcher.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/whist_core/session/matcher.py b/whist_core/session/matcher.py index d6118d30..da7070a4 100644 --- a/whist_core/session/matcher.py +++ b/whist_core/session/matcher.py @@ -67,8 +67,6 @@ def __init__(self, number_teams: int, **data): """ super().__init__(number_teams=number_teams, **data) - if len(self.distributions) == 0: - self._precalculate_distributions() def distribute(self, users: UserList) -> Distribution: """ From 876e17199bb760c9302d41d4eeb0fac12491339f Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 20:00:22 +0100 Subject: [PATCH 06/13] REMOVE: default value Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- whist_core/session/matcher.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/whist_core/session/matcher.py b/whist_core/session/matcher.py index da7070a4..dad24a38 100644 --- a/whist_core/session/matcher.py +++ b/whist_core/session/matcher.py @@ -67,7 +67,6 @@ def __init__(self, number_teams: int, **data): """ super().__init__(number_teams=number_teams, **data) - def distribute(self, users: UserList) -> Distribution: """ Distributes one player to each team each round in order of the user list. Repeats until @@ -84,7 +83,7 @@ def distribute(self, users: UserList) -> Distribution: return distribution - def _precalculate_distributions(self, number_players: int = 4): + def _precalculate_distributions(self, number_players: int): for distribution_int in sorted( set(permutations((x % self.number_teams for x in range(number_players))))): distribution = Distribution() From 9f2ecf139def5df2dd0b11660bcd4b50ed93ef9f Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 20:06:54 +0100 Subject: [PATCH 07/13] FIX: calculation Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- whist_core/session/matcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/whist_core/session/matcher.py b/whist_core/session/matcher.py index dad24a38..f0ada6a2 100644 --- a/whist_core/session/matcher.py +++ b/whist_core/session/matcher.py @@ -104,7 +104,7 @@ def distribute(self, users: UserList) -> Distribution: :rtype: None """ players = users.players - team_size: int = int(self.number_teams / len(players)) + team_size: int = int(len(players) / self.number_teams) teams: list = list(range(0, team_size)) * self.number_teams distribution: Distribution = Distribution() for player_index in range(len(players)): From f85cbe13751ffc136d4f2a4d1f5f1761c965acc3 Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 20:07:13 +0100 Subject: [PATCH 08/13] UPDATE: assertions Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- tests/whist_core/session/test_matcher.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/whist_core/session/test_matcher.py b/tests/whist_core/session/test_matcher.py index 1a2a30eb..f07bd34f 100644 --- a/tests/whist_core/session/test_matcher.py +++ b/tests/whist_core/session/test_matcher.py @@ -14,8 +14,8 @@ def setUp(self) -> None: player_d = Player(user_id=5, username='d', rating=1) self.players = [player_a, player_b, player_c, player_d] self.user_list = UserList() - self.random_matcher = RandomMatcher(number_teams=2, team_size=2) - self.robin_matcher = RoundRobinMatcher(number_teams=2, team_size=2) + self.random_matcher = RandomMatcher(number_teams=2) + self.robin_matcher = RoundRobinMatcher(number_teams=2) for player in self.players: self.user_list.append(player) @@ -45,17 +45,18 @@ def test_random_min_player_distribute(self): self.assertEqual(distribution[0].player_index, 0) self.assertEqual(distribution[0].team_id, 0) self.assertEqual(distribution[1].player_index, 1) + self.assertEqual(len(user_list), len(distribution)) def test_round_robin_min_player_distribute(self): user_list = UserList() user_list.append(self.players[0]) user_list.append(self.players[1]) - robin_matcher = RoundRobinMatcher(number_teams=2, team_size=2) + robin_matcher = RoundRobinMatcher(number_teams=2) distribution = robin_matcher.distribute(user_list) self.assertEqual(distribution[0].player_index, 0) self.assertEqual(distribution[0].team_id, 0) self.assertEqual(distribution[1].player_index, 1) - + self.assertEqual(len(user_list), len(distribution)) def test_second_round_robin(self): _ = self.robin_matcher.distribute(self.user_list) From 71fddb747d140741f73f286689d9657da8f9edb7 Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 20:12:59 +0100 Subject: [PATCH 09/13] REMOVE: no longer used argument Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- tests/whist_core/session/test_table.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/whist_core/session/test_table.py b/tests/whist_core/session/test_table.py index b60df39f..1f2c51bc 100644 --- a/tests/whist_core/session/test_table.py +++ b/tests/whist_core/session/test_table.py @@ -16,23 +16,23 @@ def setUp(self) -> None: super().setUp() self.mock_user_list = MagicMock() self.table = Table(name='test table', min_player=1, max_player=4, - matcher=RoundRobinMatcher(number_teams=2, team_size=2)) + matcher=RoundRobinMatcher(number_teams=2)) def test_table_random_matcher_from_dict(self): - self.table.matcher = RandomMatcher(number_teams=2, team_size=2) + self.table.matcher = RandomMatcher(number_teams=2) table_from_dict = Table(**self.table.dict()) self.assertEqual(self.table, table_from_dict) self.assertIsInstance(table_from_dict.matcher, RandomMatcher) def test_table_random_matcher_from_dict_generic(self): - self.table.matcher = RandomMatcher(number_teams=2, team_size=2) + self.table.matcher = RandomMatcher(number_teams=2) table_dict = dict(self.table) table_from_dict = Table(**table_dict) self.assertEqual(self.table, table_from_dict) self.assertIsInstance(table_from_dict.matcher, RandomMatcher) def test_table_random_matcher_from_json(self): - self.table.matcher = RandomMatcher(number_teams=2, team_size=2) + self.table.matcher = RandomMatcher(number_teams=2) table_json = self.table.json() table_dict_from_json = json.loads(table_json) table_from_json = Table(**table_dict_from_json) @@ -40,7 +40,7 @@ def test_table_random_matcher_from_json(self): self.assertIsInstance(table_from_json.matcher, RandomMatcher) def test_table_random_matcher_from_json_generic(self): - self.table.matcher = RandomMatcher(number_teams=2, team_size=2) + self.table.matcher = RandomMatcher(number_teams=2) table_json = self.table.json() table_from_json = Table(**json.loads(table_json)) self.assertEqual(self.table, table_from_json) @@ -49,7 +49,7 @@ def test_table_random_matcher_from_json_generic(self): def test_min_max_validation(self): with self.assertRaises(TableSettingsError): _ = Table(name='faulty table', min_player=3, max_player=2, - matcher=RandomMatcher(number_teams=2, team_size=2)) + matcher=RandomMatcher(number_teams=2)) def test_ready(self): self.table.join(self.player) @@ -64,7 +64,7 @@ def test_not_ready(self): def test_not_ready_min_player(self): table = Table(name='test table', min_player=2, max_player=4, - matcher=RandomMatcher(number_teams=2, team_size=2)) + matcher=RandomMatcher(number_teams=2)) table.join(self.player) table.player_ready(self.player) self.assertFalse(table.ready) @@ -109,7 +109,7 @@ def test_conversion(self): self.assertEqual(self.table, table) def test_start_random(self): - self.table.matcher = RandomMatcher(number_teams=2, team_size=2) + self.table.matcher = RandomMatcher(number_teams=2) self._ready_four_players() self.table.start() self.assertTrue(self.table.started) From 278493a51c7c8403cf61c3d89e4a9f5d46e532b3 Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 20:17:29 +0100 Subject: [PATCH 10/13] BUMP: release version Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- pyproject.toml | 2 +- whist_core/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 7131aa34..cc6b1cc1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "whist-core" # remember to also update the version in __init__.py! -version = "0.7.0" +version = "0.7.1" description = "Whist rules implementation" authors = ["Whist-Team"] license = "MIT" diff --git a/whist_core/__init__.py b/whist_core/__init__.py index 0e4e8974..67bce45b 100644 --- a/whist_core/__init__.py +++ b/whist_core/__init__.py @@ -4,7 +4,7 @@ import os # remember to also update the version in pyproject.toml! -__version__ = '0.7.0' +__version__ = '0.7.1' ALGORITHM = os.getenv('ALGORITHM', 'HS256') SECRET_KEY = os.getenv('SECRET_KEY', 'geheim') From d58e054d3fc77417bf705880af42bd6258ca9446 Mon Sep 17 00:00:00 2001 From: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> Date: Fri, 25 Nov 2022 20:24:15 +0100 Subject: [PATCH 11/13] REMOVE: no longer used import Signed-off-by: Segelzwerg <25705862+Segelzwerg@users.noreply.github.com> --- whist_core/session/matcher.py | 1 - 1 file changed, 1 deletion(-) diff --git a/whist_core/session/matcher.py b/whist_core/session/matcher.py index f0ada6a2..74506786 100644 --- a/whist_core/session/matcher.py +++ b/whist_core/session/matcher.py @@ -8,7 +8,6 @@ from pydantic import BaseModel -from whist_core.error.matcher_error import NotEnoughPlayersError from whist_core.session.distribution import Distribution, DistributionEntry from whist_core.session.userlist import UserList From 88b5225611944d05393cdc4846d486c2cea1deee Mon Sep 17 00:00:00 2001 From: Marcel <25705862+Segelzwerg@users.noreply.github.com> Date: Sat, 26 Nov 2022 15:01:31 +0100 Subject: [PATCH 12/13] Update pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f125f35c..2e721c9c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "whist-core" # remember to also update the version in __init__.py! -version = "0.7.1" +version = "0.8.0" description = "Whist rules implementation" authors = ["Whist-Team"] license = "MIT" From ca00c823037f5430f540360a4cff54d322a631e2 Mon Sep 17 00:00:00 2001 From: Marcel <25705862+Segelzwerg@users.noreply.github.com> Date: Sat, 26 Nov 2022 15:02:09 +0100 Subject: [PATCH 13/13] Update __init__.py --- whist_core/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/whist_core/__init__.py b/whist_core/__init__.py index 67bce45b..b2b976c6 100644 --- a/whist_core/__init__.py +++ b/whist_core/__init__.py @@ -4,7 +4,7 @@ import os # remember to also update the version in pyproject.toml! -__version__ = '0.7.1' +__version__ = '0.8.0' ALGORITHM = os.getenv('ALGORITHM', 'HS256') SECRET_KEY = os.getenv('SECRET_KEY', 'geheim')