本文整理汇总了Python中models.Player.get_or_create方法的典型用法代码示例。如果您正苦于以下问题:Python Player.get_or_create方法的具体用法?Python Player.get_or_create怎么用?Python Player.get_or_create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Player
的用法示例。
在下文中一共展示了Player.get_or_create方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: player_data
# 需要导入模块: from models import Player [as 别名]
# 或者: from models.Player import get_or_create [as 别名]
def player_data():
if not request.json or 'player' not in request.json:
print(request, request.json)
abort(400)
db = get_db()
player_data = request.json['player']
friends = player_data['friends']
puzzle_data = player_data['puzzle_data']
del player_data['friends']
del player_data['puzzle_data']
player = Player.get_or_create(player_data)
db.session.query(Player).filter(
Player.social_id == player.social_id).update(player_data)
for friend_social_id in friends:
friend = Player.get_or_create({"social_id": friend_social_id})
player.add_friendship(friend)
for index, puzzle_id in enumerate(puzzle_data['puzzles']):
Score.maybe_update_score({
'player_social_id': player.social_id,
'puzzle_id': puzzle_id,
'score': puzzle_data['scores'][index],
'stars': puzzle_data['stars'][index]})
db.session.commit()
return jsonify(
_assemble_response(player))
示例2: join
# 需要导入模块: from models import Player [as 别名]
# 或者: from models.Player import get_or_create [as 别名]
def join():
"""
Route invoked when a new player joins the room.
Adds player to the room and sets his session cookie.
Posts the join message to other players in the room.
"""
username = request.forms.get("username")
if len(username) == 0:
return HTTPError(403, "Username too short")
room_code = request.forms.get("room").upper()
try:
room = Room.select(Room).where(Room.code == room_code).get()
except Room.DoesNotExist:
return HTTPError(404, "Room does not exist")
sessid = request.cookies.get("sessid", generate_sessid())
player, created = Player.get_or_create(sessid=sessid, room=room, defaults={"name": username})
if created:
message_data = {"player": {"id": player.id, "name": player.name}}
post_message([Msg("join", message_data)], room=room)
response.set_cookie("sessid", sessid, path="/")
return redirect("/game/%s/lobby" % room.code)