-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
248 lines (178 loc) · 6.6 KB
/
Copy pathgame.py
File metadata and controls
248 lines (178 loc) · 6.6 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import os
import jinja2
from google.appengine.api import channel
from google.appengine.ext import ndb
from player import Player
from deck import Deck
import json
import logging
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class Game(ndb.Model):
name = ndb.StringProperty(required=True)
deck = ndb.KeyProperty()
playing = ndb.BooleanProperty(default=False)
p_max = ndb.IntegerProperty(default=5)
p_cur = ndb.IntegerProperty(default=0)
dealer = ndb.KeyProperty()
players = ndb.KeyProperty(repeated=True)
@classmethod
def new(self, gid, name, p_max):
oldgame = Game.get_by_id(gid)
if oldgame:
return oldgame.key
new_game = Game(id = gid,
name = name,
p_max = p_max,
p_cur = 1,
dealer = None,
deck = None,
playing = False)
new_dealer = Player(id = 'dealer',
name = 'John Goodman, the Dealer',
tokens = 0,
a_url = '/static/img/dealer.png',
cards_vis = [],
cards_inv = [],
sync = 0)
new_game.players.append(new_dealer.key)
new_game.dealer = new_dealer.key
new_game.deck = Deck.new(1, new_game.key)
new_dealer.put()
new_game.put()
return new_game.key
def as_dict(self):
return {'name': self.name,
'id': self.key.id(),
'players_max': self.p_max,
'players_current': self.p_cur-1}
def gamestatus_as_dict(self, player):
deck = self.deck.get()
return {
'your_actions': player.actions(self),
'your_cards_visible': [deck.card_as_str(c) for c in\
player.cards_vis + player.cards_inv],
'common_cards_visible': [],
'players': self.players_as_dict()
}
def players_as_dict(self):
return [p.info_as_dict(self) for p in\
ndb.get_multi(self.players)]
def players_sorted_by_sync(self):
ret = (list(), list(), list(), list(), list())
for p in ndb.get_multi(self.players):
ret[p.sync].append(p)
return ret
def selfupdate(self):
players_sorted = self.players_sorted_by_sync()
# Check if the round is done
if self.playing:
players_ready = 0
for i in range(2, 5):
players_ready += len(players_sorted[i])
if players_ready == self.p_cur:
self.finish_round()
# Check if the round should start
else:
players_ready = len(players_sorted[0])
if players_ready == self.p_cur and self.p_cur > 1:
self.start_round()
# Send notification messages to connected clients
self.notify_players()
def start_round(self):
self.playing = True
# Get everyone
dealer = self.dealer.get()
deck = self.deck.get()
players = ndb.get_multi(self.players)
# Remove old cards from player hands, and if they're playing,
# give them new ones
anybody_playing = False
for p in players:
# Skip the dealer
if p.key.id() == 'dealer':
continue
# Remove players' cards
p.cards_vis = []
p.cards_inv = []
p.cards_spl = []
# If the player is sitting out, set them as ready
# for the round to end
if p.wager is None:
p.sync = 4
# Give cards to those playing
if p.sync == 0:
anybody_playing = True
p._hit(self)
p._hit(self)
# If nobody is actually playing, abort without changing the datastore
if not anybody_playing:
return
# Give dealer cards
dealer.cards_vis = []
dealer.cards_inv = []
dealer.cards_vis.append(deck.draw().key)
dealer.cards_inv.append(deck.draw().key)
dealer.sync = 4
# Save game and players to datastore
ndb.put_multi(players)
self.put()
def finish_round(self):
players_sorted = self.players_sorted_by_sync()
# Dealer plays
dealer = self.dealer.get()
dealer.cards_vis.append(dealer.cards_inv.pop())
hand_vals = dealer.hand_values(self, dealer.cards_vis)
while all(v < 19 or v > 21 for v in hand_vals) and\
not all(v > 21 for v in hand_vals):
dealer._hit(self)
hand_vals = dealer.hand_values(self, dealer.cards_vis)
# Get surrender-ers
cowards = players_sorted[3]
# Sort out the rest
others = players_sorted[2]
winners = []
losers = []
dealermax = 0
dealerhand = self.dealer.get().hand_values(self)
for v in dealerhand:
if v < 22 and v > dealermax:
dealermax = v
for player in others:
if player.key.id() == 'dealer':
continue
if any(v > dealermax and v < 22 for v in player.hand_values(self)):
winners.append(player)
else:
losers.append(player)
# Mark the game as finished
self.playing = False
# Finish up with the players
players = cowards + others
for p in players:
# Rewards
if p in cowards:
p.tokens += int(p.wager/2)
if p in winners:
p.tokens += self.winnings(p)
# Reset sync, cards, and bets
p.sync = 4
p.wager = 0
# Set the dealer as ready to play the next game
dealer.sync = 0
players.append(dealer)
ndb.put_multi(players)
self.put()
def winnings(self, player):
return int(player.wager*1.5)
def notify_players(self):
players_as_dict = self.players_as_dict()
logging.info(players_as_dict)
tvars = {'players': players_as_dict}
template = jinja_environment.get_template('/templates/table.html')
table = template.render(tvars)
for p in self.players:
update_dict = {'status': self.gamestatus_as_dict(p.get()),
'table': table}
update_msg = json.dumps(update_dict)
channel.send_message(p.id(), update_msg)