Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Add subsidies to dividend step #11202

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/app/view/game/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def render
@step.respond_to?(:dividend_name) ? @step.dividend_name(type) : type
end

corp_income = option[:corporation] + option[:divs_to_corporation]
corp_income = option[:corporation] + option[:divs_to_corporation] + @step.total_subsidy

direction =
if (new_share_price = entity.share_price) && option[:share_direction]
Expand Down
7 changes: 5 additions & 2 deletions lib/engine/action/run_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
module Engine
module Action
class RunRoutes < Base
attr_reader :routes, :extra_revenue
attr_reader :routes, :extra_revenue, :subsidy

def initialize(entity, routes:, extra_revenue: 0)
def initialize(entity, routes:, extra_revenue: 0, subsidy: 0)
super(entity)
@routes = routes
@extra_revenue = extra_revenue
@subsidy = subsidy
end

def self.h_to_args(h, game)
Expand Down Expand Up @@ -41,6 +42,7 @@ def self.h_to_args(h, game)
{
routes: routes,
extra_revenue: h['extra_revenue'],
subsidy: h['subsidy'],
}
end

Expand All @@ -62,6 +64,7 @@ def args_to_h
{
'routes' => routes,
'extra_revenue' => extra_revenue,
'subsidy' => subsidy,
}
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/engine/game/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,9 @@ def process_action(action, add_auto_actions: false, validate_auto_actions: false
@last_processed_action = action.id

self
rescue StandardError => e
rescue_exception(e, action)
self
# rescue StandardError => e
# rescue_exception(e, action)
# self
end

def process_single_action(action)
Expand All @@ -838,8 +838,8 @@ def process_single_action(action)
transition_to_next_round!
end
end
rescue Engine::GameError => e
rescue_exception(e, action)
# rescue Engine::GameError => e
# rescue_exception(e, action)
end

def rescue_exception(e, action)
Expand Down
24 changes: 11 additions & 13 deletions lib/engine/game/g_1822/step/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ def dividend_options(entity)
if extra_train
train = find_extra_train(entity)
extra_train_revenue = routes.find { |r| r.train == train }.revenue
extra_train_payout = send(@extra_train_choice, entity, extra_train_revenue, 0)
extra_train_payout = send(@extra_train_choice, entity, extra_train_revenue)
revenue = total_revenue - extra_train_revenue
else
revenue = total_revenue
end
subsidy = @game.routes_subsidy(routes)
total_revenue += subsidy
dividend_types.to_h do |type|
payout = send(type, entity, revenue, subsidy)
payout = send(type, entity, revenue)
if extra_train
payout[:corporation] += extra_train_payout[:corporation]
payout[:per_share] += extra_train_payout[:per_share]
Expand All @@ -67,9 +65,9 @@ def find_extra_train(entity)
revenue.positive? && revenue != @game.routes_revenue(routes) ? train : nil
end

def half(entity, revenue, subsidy)
def half(entity, revenue)
withheld = half_pay_withhold_amount(entity, revenue)
{ corporation: withheld + subsidy, per_share: payout_per_share(entity, revenue - withheld) }
{ corporation: withheld, per_share: payout_per_share(entity, revenue - withheld) }
end

def half_pay_withhold_amount(entity, revenue)
Expand All @@ -83,7 +81,7 @@ def holder_for_corporation(entity)
def log_run_payout(entity, kind, revenue, subsidy, _action, payout)
@log << "#{entity.name} runs for #{@game.format_currency(revenue)} and pays half" if kind == 'half'

withhold = payout[:corporation] - subsidy
withhold = payout[:corporation]
if withhold.positive?
@log << "#{entity.name} withholds #{@game.format_currency(withhold)}"
elsif payout[:per_share].zero?
Expand All @@ -92,8 +90,8 @@ def log_run_payout(entity, kind, revenue, subsidy, _action, payout)
@log << "#{entity.name} earns subsidy of #{@game.format_currency(subsidy)}" if subsidy.positive?
crericha marked this conversation as resolved.
Show resolved Hide resolved
end

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

def payout_shares(entity, revenue)
Expand Down Expand Up @@ -138,8 +136,8 @@ def process_dividend(action)

@round.routes = []
log_run_payout(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?
payout_corporation(payout[:corporation] + subsidy, entity)
payout_shares(entity, revenue - payout[:corporation]) if payout[:per_share].positive?
change_share_price(entity, payout)

pass!
Expand Down Expand Up @@ -171,8 +169,8 @@ def share_price_change(entity, revenue = 0)
end
end

def withhold(_entity, revenue, subsidy)
{ corporation: revenue + subsidy, per_share: 0 }
def withhold(_entity, revenue)
{ corporation: revenue, per_share: 0 }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/engine/game/g_1825/step/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def process_dividend(action)
@round.receivership_loan = 0
@round.routes = []

log_run_payout(entity, kind, revenue, action, payout)
log_run_payout(entity, kind, revenue, 0, action, payout)

payout_corporation(payout[:corporation], entity)

Expand Down
2 changes: 1 addition & 1 deletion lib/engine/game/g_1840/step/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def payout_corporation(amount, entity)
major.spend(to_pay, @game.bank)
end

def log_run_payout(entity, kind, revenue, action, payout)
def log_run_payout(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
Expand Down
43 changes: 8 additions & 35 deletions lib/engine/game/g_1856/step/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class Dividend < Engine::Step::Dividend
def actions(entity)
# National must withhold if it never owned a permanent
return [] if entity.corporation? && entity == @game.national && [email protected]_ever_owned_permanent
return [] if entity.company? || routes.empty?

super
Engine::Step::Dividend::ACTIONS
end

def dividend_options(entity)
penalty = @round.interest_penalty[entity] || 0
revenue = @game.routes_revenue(routes) - penalty
revenue = total_revenue

dividend_types.to_h do |type|
payout = send(type, entity, revenue)
Expand All @@ -32,37 +32,6 @@ def payout_per_share(entity, revenue)
(revenue / entity.total_shares.to_f)
end

def process_dividend(action)
entity = action.entity
penalty = @round.interest_penalty[entity] || 0
revenue = @game.routes_revenue(routes) - penalty
kind = action.kind.to_sym
payout = dividend_options(entity)[kind]

rust_obsolete_trains!(entity)

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(entity, kind, revenue, action, payout)

@game.bank.spend(payout[:corporation], entity) if payout[:corporation].positive?

payout_shares(entity, revenue - payout[:corporation]) if payout[:per_share].positive?

change_share_price(entity, payout)

pass!
end

def share_price_change(entity, _revenue)
# Share price of national does not change until it owns a permanent
return {} if entity == @game.national && [email protected]_ever_owned_permanent
Expand All @@ -75,7 +44,11 @@ def holder_for_corporation(_entity)
@game.share_pool
end

def log_run_payout(entity, kind, revenue, action, payout)
def total_revenue
super - (@round.interest_penalty[@round.current_operator] || 0)
crericha marked this conversation as resolved.
Show resolved Hide resolved
end

def log_run_payout(entity, kind, revenue, _subsidy, action, payout)
if (@round.interest_penalty[entity] || 0).positive?
@log << "#{entity.name} deducts #{@game.format_currency(@round.interest_penalty[entity])}"\
' for unpaid interest'
Expand Down
49 changes: 7 additions & 42 deletions lib/engine/game/g_1860/step/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,17 @@ module Step
class Dividend < Engine::Step::Dividend
def dividend_options(entity)
revenue = @game.routes_revenue(routes)
subsidy = @game.routes_subsidy(routes)
dividend_types.to_h do |type|
payout = send(type, entity, revenue, subsidy)
payout = send(type, entity, revenue)
payout[:divs_to_corporation] = 0
[type, payout.merge(share_price_change(entity, payout[:per_share].positive? ? revenue : 0))]
end
end

def process_dividend(action)
entity = action.entity
revenue = @game.routes_revenue(routes)
subsidy = @game.routes_subsidy(routes)
kind = action.kind.to_sym
payout = dividend_options(entity)[kind]

entity.operating_history[[@game.turn, @round.round_num]] = OperatingInfo.new(
routes,
(@game.insolvent?(entity) ? @game.actions.last : action),
revenue,
@round.laid_hexes
)

entity.trains.each { |train| train.operated = true } unless @game.insolvent?(entity)

@round.routes = []

log_run_payout(entity, kind, revenue, subsidy, action, payout)
@game.bank.spend(payout[:corporation], entity) if payout[:corporation].positive?
payout_shares(entity, revenue) if payout[:per_share].positive?
change_share_price(entity, payout)
super
@game.check_bank_broken!
pass!
@game.check_bankruptcy!(entity)
end

def log_run_payout(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[:per_share].zero? && payout[:corporation].zero?
@log << "#{entity.name} does not run"
elsif payout[:per_share].zero?
@log << "#{entity.name} withholds #{@game.format_currency(revenue)}"
end
@log << "#{entity.name} earns subsidy of #{@game.format_currency(subsidy)}" if subsidy.positive?
@game.check_bankruptcy!(action.entity)
end

def share_price_change(entity, revenue)
Expand All @@ -78,12 +43,12 @@ def share_price_change(entity, revenue)
end
end

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

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

def payout_shares(entity, revenue)
Expand Down
23 changes: 12 additions & 11 deletions lib/engine/game/g_1862/step/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def dividend_options(entity)
revenue = @game.routes_revenue(routes)
subsidy = @game.routes_subsidy(routes)
actual_dividend_types(entity, revenue, subsidy).to_h do |type|
payout = send(type, entity, revenue, subsidy)
payout = send(type, entity, revenue)
payout[:divs_to_corporation] = corporation_dividends(entity, payout[:per_share])
net_revenue = revenue
net_revenue += hudson_delta(entity, revenue) if type == :hudson
Expand Down Expand Up @@ -95,10 +95,11 @@ def process_dividend(action)
@round.routes = []

log_run_payout(entity, kind, revenue, subsidy, action, payout)
if kind == :hudson && payout[:corporation].negative?
entity.spend(-payout[:corporation], @game.bank)
elsif payout[:corporation].positive?
@game.bank.spend(payout[:corporation], entity)
corporation_total_payout = payout[:corporation] + subsidy
if kind == :hudson && corporation_total_payout.negative?
entity.spend(-corporation_total_payout, @game.bank)
elsif corporation_total_payout.positive?
@game.bank.spend(corporation_total_payout, entity)
end
payout_shares(entity, revenue) if payout[:per_share].positive?
change_share_price(entity, payout)
Expand Down Expand Up @@ -151,17 +152,17 @@ def share_price_change(entity, revenue)
end
end

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

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

def hudson(entity, revenue, subsidy)
def hudson(entity, revenue)
diff = hudson_delta(entity, revenue)
{ corporation: subsidy - diff, per_share: payout_per_share(entity, revenue + diff) }
{ corporation: -diff, per_share: payout_per_share(entity, revenue + diff) }
end

def handle_warranties!(entity)
Expand Down
Loading
Loading