Skip to content

Commit

Permalink
implement 18esp
Browse files Browse the repository at this point in the history
  • Loading branch information
bena authored and BenA committed Jan 9, 2024
1 parent 0e5257c commit d75f430
Show file tree
Hide file tree
Showing 18 changed files with 583 additions and 29 deletions.
171 changes: 171 additions & 0 deletions assets/app/view/game/combined_trains.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# frozen_string_literal: true

require 'view/game/actionable'

module View
module Game
class CombinedTrains < Snabberb::Component
include Actionable
include Lib::Settings

needs :selected_trains, store: true, default: {}

def render
@step = @game.active_step
current_entity = @game.round.current_entity
base_trains = @game.combined_base_trains_candidates(current_entity)
obsolete_trains = @game.combined_obsolete_trains_candidates(current_entity)

rendered_base_trains = base_trains.flat_map do |train|
onclick = lambda do
@selected_trains[train] = !@selected_trains[train]
store(:selected_trains, @selected_trains, skip: false)
end

style = {
border: 'solid 1px',
display: 'inline-block',
cursor: 'pointer',
margin: '0.1rem 0rem',
padding: '3px 6px',
minWidth: '1.5rem',
textAlign: 'center',
whiteSpace: 'nowrap',
}

bg_color = route_prop(1, :color)
style[:backgroundColor] = @selected_trains[train] ? bg_color : color_for(:bg)
style[:color] = contrast_on(bg_color)

[
h(:tr,
[h('td.middle', [
h(:div, { style: style, on: { click: onclick } }, train.name),
])]),
]
end
rendered_obsolete_trains = obsolete_trains.flat_map do |train|
train.variants.flat_map do |_name, variant|
onclick = lambda do
train.variant = variant[:name]
@selected_trains[train] = !@selected_trains[train]
store(:selected_trains, @selected_trains, skip: false)
end

children = []

style = {
border: 'solid 1px',
display: 'inline-block',
cursor: 'pointer',
margin: '0.1rem 0rem',
padding: '3px 6px',
minWidth: '1.5rem',
textAlign: 'center',
whiteSpace: 'nowrap',
}

bg_color = route_prop(1, :color)
style[:backgroundColor] = @selected_trains[train] && train.name == variant[:name] ? bg_color : color_for(:bg)
style[:color] = contrast_on(bg_color)

td_props = { style: { paddingRight: '0.8rem' } }
children << h('td.right.middle', td_props, @game.format_currency(variant[:price] * 2))
[
h(:tr,
[h('td.middle', [
h(:div, { style: style, on: { click: onclick } }, variant[:name]),
]), *children]),
]
end
end

div_props = {
key: 'combined_trains',
hook: {
destroy: -> { cleanup },
},
}
table_props = {
style: {
marginTop: '0.5rem',
textAlign: 'left',
},
}

description = 'Each turn, trains may be added together to run as a single longer train for that turn.'

h(:div, div_props, [
h(:h3, 'Combined Trains'),
h('div.small_font', description),
h(:table, table_props, [
h(:thead, [
h(:tr, [
h(:th, 'Base Train'),
]),
]),
h(:tbody, rendered_base_trains),
h(:thead, [
h(:tr, [
h(:th, 'Obsoslete Train'),
h(:th, 'Cost'),
]),
]),
h(:tbody, rendered_obsolete_trains),
]),
actions,
].compact)
end

def cleanup(skip: true)
store(:selected_trains, {}, skip: skip)
end

def actions
selected_count = 0
cities = 0
towns = 0

trains = @selected_trains.map do |train, selected|
next unless selected
next if train.is_a?(String)

selected_count += 1

c, t = @game.distance(train)
cities += c
towns += t

train
end.compact

disabled = trains.size < 2 || trains.first.track_type == trains.last.track_type
submit_txt = disabled ? 'Select trains' : "Form #{cities}+#{towns} train"
base, variant = trains.partition { |t| t.owner == @game.current_entity }
variant = variant.first
base = base.first

submit = lambda do
process_action(Engine::Action::CombinedTrains.new(@game.current_entity, base: base, additional_train: variant,
additional_train_variant: variant.name))
cleanup
end

reset = lambda do
cleanup(skip: false)
end

submit_style = {
minWidth: '6.5rem',
marginTop: '1rem',
padding: '0.2rem 0.5rem',
}

h(:div, { style: { overflow: 'auto', marginBottom: '1rem' } }, [
h(:button, { attrs: { disabled: disabled }, style: submit_style, on: { click: submit } }, submit_txt),
h(:button, { style: submit_style, on: { click: reset } }, 'Reset'),
])
end
end
end
end
2 changes: 2 additions & 0 deletions assets/app/view/game/round/operating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'view/game/route_selector'
require 'view/game/cash_crisis'
require 'view/game/double_head_trains'
require 'view/game/combined_trains'
require 'view/game/buy_token'

module View
Expand Down Expand Up @@ -54,6 +55,7 @@ def render
left << h(SwitchTrains) if @current_actions.include?('switch_trains')
left << h(ReassignTrains) if @current_actions.include?('reassign_trains')
left << h(DoubleHeadTrains) if @current_actions.include?('double_head_trains')
left << h(CombinedTrains) if @current_actions.include?('combined_trains')
left << h(Choose) if @current_actions.include?('choose')
left << h(BuyToken, entity: entity) if @current_actions.include?('buy_token')

Expand Down
34 changes: 34 additions & 0 deletions lib/engine/action/combined_trains.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require_relative 'base'

module Engine
module Action
class CombinedTrains < Base
attr_reader :base, :additional_train, :additional_train_variant

def initialize(entity, base:, additional_train:, additional_train_variant:)
super(entity)
@base = base
@additional_train = additional_train
@additional_train_variant = additional_train_variant
end

def self.h_to_args(h, game)
{
base: game.train_by_id(h['base']),
additional_train_variant: h['additional_train_variant'],
additional_train: game.train_by_id(h['additional_train']),
}
end

def args_to_h
{
'base' => base&.id,
'additional_train' => additional_train&.id,
'additional_train_variant' => additional_train_variant,
}
end
end
end
end
118 changes: 117 additions & 1 deletion lib/engine/game/g_18_esp/entities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ module Entities
color: nil,
abilities: [{ type: 'shares', shares: 'CRB_1' }],
},
{
sym: 'P6',
name: 'Ferrocarril Vasco-Navarro',
value: 160,
revenue: 20,
desc: 'It provides a 10% certificate from the Southern company CRB.',
color: nil,
abilities: [{ type: 'shares', shares: 'random_share' }],
},
{
sym: 'P7',
name: 'Ferrocarril de Carreño',
Expand All @@ -125,7 +134,6 @@ module Entities
},
].freeze

# corporations with different properties in 1st Edition
CORPORATIONS = [
{
float_percent: 40,
Expand Down Expand Up @@ -342,6 +350,114 @@ module Entities
startable: true,
},
].freeze

EXTRA_CORPORATIONS = [
{
float_percent: 40,
sym: 'SFVA',
name: 'Sociedad General de Ferrocarriles Vasco Asturiana',
logo: '18_esp/SFVA',
coordinates: 'D6',
color: '#75151E',
max_ownership_percent: 60,
tokens: [0, 50, 50, 50, 50],
type: 'major',
destination: 'C1',
},
{
float_percent: 40,
sym: 'FDC',
name: 'Ferrocarril del Cantábrico',
logo: '18_esp/FDC',
coordinates: 'I5',
color: '#5D9B9B',
max_ownership_percent: 60,
tokens: [0, 50, 50, 50, 50],
type: 'major',
destination: 'G5',
},
{
float_percent: 40,
sym: 'GSSR',
name: 'Great Southern of Spain Railway Company Limited',
logo: '18_esp/GSSR',
coordinates: 'I29',
city: 0,
max_ownership_percent: 60,
tokens: [0, 50, 50, 50, 50, 50],
color: '#6A5F31',
type: 'major',
destination: 'F32',
},
{
float_percent: 40,
sym: 'AVT',
name: 'Sociedad de los Ferrocarriles de Almansa a Valencia y Tarragona',
logo: '18_esp/AVT',
coordinates: 'K25',
city: 0,
max_ownership_percent: 60,
tokens: [0, 50, 50, 50, 50, 50],
color: '#7DCCE5',
type: 'major',
destination: 'L22',
},
{
float_percent: 40,
sym: 'TBF',
name: 'Compañía de los Ferrocarriles de Tarragona a Barcelona y Francia',
logo: '18_esp/TBF',
coordinates: 'L22',
city: 0,
max_ownership_percent: 60,
tokens: [0, 50, 50, 50, 50, 50],
color: '#F4A900',
type: 'major',
destination: 'N18',
},

{
sym: 'MH',
name: 'Ferrocarril de Madrid a Hendaya',
logo: '18_esp/MH',
coordinates: 'E21',
color: '#4C2F27',
tokens: [0],
type: 'minor',
shares: [100],
float_percent: 100,
max_ownership_percent: 100,
startable: true,
},

{
sym: 'CA',
name: 'Compañía del Ferrocarril Central de Aragón',
logo: '18_esp/CA',
coordinates: 'E21',
color: '#2E3A23',
tokens: [0],
type: 'minor',
shares: [100],
float_percent: 100,
max_ownership_percent: 100,
startable: true,
},
{
sym: 'CSE',
name: 'Compañía de los Caminos de Hierro del Sur de España ',
logo: '18_esp/CSE',
coordinates: 'E21',
color: '#2E3A23',
tokens: [0],
type: 'minor',
shares: [100],
float_percent: 100,
max_ownership_percent: 100,
startable: true,
},

].freeze
end
end
end
Expand Down
Loading

0 comments on commit d75f430

Please sign in to comment.