Class: YolodiceValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/yolodice_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_file) ⇒ YolodiceValidator

Returns a new instance of YolodiceValidator.



73
74
75
76
# File 'lib/yolodice_validator.rb', line 73

def initialize input_file
  @input_file = input_file
  @mismatch_count = 0
end

Class Method Details

.parse_opts(argv) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/yolodice_validator.rb', line 132

def parse_opts argv
  # There is only one option - pass a file containing a CSV dump
  usage = "Usage: yolodice_validator DUMP_FILE"
  unless argv.length == 1
    puts usage
    exit
  end
  return { input_file: argv[0] }
end

.run(opts) ⇒ Object



142
143
144
145
# File 'lib/yolodice_validator.rb', line 142

def run opts
  yv = YolodiceValidator.new opts[:input_file]
  yv.run
end

Instance Method Details

#runObject



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
# File 'lib/yolodice_validator.rb', line 78

def run
  CSV.open @input_file do |file|
    # parse first line
    seed_id0, secret_hashed_hex0, secret_hex0, client_phrase0, seed_created_at0 = file.shift
    @generator = YolodiceGenerator.new secret_hex0, client_phrase0
    assume 'secret_hashed_hex', secret_hashed_hex0, @generator.server_key_hash_hex
    if @mismatch_count == 0
      puts "Seed seems OK, validating individual bets."
    else
      puts "Seed data is not valid, checking bets anyway."
    end
    print '.'
    @last_dot = true
    # read the rest of lines, verify individual bets
    while bet_data = file.shift do
      bet_id0, nonce0, rolled0, target0, range0, amount0, profit0 = bet_data
      bet_id0 = bet_id0.to_i
      nonce0 = nonce0.to_i
      rolled0 = rolled0.to_i
      target0 = target0.to_i
      amount0 = amount0.to_i
      profit0 = profit0.to_i

      bet = @generator.generate_bet nonce0, amount0, target0, range0
      assume "bet #{bet_id0} result", rolled0.to_i, bet[:result]
      assume "bet #{bet_id0} profit", profit0.to_i, bet[:profit]
      if nonce0 % 1000 == 0
        print '.' 
        @last_dot = true
      end
    end
    print "\n"
    if @mismatch_count == 0
      puts "All bets verified OK"
    else
      puts "#{@mismatch_count} errors found."
    end

  end
end