0

So, I was working with an osu API wrapper on node.js and I need to convert the mods from bitwise enums to a list but I don't know-how.

I found a related topic about this but it was in c# but I need it in node.js.

These are the list I need to convert back from the bits

{
    None           = 0,
    NoFail         = 1,
    Easy           = 2,
    TouchDevice    = 4,
    Hidden         = 8,
    HardRock       = 16,
    SuddenDeath    = 32,
    DoubleTime     = 64,
    Relax          = 128,
    HalfTime       = 256,
    Nightcore      = 512, // Only set along with DoubleTime. i.e: NC only gives 576
    Flashlight     = 1024,
    Autoplay       = 2048,
    SpunOut        = 4096,
    Relax2         = 8192,    // Autopilot
    Perfect        = 16384, // Only set along with SuddenDeath. i.e: PF only gives 16416  
    Key4           = 32768,
    Key5           = 65536,
    Key6           = 131072,
    Key7           = 262144,
    Key8           = 524288,
    FadeIn         = 1048576,
    Random         = 2097152,
    Cinema         = 4194304,
    Target         = 8388608,
    Key9           = 16777216,
    KeyCoop        = 33554432,
    Key1           = 67108864,
    Key3           = 134217728,
    Key2           = 268435456,
    ScoreV2        = 536870912,
    LastMod        = 1073741824,
    KeyMod = Key1 | Key2 | Key3 | Key4 | Key5 | Key6 | Key7 | Key8 | Key9 | KeyCoop,
    FreeModAllowed = NoFail | Easy | Hidden | HardRock | SuddenDeath | Flashlight | FadeIn | Relax | Relax2 | SpunOut | KeyMod,
    ScoreIncreaseMods = Hidden | HardRock | DoubleTime | Flashlight | FadeIn
}
Edqe
  • 1
  • 1
  • 3
  • Can you be more specific on what sort of output are you expecting? – matcheek Aug 31 '19 at 13:53
  • You should use a lib like https://www.npmjs.com/package/enum, it's pretty straight forward. – rafaelcastrocouto Aug 31 '19 at 13:54
  • I was expecting to convert a bit (for example 72) to be converted to 'HiddenDoubleTime' or a list (could be an array/object), just like the [c#](https://stackoverflow.com/questions/57574668/is-there-any-way-to-convert-a-sum-of-numbers-back-to-enum) example, but my target is to convert a bit (summed bits also) to a list. – Edqe Aug 31 '19 at 13:59

1 Answers1

0

Well, I found a solution for this from a Discord bot called owo and basically make the function from python to node.js

function numToMod(num) {
    let number = parseInt(num)
    let mod_list = []

    if(number & 1<<0)   mod_list.push('NF')
    if(number & 1<<1)   mod_list.push('EZ')
    if(number & 1<<3)   mod_list.push('HD')
    if(number & 1<<4)   mod_list.push('HR')
    if(number & 1<<5)   mod_list.push('SD')
    if(number & 1<<9)   mod_list.push('NC')
    else if(number & 1<<6) mod_list.push('DT')
    if(number & 1<<7)   mod_list.push('RX')
    if(number & 1<<8)   mod_list.push('HT')
    if(number & 1<<10)  mod_list.push('FL')
    if(number & 1<<12)  mod_list.push('SO')
    if(number & 1<<14)  mod_list.push('PF')
    if(number & 1<<15)  mod_list.push('4 KEY')
    if(number & 1<<16)  mod_list.push('5 KEY')
    if(number & 1<<17)  mod_list.push('6 KEY')
    if(number & 1<<18)  mod_list.push('7 KEY')
    if(number & 1<<19)  mod_list.push('8 KEY')
    if(number & 1<<20)  mod_list.push('FI')
    if(number & 1<<24)  mod_list.push('9 KEY')
    if(number & 1<<25)  mod_list.push('10 KEY')
    if(number & 1<<26)  mod_list.push('1 KEY')
    if(number & 1<<27)  mod_list.push('3 KEY')
    if(number & 1<<28)  mod_list.push('2 KEY')

    return mod_list
}
Edqe
  • 1
  • 1
  • 3