Skip to content

Commit

Permalink
[1858 Switzerland] Avoid modifying 1858's train definitions
Browse files Browse the repository at this point in the history
#11372 was caused by modifying 1858's train definitions without a
making a deep copy. If a game of 1858 is loaded on the server after a
game of 1858 Switzerland then it is was seeing the version of the definitions
after these modifications, causing errors as 1858 does not have a method
for handling the `blue_privates_available` event.

Making a deep copy of the train definitions prevents this error.

This commit also makes a deep copy of the phase definitions. No problems
have been observed with this, but making the copy is safer.

Fixes #11372.
  • Loading branch information
ollybh committed Dec 21, 2024
1 parent b00e090 commit f724851
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions lib/engine/game/g_1858_switzerland/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ class Game < G1858::Game
ROBOT_MAJOR_TILE_LAYS = [{ lay: true, upgrade: true }].freeze

def game_phases
phases = super
_phase2, _phase3, phase4, phase5, phase6 = phases
phase4[:status] = %w[all_privates narrow_gauge]
phase5[:status] = %w[blue_privates public_companies dual_gauge]
phase6[:tiles] = %i[yellow green brown gray]
phases
unless @game_phases
@game_phases = super.map(&:dup)
_phase2, _phase3, phase4, phase5, phase6 = @game_phases
phase4[:status] = %w[all_privates narrow_gauge]
phase5[:status] = %w[blue_privates public_companies dual_gauge]
phase6[:tiles] = %i[yellow green brown gray]
end
@game_phases
end

def timeline
Expand Down Expand Up @@ -118,16 +120,25 @@ def event_privates_close2!
GREY_TRAINS = %w[6E 5M 5D].freeze

def game_trains
trains = super
train_2h, _train_4h, train_6h, _train_5e, train_6e, train_5d = trains
train_2h[:events] = [{ 'type' => 'sbb_starts' }] if robot?
train_6h.delete(:obsolete_on) # Wounded on second grey train, handled in code
train_6h[:events] = [{ 'type' => 'blue_privates_available' }]
train_6e[:events] = [{ 'type' => 'privates_close2' }]
train_6e[:price] = 700
train_6e[:variants][0][:price] = 600
train_5d[:available_on] = '6'
trains
unless @game_trains
# Need to make a deep copy of 1858's train definitions.
# https://github.com/tobymao/18xx/issues/11372 was caused by
# modifying the definitions without a deep copy. If a game of 1858
# is loaded on the server after a game of 1858 Switzerland then it
# is was seeing the version of the definitions after these
# modifications, causing errors as 1858 does not have a method for
# handling the `blue_privates_available` event.
@game_trains = super.map(&:dup)
train_2h, _train_4h, train_6h, _train_5e, train_6e, train_5d = @game_trains
train_2h[:events] = [{ 'type' => 'sbb_starts' }] if robot?
train_6h.delete(:obsolete_on) # Wounded on second grey train, handled in code
train_6h[:events] = [{ 'type' => 'blue_privates_available' }]
train_6e[:events] = [{ 'type' => 'privates_close2' }]
train_6e[:price] = 700
train_6e[:variants][0][:price] = 600
train_5d[:available_on] = '6'
end
@game_trains
end

def num_trains(train)
Expand Down

0 comments on commit f724851

Please sign in to comment.