summaryrefslogtreecommitdiffstats
path: root/lib/colonial_twilight/cli.rb
blob: d5a5c5312c092e776e84990b490ee774b59ca44f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-

require 'colonial_twilight'
require 'colonial_twilight/colorized_string'
require 'colonial_twilight/game'

module ColonialTwilight

  class Cli

    def initialize options
      @options = options
      @game = ColonialTwilight::Game.new options
    end

    def start
      logo
      ret = []
      ret << chose('Choose a scenario', @game.scenarios) { |s| a = s.split(':'); a[0] = a[0].yellow; a.join(':') }
      exit(0) if ret[-1] < 0
      ret << chose('Choose a ruleset', @game.rules) { |s| a = s.split('-'); a[0] = a[0].yellow; a.join('-') }
      exit(0) if ret[-1] < 0
      @game.start self, *ret
    end

    def logo
      clear_screen
      puts ('  ____      _             _       _   _____          _ _ _       _     _    '+
            "\n"' / ___|___ | | ___  _ __ (_) __ _| | |_   _|_      _(_) (_) __ _| |__ | |_  ' +
            "\n"'| |   / _ \| |/ _ \| \'_ \| |/ _` | |   | | \ \ /\ / / | | |/ _` | \'_ \| __| ' +
            "\n"'| |__| (_) | | (_) | | | | | (_| | |   | |  \ V  V /| | | | (_| | | | | |_  ' +
            "\n"' \____\___/|_|\___/|_| |_|_|\__,_|_|   |_|   \_/\_/ |_|_|_|\__, |_| |_|\__| ' +
            "\n"'                                                           |___/            ').white.bold.on_light_green
      puts "version : #{ColonialTwilight::VERSION.red}".black.on_white
    end

    def clear_screen
      puts String::CLS
    end

    PS = { :FLN => 'FLN'.red, :GOV => 'Government'.red }

    def turn_start turn, first, second
      clear_screen if @options.clearscreen
      puts
      puts ("=" * 80).white.bold.on_light_green
      puts " Turn : #{turn.to_s.red} ".black.on_white + "\t First Eligible  : #{PS[first.faction]} ".black.on_white
      puts "\t\t Second Eligible : #{PS[second.faction]} ".black.on_white
    end

    def pull_card max
      puts
      printf "Enter the current #{'card number'.yellow} : "
      while true
        s = gets.chomp
        if s.to_i.to_s == s.to_s
          ret = s.to_i
          return ret if ret < max
        end
        puts "\t\t\t\t'#{s}' is not valid, must be one of [1..#{max}]"
        printf "\t$ "
      end
    end

    def show_card card
      puts
      puts "Current event card : ##{card.num.to_s.yellow} #{card.title.red}"
    end

    def player p, first
      puts
      clear_screen if @options.clearscreen
      puts
      puts " #{PS[p.faction]} is #{first ? 'First Eligible' : 'Second Eligible'}".black.on_white
    end

    def chose prompt, list, quit=false
      puts
      puts " => #{prompt.yellow}:"
      puts ('-'*(prompt.size + 5)).white.bold
      list.each_with_index do |el, i|
        puts "\t#{(i+1).to_s.bold}) : #{block_given? ? yield(el): el}"
      end
      puts "\tq) : Quit" if quit

      printf "\t$ "
      ret = -1
      while true
        s = gets.chomp
        return -1 if s == 'q'
        if s.to_i.to_s == s.to_s
          ret = s.to_i
          return ret - 1 if ret >= 1 and ret <= list.length
        end
        puts "\t\t\t\t'#{s}' is not valid, must be one of [1..#{list.length}]"
        printf "\t$ "
      end
    end

    YES=['y','yes']
    NO=['n','no']
    def ask prompt, default=nil
      puts
      c = (default.nil? ? 'y/n' : (default ? 'Y/n' : 'y/N'))
      printf " => #{prompt.yellow} (#{c}) ? "
      while true
        ret = gets.chomp.downcase
        return true if YES.include? ret
        return false if NO.include? ret
        return default if not default.nil?
        puts "\t\t\t\t'#{ret}' is not valid, (y/n) ?"
        printf "\t$ "
      end
    end

  end

end

if $PROGRAM_NAME == __FILE__
  io = ColonialTwilight::Cli.new
  io.logo
  puts
  l = ['Short: 1960-1962: The End Game','Medium: 1957-1962: Midgame Development','Full: 1955-1962: Algerie Francaise!']
  ret = io.chose('Choose a scenario', l) { |s| a = s.split(':'); a[0] = a[0].yellow; a.join(':') }
  puts l[ret]
  ret = io.ask 'Are you sure'
  puts ret
  ret = io.ask 'Are you sure', true
  puts ret
  ret = io.ask 'Are you sure', false
  puts ret
  puts
  io.turn_start(1, [:FLN, :GOV])
end