-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.py
261 lines (218 loc) · 8.98 KB
/
repository.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env python
# Copyright 2010 Leonid Movshovich <[email protected]>
# This file is part of Webridge.
# Webridge is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Webridge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with Webridge. If not, see <http://www.gnu.org/licenses/>.
import logging
import sys
from google.appengine.ext import db
from google.appengine.api import users, channel
from django.utils import simplejson as json
import bridge
from utils import m
class Deal(db.Model) :
N = db.ListProperty(int, required=True)
S = db.ListProperty(int, required=True)
E = db.ListProperty(int, required=True)
W = db.ListProperty(int, required=True)
vulnerability = db.IntegerProperty(choices = bridge.VULN_OPTIONS, required=True)
dealer = db.IntegerProperty(choices = bridge.DEALERS, required=True)
createDate = db.DateTimeProperty(auto_now_add=True)
@staticmethod
def create(deal, vuln, dealer) :
d = Deal(vulnerability=vuln, dealer=dealer, **deal)
d.put()
return d
def hand_by_side(self, side) :
return self.__getattribute__(side)
def todeck(self) :
return [('N', self.N), ('E', self.E), ('S', self.S), ('W', self.W)]
class Protocol(db.Model) :
N = db.UserProperty(required=True)
S = db.UserProperty(required=True)
E = db.UserProperty(required=True)
W = db.UserProperty(required=True)
bidding = db.StringListProperty()
moves = db.ListProperty(int)
deal = db.ReferenceProperty(Deal, required=True)
contract = db.StringProperty() # <level><suit>[d|r]<decl> eg. 3SdN, 2DS
tricks = db.IntegerProperty() # 0 = just made, +1 = one overtrick, -1 = one down
result = db.IntegerProperty() # +100, -800, etc.
playStarted = db.DateTimeProperty(auto_now_add=True)
@staticmethod
def create(dealmodel, N, E, S, W) :
return Protocol(deal = dealmodel, N = N, E = E, S = S, W = W).put()
@staticmethod
def get_by_deal(deal) :
return Protocol.all().filter('deal =', deal).order('playStarted')
@staticmethod
def get_deals(protoq, deals) :
proceed = True
while proceed :
ps = protoq.fetch(100)
deals.update([p.deal.key() for p in ps])
protoq.with_cursor(protoq.cursor())
proceed = len(ps) > 0
@staticmethod
def get_unused_deal(playerlist) :
used = set()
for player in playerlist :
Protocol.get_deals(Protocol.all().filter('N = ', player), used)
Protocol.get_deals(Protocol.all().filter('S = ', player), used)
Protocol.get_deals(Protocol.all().filter('E = ', player), used)
Protocol.get_deals(Protocol.all().filter('W = ', player), used)
for d in db.GqlQuery("select __key__ from Deal") :
if d not in used :
return db.get(d)
return None
def round_ended(self) :
return len(self.moves) % 4 == 0
def finished(self) :
return len(self.moves) == 52
def dummy(self) :
if self.contract is None or self.contract == 'pass' :
return None
return bridge.SIDES[(bridge.SIDES.index(self.contract[-1]) + 2) % 4]
class Table(db.Model) :
protocol = db.ReferenceProperty(Protocol)
whosmove = db.StringProperty(choices = bridge.SIDES)
claim = db.StringProperty()
def nextmove(self) :
self.whosmove = bridge.SIDES[(bridge.SIDES.index(self.whosmove) + 1) % 4]
def user_by_side(self, side) :
res = TablePlace.get1(table=self, side=side)
if res is None :
return None
else :
return res.user
def side(self, user, player='own') :
try :
return bridge.SIDES[self.side_idx(user, player)]
except TypeError :
return None
def side_idx(self, user, player='own') :
try :
return (bridge.SIDES.index(TablePlace.get1(table=self, user=user).side)
+ bridge.REL_SIDES.index(player)) % 4
except ValueError :
return None
def sit(self, place, user):
tp = TablePlace.get1(table=self, side=place)
if tp is None :
tp = TablePlace(table=self, side=place, user=user)
else :
tp.user = user
return tp
def pcount(self):
return TablePlace.player_q(self).count(4)
def bcast(self, m, q) :
return UserProfile.uenqueue([tp.user for tp in q], m)
def broadcast(self, m) :
return self.bcast(m, TablePlace.all().filter('table', self))
def kib_broadcast(self, m) :
return self.bcast(m, TablePlace.kibitzer_q(self))
def userlist(self) :
return [tp.user for tp in TablePlace.player_q(self).fetch(4)]
# MAYBE store it in memory in place of each time recalculation
def usermap(self) :
return dict([(str(tp.side), tp.user) for tp in TablePlace.player_q(self).fetch(4)])
class TablePlace(db.Model) :
user = db.UserProperty(required=True)
side = db.StringProperty(choices = bridge.SIDES) #None for kibitzers
table = db.ReferenceProperty(Table, required=True)
@staticmethod
def get1(**kwargs) :
q = TablePlace.all()
for k, v in kwargs.items() :
q = q.filter(k, v)
return q.get()
@staticmethod
def player_q(table) :
return TablePlace.all().filter('table', table).filter('side !=', None)
@staticmethod
def kibitzer_q(table) :
return TablePlace.all().filter('table', table).filter('side', None)
class UserProfile(db.Model) :
chanid = db.StringProperty()
user = db.UserProperty(required=True)
loggedin = db.BooleanProperty()
connected = db.BooleanProperty()
messages = db.StringListProperty()
lastact = db.DateTimeProperty(auto_now = True)
nickname = db.StringProperty(default='')
bridgeinfo = db.StringProperty(default='')
autosingleton = db.BooleanProperty(default=False)
skypeid = db.StringProperty(default='')
facebookid = db.StringProperty(default='')
twitterid = db.StringProperty(default='')
# MAYBE time consuming, optimize using background task
@staticmethod
def broadcast(m, exclude = None) :
users = UserProfile.all().filter('loggedin =', True)
if exclude is not None :
users = users.filter('user !=', exclude)
return filter(lambda x: x.enqueue(m), users)
@staticmethod
def uenqueue(users, m) :
if users is None :
return []
if not isinstance(users, list) :
users = [users]
res = []
for prof in UserProfile.all().filter('user in', users):
if prof.enqueue(m) :
res.append(prof)
return res
@staticmethod
def get_or_create(user) :
res = UserProfile.get(user)
if res is None :
res = UserProfile(user = user)
res.put()
return res
@staticmethod
def get(user) :
if isinstance(user, users.User) :
return UserProfile.gql('WHERE user = :1', user).get()
elif isinstance(user, str) :
return UserProfile.gql('WHERE user = USER(:1)', user).get()
def enqueue(self, m) :
if not self.loggedin :
return False
logging.info('%s - %s: %s', self.user.nickname(), self.connected, m)
if self.connected :
try :
channel.send_message(self.chanid, json.dumps(m))
except :
einfo = sys.exc_info()
logging.warn('send to %s failed: %s - %s', self.user.nickname(), einfo[0], einfo[1])
self.connected = False
else :
return False
if not isinstance(m, list) :
m = [m]
self.messages += [json.dumps(x) for x in m]
return True
def send_stored_messages(self) :
logging.warn('%s: release messages %s', self.user, self.messages)
channel.send_message(self.chanid, '[' + ','.join(self.messages) + ']')
self.messages = []
self.connected = True
def logoff(self, toput):
self.loggedin = False
tps = TablePlace.all().filter('user', self.user).fetch(100)
db.delete(tps)
for tp in tps :
table = tp.table
toput.append(UserProfile.broadcast(m('player.leave', tid = table.key().id(), position = tp.side)))
if TablePlace.player_q(table).count(1) == 0 :
db.delete(table)
toput.append(UserProfile.broadcast(m('table.remove', tid = table.key().id())))