-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
165 lines (132 loc) · 5.3 KB
/
models.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
import random
from datetime import date
from protorpc import messages
from google.appengine.ext import ndb
from boggleBoard import getBoggleBoard
class User(ndb.Model):
"""User profile"""
name = ndb.StringProperty(required=True)
email = ndb.StringProperty(required=True)
wins = ndb.IntegerProperty(default=0)
total_played = ndb.IntegerProperty(default=0)
points = ndb.IntegerProperty(default=0)
@property
def win_percentage(self):
if self.total_played > 0:
return float(self.wins)/float(self.total_played)
else:
return 0.0
def to_form(self):
return UserForm(name=self.name,
email=self.email,
wins=self.wins,
total_played=self.total_played,
win_percentage=self.win_percentage)
def add_win(self):
"""Add a win"""
self.wins += 1
self.total_played += 1
self.put()
def add_loss(self):
"""Add a loss"""
self.total_played += 1
self.put()
class Game(ndb.Model):
"""Game object"""
board = ndb.PickleProperty(required=True)
next_move = ndb.KeyProperty(required=True) # The User's whose turn it is
user_x = ndb.KeyProperty(required=True, kind='User')
user_o = ndb.KeyProperty(required=True, kind='User')
game_over = ndb.BooleanProperty(required=True, default=False)
all_solutions = ndb.PickleProperty(required=True)
# boggle is played by rounds
# score should be a global feature
winner = ndb.KeyProperty()
history = ndb.PickleProperty(required=True)
@classmethod
def new_game(cls, user_x, user_o):
"""Creates and returns a new game"""
game = Game(user_x=user_x,
user_o=user_o,
next_move=user_x)
game.board, game.all_solutions = getBoggleBoard()
game.history = []
game.put()
return game
def to_form(self):
"""Returns a GameForm representation of the Game"""
form = GameForm(urlsafe_key=self.key.urlsafe(),
board = str(self.board),
all_solutions = str(self.all_solutions),
user_x=self.user_x.get().name,
user_o=self.user_o.get().name,
next_move=self.next_move.get().name,
game_over=self.game_over)
if self.winner:
form.winner = self.winner.get().name
return form
def end_game(self, winner):
"""Ends the game"""
self.winner = winner
self.game_over = True
self.put()
loser = self.user_o if winner == self.user_x else self.user_x
# Add the game to the score 'board'
score = Score(date=date.today(), winner=winner, loser=loser)
score.put()
# Update the user models
winner.get().add_win()
loser.get().add_loss()
class Score(ndb.Model):
"""Score object"""
date = ndb.DateProperty(required=True)
winner = ndb.KeyProperty(required=True)
loser = ndb.KeyProperty(required=True)
def to_form(self):
return ScoreForm(date=str(self.date),
winner=self.winner.get().name,
loser=self.loser.get().name)
class GameForm(messages.Message):
"""GameForm for outbound game state information"""
urlsafe_key = messages.StringField(1, required=True)
board = messages.StringField(2, required=True)
user_x = messages.StringField(3, required=True)
user_o = messages.StringField(4, required=True)
next_move = messages.StringField(5, required=True)
all_solutions = messages.StringField(6, required=True)
game_over = messages.BooleanField(7, required=True)
winner = messages.StringField(8)
class GameForms(messages.Message):
"""Container for multiple GameForm"""
items = messages.MessageField(GameForm, 1, repeated=True)
class NewGameForm(messages.Message):
"""Used to create a new game"""
user_x = messages.StringField(1, required=True)
user_o = messages.StringField(2, required=True)
class MakeMoveForm(messages.Message):
"""Used to make a move in an existing game"""
user_name = messages.StringField(1, required=True)
word = messages.StringField(2, required=True)
# a tuple in string format
move = messages.StringField(3, required=True)
class ScoreForm(messages.Message):
"""ScoreForm for outbound Score information"""
date = messages.StringField(1, required=True)
winner = messages.StringField(2, required=True)
loser = messages.StringField(3, required=True)
class ScoreForms(messages.Message):
"""Return multiple ScoreForms"""
items = messages.MessageField(ScoreForm, 1, repeated=True)
class StringMessage(messages.Message):
"""StringMessage-- outbound (single) string message"""
message = messages.StringField(1, required=True)
class UserForm(messages.Message):
"""User Form"""
name = messages.StringField(1, required=True)
email = messages.StringField(2)
wins = messages.IntegerField(3, required=True)
total_played = messages.IntegerField(4, required=True)
win_percentage = messages.FloatField(5, required=True)
class UserForms(messages.Message):
"""Container for multiple User Forms"""
items = messages.MessageField(UserForm, 1, repeated=True)