Skip to content

Commit

Permalink
Merge remote-tracking branch 'tobymao/master' into tiles-test-page
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljb committed May 14, 2023
2 parents d662c27 + a6d1e04 commit a7fe6e4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
22 changes: 14 additions & 8 deletions assets/app/view/game/button/buy_share.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions lib/engine/game/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,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
Expand Down
13 changes: 13 additions & 0 deletions lib/engine/game/g_1858/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a7fe6e4

Please sign in to comment.