-
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.
Merge pull request #10207 from patrikolesen/18_uruguay_pre_alpha_deli…
…ver_goods [18Uruguay] Adding support for delivering goods
- Loading branch information
Showing
4 changed files
with
333 additions
and
26 deletions.
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,94 @@ | ||
# frozen_string_literal: true | ||
|
||
module Engine | ||
module Game | ||
module G18Uruguay | ||
module Goods | ||
GOODS_DESCRIPTION_STR = 'Number of goods: ' | ||
|
||
def goods_setup | ||
@pickup_hex_for_train = {} | ||
@goods_on_ship = {} | ||
@number_of_goods_at_harbor = 0 | ||
end | ||
|
||
# Train delivery | ||
def train_with_goods?(train) | ||
return unless train | ||
|
||
@pickup_hex_for_train.key?(train.id) | ||
end | ||
|
||
def attach_good_to_train(train, hex) | ||
@pickup_hex_for_train[train.id] = hex | ||
end | ||
|
||
def good_pickup_hex(train) | ||
@pickup_hex_for_train[train.id] | ||
end | ||
|
||
def unload_good(train) | ||
@pickup_hex_for_train.delete(train.id) if train_with_goods?(train) | ||
end | ||
|
||
# Harbor | ||
def visits_include_port?(visits) | ||
visits.any? { |visit| self.class::PORTS.include?(visit.hex.id) } | ||
end | ||
|
||
def route_include_port?(route) | ||
route.hexes.any? { |hex| self.class::PORTS.include?(hex.id) } | ||
end | ||
|
||
def check_for_goods_if_run_to_port(route, visits) | ||
true if route.corporation == @rptla | ||
visits_include_port?(visits) || !train_with_goods?(route.train) | ||
end | ||
|
||
def check_for_port_if_goods_attached(route, visits) | ||
true if route.corporation == @rptla | ||
!visits_include_port?(visits) || train_with_goods?(route.train) | ||
end | ||
|
||
def number_of_goods_at_harbor | ||
@number_of_goods_at_harbor | ||
end | ||
|
||
def add_good_to_rptla | ||
ability = @rptla.abilities.find { |a| a.type == :Goods } | ||
return if ability.nil? | ||
|
||
@number_of_goods_at_harbor += 1 | ||
ability.description = GOODS_DESCRIPTION_STR + @number_of_goods_at_harbor.to_s | ||
end | ||
|
||
def remove_goods_from_rptla(goods_count) | ||
return if @number_of_goods_at_harbor < goods_count | ||
|
||
ability = @rptla.abilities.find { |a| a.type == :Goods } | ||
return if ability.nil? | ||
|
||
@number_of_goods_at_harbor -= goods_count | ||
ability.description = GOODS_DESCRIPTION_STR + count.to_s | ||
end | ||
|
||
# Ship gooods | ||
def add_goods_to_ship(ship, count) | ||
@goods_on_ship[ship.id] = count | ||
end | ||
|
||
def remove_goods_from_ship(ship) | ||
return unless @goods_on_ship.key?(ship.id) | ||
|
||
@goods_on_ship[ship.id] = 0 | ||
end | ||
|
||
def goods_on_ship(ship) | ||
return 0 unless @goods_on_ship.key?(ship.id) | ||
|
||
@goods_on_ship[ship.id] | ||
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,130 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../../step/route' | ||
|
||
module Engine | ||
module Game | ||
module G18Uruguay | ||
module Step | ||
class Route < Engine::Step::Route | ||
def setup | ||
@round.current_routes = {} | ||
end | ||
|
||
def actions(entity) | ||
return %w[].freeze if entity.corporation == @game.rptla | ||
return [] if !entity.operator? || @game.route_trains(entity).empty? || !@game.can_run_route?(entity) | ||
return [] if entity.corporation? && entity.type == :minor | ||
|
||
actions = ACTIONS.dup | ||
actions << 'choose' if choosing?(entity) | ||
actions | ||
end | ||
|
||
def choosing?(_entity) | ||
true | ||
end | ||
|
||
def choice_name | ||
return 'Attach goods to ships' if current_entity == @game.rptla | ||
|
||
'Attach good to a train' | ||
end | ||
|
||
def goods_hexes | ||
@game.hexes.select do |hex| | ||
hex.assignments.keys.find { |a| a.include? 'GOODS' } | ||
end | ||
end | ||
|
||
def choices | ||
choices = {} | ||
goods_train_choices(current_entity).each_with_index do |train, _index| | ||
hex = train['hex'] | ||
index_str = "train\##{train['train_index']}" | ||
index_str += "_#{train['hex'].id}" unless hex.nil? | ||
choices[index_str] = "#{train['train'].name} train\##{train['train_index']} (#{train['hex'].id})" unless hex.nil? | ||
choices[index_str] = "#{train['train'].name} train\##{train['train_index']} unload" if hex.nil? | ||
end | ||
choices | ||
end | ||
|
||
def route_for_train(train) | ||
@round.current_routes[train] unless train.nil? | ||
end | ||
|
||
def get_train_goods_combo(name) | ||
str_split = name.split('_') | ||
train_index = str_split[0].split('#')[1] | ||
train = @game.route_trains(current_entity)[train_index.to_i - 1] | ||
hex = @game.hex_by_id(str_split[1]) if str_split.size > 1 | ||
[train, hex] | ||
end | ||
|
||
def goods_train_choices(entity) | ||
choices_array = [] | ||
@game.route_trains(entity).each_with_index do |train, index| | ||
route = route_for_train(train) | ||
if @game.train_with_goods?(train) | ||
choices_array.push({ train: train, train_index: index + 1, hex: nil, loaded: true }) | ||
else | ||
goods_hexes.each do |hex| | ||
if route | ||
val = { train: train, train_index: index + 1, hex: hex, loaded: false } | ||
choices_array.push(val) if route.hexes.include?(hex) | ||
end | ||
end | ||
end | ||
end | ||
choices_array | ||
end | ||
|
||
def process_choose(action) | ||
entity = action.entity | ||
|
||
train, hex = get_train_goods_combo(action.choice) | ||
|
||
if hex | ||
@log << "#{entity.id} attaches good from #{hex.id} to a #{train.name} train" | ||
|
||
@game.attach_good_to_train(train, hex) | ||
else | ||
@log << "#{entity.id} remove good from #{train.name} train" | ||
@game.unload_good(train) | ||
end | ||
end | ||
|
||
def detach_goods(routes) | ||
routes.each do |route| | ||
train = route.train | ||
next unless @game.train_with_goods?(train) | ||
|
||
hex = @game.good_pickup_hex(train) | ||
good = hex.assignments.keys.find { |a| a.include? 'GOODS' } | ||
@game.unload_good(train) | ||
raise NoToken, "No good token found at Hex #{hex&.id}" if good.nil? | ||
raise NoToken, "Hex #{hex&.id} is not included in route for train #{train.name}" unless route.hexes.include?(hex) | ||
|
||
hex.remove_assignment!(good) | ||
@log << "#{current_entity.id} moves a good to the harbor" | ||
@game.add_good_to_rptla unless good.nil? | ||
end | ||
end | ||
|
||
def process_run_routes(action) | ||
super | ||
entity = action.entity | ||
detach_goods(action.routes) unless action.entity == @game.rptla | ||
@game.route_trains(entity)&.each do |train| | ||
@game.unload_good(train) | ||
end | ||
end | ||
|
||
def round_state | ||
super.merge({ current_routes: {} }) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.