From ef2fe0412a7ad8e746fdc43b4cf6b4c5a7fb5593 Mon Sep 17 00:00:00 2001 From: Oliver Burnett-Hall Date: Mon, 1 May 2023 09:54:44 +0100 Subject: [PATCH 1/2] Add hook to allow consent check to be made when buying shares This is needed for 1858, which requires consent when a private company is exchanged for a treasury share. A new method is added to the Game class, `consenter_for_buy_shares`. This should return the player who needs to consent to the action. The default implementation always returns nil, which means that consent is not needed. --- assets/app/view/game/button/buy_share.rb | 22 ++++++++++++++-------- lib/engine/game/base.rb | 6 ++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/assets/app/view/game/button/buy_share.rb b/assets/app/view/game/button/buy_share.rb index bfcfef7689..e079aeff71 100644 --- a/assets/app/view/game/button/buy_share.rb +++ b/assets/app/view/game/button/buy_share.rb @@ -37,14 +37,20 @@ def render text += " (#{@game.format_currency(modified_price)})" if modified_price text += " for #{@purchase_for.name}" if @purchase_for - h(:button, { - on: { - click: lambda { - buy_shares(@entity, bundle, share_price: modified_price, swap: @swap_share, - purchase_for: @purchase_for, borrow_from: @borrow_from) - }, - }, - }, text) + process_buy = lambda do + do_buy = lambda do + buy_shares(@entity, bundle, share_price: modified_price, swap: @swap_share, + purchase_for: @purchase_for, borrow_from: @borrow_from) + end + + if (consenter = @game.consenter_for_buy_shares(@entity, bundle)) + check_consent(@entity, consenter, do_buy) + else + do_buy.call + end + end + + h(:button, { on: { click: process_buy } }, text) end def buy_shares(entity, bundle, share_price: nil, swap: nil, purchase_for: nil, borrow_from: nil) diff --git a/lib/engine/game/base.rb b/lib/engine/game/base.rb index 3b023c232c..a912449c5f 100644 --- a/lib/engine/game/base.rb +++ b/lib/engine/game/base.rb @@ -1207,6 +1207,12 @@ def share_jumps(steps) end end + # A hook to allow a game to request a consent check for a share exchange + # or purchase. If consent is needed then this method should return the + # player that needs to consent to this action. Returning nil or false + # means that consent is not required. + def consenter_for_buy_shares(_entity, _bundle); end + def can_run_route?(entity) graph_for_entity(entity).route_info(entity)&.dig(:route_available) end From a2617331290bd7089709ea574407b0d919278322 Mon Sep 17 00:00:00 2001 From: Oliver Burnett-Hall Date: Mon, 1 May 2023 09:58:29 +0100 Subject: [PATCH 2/2] [1858] Check consent when exchanging for a treasury share When a private railway company is exchanged for a treasury share, consent is needed from the president of the corporation if that is not the same player as the one who owns the private company. If the exchange is for a market share (only possible in the private closure round) then consent is not needed. --- lib/engine/game/g_1858/game.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/engine/game/g_1858/game.rb b/lib/engine/game/g_1858/game.rb index dd94220faf..711f899815 100644 --- a/lib/engine/game/g_1858/game.rb +++ b/lib/engine/game/g_1858/game.rb @@ -331,6 +331,19 @@ def home_hex?(operator, hex) operator.coordinates.include?(hex.coordinates) end + # Constent for a share purchase is only needed in one circumstance: + # - A private railway company is being exchanged for a share. + # - The share is from the corporation's treasury (not the market). + # - The private railway and corporation are controlled by different players. + def consenter_for_buy_shares(entity, bundle) + return unless entity.minor? + return unless bundle.share_price.nil? + return if entity.owner == bundle.corporation.owner + return unless bundle.shares.first.owner.corporation? + + bundle.corporation.owner + end + def tile_lays(entity) entity.corporation? ? TILE_LAYS : MINOR_TILE_LAYS end