-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
121 lines (85 loc) · 3.72 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
from protorpc import messages
from google.appengine.ext import ndb
class Player(ndb.Model):
""" This class describes a player"""
username = ndb.StringProperty(required=True)
email = ndb.StringProperty(required=True)
class Game(ndb.Model):
""" This class describes an encounter between two players"""
player = ndb.KeyProperty(kind='Player', required=True)
opponent = ndb.KeyProperty(kind='Player')
created = ndb.DateTimeProperty(auto_now_add=True)
status = ndb.StringProperty(default="start")
def gameOver(self, winner):
winner_score = Score.query(Score.game == self.key,
Score.player == winner.player).get()
winner_score.won = True
winner_score.put()
self.status = "over"
self.put()
def toForm(self):
return HistoryForm(player=self.player.get().username,
opponent=self.opponent.get().username,
created="{}".format(self.created),
urlsafekey=self.key.urlsafe())
class Position(ndb.Model):
""" This class describes a player position"""
game = ndb.KeyProperty(kind='Game')
player = ndb.KeyProperty(kind='Player')
position = ndb.IntegerProperty(default=0)
isPlayingNow = ndb.BooleanProperty(default=False)
class Score(ndb.Model):
""" This class records a player score"""
game = ndb.KeyProperty(kind='Game')
player = ndb.KeyProperty(kind='Player')
score = ndb.IntegerProperty(default=0)
won = ndb.BooleanProperty(default=False)
class GameHistory(ndb.Model):
game = ndb.KeyProperty(kind='Game')
message = ndb.StringProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
def toForm(self):
players = "%s VS %s" % (self.game.get().player.get(
).username, self.game.get().opponent.get().username)
return GameHistoryForm(player_opponent=players,
message=self.message,
date=str(self.date),
urlsafekey=self.game.urlsafe())
# Messages classes for Request and Response
class PlayerForm(messages.Message):
username = messages.StringField(1, required=True)
email = messages.StringField(2, required=True)
class NewGameForm(messages.Message):
player = messages.StringField(1, required=True)
opponent = messages.StringField(2)
urlsafekey = messages.StringField(3)
class GameForm(messages.Message):
urlsafekey = messages.StringField(1, required=True)
class MakeMoveForm(messages.Message):
player = messages.StringField(1, required=True)
dice = messages.IntegerField(2, required=True)
position = messages.IntegerField(3, required=True)
score = messages.IntegerField(4, required=True)
status = messages.StringField(5)
class MessageForm(messages.Message):
message = messages.StringField(1, required=True)
class HistoryForm(messages.Message):
player = messages.StringField(1)
opponent = messages.StringField(2)
created = messages.StringField(3)
urlsafekey = messages.StringField(4)
class ListHistoryForm(messages.Message):
history = messages.MessageField(HistoryForm, 1, repeated=True)
class GameHistoryForm(messages.Message):
player_opponent = messages.StringField(1)
message = messages.StringField(2)
date = messages.StringField(3)
urlsafekey = messages.StringField(4)
class ListGameHistory(messages.Message):
history = messages.MessageField(GameHistoryForm, 1, repeated=True)
class RankingForm(messages.Message):
player = messages.StringField(1)
number_games = messages.IntegerField(2)
won = messages.IntegerField(3)
class ListRanking(messages.Message):
items = messages.MessageField(RankingForm, 1, repeated=True)