Skip to content

Commit

Permalink
[18Uruguay] Adding support for RPTLA dividens
Browse files Browse the repository at this point in the history
  • Loading branch information
patrikolesen committed Feb 2, 2024
1 parent b3a5d61 commit 2d150eb
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/engine/game/g_18_uruguay/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def operating_round(round_num)
Engine::Step::Track,
G18Uruguay::Step::Token,
Engine::Step::Route,
Engine::Step::Dividend,
G18Uruguay::Step::Dividend,
Engine::Step::DiscardTrain,
Engine::Step::BuyTrain,
[G18Uruguay::Step::TakeLoanBuyCompany, { blocks: true }],
Expand Down
110 changes: 110 additions & 0 deletions lib/engine/game/g_18_uruguay/step/dividend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# frozen_string_literal: true

require_relative '../../../step/dividend'
require_relative '../../../step/half_pay'

module Engine
module Game
module G18Uruguay
module Step
class Dividend < Engine::Step::Dividend
DIVIDEND_TYPES = %i[payout withhold].freeze

ACTIONS = %w[dividend].freeze

def actions(entity)
return [] if !entity.corporation? || missing_revenue(entity)

ACTIONS
end

def missing_revenue(entity)
(@game.routes_revenue(routes, entity).zero? && @game.routes_subsidy(routes, entity).zero?)
end

def description
'Pay or Withhold Dividends'
end

def dividend_options(entity)
total_revenue = @game.routes_revenue(routes, entity)
revenue = total_revenue

subsidy = @game.routes_subsidy(routes, entity)
total_revenue += subsidy
dividend_types.to_h do |type|
payout = send(type, entity, revenue, subsidy)
payout[:divs_to_corporation] = corporation_dividends(entity, payout[:per_share])
[type, payout.merge(share_price_change(entity, total_revenue - payout[:corporation]))]
end
end

def holder_for_corporation(_entity)
@game.share_pool
end

def payout(entity, revenue, subsidy)
if @game.nationalized? && entity.loans.size.positive?
return {
corporation: subsidy + (payout_per_share(entity, revenue) * 10),
per_share: 0,
}
end

{ corporation: subsidy, per_share: payout_per_share(entity, revenue) }
end

def withhold(_entity, revenue, subsidy)
{ corporation: revenue + subsidy, per_share: 0 }
end

def process_dividend_rptla(action)
entity = action.entity
revenue = @game.routes_revenue(routes, entity)
subsidy = @game.routes_subsidy(routes, entity)
kind = action.kind.to_sym
payout = dividend_options(entity)[kind]
entity.operating_history[[@game.turn, @round.round_num]] = OperatingInfo.new(
routes,
action,
revenue,
@round.laid_hexes
)

entity.trains.each { |train| train.operated = true }

@round.routes = []
log_run_payout_sub(entity, kind, revenue, subsidy, action, payout)
@game.bank.spend(payout[:corporation], entity) if payout[:corporation].positive?
payout_shares(entity, revenue + subsidy - payout[:corporation]) if payout[:per_share].positive?
change_share_price(entity, payout)

pass!
end

def process_dividend(action)
return process_dividend_rptla(action) if action.entity == @game.rptla

super
loans_to_pay_off = [(current_entity.cash / 100).floor, current_entity&.loans&.size].min
return if !loans_to_pay_off.positive? || !@game.nationalized?

@game.payoff_loan(current_entity, loans_to_pay_off, current_entity)
end

def log_run_payout_sub(entity, kind, revenue, _subsidy, _action, payout)
unless Dividend::DIVIDEND_TYPES.include?(kind)
@log << "#{entity.name} runs for #{@game.format_currency(revenue)} and pays #{action.kind}"
end

if payout[:corporation].positive?
@log << "#{entity.name} withholds #{@game.format_currency(payout[:corporation])}"
elsif payout[:per_share].zero?
@log << "#{entity.name} does not run"
end
end
end
end
end
end
end

0 comments on commit 2d150eb

Please sign in to comment.