Module: Osu::API::Mods

Defined in:
lib/osu/api/mods.rb

Constant Summary collapse

MODS =

A hashmap of Osu game mods

{
  0  => :no_fail,      # 1
  1  => :easy,         # 2
  2  => :no_video,     # 4
  3  => :hidden,       # 8
  4  => :hard_rock,    # 16
  5  => :sudden_death, # 32
  6  => :double_time,  # 64
  7  => :relax,        # 128
  8  => :half_time,    # 256
  9  => :nightcore,    # 512
  10 => :flashlight,   # 1024
  11 => :autoplay,     # 2048
  12 => :spun_out,     # 4096
  13 => :relax2,       # 8192
  14 => :perfect,      # 16384
  16 => :key4,         # 32768
  17 => :key5,         # 65536
  18 => :key6,         # 131072
  19 => :key7,         # 262144
  20 => :key8,         # 524288
  21 => :fade_in,      # 1048576
  22 => :random,       # 2097152
  23 => :last_mod,     # 4194304
  24 => :key9,         # 16777216
  25 => :key10,        # 33554432
  26 => :key1,         # 67108864
  27 => :key3,         # 134217728
  28 => :key2          # 268435456
}.freeze

Class Method Summary collapse

Class Method Details

.bits(mods_list) ⇒ Integer

Convert an array of symbols into bits to be used in filtering API results in a request


63
64
65
66
67
68
69
70
71
72
# File 'lib/osu/api/mods.rb', line 63

def bits(mods_list)
  value = 0

  MODS.each do |position, flag|
    puts "#{position} : #{flag} | #{mods_list.include? flag}"
    value += 2**position if mods_list.include? flag
  end

  value
end

.mods(bits, stringify = false) ⇒ Array<Symbol>, Array<String>

Convert bits from the API into a readable list of the mods applied.


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/osu/api/mods.rb', line 44

def mods(bits, stringify = false)
  return stringify ? ['None'] : [:none] if bits.zero?

  flags = []

  MODS.each do |position, flag|
    flags << flag if ((bits >> position) & 0x1) == 1
  end

  flags.map! { |s| s.to_s.split('_').map(&:capitalize) * ' ' } if stringify

  flags
end