awacke1 commited on
Commit
4fa2d3a
·
1 Parent(s): 8a90296

Create new file

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Tuple
2
+
3
+ import streamlit as st
4
+ from treys import Card, Deck
5
+ from treys import Evaluator
6
+
7
+ from models import Hand, SC
8
+
9
+
10
+ # credits
11
+ # Treys python package
12
+ # https://github.com/ihendley/treys
13
+ # StreamLit - statefull webapp
14
+ # https://blog.streamlit.io/session-state-for-streamlit/
15
+
16
+
17
+ def app():
18
+ placeholder = st.empty()
19
+ # INIT
20
+ st.title('Poker play')
21
+ if 'count' not in st.session_state:
22
+ st.session_state.count = 0
23
+ st.session_state.sc = SC(st)
24
+ # END INIT
25
+
26
+ # Streamlit runs from top to bottom on every iteraction so
27
+ st.session_state.count += 1
28
+ placeholder.write('INIT:')
29
+ st.write('Count of screen refresh = ', st.session_state.count)
30
+
31
+ # shuffle cards
32
+ btn_shuffle_cards = st.button('shuffle cards')
33
+ if btn_shuffle_cards:
34
+ _sc: SC = st.session_state.sc
35
+ _sc.deck = Deck()
36
+ _sc.isTableSet = True
37
+ placeholder.write('INIT:TableSet')
38
+
39
+ btnTablePreflop = st.button('Table : preFlop')
40
+ if btnTablePreflop:
41
+ print('-btn preFlop')
42
+ _sc: SC = st.session_state.sc
43
+ # check pre condition
44
+ # draw preFlop to players
45
+ for index, player in enumerate(_sc.players_dict.values()):
46
+ player.hand.card1 = _sc.deck.draw(1)
47
+ for index, player in enumerate(_sc.players_dict.values()):
48
+ player.hand.card2 = _sc.deck.draw(1)
49
+
50
+ st.session_state.isPreFlop = True
51
+ placeholder.write('INIT:TableSet:PreFlop')
52
+
53
+ btn_flop = st.button('Table : flop')
54
+ if btn_flop:
55
+ print('-btn Flop')
56
+ _sc: SC = st.session_state.sc
57
+ _sc.board_flop = _sc.deck.draw(3)
58
+ _sc.isFlop = True
59
+ print('-btn Flop done')
60
+ placeholder.write('INIT:TableSet:PreFlop:Flop')
61
+
62
+ btn_turn = st.button('Table : turn')
63
+ if btn_turn:
64
+ _sc: SC = st.session_state.sc
65
+ _sc.board_turn = _sc.deck.draw(1)
66
+ _sc.isTurn = True
67
+ placeholder.write('INIT:TableSet:PreFlop:Flop:Turn')
68
+
69
+ btn_river = st.button('Table : river')
70
+ if btn_river:
71
+ _sc: SC = st.session_state.sc
72
+ _sc.board_river = _sc.deck.draw(1)
73
+ _sc.isRiver = True
74
+ placeholder.write('INIT:TableSet:PreFlop:Flop:Turn:River')
75
+
76
+ btn_show = st.button('Table : show')
77
+ if btn_show:
78
+ placeholder.write('INIT:TableSet:PreFlop:Flop:Turn:River===showing')
79
+ layTable(st.session_state.sc)
80
+
81
+
82
+ def eval_print(msg: str, player_num: int, player_hand: Hand, rankDesc: str, score: int):
83
+ st.write(msg, 'Player ', player_num, ' hand is '
84
+ , Card.print_pretty_cards([player_hand.card1, player_hand.card2])
85
+ , 'desc:', rankDesc
86
+ , 'score:', score
87
+ )
88
+ st.write(' ')
89
+
90
+
91
+ def eval_player(board: List[Card], hand: Hand, evaluator: Evaluator) -> Tuple[int, str]:
92
+ eval_score = evaluator.evaluate(board, [hand.card1, hand.card2]) # Important Treys looks at hand as array of int
93
+ rank_class = evaluator.get_rank_class(eval_score)
94
+ descClass = evaluator.class_to_string(rank_class) # description of the rank class
95
+ return eval_score, descClass
96
+
97
+
98
+ def layTable(sc: SC):
99
+ _board = None
100
+ if sc.isFlop:
101
+ st.write('Flop = ', Card.print_pretty_cards(sc.board_flop))
102
+ _board = sc.board_flop.copy()
103
+ if sc.isTurn:
104
+ st.write('Turn = ', Card.print_pretty_card(sc.board_turn))
105
+ _board.append(sc.board_turn)
106
+ if sc.isRiver:
107
+ st.write('River = ', Card.print_pretty_card(sc.board_river))
108
+ _board.append(sc.board_river)
109
+
110
+ # eval now
111
+ evaluator = Evaluator()
112
+
113
+ for index, player in enumerate(sc.players_dict.values()):
114
+ tuple2 = eval_player(_board, player.hand, evaluator)
115
+ player.score_abs = tuple2[0]
116
+ player.score_desc = tuple2[1]
117
+
118
+ from collections import OrderedDict
119
+ # order the players : key is auto generated 0,1,2,3..., value is x[1].score_abs ->translated to -- 1,2,3...
120
+ orderd = OrderedDict(sorted(sc.players_dict.items(), key=lambda x: x[1].score_abs))
121
+ # get the tuple example (3)
122
+ winner = next(iter(orderd.items()))
123
+
124
+ for index, (k, player) in enumerate(sc.players_dict.items()):
125
+ if k == winner[0]:
126
+ # eval_print(msg, player_num, player_hand, rankDesc):
127
+ eval_print('winner', player.pos, player.hand, player.score_desc, player.score_abs)
128
+ else:
129
+ eval_print(' - ', player.pos, player.hand, player.score_desc, player.score_abs)
130
+
131
+ # print summy on the console
132
+ # evaluator.hand_summary(_board, all-hands)
133
+
134
+ st.write('game ended')
135
+
136
+
137
+ # run
138
+ app()