Skip to content

Commit d866e3b

Browse files
authored
Merge pull request #12 from rainleander/rainleander-patch-1
Update main.py
2 parents 3d87be8 + 74466ef commit d866e3b

File tree

1 file changed

+132
-127
lines changed

1 file changed

+132
-127
lines changed

main.py

Lines changed: 132 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import random
22
import asyncio
33
import logging
4+
from datetime import timedelta
45

56
from temporalio import activity, workflow
67
from temporalio.client import Client
78
from temporalio.worker import Worker
89

9-
# workflow
1010
class Card(object):
1111
def __init__(self, name, value, suit, symbol):
1212
self.value = value
@@ -21,7 +21,6 @@ def __repr__(self):
2121
else:
2222
return "Card"
2323

24-
# activity because of random.shuffle which makes this section nondeterministic
2524
class Deck(object):
2625
def shuffle(self, times=1):
2726
random.shuffle(self.cards)
@@ -30,7 +29,6 @@ def shuffle(self, times=1):
3029
def deal(self):
3130
return self.cards.pop(0)
3231

33-
# workflow
3432
class StandardDeck(Deck):
3533
def __init__(self):
3634
self.cards = []
@@ -61,7 +59,6 @@ def __init__(self):
6159
def __repr__(self):
6260
return "Standard deck of cards:{0} remaining".format(len(self.cards))
6361

64-
# workflow
6562
class Player(object):
6663
def __init__(self):
6764
self.cards = []
@@ -72,7 +69,7 @@ def cardCount(self):
7269
def addCard(self, card):
7370
self.cards.append(card)
7471

75-
# workflow
72+
7673
class PokerScorer(object):
7774
def __init__(self, cards):
7875
# Number of cards
@@ -155,129 +152,138 @@ def fullHouse(self):
155152

156153
return False
157154

155+
@activity.defn
156+
async def poker_game() -> str:
157+
activity.logger.error("Running activity")
158+
player = Player()
159+
160+
# Initial Amount
161+
points = 100
162+
163+
end = False
164+
while not end:
165+
print("You have {0} points".format(points))
166+
print()
167+
168+
points -= 5
169+
170+
# Hand Loop
171+
deck = StandardDeck()
172+
deck.shuffle()
173+
174+
# Deal Out
175+
for i in range(5):
176+
player.addCard(deck.deal())
177+
178+
# Make them visible
179+
for card in player.cards:
180+
card.showing = True
181+
print(player.cards)
182+
183+
validInput = False
184+
while not validInput:
185+
print("Which cards do you want to discard? ( ie. 1, 2, 3 )")
186+
print("*Just hit return to hold all, type exit to quit")
187+
inputStr = input()
188+
189+
if inputStr == "exit":
190+
end = True
191+
break
192+
193+
try:
194+
inputList = [int(inp.strip()) for inp in inputStr.split(",") if inp]
195+
196+
for inp in inputList:
197+
if inp > 6:
198+
continue
199+
if inp < 1:
200+
continue
201+
202+
for inp in inputList:
203+
player.cards[inp - 1] = deck.deal()
204+
player.cards[inp - 1].showing = True
205+
206+
validInput = True
207+
except:
208+
print("Input Error: use commas to separated the cards you want to hold")
209+
210+
print(player.cards)
211+
# Score
212+
score = PokerScorer(player.cards)
213+
straight = score.straight()
214+
flush = score.flush()
215+
highestCount = score.highestCount()
216+
pairs = score.pairs()
217+
218+
# Royal flush
219+
if straight and flush and straight == 14:
220+
print("Royal Flush!!!")
221+
print("+2000")
222+
points += 2000
223+
224+
# Straight flush
225+
elif straight and flush:
226+
print("Straight Flush!")
227+
print("+250")
228+
points += 250
229+
230+
# 4 of a kind
231+
elif score.fourKind():
232+
print("Four of a kind!")
233+
print("+125")
234+
points += 125
235+
236+
# Full House
237+
elif score.fullHouse():
238+
print("Full House!")
239+
print("+40")
240+
points += 40
241+
242+
# Flush
243+
elif flush:
244+
print("Flush!")
245+
print("+25")
246+
points += 25
247+
248+
# Straight
249+
elif straight:
250+
print("Straight!")
251+
print("+20")
252+
points += 20
253+
254+
# 3 of a kind
255+
elif highestCount == 3:
256+
print("Three of a Kind!")
257+
print("+15")
258+
points += 15
259+
260+
# 2 pair
261+
elif len(pairs) == 2:
262+
print("Two Pairs!")
263+
print("+10")
264+
points += 10
265+
266+
# Jacks or better
267+
elif pairs and pairs[0] > 10:
268+
print("Jacks or Better!")
269+
print("+5")
270+
points += 5
271+
272+
player.cards = []
273+
274+
print()
275+
print()
276+
print()
277+
158278
@workflow.defn
159279
class PokerWorkflow:
160280
@workflow.run
161-
async def Poker():
162-
player = Player()
163-
164-
# Initial Amount
165-
points = 100
166-
167-
end = False
168-
while not end:
169-
print("You have {0} points".format(points))
170-
print()
171-
172-
points -= 5
173-
174-
# Hand Loop
175-
deck = StandardDeck()
176-
deck.shuffle()
177-
178-
# Deal Out
179-
for i in range(5):
180-
player.addCard(deck.deal())
181-
182-
# Make them visible
183-
for card in player.cards:
184-
card.showing = True
185-
print(player.cards)
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 and straight == 14:
224-
print("Royal Flush!!!")
225-
print("+2000")
226-
points += 2000
227-
228-
# Straight flush
229-
elif straight and flush:
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()
281+
async def run(self) -> str:
282+
workflow.logger.error("Running workflow")
283+
return await workflow.execute_activity(
284+
poker_game,
285+
start_to_close_timeout=timedelta(seconds=10),
286+
)
281287

282288
async def main():
283289
logging.basicConfig(level=logging.DEBUG)
@@ -288,12 +294,11 @@ async def main():
288294
client,
289295
task_queue="poker-task-queue",
290296
workflows=[PokerWorkflow],
291-
# activities=[shuffle_deck],
297+
activities=[poker_game],
292298
):
293299

294300
result = await client.execute_workflow(
295301
PokerWorkflow.run,
296-
input("Do you want to play some poker?"),
297302
id="poker-workflow-id",
298303
task_queue="poker-task-queue",
299304
)

0 commit comments

Comments
 (0)