From 1ea408bc51382058fbeb54e88678432a459a44c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 3 Sep 2020 15:45:54 +0200 Subject: Board/Cli : introduce Box class, clean up CLI --- lib/colonial_twilight/board.rb | 22 ++++++++++++++------ lib/colonial_twilight/cli.rb | 44 +++++++++++++++++++++++++--------------- lib/colonial_twilight/fln_bot.rb | 19 ++++++----------- 3 files changed, 50 insertions(+), 35 deletions(-) diff --git a/lib/colonial_twilight/board.rb b/lib/colonial_twilight/board.rb index ba6956b..6ddd97b 100644 --- a/lib/colonial_twilight/board.rb +++ b/lib/colonial_twilight/board.rb @@ -137,6 +137,14 @@ module ColonialTwilight end + class Box < Forces + attr_reader :name + def initialize sym + super sym + @name = sym + end + end + class Sector MOUNTAIN=1 @@ -265,9 +273,9 @@ module ColonialTwilight @names = [] @spaces_h = {} @capabilities = [] - @available = Forces.new :available - @casualties = Forces.new :casualties - @out_of_play = Forces.new :out_of_play + @available = Box.new :available + @casualties = Box.new :casualties + @out_of_play = Box.new :out_of_play feed @spaces = @spaces_h.values @sectors = @spaces.select { |s| not s.country? } @@ -283,10 +291,12 @@ module ColonialTwilight end def transfer n, what, from, to, towhat=nil - from = (from.is_a?(Sector) ? from : get_local(from) ) - to = (to.is_a?(Sector) ? to : get_local(to) ) + towhat = what if towhat.nil? + from = get_var from if from.is_a? Symbol + to = get_var to if to.is_a? Symbol from.add what, -n to.add towhat, n + { :n => n, :what => what, :from=>from, :to => to, :towhat=> towhat } end def terror where, n @@ -394,7 +404,7 @@ module ColonialTwilight private - def get_local sym + def get_var sym case sym when :available; return @available when :casualties; return @casualties diff --git a/lib/colonial_twilight/cli.rb b/lib/colonial_twilight/cli.rb index f0ca895..97700ea 100644 --- a/lib/colonial_twilight/cli.rb +++ b/lib/colonial_twilight/cli.rb @@ -7,6 +7,20 @@ require 'colonial_twilight/game' module ColonialTwilight + class Sector + def cli + name.magenta + end + def towhat?; true end + end + + class Box + def cli + Cli::BOXES[name].cyan + end + def towhat?; false end + end + class Cli FRANCE_TRACK=[' A ',' B ',' C ',' D ',' E ',' F '].map {|e| e.white.on_blue}.freeze @@ -139,17 +153,17 @@ module ColonialTwilight # def show_control selected, control # s = "\t => shift #{'control'.cyan} to #{CONTROL[control]}" - # s += " in #{selected.name.magenta}" if @options.verbose + # s += " in #{selected.magenta}" if @options.verbose # puts s # end def show_controls selected, controls controls.each do |k,v| - puts "\t => shift #{'control'.cyan} to #{CONTROL[v[1]]} in #{k.name.magenta}" if k != selected + puts "\t => shift #{'control'.cyan} to #{CONTROL[v[1]]} in #{k.cli}" if k != selected end if controls.has_key? selected s = "\t => shift #{'control'.cyan} to #{CONTROL[controls[selected][1]]}" - s += " in #{selected.name.magenta}" if @options.verbose + s += " in #{selected.cli}" if @options.verbose puts s end end @@ -163,13 +177,13 @@ module ColonialTwilight puts "\t#{'PASS'.red} increase #{'resources'.yellow} by #{v} to #{rcs[:value].to_s.red}" else action = action.to_s.capitalize - sym = selected.is_a?(Symbol) - where = sym ? "on #{BOXES[selected]}" : "in #{selected.name.magenta}" + sym = selected.is_a?(Symbol) # FIXME Symbol => France track or Border track + where = sym ? "on #{BOXES[selected]}" : "in #{selected.cli}" cost = (incr == 0 ? nil : "#{incr < 0 ? 'increase' : 'decrease'} #{'resources'.yellow} by #{v} to #{rcs[:value].to_s.red}") if @options.verbose puts "\t#{faction} executes a #{action.yellow} Operation #{where}" puts "\t#{cost}" unless cost.nil? - puts "\tin #{selected.name.magenta} :" unless sym + puts "\tin #{selected.cli} :" unless sym else s = "\t#{action.black.on_white} #{where} " s += cost unless cost.nil? @@ -185,17 +199,15 @@ module ColonialTwilight what, towhat = tr[:what], tr[:towhat] if from == to s = "\t => flip #{n} #{FORCES[what]} to #{FORCES[towhat]}" - s += " in #{selected.name.cyan}" if @options.verbose + s += " in #{selected.cyan}" if @options.verbose puts s else - froms = (from.is_a?(Sector) ? from.name.magenta : BOXES[from] ) - tos = (to.is_a?(Sector) ? to.name.magenta : BOXES[to] ) s = "\t => transfer #{n} #{FORCES[what]}" - s += " from #{froms}" if @options.verbose or from != selected - s += " to #{tos}" if @options.verbose or to != selected - s += " as #{FORCES[towhat]}" if @options.verbose or (what != towhat and to.is_a?(Sector)) - # puts "\t => shift #{'control'.cyan} to #{CONTROL[controls[from]]} in #{froms}" if from != selected and controls.has_key?(from) - # puts "\t => shift #{'control'.cyan} to #{CONTROL[controls[to]]} in #{tos}" if to != selected and controls.has_key?(to) + s += " from #{from.cli}" if @options.verbose or from != selected + s += " to #{to.cli}" if @options.verbose or to != selected + s += " as #{FORCES[towhat]}" if @options.verbose or (what != towhat and to.towhat?) + # puts "\t => shift #{'control'.cyan} to #{CONTROL[controls[from]]} in #{from.cli}" if from != selected and controls.has_key?(from) + # puts "\t => shift #{'control'.cyan} to #{CONTROL[controls[to]]} in #{to.cli}" if to != selected and controls.has_key?(to) puts s end end @@ -208,11 +220,11 @@ module ColonialTwilight when :terror act = (n > 0 ? 'add' : 'remove').red s = "\t => #{act} #{n.to_s.cyan} #{'Terror'.yellow} markers" - s += " in #{selected.name.magenta} from #{from.to_s.cyan} to #{to.to_s.red}" if @options.verbose + s += " in #{selected.cli} from #{from.to_s.cyan} to #{to.to_s.red}" if @options.verbose puts s when :alignment if @options.verbose - puts "\t => shift #{selected.name.magenta} #{n.to_s.cyan} times towards #{ALIGNMENT[towards]} from #{ALIGNMENT[from]} to #{ALIGNMENT[to]}" + puts "\t => shift #{selected.cli} #{n.to_s.cyan} times towards #{ALIGNMENT[towards]} from #{ALIGNMENT[from]} to #{ALIGNMENT[to]}" else puts "\t => shift #{'Alignment'.yellow} onto #{ALIGNMENT[to]}" end diff --git a/lib/colonial_twilight/fln_bot.rb b/lib/colonial_twilight/fln_bot.rb index 0cb8b46..e119d36 100644 --- a/lib/colonial_twilight/fln_bot.rb +++ b/lib/colonial_twilight/fln_bot.rb @@ -576,11 +576,9 @@ module ColonialTwilight end def transfer h, n, what, from, to, towhat=nil - towhat = what if towhat.nil? h[:controls][from] ||= from.control unless from.is_a? Symbol h[:controls][to] ||= to.control unless to.is_a? Symbol - @board.transfer n, what, from, to, towhat - h[:transfers] << { :n => n, :what => what, :from=>from, :to => to, :towhat=> towhat } + h[:transfers] << @board.transfer( n, what, from, to, towhat) # puts h[:transfers][-1] if @debug end @@ -595,15 +593,16 @@ module ColonialTwilight IGNORE = [:pass, :event, :agitate].freeze def apply_action h + selected = h[:selected] action = h[:action] if OPERATIONS.include? action operation_done action - raise "already selected #{h[:selected].name}" if @selected_spaces.include? h[:selected] - @selected_spaces << h[:selected] #unless @selected_spaces.include? h[:selected] + raise "already selected #{selected.name}" if @selected_spaces.include? selected + @selected_spaces << selected #unless @selected_spaces.include? selected puts 'selected spaces :: ' + @selected_spaces.collect(){|s| s.is_a?(Symbol) ? s.to_s : s.name}.join(' :: ') if @debug elsif SPECIAL_ACTIVITIES.include? action special_activity_done action - @selected_spaces << h[:selected] if action == :ambush + @selected_spaces << selected if action == :ambush elsif not IGNORE.include? action raise "apply unknown action #{h[:action]}" else @@ -613,13 +612,7 @@ module ColonialTwilight @board.shift_resources :fln, -cost @expended_resources += cost unless h.has_key? :already_expended # _reserve_agitate h[:resources] = {:cost=>cost, :value=>@board.fln_resources} - h[:controls].each do |k,v| - if v != k.control - h[:controls][k] = [v, k.control] - else - h[:controls].delete k - end - end + h[:controls] = h[:controls].inject({}){|ch,(k,v)| ch[k] = [v, k.control] if v != k.control; ch} @ui.show_player_action self, h true end -- cgit v1.1-2-g2b99