def player_turn(): """When it's a human player turn, render form to complete bid.""" die_choice = request.form.get('die_choice') die_count = request.form.get('die_count') game_id = request.form.get('game_id') player = AbstractPlayer.query.filter(AbstractPlayer.game_id == game_id, AbstractPlayer.position == 1).first() players = get_players_in_game(game_id) game = Game.query.filter(Game.id == game_id).first() #save bid ##make function? new_bid = BidHistory(game_id, player.id, die_choice, die_count) db.session.add(new_bid) db.session.commit() #update turn marker update_turn_marker(game) current_turn_player = get_current_turn_player(game) total_dice = get_total_dice(players) requests = { 'name': player.name, 'die_choice': die_choice, 'die_count': die_count, 'turn_marker': game.turn_marker, 'turn_marker_name': current_turn_player.name, 'game_id': game.id, 'total_dice': total_dice } return jsonify(requests)
def get_game_details(): """Get details needed to update the values in the die count filters.""" game_id = request.args.get('game_id') #check if this is the initial round setup (if so, starting turn will be passed) if request.args.get('starting_turn'): starting_turn = True else: starting_turn = False game = Game.query.filter(Game.id == game_id).first() players = get_active_players_in_game(game.id) # if this is the first time the page ever loads, roll dice if players[0].current_die_roll is None and players[0].die_count == 5: roll_player_dice(players) total_dice = get_total_dice(players) #pass current bids into table on game page... last_bid = (BidHistory.query.filter( BidHistory.game_id == game_id).order_by( BidHistory.created_at.desc()).first()) #add bidding probabilities if it's the player's turn if game.turn_marker == 1 and last_bid: player_probs = get_player_probs(game_id, last_bid.die_choice, last_bid.die_count) else: player_probs = None current_turn_player = get_current_turn_player(game) if not last_bid: results = { 'total_dice': total_dice, 'die_count': None, 'die_choice': None, 'turn_marker': game.turn_marker, 'turn_marker_name': current_turn_player.name, 'starting_turn': starting_turn, 'player_probs': player_probs } else: results = { 'total_dice': total_dice, 'die_count': last_bid.die_count, 'die_choice': last_bid.die_choice, 'turn_marker': game.turn_marker, 'turn_marker_name': current_turn_player.name, 'starting_turn': starting_turn, 'player_probs': player_probs } return jsonify(results)
def play_game(game_id): """Page for game play.""" game = Game.query.filter(Game.id == game_id).first() players = get_active_players_in_game(game.id) # if this is the first time the page ever loads, roll dice if players[0].current_die_roll is None and players[0].die_count == 5: roll_player_dice(players) total_dice = get_total_dice(players) current_turn_player = get_current_turn_player(game) #pass current bids into table on game page... bids = (BidHistory.query.filter(BidHistory.game_id == game_id).order_by( BidHistory.id).all()) return render_template("play_game.html", game=game, players=players, total_dice=total_dice, bids=bids, current_turn_name=current_turn_player.name)
def comp_bidding(): """Start the bidding for a computer""" #if human, show form on front end and get bid information, if computer, calc #bid odds and determine resulting bid game_id = int(request.form.get('game_id')) game = Game.query.filter(Game.id == game_id).first() players = get_players_in_game(game_id) player = (AbstractPlayer.query.filter( AbstractPlayer.game_id == game_id, AbstractPlayer.position == game.turn_marker).first()) #have comp make a bid, save to db, and set turn marker bid = player.comp.bidding() if bid == "Challenge" or bid == "Exact": request_items = {'bid': bid.lower(), 'game_id': game_id} return jsonify(request_items) update_turn_marker(game) current_turn_player = get_current_turn_player(game) total_dice = get_total_dice(players) #add bidding probabilities if it's the player's turn next if game.turn_marker == 1: player_probs = get_player_probs(game_id, bid.die_choice, bid.die_count) else: player_probs = None requests = { 'name': player.name, 'die_choice': bid.die_choice, 'die_count': bid.die_count, 'turn_marker_name': current_turn_player.name, 'turn_marker': game.turn_marker, 'game_id': game.id, 'total_dice': total_dice, 'player_probs': player_probs } return jsonify(requests)
def get_player_probs(game_id, die_choice, die_count): """Return the probabilities of all possible actions for a player. Given a game id, and the current bid die choice and die count, determine the probability of a successful challenge, exact call. Also, give the probability of all possible bids a player can make. Return the prob_map dictionary""" human_player = (AbstractPlayer.query.filter( AbstractPlayer.game_id == game_id, AbstractPlayer.position == 1).first()) players = get_active_players_in_game(game_id) total_dice = get_total_dice(players) prob_map = get_prob_mapping(get_total_dice(players), human_player.current_die_roll) #get challenge and exact probs (before deleting keys) try: challenge_prob = 1 - prob_map[die_choice][die_count] except KeyError: #if it's not in the map, it's not a possible bid based on player's dice. challenge_prob = 1 try: exact_prob = (prob_map[die_choice][die_count] - prob_map[die_choice][die_count + 1]) except KeyError: #if it's not in the map, it's not a possible bid based on player's dice. exact_prob = 0 #only keep valid bids in the map for a player to choose from. for k in prob_map: if k > die_choice: #delete keys (where k <= choice) up to (not including) #the current die count (since those are not possible to bid on) for i in range(die_count): try: del prob_map[k][i] #key may not exist except KeyError: continue #delete keys for bigger numbers, which are less likely to be bid on for i in range(die_count + 2, total_dice + 1): try: del prob_map[k][i] #key may not exist except KeyError: continue else: #delete keys (where k <= choice) up to and including #the current die count (since those are not possibl to bid on) for i in range(die_count + 1): try: del prob_map[k][i] #key may not exist except KeyError: continue #delete keys for bigger numbers, which are less likely to be bid on for i in range(die_count + 3, total_dice + 1): try: del prob_map[k][i] #key may not exist except KeyError: continue #add challenge/exact probs to map prob_map['Challenge'] = {} prob_map['Exact'] = {} prob_map['Challenge']['Challenge'] = challenge_prob prob_map['Exact']['Exact'] = exact_prob return prob_map