def get_player(nick_name='', steam_id=''): """ gets player by given params. If player changed nickname - replaces old one in db If no such player - creates new :param nick_name: :param steam_id: :param may_be_new: :return:Player object """ print nick_name, steam_id if nick_name and not steam_id: try: # FIXME: temporary crutch while checking old players steam_id is impossible (asker required) return Player.get(nick_name=nick_name) except: player = Player.create(steam_id=0, nick_name=nick_name) return player elif steam_id and not nick_name: return Player.get(steam_id=steam_id) elif steam_id and nick_name: try: player = Player.get(steam_id=steam_id) except: player = Player.create(steam_id=steam_id, nick_name=nick_name) if nick_name != player.nick_name: player.nick_name = nick_name # TODO: test for it player.save() return player
def accept_or_decline_game(id, action, user, content): """Join/invite/decline actions for games.""" game = Game.get(id) player = Player.get(user, game) started = False if action == 'add': player.set_status_joined() joined = [p for p in game.players if p.status == Player.JOINED] if len(joined) == game.max_players: notify( [p.user.device for p in game.players if p.user.device and p != player], 'Politically Incorrect', 'The game "{}" has started!'.format(game.name) ) game.start() started = True elif action == 'delete': player.set_status_denied() else: return jsonify(), 404 try: db.session.commit() except: db.session.rollback() raise return jsonify(started=started)
def play(id, user, content): """ Handle main gameplay. If the requesting user is the current judge, then expect an email in the request body and interpret it as the round winner chosen by the judge. Otherwise, the request body should contain card ids in the user's hand, which should be placed on the table. request := POST ({ 'cards': [int] } | { 'email': str }) """ game = Game.get(id) player = Player.get(user, game) if game.status != Game.ONGOING: return jsonify(message='This game isn\'t in-progress.'), 418 if user == game.judge.user: email = content['email'] winner = next( (player for player in game.players if player.user.email == email), None) winner.add_points(1) game.new_round(winner) else: if player.played: return jsonify(), 418 cards = content['cards'] map = {card.id: card for card in player.hand} cards = [map[card] for card in cards] if len(cards) != game.black_card.answers: return jsonify(message='Invalid play, mate.'), 418 player.play_cards(cards) notify( [p.user.device for p in game.players if p.user.device and p != player], 'Politically Incorrect', 'Something happened in "{}"!'.format(game.name)) for p in game.players: p.seen = False player.seen = True try: db.session.commit() except: db.session.rollback() return jsonify()
def accept_or_decline_game(id, action, user, content): """Join/invite/decline actions for games.""" game = Game.get(id) player = Player.get(user, game) started = False if action == 'add': player.set_status_joined() joined = [p for p in game.players if p.status == Player.JOINED] if len(joined) == game.max_players: notify([ p.user.device for p in game.players if p.user.device and p != player ], 'Politically Incorrect', 'The game "{}" has started!'.format(game.name)) game.start() started = True elif action == 'delete': player.set_status_denied() else: return jsonify(), 404 try: db.session.commit() except: db.session.rollback() raise return jsonify(started=started)
def set_answer(puzzle_name, player_id, question, content_type, content): player = Player.get(Player.id == player_id) puzzle, _ = Puzzle.get_or_create(name=puzzle_name) try: answer = Answer.get( (Answer.puzzle == puzzle) & (Answer.player == player)) except Answer.DoesNotExist: answer = Answer(puzzle=puzzle, player=player) answer.question = question if content_type == 'text': answer.value = content elif content_type == 'image': filename = '%s_%s.jpg' % (puzzle.id, player.id) path = '%s/images/%s' % (STATIC_DIR, filename) with open(path, 'wb') as fp: fp.write(content) answer.value = 'image:' + filename elif content_type == 'video': filename = '%s_%s.mp4' % (puzzle.id, player.id) path = '%s/videos/%s' % (STATIC_DIR, filename) with open(path, 'wb') as fp: fp.write(content) answer.value = 'video:' + filename answer.save()
def set_answer(puzzle_name, player_id, question, content_type, content): player = Player.get(Player.id == player_id) puzzle, _ = Puzzle.get_or_create(name=puzzle_name) try: answer = Answer.get((Answer.puzzle == puzzle) & (Answer.player == player)) except Answer.DoesNotExist: answer = Answer(puzzle=puzzle, player=player) answer.question = question if content_type == 'text': answer.value = content elif content_type == 'image': filename = '%s_%s.jpg' % (puzzle.id, player.id) path = '%s/images/%s' % (STATIC_DIR, filename) with open(path, 'wb') as fp: fp.write(content) answer.value = 'image:' + filename elif content_type == 'video': filename = '%s_%s.mp4' % (puzzle.id, player.id) path = '%s/videos/%s' % (STATIC_DIR, filename) with open(path, 'wb') as fp: fp.write(content) answer.value = 'video:' + filename answer.save()
def who_am_i(player_id): player = Player.get(Player.id == player_id) anagram, description = WHO_AM_I_CLUES[player.order] # Shuffle the letters. anagram = list(anagram) random.shuffle(anagram) anagram = ''.join(anagram) return '%s|%s' % (anagram, description)
def register(): group_name = request.form['group'].strip() player_name = request.form['player'].strip() try: # Perform case-insensitive search. group = Group.get(fn.Lower(Group.name) == group_name.lower()) except: group = Group.create(name=group_name) try: # Perform case-insensitive search. player = Player.get(fn.lower(Player.name) == player_name.lower(), group=group) except Player.DoesNotExist: player = Player.create( group=group, name=player_name, order=group.players.count()) return str(player.id)
def register(): group_name = request.form['group'].strip() player_name = request.form['player'].strip() try: # Perform case-insensitive search. group = Group.get(fn.Lower(Group.name) == group_name.lower()) except: group = Group.create(name=group_name) try: # Perform case-insensitive search. player = Player.get(fn.lower(Player.name) == player_name.lower(), group=group) except Player.DoesNotExist: player = Player.create(group=group, name=player_name, order=group.players.count()) return str(player.id)
def game(room_code): sessid = request.cookies.get("sessid") you = Player.select(Player, Room).join(Room).where(Player.sessid == sessid, Room.code == room_code).get() room = you.room leader = Player.get(queue_position=room.leader_queue, room=room) players = room.players.execute() initial_data = { "game": {"phase": room.phase, "round": room.round, "leader_queue": room.leader_queue}, "leader": {"id": leader.id, "name": leader.name}, "players": [{"id": plr.id, "name": plr.name, "queue_position": plr.queue_position} for plr in players], "you": {"id": you.id, "name": you.name, "role": you.role}, } context = { "you": you, "players": players, "base_url": "/game/%s" % room_code, "timestamp": time.time(), "initial_data": json.dumps(initial_data), } if you.role == "spy": spies = you.room.players.where(Player.role == "spy").execute() context["spies"] = spies return template("game", **context)
def players(id): player = Player.get(id=id) player.delete_instance() return ('', 204)
def sign_language(player_id): player = Player.get(Player.id == player_id) return SIGN_LANGUAGE_ASSIGNMENTS[player.order]
def doodle(player_id): player = Player.get(Player.id == player_id) return DOODLE_ASSIGNMENTS[player.order]
def get(self): # check for a cached version #--------------------------------------------------------------------# player_key = db.Key.from_path("Player", self.request.get("player_key_name")) if self.emit(self.response.out, player_key): return # not cached or evicted from cache; regenerate #--------------------------------------------------------------------# player = Player.get(player_key) if not player: return # player card #--------------------------------------------------------------------# banner_text_color = misc.get_contrasting_monochrome(player.team.color) attr_table = Table( columns=[ Column("MV", "Movement Allowance", attrgetter("mv")), Column("ST", "Strength", attrgetter("st")), Column("AG", "Agility", attrgetter("ag")), Column("AV", "Armor Value", attrgetter("av")), ], query=(player, ), cls="attr", ) injuries = [] for injury_key in player.injuries: injury = db.get(injury_key) if injury.key().name() == "Dead": injuries.append("DEAD") elif injury.permanent: injuries.append("%s (%s)" % (injury.key().name(), injury.effect)) injuries = ", ".join(x for x in injuries) skills = [k.name() for k in player.skills] if player.level_up_pending: skills.append("<i>Pending Level-Up</i>") skills = ", ".join(skills) # stat_table #--------------------------------------------------------------------# def get_left_header(tuple): player, which = tuple return which.capitalize() def getter(stat): def getter_closure(tuple): player, which = tuple try: attr = getattr(player, "%s_%s" % (stat, which)) ave_attr = attr if player.played == 0 else attr / float( player.played) return """<div class='tot_stat'>%d</div> <div class='ave_stat'>%.1f</div>""" % (attr, ave_attr) except AttributeError: return "-" return getter_closure stat_table = Table( columns=[ Column( "<span class='link' id='%s-show-averages-toggle'></span>" % player.key(), "", get_left_header), Column("TD", "Touchdowns", getter("tds")), Column("P", "Pass completions", getter("passes")), Column("YP", "Yards passing", getter("pyards")), Column("R", "Pass receptions", getter("rec")), Column("YR", "Yards rushing", getter("ryards")), Column("K", "Kills", getter("kills")), Column("C", "Casualties", getter("cas")), Column("KO", "Knock outs", getter("ko")), Column("S", "Stuns", getter("stun")), Column("T", "Tackles", getter("tckl")), Column("I", "Interceptions", getter("int")), ], query=((player, "for"), (player, "against")), cls="fancytable stats", ) # match_table #--------------------------------------------------------------------# def name_getter(record): opponent = record.team_record.opponent_record.team return "<a rel='player_opponents' href='%s'>%s</a>" % ( opponent.get_box_href(), opponent.key().name()) def race_getter(record): opponent = record.team_record.opponent_record.team return "<img src='%s' />" % opponent.race.get_image_src(thumb=True) def date_getter(record): return record.get_match().created.date() def link_getter(record): return """ <a rel='player_matches' href='%s'> <span class='go_to_arrow' title='Go to match page'>→</span></a>""" % ( record.get_match().get_box_href()) match_table = Table( columns=[ Column("Date", "Match date", date_getter), Column("Opponent", "Opponent name", name_getter), Column(" ", "Race", race_getter, center=True), Column("TD", "Touchdowns", attrgetter("tds_for")), Column("MVP", "Most Valuable Player Awards", attrgetter("mvps")), Column("P", "Pass completions", attrgetter("passes_for")), Column("YP", "Yards passing", attrgetter("pyards_for")), Column("R", "Pass receptions", attrgetter("rec_for")), Column("YR", "Yards rushing", attrgetter("ryards_for")), Column("K+", "Kills for", attrgetter("kills_for")), Column("C+", "Casualties for", attrgetter("cas_for")), Column("KO+", "Knock outs for", attrgetter("ko_for")), Column("S+", "Stuns for", attrgetter("stun_for")), Column("T+", "Tackles for", attrgetter("tckl_for")), Column("I+", "Interceptions for", attrgetter("int_for")), Column("K-", "Kills against", attrgetter("kills_against")), Column("C-", "Casualties against", attrgetter("cas_against")), Column("KO-", "Knock outs against", attrgetter("ko_against")), Column("S-", "Stuns against", attrgetter("stun_against")), Column("T-", "Tackles against", attrgetter("tckl_against")), Column("I-", "Interceptions against", attrgetter("int_against")), Column("", "", link_getter, center=True) ], query=player.playerrecord_set.filter("played =", 1), cls="tablesorter match_stats", ) # render and update #--------------------------------------------------------------------# player_box = misc.render('player_box.html', locals()) self.update(player_box, player_key) self.response.out.write(player_box)
def get_game(id, user, content): """Returns a game in a format dependent on the game's status.""" game = Game.get(id) player = Player.get(user, game) if player not in [p for p in game.players if p.status != Player.REJECTED]: return jsonify( message='You can\'t see this game because you\'re not in it!' ), 418 if game.status == Game.PENDING: return jsonify(**{ 'id' : game.id, 'host' : { 'name' : game.host.name, 'email': game.host.email }, 'name' : game.name, 'max_points' : game.max_points, 'max_players': game.max_players, 'status' : game.status, 'players' : [ { 'name' : p.user.name, 'email' : p.user.email, 'picture': p.user.picture, 'status' : p.status, 'points' : 0 } for p in game.players ] }) elif game.status == Game.ONGOING: if not player.seen: player.seen = True try: db.session.commit() except: db.session.rollback() raise return jsonify(**{ 'id' : game.id, 'host' : { 'name' : game.host.name, 'email': game.host.email }, 'name' : game.name, 'max_points' : game.max_points, 'max_players': game.max_players, 'status' : game.status, 'black_card' : { 'id' : game.black_card.id, 'text' : game.black_card.text, 'answers': game.black_card.answers }, 'table' : [ { 'name' : p.user.name, 'email' : p.user.email, 'picture': p.user.picture, 'cards' : [ { 'id' : card.id, 'text': card.text } for card in p.get_played() ] } for p in game.players if p.played and p != game.judge ], 'hand' : [ { 'id' : card.id, 'text': card.text } for card in player.hand ], 'judge' : { 'name' : game.judge.user.name, 'email': game.judge.user.email, }, 'players' : [ { 'name' : p.user.name, 'email' : p.user.email, 'picture': p.user.picture, 'status' : p.status, 'points' : p.score } for p in game.players ], 'previous' : game.previous_round }) elif game.status == Game.ENDED: return jsonify(**{ 'id' : game.id, 'host' : { 'name' : game.host.name, 'email': game.host.email }, 'name' : game.name, 'max_points' : game.max_points, 'max_players': game.max_players, 'status' : game.status, 'players' : [ { 'name' : p.user.name, 'email' : p.user.email, 'picture': p.user.picture, 'status' : p.status, 'points' : p.score } for p in game.players ], 'previous' : game.previous_round }) return jsonify(), 418
def drop(id): player = Player.get(id=id) player.drop() player.save() return ('', 204)
def icebreaker(player_id): player = Player.get(Player.id == player_id) return ICEBREAKER_QUESTIONS[player.order]
def get_game(id, user, content): """Returns a game in a format dependent on the game's status.""" game = Game.get(id) player = Player.get(user, game) if player not in [p for p in game.players if p.status != Player.REJECTED]: return jsonify( message='You can\'t see this game because you\'re not in it!'), 418 if game.status == Game.PENDING: return jsonify( **{ 'id': game.id, 'host': { 'name': game.host.name, 'email': game.host.email }, 'name': game.name, 'max_points': game.max_points, 'max_players': game.max_players, 'status': game.status, 'players': [{ 'name': p.user.name, 'email': p.user.email, 'picture': p.user.picture, 'status': p.status, 'points': 0 } for p in game.players] }) elif game.status == Game.ONGOING: if not player.seen: player.seen = True try: db.session.commit() except: db.session.rollback() raise return jsonify( **{ 'id': game.id, 'host': { 'name': game.host.name, 'email': game.host.email }, 'name': game.name, 'max_points': game.max_points, 'max_players': game.max_players, 'status': game.status, 'black_card': { 'id': game.black_card.id, 'text': game.black_card.text, 'answers': game.black_card.answers }, 'table': [{ 'name': p.user.name, 'email': p.user.email, 'picture': p.user.picture, 'cards': [{ 'id': card.id, 'text': card.text } for card in p.get_played()] } for p in game.players if p.played and p != game.judge], 'hand': [{ 'id': card.id, 'text': card.text } for card in player.hand], 'judge': { 'name': game.judge.user.name, 'email': game.judge.user.email, }, 'players': [{ 'name': p.user.name, 'email': p.user.email, 'picture': p.user.picture, 'status': p.status, 'points': p.score } for p in game.players], 'previous': game.previous_round }) elif game.status == Game.ENDED: return jsonify( **{ 'id': game.id, 'host': { 'name': game.host.name, 'email': game.host.email }, 'name': game.name, 'max_points': game.max_points, 'max_players': game.max_players, 'status': game.status, 'players': [{ 'name': p.user.name, 'email': p.user.email, 'picture': p.user.picture, 'status': p.status, 'points': p.score } for p in game.players], 'previous': game.previous_round }) return jsonify(), 418
def get(self): # check for a cached version #--------------------------------------------------------------------# player_key = db.Key.from_path("Player", self.request.get("player_key_name")) if self.emit(self.response.out, player_key): return # not cached or evicted from cache; regenerate #--------------------------------------------------------------------# player = Player.get(player_key) if not player: return # player card #--------------------------------------------------------------------# banner_text_color = misc.get_contrasting_monochrome(player.team.color) attr_table = Table( columns = [ Column("MV", "Movement Allowance", attrgetter("mv")), Column("ST", "Strength", attrgetter("st")), Column("AG", "Agility", attrgetter("ag")), Column("AV", "Armor Value", attrgetter("av")), ], query = (player,), cls = "attr", ) injuries = [] for injury_key in player.injuries: injury = db.get(injury_key) if injury.key().name() == "Dead": injuries.append("DEAD") elif injury.permanent: injuries.append("%s (%s)" % (injury.key().name(), injury.effect)) injuries = ", ".join(x for x in injuries) skills = [k.name() for k in player.skills] if player.level_up_pending: skills.append("<i>Pending Level-Up</i>") skills = ", ".join(skills) # stat_table #--------------------------------------------------------------------# def get_left_header(tuple): player, which = tuple return which.capitalize() def getter(stat): def getter_closure(tuple): player, which = tuple try: attr = getattr(player, "%s_%s" % (stat, which)) ave_attr = attr if player.played == 0 else attr / float(player.played) return """<div class='tot_stat'>%d</div> <div class='ave_stat'>%.1f</div>""" % ( attr, ave_attr) except AttributeError: return "-" return getter_closure stat_table = Table( columns = [ Column("<span class='link' id='%s-show-averages-toggle'></span>" % player.key(), "", get_left_header), Column("TD", "Touchdowns", getter("tds")), Column("P", "Pass completions", getter("passes")), Column("YP", "Yards passing", getter("pyards")), Column("R", "Pass receptions", getter("rec")), Column("YR", "Yards rushing", getter("ryards")), Column("K", "Kills", getter("kills")), Column("C", "Casualties", getter("cas")), Column("KO", "Knock outs", getter("ko")), Column("S", "Stuns", getter("stun")), Column("T", "Tackles", getter("tckl")), Column("I", "Interceptions", getter("int")), ], query = ((player, "for"), (player, "against")), cls = "fancytable stats", ) # match_table #--------------------------------------------------------------------# def name_getter(record): opponent = record.team_record.opponent_record.team return "<a rel='player_opponents' href='%s'>%s</a>" % ( opponent.get_box_href(), opponent.key().name()) def race_getter(record): opponent = record.team_record.opponent_record.team return "<img src='%s' />" % opponent.race.get_image_src(thumb=True) def date_getter(record): return record.get_match().created.date() def link_getter(record): return """ <a rel='player_matches' href='%s'> <span class='go_to_arrow' title='Go to match page'>→</span></a>""" % ( record.get_match().get_box_href()) match_table = Table( columns = [ Column("Date", "Match date", date_getter), Column("Opponent", "Opponent name", name_getter), Column(" ", "Race", race_getter, center=True), Column("TD", "Touchdowns", attrgetter("tds_for")), Column("MVP", "Most Valuable Player Awards", attrgetter("mvps")), Column("P", "Pass completions", attrgetter("passes_for")), Column("YP", "Yards passing", attrgetter("pyards_for")), Column("R", "Pass receptions", attrgetter("rec_for")), Column("YR", "Yards rushing", attrgetter("ryards_for")), Column("K+", "Kills for", attrgetter("kills_for")), Column("C+", "Casualties for", attrgetter("cas_for")), Column("KO+", "Knock outs for", attrgetter("ko_for")), Column("S+", "Stuns for", attrgetter("stun_for")), Column("T+", "Tackles for", attrgetter("tckl_for")), Column("I+", "Interceptions for", attrgetter("int_for")), Column("K-", "Kills against", attrgetter("kills_against")), Column("C-", "Casualties against", attrgetter("cas_against")), Column("KO-", "Knock outs against", attrgetter("ko_against")), Column("S-", "Stuns against", attrgetter("stun_against")), Column("T-", "Tackles against", attrgetter("tckl_against")), Column("I-", "Interceptions against", attrgetter("int_against")), Column("", "", link_getter, center=True) ], query = player.playerrecord_set.filter("played =", 1), cls = "tablesorter match_stats", ) # render and update #--------------------------------------------------------------------# player_box = misc.render('player_box.html', locals()) self.update(player_box, player_key) self.response.out.write(player_box)
def play(id, user, content): """ Handle main gameplay. If the requesting user is the current judge, then expect an email in the request body and interpret it as the round winner chosen by the judge. Otherwise, the request body should contain card ids in the user's hand, which should be placed on the table. request := POST ({ 'cards': [int] } | { 'email': str }) """ game = Game.get(id) player = Player.get(user, game) if game.status != Game.ONGOING: return jsonify(message='This game isn\'t in-progress.'), 418 if user == game.judge.user: email = content['email'] winner = next( (player for player in game.players if player.user.email == email), None ) winner.add_points(1) game.new_round(winner) else: if player.played: return jsonify(), 418 cards = content['cards'] map = {card.id: card for card in player.hand} cards = [map[card] for card in cards] if len(cards) != game.black_card.answers: return jsonify(message='Invalid play, mate.'), 418 player.play_cards(cards) notify( [p.user.device for p in game.players if p.user.device and p != player], 'Politically Incorrect', 'Something happened in "{}"!'.format(game.name) ) for p in game.players: p.seen = False player.seen = True try: db.session.commit() except: db.session.rollback() return jsonify()