class YolodiceGenerator

Public Class Methods

new(server_key_hex, client_phrase) click to toggle source
# File lib/yolodice_validator.rb, line 26
def initialize server_key_hex, client_phrase
  @server_key = [server_key_hex].pack("H*")
  @client_phrase = client_phrase
end

Public Instance Methods

generate_bet(nonce, amount, target, range) click to toggle source

When given bet result and bet input parameters the function returns a Hash that include bet profit.

# File lib/yolodice_validator.rb, line 52
def generate_bet nonce, amount, target, range
  result = roll nonce
  profit = (amount * (1_000_000.0 * (1 - 0.01) / target - 1)).floor
  win = case range
        when 'lo'
          result < target
        when 'hi'
          result > 999_999 - target
        end
  profit = -amount unless win
  {
    result: result,
    profit: profit,
    win: win
  }
end
roll(nonce) click to toggle source

Runs the algorithm that returns bet result.

# File lib/yolodice_validator.rb, line 41
def roll nonce
  hash = OpenSSL::HMAC.hexdigest 'sha512', @server_key, "#{@client_phrase}.#{nonce}"
  out = nil
  while !out || out >= 1_000_000
    out = hash.slice!(0,5).to_i 16
  end
  out
end
server_key_hash_hex() click to toggle source
# File lib/yolodice_validator.rb, line 35
def server_key_hash_hex
  # OpenSSL::Digest::SHA256.hexdigest server_key_hex
  OpenSSL::Digest::SHA256.hexdigest server_key_hex
end
server_key_hex() click to toggle source
# File lib/yolodice_validator.rb, line 31
def server_key_hex
  @server_key.unpack('H*')[0]
end