class Rsrb::Misc::NameUtils

Provides utility methods for converting and validating player usernames.

Constants

VALID_CHARS

Public Class Methods

fix_name(str) click to toggle source

Fixes capitalization in the username.

# File lib/rsrb/core/util.rb, line 231
def NameUtils.fix_name(str)
  return str if str.length < 1
  
  (0...str.length).each {|i|
    if str[i].chr == "_"
      str[i] = " " 
      if i+1 < str.length && str[i+1].chr >= 'a' && str[i+1].chr <= 'z'
        str[i+1] = ((str[i+1].bytes.first + 65) - 97).chr
      end
    end
  }
  
  if str[0].chr >= 'a' && str[0].chr <= 'z'
    str[0] = ((str[0].bytes.first + 65) - 97).chr
  end
  
  str
end
format_name(str) click to toggle source

Formats the username for display.

# File lib/rsrb/core/util.rb, line 251
def NameUtils.format_name(str)
  NameUtils.fix_name str.gsub(" ", "_")
end
format_name_protocol(str) click to toggle source

Formats the username for protocol usage.

# File lib/rsrb/core/util.rb, line 256
def NameUtils.format_name_protocol(str)
  str.downcase.gsub(" ", "_")
end
long_to_name(n) click to toggle source

Converts the 64 bit integer version of the username into a string.

# File lib/rsrb/core/util.rb, line 285
def NameUtils.long_to_name(n)
  n = n.long
  str = ""
  i = 0
  while n != 0
    k = n.long
    n = (n / 37).long
    str << VALID_CHARS[(k-n*37).int]
    i = i+1
  end
  
  str.reverse
end
name_to_long(name) click to toggle source

Converts the username into a 64 bit integer.

# File lib/rsrb/core/util.rb, line 266
def NameUtils.name_to_long(name)
  l = 0
  
  (0...name.length).each {|i|
    c = name[i].chr
    l *= 37
    l += (1  + name[i].bytes.first) - 65 if c >= 'A' and c <= 'Z'
    l += (1  + name[i].bytes.first) - 97 if c >= 'a' and c <= 'z'
    l += (27 + name[i].bytes.first) - 48 if c >= '0' and c <= '9'
  }
  
  while l % 37 == 0 && l != 0
    l /= 37
  end
  
  l
end
valid_name?(str) click to toggle source

Checks whether the username follows the protocol format.

# File lib/rsrb/core/util.rb, line 261
def NameUtils.valid_name?(str)
  NameUtils.format_name_protocol(str) =~ /^[a-z0-9_]+$/
end