-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamelogic.py
175 lines (137 loc) · 3.98 KB
/
gamelogic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import random
import banklogic
import json
import logging
class Card(object):
def __init__(self, newSuite, newNumber):
self.suite = newSuite
self.number = newNumber
def getValue(self):
if self.number >= 10:
return 10
if self.number == 1:
return 11
else:
return self.number
def getDict(self):
return {
'suite': self.suite,
'number': self.number
}
class Hand(object):
def __init__(self, newDeck):
self.cards = []
self.deck = newDeck
self.hitMe()
self.hitMe()
def hitMe(self):
if len(self.cards) < 5 and self.score() < 21:
self.cards.append(self.deck.deal());
def score(self):
score = 0
aces = 0
for card in self.cards:
value = card.getValue()
if value == 11:
aces += 1
score += 10
else:
score += value
while score > 20 and aces > 0:
score -= 10
aces -= 1
return score
def getDict(self):
cardsDictList = []
for card in self.cards:
cardsDictList.append(card.getDict())
return cardsDictList
class Deck(object):
def __init__(self):
self.cards = []
for i in range(1, 53):
suite = 1 + i%4
number = 1 + i%13
self.cards.append(Card(suite, number))
def shuffle(self):
for i in range(0,52):
randomIndex = random.randrange(52)
self.cards[i], self.cards[randomIndex] = self.cards[randomIndex], self.cards[i]
def deal(self):
return self.cards.pop()
class Dealer():
def __init__(self, deck):
self.hand = Hand(deck)
def getHand(self):
return self.hand
def getDict(self):
return self.hand.getDict()
def hitMe(self):
self.hand.hitMe()
class User():
def __init__(self, deck, new_id):
self.bet = 0
self.game_result = ''
self.user_id = new_id
self.hand = Hand(deck)
self.getAPIInfo()
def getAPIInfo(self):
# Call the banking API to get current balance
# Example: casino.curtiswendel.me:3000/api/getUser/1
self.balance = 0
self.name = 'User'
try:
result = banklogic.getUser(self.user_id)
if result:
self.balance = result['balance']
self.name = result['screenName']
except Exception as e:
logging.error('Error while checking user balance...' + str(e))
pass
def getHand(self):
return self.hand
def getDict(self):
if self.hand:
cards_result = self.hand.getDict()
else:
cards_result = ''
return {
'cards': cards_result,
'bet' :self.bet,
'game_result': self.game_result,
'user_id':self.user_id,
'balance':self.balance,
'name':self.name
}
def hitMe(self):
logging.info('user hand score: ' + str(self.hand.score()))
if(self.hand.score() < 21):
self.hand.hitMe()
def changeBet(self, increaseAmount):
responseJSON = banklogic.requestFunds(self.user_id, increaseAmount)
if responseJSON:
logging.info('Raw data from \'RequestFunds\': ' + responseJSON)
response = json.loads(responseJSON)
if 'error' in response:
logging.error('Error received from \'RequestFunds\': ' + response['error'])
raise ValueError( "Insufficient funds!" )
else:
self.bet += increaseAmount
self.balance -= increaseAmount
else:
logging.error('no response from \'RequestFunds\'')
def setGameResult(self, dealerHand):
if self.hand.score() > 21:
self.game_result = 'user_bust'
elif dealerHand.score() > 21:
self.game_result = 'dealer_bust'
elif self.hand.score() <= dealerHand.score():
self.game_result = 'dealer_win'
else:
self.game_result = 'user_win'
if self.game_result == 'user_win' or self.game_result == 'dealer_bust':
# add in twice the amount of the bet, because the amount of the bet was deducted when they placed the bet.
# add in once to get to original amount, twice to actually add $
banklogic.addTransaction(self.user_id, 2*self.bet)
else:
banklogic.addTransaction(self.user_id, -self.bet)