Skip to content

Commit 0ace227

Browse files
committed
Created separate classes for views, removing them from templates
1 parent bcedbee commit 0ace227

File tree

11 files changed

+169
-119
lines changed

11 files changed

+169
-119
lines changed

rummy/controller/ai_controller.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
from random import choice
22

3+
from rummy.controller.player_controller import PlayerController
34
from rummy.player.player import Player
45
from rummy.ui.view import View
6+
from rummy.view.ai_view import AiView
57

68

7-
class AiController:
9+
class AiController(PlayerController):
810

911
@staticmethod
1012
def show_start_turn(player: Player):
1113
output = ''
1214
if player.ai_only:
13-
output += View.template_turn_start(player)
15+
output += AiView.turn_start(player)
1416
else:
15-
output += View.template_ai_turn_start(player)
16-
output += View.template_ai_thought(player, 'Choosing pick up')
17+
output += AiView.turn_start(player)
18+
output += AiView.thinking(player, 'Choosing pick up')
1719
View.render(output)
1820

1921
@staticmethod
2022
def show_end_turn(player: Player):
2123
output = ''
2224
if player.ai_only:
23-
output += View.template_ai_turn_end(player)
24-
output += View.template_ai_thought(player, 'Choosing card to discard')
25+
output += AiView.turn_end(player)
26+
output += AiView.thinking(player, 'Choosing card to discard')
2527
View.render(output)
2628

2729
@staticmethod
2830
def show_knocked(player):
2931
if player.has_someone_knocked():
30-
View.render(View.prepare_template('/knocked.txt'))
32+
View.render(AiView.knocked())
3133

3234
@staticmethod
3335
def show_discard(player: Player):
3436
output = ''
3537
if player.ai_only:
36-
output += View.template_ai_hand_data(player.hand.get_score())
37-
output += View.template_player_discarded(player.round.deck.inspect_discard())
38+
output += AiView.hand_data(player.hand.get_score())
39+
output += AiView.discarded(player.round.deck.inspect_discard())
3840
View.render(output)
3941

4042
@classmethod
@@ -44,22 +46,22 @@ def draw_card(cls, player):
4446
current_score = player.hand.get_score()
4547
scores = player.melds.find_discard_scores(player.hand.get_hand(), player.round.deck.inspect_discard())
4648
if player.ai_only:
47-
output += View.template_ai_discard_data(current_score, scores)
49+
output += AiView.discard_data(current_score, scores)
4850
output += cls._choose_pickup(player, current_score, scores)
4951
else:
5052
player.take_from_deck()
51-
output += View.template_ai_thought(player, 'Drawing from deck')
53+
output += AiView.thinking(player, 'Drawing from deck')
5254
View.render(output)
5355

5456
@staticmethod
5557
def _choose_pickup(player, current_score, scores):
5658
output = ''
5759
if min(scores) < current_score - 4 or min(scores) <= 10:
5860
player.take_from_discard()
59-
output += View.template_ai_thought(player, 'Drawing from discard')
61+
output += AiView.thinking(player, 'Drawing from discard')
6062
else:
6163
player.take_from_deck()
62-
output += View.template_ai_thought(player, 'Drawing from deck')
64+
output += AiView.thinking(player, 'Drawing from deck')
6365
return output
6466

6567
@classmethod

rummy/controller/human_controller.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
from rummy.player.human import Human
1+
from rummy.controller.player_controller import PlayerController
22
from rummy.player.player import Player
33
from rummy.ui.player_action_dialog import PlayerActionDialog
44
from rummy.ui.user_input import UserInput
55
from rummy.ui.view import View
6+
from rummy.view.human_view import HumanView
67

78

8-
class HumanController:
9+
class HumanController(PlayerController):
910

1011
@staticmethod
1112
def show_start_turn(player: Player):
12-
if isinstance(player, Human):
13-
View.render(View.template_turn_start(player))
13+
View.render(HumanView.turn_start(player))
1414

1515
@staticmethod
1616
def show_end_turn(player: Player):
17-
if isinstance(player, Human):
18-
View.render(View.template_player_turn_end(player))
17+
View.render(HumanView.turn_end(player))
1918

2019
@staticmethod
2120
def show_knocked(player):
2221
if player.has_someone_knocked():
23-
View.render(View.prepare_template('/knocked.txt'))
22+
View.render(HumanView.knocked())
2423

2524
@staticmethod
2625
def show_discard(player: Player):
27-
if isinstance(player, Human):
28-
View.render(View.template_player_discarded(player.round.deck.inspect_discard()))
26+
View.render(HumanView.discarded(player))
2927

3028
@classmethod
3129
def draw_card(cls, player):

rummy/controller/player_controller.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,11 @@ def show_discard(player: Player):
2626
def draw_card(cls, player):
2727
pass
2828

29-
@staticmethod
30-
@abstractmethod
31-
def _choose_pick_up(player):
32-
pass
33-
3429
@classmethod
3530
@abstractmethod
3631
def discard_or_knock(cls, player):
3732
pass
3833

39-
@staticmethod
40-
@abstractmethod
41-
def _choose_discard(player):
42-
pass
43-
4434
@staticmethod
4535
def show_knocked(player):
4636
if player.has_someone_knocked():

rummy/game/score.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from ansi_colours import AnsiColours as Colour
44

5-
from rummy.ui.view import View
5+
from rummy.view.round_view import RoundView
66

77

88
class Score:
@@ -15,7 +15,7 @@ def get_current_game_scores(self):
1515
def get_end_of_round_scores(self):
1616
output = ''
1717
for p in self.players:
18-
output += View.template_end_of_round_scores(p)
18+
output += RoundView.end_of_round_scores(p)
1919
return output
2020

2121
def update_player_scores(self):

rummy/play.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from rummy.ui.menu_action_dialog import MenuActionDialog
1616
from rummy.ui.user_input import UserInput
1717
from rummy.ui.view import View
18+
from rummy.view.round_view import RoundView
1819

1920

2021
class Play:
@@ -83,7 +84,7 @@ def confirm_start_new_round():
8384

8485
def end_round(self):
8586
self.score.update_player_scores()
86-
return View.template_this_round_score(
87+
return RoundView.this_round_score(
8788
self.score.get_end_of_round_scores(),
8889
self.score.get_current_game_scores()
8990
)

rummy/ui/view.py

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# coding=utf-8
22

3-
from time import sleep
4-
53
from text_template import TextTemplate
64

75
from rummy.constants.resource_path import TEMPLATE_PATH
86

97

8+
# Todo: Refactor -- These look more like controllers the current controller is more like a service.
9+
10+
1011
class View:
1112

1213
@staticmethod
@@ -22,85 +23,3 @@ def prepare_template(template, **kwargs):
2223
complete_template = TEMPLATE_PATH + template
2324
return TextTemplate.render(complete_template, **kwargs)
2425

25-
@staticmethod
26-
def template_turn_start(player):
27-
return TextTemplate.render(
28-
TEMPLATE_PATH + '/player-turn-start.txt',
29-
turn_number=player.round.turn,
30-
player_number=player.round.current_player + 1,
31-
score=player.hand.get_score(),
32-
hand=str(player.hand),
33-
discard=player.round.deck.show_discard()
34-
)
35-
36-
@staticmethod
37-
def template_player_turn_end(player):
38-
return TextTemplate.render(
39-
TEMPLATE_PATH + '/player-turn-end.txt',
40-
hand=str(player.hand),
41-
key=player.hand.get_key()
42-
)
43-
44-
@staticmethod
45-
def template_ai_thought(ai, action):
46-
output = TextTemplate.render(
47-
TEMPLATE_PATH + '/ai-thinking.txt', action=action)
48-
if not ai.ai_only:
49-
sleep(0.8)
50-
return output
51-
52-
@staticmethod
53-
def template_ai_turn_start(player):
54-
return TextTemplate.render(
55-
TEMPLATE_PATH + '/turn-start.txt',
56-
turn_number=player.round.turn,
57-
player_number=player.round.current_player + 1,
58-
discard=player.round.deck.show_discard()
59-
)
60-
61-
@staticmethod
62-
def template_ai_turn_end(player):
63-
return TextTemplate.render(
64-
TEMPLATE_PATH + '/ai-turn-end.txt',
65-
hand=str(player.hand)
66-
)
67-
68-
@staticmethod
69-
def template_player_discarded(discard):
70-
return TextTemplate.render(
71-
TEMPLATE_PATH + '/player-discarded.txt',
72-
discard=discard
73-
)
74-
75-
@staticmethod
76-
def template_ai_discard_data(current_score, scores):
77-
return TextTemplate.render(
78-
TEMPLATE_PATH + '/ai-discard-data.txt',
79-
current_score=str(current_score),
80-
scores=str(scores),
81-
min_score=str(min(scores))
82-
)
83-
84-
@staticmethod
85-
def template_ai_hand_data(score):
86-
return TextTemplate.render(
87-
TEMPLATE_PATH + '/ai-hand-data.txt',
88-
score=str(score)
89-
)
90-
91-
@staticmethod
92-
def template_end_of_round_scores(player):
93-
return TextTemplate.render(
94-
TEMPLATE_PATH + '/hand-score.txt',
95-
player=player.get_name(),
96-
hand=str(player.hand),
97-
score=player.hand.get_score()
98-
)
99-
100-
@staticmethod
101-
def template_this_round_score(round_scores, game_scores):
102-
return TextTemplate.render(
103-
TEMPLATE_PATH + '/round-end.txt',
104-
round_scores=round_scores,
105-
game_scores=game_scores
106-
)

rummy/view/__init__.py

Whitespace-only changes.

rummy/view/ai_view.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from time import sleep
2+
3+
from text_template import TextTemplate
4+
5+
from rummy.constants.resource_path import TEMPLATE_PATH
6+
from rummy.view.player_view import PlayerView
7+
8+
9+
class AiView(PlayerView):
10+
11+
@staticmethod
12+
def thinking(ai, action):
13+
output = TextTemplate.render(
14+
TEMPLATE_PATH + '/ai-thinking.txt', action=action)
15+
if not ai.ai_only:
16+
sleep(0.8)
17+
return output
18+
19+
@staticmethod
20+
def turn_start(player):
21+
return TextTemplate.render(
22+
TEMPLATE_PATH + '/turn-start.txt',
23+
turn_number=player.round.turn,
24+
player_number=player.round.current_player + 1,
25+
discard=player.round.deck.show_discard()
26+
)
27+
28+
@staticmethod
29+
def turn_end(player):
30+
return TextTemplate.render(
31+
TEMPLATE_PATH + '/ai-turn-end.txt',
32+
hand=str(player.hand)
33+
)
34+
35+
@staticmethod
36+
def discarded(discard):
37+
return TextTemplate.render(
38+
TEMPLATE_PATH + '/player-discarded.txt',
39+
discard=discard
40+
)
41+
42+
@staticmethod
43+
def discard_data(current_score, scores):
44+
return TextTemplate.render(
45+
TEMPLATE_PATH + '/ai-discard-data.txt',
46+
current_score=str(current_score),
47+
scores=str(scores),
48+
min_score=str(min(scores))
49+
)
50+
51+
@staticmethod
52+
def hand_data(score):
53+
return TextTemplate.render(
54+
TEMPLATE_PATH + '/ai-hand-data.txt',
55+
score=str(score)
56+
)

rummy/view/human_view.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from text_template import TextTemplate
2+
3+
from rummy.constants.resource_path import TEMPLATE_PATH
4+
from rummy.view.player_view import PlayerView
5+
6+
7+
class HumanView(PlayerView):
8+
9+
@staticmethod
10+
def turn_start(player):
11+
return TextTemplate.render(
12+
TEMPLATE_PATH + '/player-turn-start.txt',
13+
turn_number=player.round.turn,
14+
player_number=player.round.current_player + 1,
15+
score=player.hand.get_score(),
16+
hand=str(player.hand),
17+
discard=player.round.deck.show_discard()
18+
)
19+
20+
@staticmethod
21+
def turn_end(player):
22+
return TextTemplate.render(
23+
TEMPLATE_PATH + '/player-turn-end.txt',
24+
hand=str(player.hand),
25+
key=player.hand.get_key()
26+
)
27+
28+
@staticmethod
29+
def discarded(player):
30+
return TextTemplate.render(
31+
TEMPLATE_PATH + '/player-discarded.txt',
32+
discard=player.round.deck.inspect_discard()
33+
)

rummy/view/player_view.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from abc import abstractmethod
2+
3+
from text_template import TextTemplate
4+
5+
from rummy.constants.resource_path import TEMPLATE_PATH
6+
7+
8+
class PlayerView:
9+
10+
@staticmethod
11+
@abstractmethod
12+
def turn_start(player):
13+
pass
14+
15+
@staticmethod
16+
@abstractmethod
17+
def turn_end(player):
18+
pass
19+
20+
@staticmethod
21+
@abstractmethod
22+
def discarded(discard):
23+
pass
24+
25+
@staticmethod
26+
def knocked():
27+
return TextTemplate.render(
28+
TEMPLATE_PATH + '/knocked.txt'
29+
)

0 commit comments

Comments
 (0)