-
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 basic support for loans
All corperations will take a loan from RTPLA when floating It is then optional for the corperation to take one addtional loan per OR The interest is payed end of OR It is not possible to repay until Nationalization started This commit will only add the basic support additional possibilities to take loan and the Nationalization will be handled later
- Loading branch information
1 parent
8429790
commit b3a5d61
Showing
4 changed files
with
297 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,91 @@ | ||
# frozen_string_literal: true | ||
|
||
module Engine | ||
module Game | ||
module G18Uruguay | ||
module Loans | ||
def init_loans | ||
Array.new(self.class::NUMBER_OF_LOANS) { |id| Loan.new(id, self.class::LOAN_VALUE) } | ||
end | ||
|
||
def maximum_loans(entity) | ||
entity == @rptla ? self.class::NUMBER_OF_LOANS : entity.num_player_shares | ||
end | ||
|
||
def loans_due_interest(entity) | ||
entity.loans.size | ||
end | ||
|
||
def interest_owed(entity) | ||
return 0 if entity == @rptla | ||
|
||
10 * loans_due_interest(entity) | ||
end | ||
|
||
def interest_owed_for_loans(count) | ||
10 * count | ||
end | ||
|
||
def can_take_loan?(entity, ebuy: nil) | ||
# return false if nationalized? | ||
return false if entity == @rlpta | ||
return true if ebuy | ||
|
||
entity.corporation? && | ||
entity.loans.size < maximum_loans(entity) && | ||
!@loans.empty? | ||
end | ||
|
||
def take_loan(entity, loan, ebuy: nil) | ||
raise GameError, "Cannot take more than #{maximum_loans(entity)} loans" unless can_take_loan?(entity, ebuy: ebuy) | ||
|
||
# raise GameError, "Not allowed to take loans after nationalization" if @game.nationalized? | ||
|
||
@bank.spend(loan.amount, entity) | ||
entity.loans << loan | ||
@rptla.loans << loan.dup | ||
@loans.delete(loan) | ||
@log << "#{entity.name} takes a loan and receives #{format_currency(loan.amount)}" | ||
end | ||
|
||
def payoff_loan(entity, number_of_loans, spender) | ||
total_amount = 0 | ||
number_of_loans.times do |_i| | ||
paid_loan = entity.loans.pop | ||
amount = paid_loan.amount | ||
total_amount += amount | ||
spender.spend(amount, @bank) | ||
end | ||
@log << "#{spender.name} payoff #{number_of_loans} loan(s) for #{entity.name} and pays #{total_amount}" | ||
end | ||
|
||
def adjust_stock_market_loan_penalty(entity) | ||
delta = entity.loans.size - maximum_loans(entity) | ||
return unless delta.positive? | ||
|
||
delta.times do |_i| | ||
@stock_market.move_left(entity) | ||
end | ||
end | ||
|
||
def take_loan_if_needed_for_interest!(entity) | ||
owed = interest_owed(entity) | ||
return if owed.zero? | ||
|
||
remaining = owed - entity.cash | ||
perform_ebuy_loans(entity, remaining + 10) if remaining.positive? | ||
end | ||
|
||
def corps_pay_interest | ||
corps = @round.entities.select { |entity| entity.loans.size.positive? && entity != @rptla } | ||
corps.each do |corp| | ||
next if corp.closed? | ||
|
||
take_loan_if_needed_for_interest!(corp) | ||
pay_interest!(corp) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
61 changes: 61 additions & 0 deletions
61
lib/engine/game/g_18_uruguay/step/take_loan_buy_company.rb
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,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../../step/base' | ||
|
||
module Engine | ||
module Game | ||
module G18Uruguay | ||
module Step | ||
class TakeLoanBuyCompany < Engine::Step::BuyCompany | ||
def actions(entity) | ||
return [] if !entity.corporation? || entity != current_entity | ||
return [] if entity == @game.rptla | ||
return [] unless can_buy_company?(entity) | ||
|
||
actions = [] | ||
actions << 'take_loan' if @game.can_take_loan?(entity) && !@round.loan_taken && !@game.nationalized? | ||
actions << 'buy_company' if can_buy_company?(entity) | ||
actions << 'pass' if can_buy_company?(entity) || (@game.can_take_loan?(entity) && !@round.loan_taken) | ||
|
||
actions | ||
end | ||
|
||
def can_buy_company?(entity) | ||
companies = @game.purchasable_companies(entity) | ||
|
||
entity == current_entity && | ||
@game.phase.status.include?('can_buy_companies') && | ||
companies.any? && | ||
companies.map(&:min_price).min <= buying_power(entity) | ||
end | ||
|
||
def description | ||
'Take Loans or Buy Company' | ||
end | ||
|
||
def blocks? | ||
true | ||
end | ||
|
||
def process_take_loan(action) | ||
entity = action.entity | ||
@game.take_loan(entity, action.loan) | ||
@round.loan_taken = true | ||
end | ||
|
||
def round_state | ||
super.merge({ | ||
# has player taken a loan this or already | ||
loan_taken: false, | ||
}) | ||
end | ||
|
||
def setup | ||
# you can only take one loan per OR turn | ||
@round.loan_taken = false | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../../step/token' | ||
|
||
module Engine | ||
module Game | ||
module G18Uruguay | ||
module Step | ||
class Token < Engine::Step::Token | ||
def actions(entity) | ||
return [] if entity == @game.rptla | ||
return [] if @game.final_operating_round? | ||
|
||
@round.loan_taken |= false | ||
actions = super.map(&:clone) | ||
if !actions.empty? && @game.can_take_loan?(entity) && !@round.loan_taken && !@game.nationalized? | ||
actions << 'take_loan' | ||
end | ||
actions | ||
end | ||
|
||
def process_take_loan(action) | ||
entity = action.entity | ||
@game.take_loan(entity, action.loan) unless @round.loan_taken | ||
@round.loan_taken = true | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |