Skip to content

Commit

Permalink
Merge branch 'tobymao:master' into SOH-train-discount-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
philcampeau authored Oct 10, 2024
2 parents b7f9d89 + 588b79c commit 9ead87a
Show file tree
Hide file tree
Showing 62 changed files with 1,361 additions and 456 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CONTAINER_COMPOSE ?= $(CONTAINER_ENGINE)-compose
CONTAINER_ENGINE ?= docker

clean:
sudo rm -rfv build/ public/assets/*.js public/assets/*.js.gz public/assets/version.json
sudo rm -rfv build/ public/assets/*.js public/assets/*.js.map public/assets/*.js.gz public/assets/version.json

cleandeps:
sudo rm -rfv public/assets/deps.js
Expand Down
2 changes: 1 addition & 1 deletion api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Api < Roda
}
end

ASSETS = Assets.new(precompiled: PRODUCTION)
ASSETS = Assets.new(precompiled: PRODUCTION, source_maps: !PRODUCTION)

Bus.configure

Expand Down
2 changes: 1 addition & 1 deletion assets/app/view/game/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def render
@step.respond_to?(:dividend_name) ? @step.dividend_name(type) : type
end

corp_income = option[:corporation] + option[:divs_to_corporation]
corp_income = option[:corporation] + option[:divs_to_corporation] + @step.total_subsidy

direction =
if (new_share_price = entity.share_price) && option[:share_direction]
Expand Down
65 changes: 57 additions & 8 deletions lib/assets.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'json'
require 'opal'
require 'snabberb'
require 'tempfile'
Expand All @@ -12,7 +13,40 @@ class Assets
OUTPUT_BASE = 'public'
PIN_DIR = '/pinned/'

def initialize(compress: false, gzip: false, cache: true, precompiled: false)
class SourceMap
def initialize
@source_map = {
'version' => 3,
'sections' => [],
}
@current_line = 0
end

def append(file_contents, source_map_file)
@source_map['sections'].append({
'offset' => { 'line' => @current_line, 'column' => 0 },
'map' => JSON.parse(File.read(source_map_file)),
})
@current_line += file_contents.lines.count + 1
end

def extend(file_contents, source_map_file)
file_source_map = JSON.parse(File.read(source_map_file))
file_source_map['sections'].each do |section|
@source_map['sections'].append({
'offset' => { 'line' => @current_line + section['offset']['line'], 'column' => 0 },
'map' => section['map'],
})
end
@current_line += file_contents.lines.count + 1
end

def to_json(*args)
@source_map.to_json(*args)
end
end

def initialize(compress: false, gzip: false, cache: true, precompiled: false, source_maps: false)
@build_path = 'build'
@out_path = OUTPUT_BASE + '/assets'
@root_path = '/assets'
Expand All @@ -25,6 +59,7 @@ def initialize(compress: false, gzip: false, cache: true, precompiled: false)
@compress = compress
@gzip = gzip
@precompiled = precompiled
@source_maps = source_maps
end

def context(titles)
Expand Down Expand Up @@ -97,8 +132,8 @@ def builds(titles = [])
**game_builds(:all),
}
else
@_opal ||= compile_lib('opal')
@_deps ||= compile_lib('deps', 'assets')
@_opal ||= compile_lib('opal', 'opal')
@_deps ||= compile_lib('deps_only', 'deps', 'assets')
@_engine ||= compile('engine', 'lib', 'engine')
@_app ||= compile('app', 'assets/app', '')

Expand Down Expand Up @@ -163,9 +198,17 @@ def combine(titles = [])

@_combined.include?(key) ? next : @_combined.add(key)

source = build['files'].map { |file| File.read(file).to_s }.join("\n")
source_map = SourceMap.new
source = build['files'].map do |filepath|
file = File.read(filepath).to_s
source_map.extend(file, filepath + '.map') if @source_maps
file
end.join("\n")
source += "\n//# sourceMappingURL=#{build['path'].delete_prefix('public')}.map\n" if @source_maps

source = compress(key, source) if @compress
File.write(build['path'], source)
File.write(build['path'] + '.map', source_map.to_json) if @source_maps

next if !@gzip || build['path'] == @server_path

Expand All @@ -182,13 +225,14 @@ def combine(titles = [])
[@deps_path, @main_path, *game_paths]
end

def compile_lib(name, *append_paths)
def compile_lib(output_name, name, *append_paths)
builder = Opal::Builder.new
append_paths.each { |ap| builder.append_paths(ap) }
path = "#{@out_path}/#{name}.js"
if !@cache || !File.exist?(path) || path == @deps_path
path = "#{@out_path}/#{output_name}.js"
if !@cache || !File.exist?(path)
time = Time.now
File.write(path, builder.build(name))
File.write("#{path}.map", builder.source_map.map.to_json) if @source_maps
puts "Compiling #{name} - #{Time.now - time}"
end
path
Expand All @@ -214,17 +258,22 @@ def compile(name, lib_path, ns = nil, game: nil)

time = Time.now
File.write(opts[:js_path], compiler.compile)
File.write(opts[:js_path] + '.map', compiler.source_map.map.to_json) if @source_maps
puts "Compiling #{file} - #{Time.now - time}"
end

source_map = SourceMap.new
source = metadata.map do |_file, opts|
File.read(opts[:js_path]).to_s
file = File.read(opts[:js_path])
source_map.append(file, opts[:js_path] + '.map') if @source_maps
file
end.join("\n")

opal_load = game ? "engine/game/#{game}" : name
source += "\nOpal.load('#{opal_load}')"

File.write(output, source)
File.write(output + '.map', source_map.to_json) if @source_maps
output
end

Expand Down
7 changes: 5 additions & 2 deletions lib/engine/action/run_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
module Engine
module Action
class RunRoutes < Base
attr_reader :routes, :extra_revenue
attr_reader :routes, :extra_revenue, :subsidy

def initialize(entity, routes:, extra_revenue: 0)
def initialize(entity, routes:, extra_revenue: 0, subsidy: 0)
super(entity)
@routes = routes
@extra_revenue = extra_revenue
@subsidy = subsidy
end

def self.h_to_args(h, game)
Expand Down Expand Up @@ -41,6 +42,7 @@ def self.h_to_args(h, game)
{
routes: routes,
extra_revenue: h['extra_revenue'],
subsidy: h['subsidy'],
}
end

Expand All @@ -62,6 +64,7 @@ def args_to_h
{
'routes' => routes,
'extra_revenue' => extra_revenue,
'subsidy' => subsidy,
}
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/engine/game/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,9 @@ def process_action(action, add_auto_actions: false, validate_auto_actions: false
@last_processed_action = action.id

self
rescue StandardError => e
rescue_exception(e, action)
self
# rescue StandardError => e
# rescue_exception(e, action)
# self
end

def process_single_action(action)
Expand All @@ -838,8 +838,8 @@ def process_single_action(action)
transition_to_next_round!
end
end
rescue Engine::GameError => e
rescue_exception(e, action)
# rescue Engine::GameError => e
# rescue_exception(e, action)
end

def rescue_exception(e, action)
Expand Down
24 changes: 11 additions & 13 deletions lib/engine/game/g_1822/step/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ def dividend_options(entity)
if extra_train
train = find_extra_train(entity)
extra_train_revenue = routes.find { |r| r.train == train }.revenue
extra_train_payout = send(@extra_train_choice, entity, extra_train_revenue, 0)
extra_train_payout = send(@extra_train_choice, entity, extra_train_revenue)
revenue = total_revenue - extra_train_revenue
else
revenue = total_revenue
end
subsidy = @game.routes_subsidy(routes)
total_revenue += subsidy
dividend_types.to_h do |type|
payout = send(type, entity, revenue, subsidy)
payout = send(type, entity, revenue)
if extra_train
payout[:corporation] += extra_train_payout[:corporation]
payout[:per_share] += extra_train_payout[:per_share]
Expand All @@ -67,9 +65,9 @@ def find_extra_train(entity)
revenue.positive? && revenue != @game.routes_revenue(routes) ? train : nil
end

def half(entity, revenue, subsidy)
def half(entity, revenue)
withheld = half_pay_withhold_amount(entity, revenue)
{ corporation: withheld + subsidy, per_share: payout_per_share(entity, revenue - withheld) }
{ corporation: withheld, per_share: payout_per_share(entity, revenue - withheld) }
end

def half_pay_withhold_amount(entity, revenue)
Expand All @@ -83,7 +81,7 @@ def holder_for_corporation(entity)
def log_run_payout(entity, kind, revenue, subsidy, _action, payout)
@log << "#{entity.name} runs for #{@game.format_currency(revenue)} and pays half" if kind == 'half'

withhold = payout[:corporation] - subsidy
withhold = payout[:corporation]
if withhold.positive?
@log << "#{entity.name} withholds #{@game.format_currency(withhold)}"
elsif payout[:per_share].zero?
Expand All @@ -92,8 +90,8 @@ def log_run_payout(entity, kind, revenue, subsidy, _action, payout)
@log << "#{entity.name} earns subsidy of #{@game.format_currency(subsidy)}" if subsidy.positive?
end

def payout(entity, revenue, subsidy)
{ corporation: subsidy, per_share: payout_per_share(entity, revenue) }
def payout(entity, revenue)
{ corporation: 0, per_share: payout_per_share(entity, revenue) }
end

def payout_shares(entity, revenue)
Expand Down Expand Up @@ -138,8 +136,8 @@ def process_dividend(action)

@round.routes = []
log_run_payout(entity, kind, revenue, subsidy, action, payout)
@game.bank.spend(payout[:corporation], entity) if payout[:corporation].positive?
payout_shares(entity, revenue + subsidy - payout[:corporation]) if payout[:per_share].positive?
payout_corporation(payout[:corporation] + subsidy, entity)
payout_shares(entity, revenue - payout[:corporation]) if payout[:per_share].positive?
change_share_price(entity, payout)

pass!
Expand Down Expand Up @@ -171,8 +169,8 @@ def share_price_change(entity, revenue = 0)
end
end

def withhold(_entity, revenue, subsidy)
{ corporation: revenue + subsidy, per_share: 0 }
def withhold(_entity, revenue)
{ corporation: revenue, per_share: 0 }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/engine/game/g_1822_nrs/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module Map
'G20' => 'Blackpool',
'G22' => 'Liverpool',
'G24' => 'Chester',
'G28' => 'Shrewbury',
'G28' => 'Shrewsbury',
'H1' => 'Aberdeen',
'H3' => 'Dunfermline',
'H5' => 'Edinburgh',
Expand Down
2 changes: 1 addition & 1 deletion lib/engine/game/g_1825/step/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def process_dividend(action)
@round.receivership_loan = 0
@round.routes = []

log_run_payout(entity, kind, revenue, action, payout)
log_run_payout(entity, kind, revenue, 0, action, payout)

payout_corporation(payout[:corporation], entity)

Expand Down
2 changes: 1 addition & 1 deletion lib/engine/game/g_1840/step/dividend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def payout_corporation(amount, entity)
major.spend(to_pay, @game.bank)
end

def log_run_payout(entity, kind, revenue, action, payout)
def log_run_payout(entity, kind, revenue, _subsidy, action, payout)
unless Dividend::DIVIDEND_TYPES.include?(kind)
@log << "#{entity.name} runs for #{@game.format_currency(revenue)} and pays #{action.kind}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/engine/game/g_1847_ae/entities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module Entities
revenue: 30,
min_price: 100,
max_price: 200,
desc: 'Revenue increases to 50M when a tile is laid in E9. '\
desc: 'Revenue increases to 50M when a tile is laid in E9 (the tile may be laid only in Phase 5 or later). '\
'May be sold to a corporation for 100 to 200M. '\
'Never closes.',
sym: 'R',
Expand Down
4 changes: 2 additions & 2 deletions lib/engine/game/g_1847_ae/step/buy_single_train_of_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def buyable_trains(entity)
end

def process_buy_train(action)
from_depot = action.train.from_depot?
from_discard = @depot.discarded.include?(action.train)
super

lfk = @game.lfk
return if @game.train_bought_this_round || !lfk.floated? || !from_depot
return if @game.train_bought_this_round || !lfk.floated? || from_discard

lfk_owner = lfk.owner
if lfk_owner.player?
Expand Down
2 changes: 1 addition & 1 deletion lib/engine/game/g_1850_jr/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module G1850Jr
module Meta
include Game::Meta

DEV_STAGE = :alpha
DEV_STAGE = :beta

GAME_DESIGNER = 'Gabriele Callari, Fabio Pellegrino'
GAME_LOCATION = 'Sicily'
Expand Down
Loading

0 comments on commit 9ead87a

Please sign in to comment.