From b646eeb48b3ec6c272da44ab6ff2efd5fbc87a03 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Sat, 20 Nov 2021 00:09:01 -0800 Subject: [PATCH] Add auto play for bots --- app/controllers/games_controller.rb | 4 ++ app/controllers/tables_controller.rb | 10 +---- app/models/play.rb | 4 +- app/models/player.rb | 58 ++++++++++++++++++++++++++++ app/views/tables/_seat.html.haml | 3 +- app/views/tables/show.html.haml | 4 +- 6 files changed, 69 insertions(+), 14 deletions(-) diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 13f8352a..91b6232a 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -39,6 +39,10 @@ class GamesController < ApplicationController if params[:hand_type] == 'Pass' @game.set_controlling_player 'next' flash[:notice] = 'Successfully passed.' + # Clear play to beat if play returns to owner + if @game.play_to_beat.try(:player).try(:id) == @game.controlling_player_id + @game.play_to_beat_id = nil + end @game.save # Player action: play_hand elsif params[:player_card_ids] diff --git a/app/controllers/tables_controller.rb b/app/controllers/tables_controller.rb index c211529e..e0f6a636 100644 --- a/app/controllers/tables_controller.rb +++ b/app/controllers/tables_controller.rb @@ -31,19 +31,13 @@ class TablesController < ApplicationController # GET /tables/1.json def show # TODO refactor to ensure play for 2nd and 3rd place - + if @game.controlling_player.try("is_bot?") - flash[:notice] = "Bot is passes" - @game.controlling_player.pass + @game.controlling_player.auto_play @game.save @game.reload end - # Clear play to beat if play returns to owner - if @game.play_to_beat.try(:player).try(:id) == @game.controlling_player_id - @game.play_to_beat_id = nil - @game.save - end end def new_game diff --git a/app/models/play.rb b/app/models/play.rb index 5642c976..5b770f43 100644 --- a/app/models/play.rb +++ b/app/models/play.rb @@ -65,8 +65,8 @@ class Play < ActiveRecord::Base @truths << true return @truths else - # hand_type match? - @truths << (self.hand_type == game.play_to_beat.try("play").try("hand_type")) if game.play_to_beat.try("play").try(:hand_type) + # hand_type match? allows partial match i.e. "triple run (suited)" + @truths << (self.hand_type.match game.play_to_beat.try("play").try("hand_type")) if game.play_to_beat.try("play").try(:hand_type) # beats? @truths << (self.beats? self.game.play_to_beat.try(:play)) end diff --git a/app/models/player.rb b/app/models/player.rb index 330af995..34371574 100644 --- a/app/models/player.rb +++ b/app/models/player.rb @@ -31,7 +31,65 @@ class Player < ActiveRecord::Base return false unless self.game.controlling_player == self self.game.set_controlling_player 'next' + # Clear play to beat if play returns to owner + if self.game.play_to_beat.try(:player).try(:id) == self.game.controlling_player_id + self.game.play_to_beat_id = nil + end self.game.save end + def runs + @runs = self.inventory.chunk_while {|i, j| i.rank.to_i+1 == j.rank.to_i } + end + + def triple_runs + @triple_runs = self.runs.map{|r| r.length > 2 ? r : nil}.compact + end + + def auto_play + # TODO not sure this return clause works + return false unless self.game.controlling_player == self + + # If first play of game, play lowest card + if self.game.plays.empty? + @cards_to_play = [self.inventory[0]] + return self.play_hand @cards_to_play + end + + # If open table + if !self.game.play_to_beat + @cards_to_play = self.inventory[0..0] + else + @cards_to_beat = self.game.play_to_beat.play.player_cards + case self.game.play_to_beat.play.hand_type + when "single" + @cards_to_play = self.inventory.where("value > #{@cards_to_beat.min.value}")[0..0] + when "double" + @doubles = self.inventory.group_by {|card| card.rank}.map{|c| c if c[1].length > 1}.compact + if @doubles.present? + if @doubles[0][1][0..1].max.value > @cards_to_beat.max.value + @cards_to_play = @doubles[0][1][0..1] + end + end + when "triple run" + @runs = self.inventory.chunk_while {|i, j| i.rank.to_i+1 == j.rank.to_i } + @triple_runs = @runs.map{|r| r.length > 2 ? r : nil}.compact + if @triple_runs.present? + @cards_to_play = @triple_runs[0][0..2] + end + end + end + + if @cards_to_play.present? + if @cards_to_beat.present? + if (@cards_to_play.max_by{|c| c.value}.value > @cards_to_beat.max_by{|c| c.value}.value) + return self.play_hand @cards_to_play + end + else + return self.play_hand @cards_to_play + end + end + self.pass + end + end diff --git a/app/views/tables/_seat.html.haml b/app/views/tables/_seat.html.haml index c0851498..eda8e369 100644 --- a/app/views/tables/_seat.html.haml +++ b/app/views/tables/_seat.html.haml @@ -49,8 +49,7 @@ = image_tag('cards/png/' + (card.rank.gsub('J','jack').gsub('Q','queen').gsub('K','king').gsub('A','ace') + '_of_' + card.suit.downcase + 's.png').gsub('11','jack') .gsub('12','queen') .gsub('13','king') .gsub('14','ace') .gsub('15','2')) - disabled = @game.controlling_player != seat.player %input{type: "submit", value: "Play Hand", name: "hand_type", disabled: disabled} - %input{type: "submit", value: "Pass", name: "hand_type", disabled: disabled, - :data => { :confirm => 'Are you sure you want to pass?' }} + %input{type: "submit", value: "Pass", name: "hand_type", disabled: disabled} - else .inventory diff --git a/app/views/tables/show.html.haml b/app/views/tables/show.html.haml index 816db2c4..54000a1f 100644 --- a/app/views/tables/show.html.haml +++ b/app/views/tables/show.html.haml @@ -12,8 +12,8 @@ - if @game.startable? = button_to 'Start', start_game_path(@game) - - if @game.winner_player_id || @game.nil? - = button_to 'New Game', new_game_table_path(@table) + -# - if @game.winner_player_id || @game.nil? + -# = button_to 'New Game', new_game_table_path(@table) -# - if @game && !@game.startable? = button_to 'New Game', new_game_table_path(@table)