本文整理汇总了Python中models.Game.allocate_ids方法的典型用法代码示例。如果您正苦于以下问题:Python Game.allocate_ids方法的具体用法?Python Game.allocate_ids怎么用?Python Game.allocate_ids使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Game
的用法示例。
在下文中一共展示了Game.allocate_ids方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createGame
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import allocate_ids [as 别名]
def createGame(self, request):
"""
a player creates a game of a unique name
"""
if request.game_name is None:
raise endpoints.UnauthorizedException(
'game_name is required to create a game')
player = Player.query(Player.displayName == request.player_name).get()
if not player:
raise endpoints.NotFoundException(
'No player found with name: {}' .format(request.player_name))
elif Game.query(Game.name == request.game_name).get():
raise endpoints.ConflictException(
'A Game with that name already exists!')
else:
# allocate new Game ID with Player key as parent
# allocate_ids(size=None, max=None, parent=None, **ctx_options)
# returns a tuple with (start, end) for the allocated range,
# inclusive.
p_key = player.key
g_id = Game.allocate_ids(size=1, parent=p_key)[0]
# make Game key from ID; assign initial values to the game entity
g_key = ndb.Key(Game, g_id, parent=p_key)
data = {} # is a dict
data['key'] = g_key
data['name'] = request.game_name
data['board'] = ['' for _ in range(9)]
Game(**data).put()
taskqueue.add(params={'email': player.mainEmail,
'gameInfo': repr(request)},
url='/tasks/send_confirmation_email')
game = g_key.get()
return game._copyGameToForm
示例2: _new_game
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import allocate_ids [as 别名]
def _new_game(self, user, game_name):
# retrieve key from user_name
user_key = ndb.Key(User, user.user_name)
# generate game_id
game_id = Game.allocate_ids(size=1, parent=user_key)[0]
# create key using generated game_id and user as its ancestor
game_key = ndb.Key(Game, game_id, parent=user_key)
# generate random word for this game
rw = RandomWords()
word = rw.random_word()
guessed_chars_of_word = []
# make this impl more 'pythonic' way
for c in word:
guessed_chars_of_word.append('*')
game = Game(key=game_key,
game_id=game_id,
game_name=game_name,
word=word,
guessed_chars_of_word=guessed_chars_of_word)
# save game
game.put()
# score id
score_id = Score.allocate_ids(size=1, parent=user_key)[0]
# score key using generated score_id and user as its ancestor
score_key = ndb.Key(Score, score_id, parent=user_key)
# score entity for this game
score = Score(key=score_key,
score_id=score_id,
game_key=game_key)
# save score
score.put()
# capture game snapshot
self._capture_game_snapshot(game, '')
return game
示例3: new_game
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import allocate_ids [as 别名]
def new_game(self, request):
"""Creates new game"""
user = User.query(User.name == request.user_name).get()
p_key = ndb.Key(User,user.name)
c_id = Game.allocate_ids(size = 1 , parent=p_key)[0]
c_key = ndb.Key(Game , c_id ,parent=p_key)
# check if user is registered
if not user:
raise endpoints.NotFoundException(
'A User with that name does not exist!')
# register game in the datastore
try:
game = Game.new_game(user.key,c_key,user.name)
except ValueError:
raise endpoints.BadRequestException('Maximum must be greater '
'than minimum!')
# Use a task queue to update the average attempts remaining.
# This operation is not needed to complete the creation of a new game
# so it is performed out of sequence.
# taskqueue.add(url='/tasks/cache_average_attempts')
return game.to_form('Good luck playing hangman')
示例4: _createGameObject
# 需要导入模块: from models import Game [as 别名]
# 或者: from models.Game import allocate_ids [as 别名]
def _createGameObject(self, request):
"""Create or update Game object, returning GameForm/request."""
# preload necessary data items
user = endpoints.get_current_user()
if not user:
raise endpoints.UnauthorizedException('Authorization required')
user_id = getUserId(user)
if not request.name:
raise endpoints.BadRequestException("Game 'name' field required")
# copy ConferenceForm/ProtoRPC Message into dict
data = {field.name: getattr(request, field.name) for field in request.all_fields()}
del data['websafeKey']
del data['organizerDisplayName']
# add default values for those missing (both data model & outbound Message)
for df in DEFAULTS:
if data[df] in (None, []):
data[df] = DEFAULTS[df]
setattr(request, df, DEFAULTS[df])
# generate Profile Key based on user ID and Conference
# ID based on Profile key get Conference key from ID
p_key = ndb.Key(Profile, user_id)
g_id = Game.allocate_ids(size=1, parent=g_key)[0]
g_key = ndb.Key(Game, c_id, parent=g_key)
data['key'] = g_key
#data['organizerUserId'] = request.organizerUserId = user_id
# create Conference, send email to organizer confirming
# creation of Conference & return (modified) ConferenceForm
Game(**data).put()
return request