-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[18Uruguay] Adding support for RPTLA dividens
- Loading branch information
1 parent
b3a5d61
commit 2d150eb
Showing
2 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |