def get_dice(self, request): """Returns a player's dice.""" # Can still use if game is over or cancelled for historical purposes game = get_by_urlsafe(request.urlsafe_game_key, Game) user = User.query(User.user_name == request.user_name).get() if game: player = Player.query( Player.game == game.key, Player.user == user.key) if user: if player: if user.password != request.password: raise endpoints.UnauthorizedException( 'Invalid password!') else: # For those using themselves to test ids = [] for row in player: ids.append(row.key) dice = (Dice.query(Dice.player.IN(ids)) .order(Dice.player).order(Dice.face)) return Dice.to_form(dice) else: raise endpoints.NotFoundException('Player not found!') else: raise endpoints.NotFoundException( 'A User with the name %s does not exist!' % (user.replace("'", "''"))) else: raise endpoints.NotFoundException('Game not found!')
def call_liar(self, request): """Returns a player's dice.""" game = get_by_urlsafe(request.urlsafe_game_key, Game) # Validity logic if game.game_over or game.cancelled: return game.to_form('Game already over!') if game.turn < 1: return game.to_form( 'At least one turn must pass before calling liar.') # Get players of interest bidding_player = Player.query( Player.game == game.key, Player.order == game.bid_player).get() bidding_user = bidding_player.user.get() calling_player = Player.query( Player.game == game.key, Player.order == game.bid_player % game.players + 1).get() calling_user = calling_player.user.get() if calling_user.password != request.password: return game.to_form('Invalid password.') # Game Logic game.game_over = True # Count dice and update score """ Technically, players can cheat by playing against themselves. Ignoring validity check in the meanwhile for testing purposes. """ players = Player.query(Player.game == game.key) ids = [] dice_total = 0 for player in players: ids.append(player.key) # Dice logic dice = Dice.query(Dice.player.IN(ids)) for die in dice: if die.face == game.bid_face: dice_total += die.total # Score logic # Prevent score for being raised if playing against self if players.count() > 1: score_user = player.user.get() score = Score.query(Score.user == score_user.key).get() if score: score.games += 1 else: score = Score(user=score_user.key, games=1, wins=0, score=0) score.put() message = ('For a face of %d: real total - %d, bid total - %d. ' % (game.bid_face, dice_total, game.bid_total)) if die.total < game.bid_total: winner = calling_user.key message += ('%s was lying! %s wins!' % (bidding_user.user_name.replace("'", "''"), calling_user.user_name.replace("'", "''"))) else: winner = bidding_user.key message += ('%s was telling the truth! %s wins!' % (bidding_user.user_name.replace("'", "''"), bidding_user.user_name.replace("'", "''"))) game.winner = winner # Add score to winner # Prevent score for being raised if playing against self if players.count() > 1: winner_score = Score.query(Score.user == game.winner).get() winner_score.wins += 1 winner_score.score += game.turn winner_score.put() game.put() return game.to_form(message)
def raise_bid(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) # Validity logic if game.game_over or game.cancelled: return game.to_form('Game already over!') bidding_player = Player.query( Player.game == game.key, Player.order == game.bid_player % game.players + 1).get() bidding_user = bidding_player.user.get() if bidding_user.password != request.password: return game.to_form('Invalid password.') if (game.bid_face == game.die_faces and game.bid_total == game.dice_total * game.players): return game.to_form('Already at max possible bid. Bid cannot be ' 'rasied. Call liar to end game.') if request.bid_face < 1 or request.bid_face > game.die_faces: return game.to_form('Invalid face number. Must be between 1 ' 'and ' + str(game.die_faces) + '.') if request.bid_face < game.bid_face: return game.to_form('Invalid dice face. Must be greater than or ' 'equal to the current dice face bid:%d.' % (game.bid_face)) if (request.bid_face == game.bid_face and request.bid_total <= game.bid_total): return game.to_form('Invalid bid. If not raising dice face, ' 'must raise dice total') # Game logic game.bid_face = request.bid_face game.bid_total = request.bid_total game.turn += 1 # Update player info if game.bid_player == game.players: game.bid_player = 1 else: game.bid_player += 1 game.put() # Record History game_history = GameHistory(game=game.key, turn=game.turn, player=bidding_player.key, bid_face=game.bid_face, bid_total=game.bid_total) game_history.put() # Find the next player next_player = Player.query( Player.game == game.key, Player.order == game.bid_player % game.players + 1).get() next_user = next_player.user.get() if next_user.email is not None: task_params = ({ 'email': next_user.email, 'user_name': next_user.user_name, 'game_key': game.key.urlsafe(), 'dice': Dice.query(Dice.player == next_player.key) .order(Dice.face), 'bid_face': game.bid_face, 'bid_total': game.bid_total, 'bid_player': bidding_user.user_name}) taskqueue.add( params=task_params, url='/tasks/send_your_turn' ) return game.to_form('Current bid is now face: %d, number %d. It is ' '%s\'s turn.' % (request.bid_face, request.bid_total, next_user.user_name))
def rolls_db_service(self, rolls, session_id): """ Unpacks dict from dice_n_sides and does a few minor mediation calculations before submitting to db :param session_id: :param rolls: :return: """ db = db_session() di = Dice() di.session_id = session_id di.num_dice = rolls['num_dice'] di.num_sides = rolls['num_side'] di.num_rolls = rolls['num_rolls'] di.rolls = rolls['rolls'] di.max_num = max(rolls['rolls']) di.min_num = min(rolls['rolls']) avg = sum(rolls['rolls'])/len(rolls['rolls']) di.avg_num = avg di.max_thresh = rolls['rolls'].count(rolls['num_side'] * rolls['num_dice']) di.min_thresh = rolls['rolls'].count(rolls['num_dice']) di.avg_thresh = rolls['rolls'].count(int(avg)) db.add(di) db.commit()