Skip to content

Commit

Permalink
Basic Assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandriskel committed Oct 28, 2020
1 parent 180fa31 commit 84b58cc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 35 deletions.
6 changes: 3 additions & 3 deletions assets/app/view/game/corporation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ def render_tokens
[token.logo, token.used, token_text]
end

@corporation.assignments.each do |assignment, _active|
img = @game.class::ASSIGNMENT_TOKENS[assignment]
tokens_body << [img, true, assignment]
@corporation.assignments.each do |key, assignment|
img = @game.class::ASSIGNMENT_TOKENS[key]
tokens_body << [img, assignment.available, assignment.text]
end

h(:div, token_list_props, tokens_body.map do |logo, used, text|
Expand Down
9 changes: 7 additions & 2 deletions lib/engine/assignable.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require_relative 'assignment'

module Engine
module Assignable
def assignments
Expand All @@ -10,8 +12,11 @@ def assigned?(key)
assignments.key?(key)
end

def assign!(key, value = true)
assignments[key] = value
def assign!(key, value = nil, text = nil)
assignments[key] =
Assignment.new(key: key,
value: value,
text: text || key)
end

def remove_assignment!(key)
Expand Down
15 changes: 15 additions & 0 deletions lib/engine/assignment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Engine
class Assignment
attr_reader :key
attr_accessor :value, :text, :available

def initialize(key:, value: true, text: nil, available: true)
@key = key
@value = value
@text = text
@available = available
end
end
end
45 changes: 23 additions & 22 deletions lib/engine/game/g_18_co.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class G18CO < Base

BASE_MINE_VALUE = 10

ASSIGNMENT_TOKENS = {
'mine' => '/icons/18_co/mine.svg',
}.freeze

EVENTS_TEXT = Base::EVENTS_TEXT.merge(
'remove_mines' => ['Mines Close', 'Mine tokens removed from board and corporations']
).freeze
Expand All @@ -82,18 +86,9 @@ def imc

def setup
setup_company_price_50_to_150_percent
setup_companies
setup_corporations
end

def setup_companies
imc.add_ability(Engine::Ability::Base.new(
type: :mine_multiplier,
description: 'Mine values are doubled',
count_per_or: 2
))
end

def setup_corporations
# The DSNG comes with a 2P train
train = @depot.upcoming[0]
Expand All @@ -110,32 +105,29 @@ def init_stock_market
end

def mines_count(entity)
entity.abilities(:mine_income).sum(&:count_per_or)
entity.assignments[:mine]&.value || 0
end

def mine_multiplier(entity)
entity.abilities(:mine_multiplier).map(&:count_per_or).reject(&:zero?).reduce(1, :*)
imc.owner == entity ? 2 : 1
end

def mines_total(entity)
BASE_MINE_VALUE * mines_count(entity) * mine_multiplier(entity)
end

def mines_remove(entity)
entity.abilities(:mine_income) do |ability|
entity.remove_ability(ability)
end
entity.remove_assignment!(:mine)
end

def mine_add(entity)
mine_count = mines_count(entity) + 1
mines_remove(entity)
entity.add_ability(Engine::Ability::Base.new(
type: :mine_income,
description: "Corporation owns #{mine_count} mines",
count_per_or: mine_count,
remove: '6'
))
count = mines_count(entity) + 1
entity.assign!(:mine, count)
mine_update_text(entity)
end

def mine_update_text(entity)
entity.assignments[:mine]&.text = "+#{format_currency(mines_total(entity))}"
end

def operating_round(round_num)
Expand Down Expand Up @@ -167,6 +159,15 @@ def new_auction_round
])
end

def action_processed(action)
super

case action
when Action::BuyCompany
mine_update_text(action.entity) if action.company == imc && action.entity.corporation?
end
end

def revenue_for(route, stops)
revenue = super

Expand Down
16 changes: 8 additions & 8 deletions lib/engine/step/g_18_co/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ def share_price_change(entity, revenue = 0)
{ share_direction: %i[right up], share_times: [1, 1] }
end

def change_share_price(entity, payout)
tfm = @game.mines_total(entity)
def process_dividend(action)
super

# Only pay mines if a train produce revenue regardless of withhold or pay
if tfm.positive? && (payout[:corporation].positive? || payout[:per_share].positive?)
@game.bank.spend(tfm, entity)
entity = action.entity

@log << "#{entity.name} collects #{@game.format_currency(tfm)} from mines"
end
return unless (mine_revenue = @game.mines_total(entity)).positive?
return unless entity.operating_history[[@game.turn, @round.round_num]].revenue.positive?

super
@game.bank.spend(mine_revenue, entity)

@log << "#{entity.name} collects #{@game.format_currency(mine_revenue)} from mines"
end
end
end
Expand Down

0 comments on commit 84b58cc

Please sign in to comment.