1- # import random
2- # TODO: replace random with pythonsdk random
1+ import random
32
43import asyncio
4+ import logging
55from dataclasses import dataclass
66
7- from temporalio import workflow
7+ from temporalio import activity , workflow
88from temporalio .client import Client
99from temporalio .worker import Worker
1010
@@ -15,23 +15,18 @@ def __init__(self, name, value, suit, symbol):
1515 self .suit = suit
1616 self .name = name
1717 self .symbol = symbol
18- self .showing = False
1918
20- def __repr__ (self ):
21- if self .showing :
22- return self .symbol
23- else :
24- return "Card"
25-
26- class Deck (object ):
27- def shuffle (self , times = 1 ):
19+ @activity .defn
20+ async def DeckWorkflow (object ) -> object :
21+ def shuffle (self , times = 1 ):
22+ activity .logger .info ("Running activity with parameter %s" % object )
2823 random .shuffle (self .cards )
2924 print ("Deck Shuffled" )
3025
3126 def deal (self ):
3227 return self .cards .pop (0 )
3328
34- class StandardDeck (Deck ):
29+ class StandardDeck ():
3530 def __init__ (self ):
3631 self .cards = []
3732 suits = {"Hearts" :"♡" , "Spades" :"♠" , "Diamonds" :"♢" , "Clubs" :"♣" }
@@ -56,7 +51,7 @@ def __init__(self):
5651 symbol = str (values [name ])+ symbolIcon
5752 else :
5853 symbol = name [0 ]+ symbolIcon
59- self .cards .append (Card (name , values [name ], suit , symbol ))
54+ self .cards .append ( Card (name , values [name ], suit , symbol ) )
6055
6156 def __repr__ (self ):
6257 return "Standard deck of cards:{0} remaining" .format (len (self .cards ))
@@ -71,11 +66,13 @@ def cardCount(self):
7166 def addCard (self , card ):
7267 self .cards .append (card )
7368
69+
7470class PokerScorer (object ):
7571 def __init__ (self , cards ):
7672 # Number of cards
7773 if not len (cards ) == 5 :
7874 return "Error: Wrong number of cards"
75+
7976 self .cards = cards
8077
8178 def flush (self ):
@@ -88,7 +85,7 @@ def straight(self):
8885 values = [card .value for card in self .cards ]
8986 values .sort ()
9087
91- if not len (set (values )) == 5 :
88+ if not len ( set (values )) == 5 :
9289 return False
9390
9491 if values [4 ] == 14 and values [0 ] == 2 and values [1 ] == 3 and values [2 ] == 4 and values [3 ] == 5 :
@@ -152,128 +149,134 @@ def fullHouse(self):
152149
153150 return False
154151
155- def Poker (ctx workflow .Context ):
156- player = Player ()
157-
158- # Initial Amount
159- points = 100
160-
161- # Cost per hand
162- handCost = 5
163-
164- end = False
165- while not end :
166- print ( "You have {0} points" .format (points ) )
167- print ()
168-
169- points -= 5
170-
171- ## Hand Loop
172- deck = StandardDeck ()
173- deck .shuffle ()
174-
175- # Deal Out
176- for i in range (5 ):
177- player .addCard (deck .deal ())
178-
179- # Make them visible
180- for card in player .cards :
181- card .showing = True
182- print (player .cards )
183-
184- validInput = False
185- while not validInput :
186- print ("Which cards do you want to discard? ( ie. 1, 2, 3 )" )
187- print ("*Just hit return to hold all, type exit to quit" )
188- inputStr = input ()
189-
190- if inputStr == "exit" :
191- end = True
192- break
193-
194- try :
195- inputList = [int (inp .strip ()) for inp in inputStr .split ("," ) if inp ]
196-
197- for inp in inputList :
198- if inp > 6 :
199- continue
200- if inp < 1 :
201- continue
202-
203- for inp in inputList :
204- player .cards [inp - 1 ] = deck .deal ()
205- player .cards [inp - 1 ].showing = True
206-
207- validInput = True
208- except :
209- print ("Input Error: use commas to separated the cards you want to hold" )
210-
211- print (player .cards )
212- #Score
213- score = PokerScorer (player .cards )
214- straight = score .straight ()
215- flush = score .flush ()
216- highestCount = score .highestCount ()
217- pairs = score .pairs ()
218-
219- # Royal flush
220- if straight and flush == 14 :
221- print ("Royal Flush!!!" )
222- print ("+2000" )
223- points += 2000
224-
225- # Straight flush
226- elif straight and flush != 14 :
227- print ("Straight Flush!" )
228- print ("+250" )
229- points += 250
230-
231- # 4 of a kind
232- elif score .fourKind ():
233- print ("Four of a kind!" )
234- print ("+125" )
235- points += 125
236-
237- # Full House
238- elif score .fullHouse ():
239- print ("Full House!" )
240- print ("+40" )
241- points += 40
242-
243- # Flush
244- elif flush :
245- print ("Flush!" )
246- print ("+25" )
247- points += 25
248-
249- # Straight
250- elif straight :
251- print ("Straight!" )
252- print ("+20" )
253- points += 20
254-
255- # 3 of a kind
256- elif highestCount == 3 :
257- print ("Three of a Kind!" )
258- print ("+15" )
259- points += 15
260-
261- # 2 pair
262- elif len (pairs ) == 2 :
263- print ("Two Pairs!" )
264- print ("+10" )
265- points += 10
266-
267- # Jacks or better
268- elif pairs and pairs [0 ] > 10 :
269- print ("Jacks or Better!" )
270- print ("+5" )
271- points += 5
272-
273- player .cards = []
274-
275- print ()
276- print ()
277- print ()
152+ @workflow .defn
153+ class Poker ():
154+ # start client
155+ client = await Client .connect ("localhost:7233" )
156+
157+ # run a worker for the workflow
158+ async with Worker (
159+ client ,
160+ task_queue = "deck-task-queue" ,
161+ workflows = [DeckWorkflow ],
162+ ):
163+
164+ player = Player ()
165+
166+ # Intial Amount
167+ points = 100
168+
169+ # Cost per hand
170+ handCost = 5
171+
172+ end = False
173+ while not end :
174+ print ( "You have {0} points" .format (points ) )
175+ print ()
176+
177+ points -= 5
178+
179+ # Hand Loop
180+ deck = StandardDeck ()
181+ deck .shuffle ()
182+
183+ # Deal Out
184+ for i in range (5 ):
185+ player .addCard (deck .deal ())
186+
187+ validInput = False
188+ while not validInput :
189+ print ("Which cards do you want to discard? ( ie. 1, 2, 3 )" )
190+ print ("*Just hit return to hold all, type exit to quit" )
191+ inputStr = input ()
192+
193+ if inputStr == "exit" :
194+ end = True
195+ break
196+
197+ try :
198+ inputList = [int (inp .strip ()) for inp in inputStr .split ("," ) if inp ]
199+
200+ for inp in inputList :
201+ if inp > 6 :
202+ continue
203+ if inp < 1 :
204+ continue
205+
206+ for inp in inputList :
207+ player .cards [inp - 1 ] = deck .deal ()
208+ player .cards [inp - 1 ].showing = True
209+
210+ validInput = True
211+ except :
212+ print ("Input Error: use commas to separated the cards you want to hold" )
213+
214+ print (player .cards )
215+ # Score
216+ score = PokerScorer (player .cards )
217+ straight = score .straight ()
218+ flush = score .flush ()
219+ highestCount = score .highestCount ()
220+ pairs = score .pairs ()
221+
222+ # Royal flush
223+ if straight and flush == 14 :
224+ print ("Royal Flush!!!" )
225+ print ("+2000" )
226+ points += 2000
227+
228+ # Straight flush
229+ elif straight and flush != 14 :
230+ print ("Straight Flush!" )
231+ print ("+250" )
232+ points += 250
233+
234+ # 4 of a kind
235+ elif score .fourKind ():
236+ print ("Four of a kind!" )
237+ print ("+125" )
238+ points += 125
239+
240+ # Full House
241+ elif score .fullHouse ():
242+ print ("Full House!" )
243+ print ("+40" )
244+ points += 40
245+
246+ # Flush
247+ elif flush :
248+ print ("Flush!" )
249+ print ("+25" )
250+ points += 25
251+
252+ # Straight
253+ elif straight :
254+ print ("Straight!" )
255+ print ("+20" )
256+ points += 20
257+
258+ # 3 of a kind
259+ elif highestCount == 3 :
260+ print ("Three of a Kind!" )
261+ print ("+15" )
262+ points += 15
263+
264+ # 2 pair
265+ elif len (pairs ) == 2 :
266+ print ("Two Pairs!" )
267+ print ("+10" )
268+ points += 10
269+
270+ # Jacks or better
271+ elif pairs and pairs [0 ] > 10 :
272+ print ("Jacks or Better!" )
273+ print ("+5" )
274+ points += 5
275+
276+ player .cards = []
277+
278+ print ()
279+ print ()
280+ print ()
278281
279282Poker ()
0 commit comments