Converts ids array to one integer by math or bits operations
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md
convertids.js
index.html

README.md

ConvertIds

Converts ids array to one integer by math or bits operations.

Required

http://underscorejs.org/ lib is required for this class

Usage

  1. Set items configs
var armor = [
  { id: 1, name: 'head' },
  { id: 2, name: 'body' },
  { id: 3, name: 'legs' },
  { id: 4, name: 'arms' }
];

var weapon = [
  { id: 1, name: 'bullets' },
  { id: 2, name: 'bombs' },
  { id: 3, name: 'health' }
];
  1. Init players data
var playerArmor = [2,4];

var playerWeapon = [
  { id: 1, amount: 10 },
  { id: 3, amount: 5 }
];
  1. Init ConvertIds
var armorConverter = new ConvertIds(armor, ConvertIds.BITS);
var weaponConverter = new ConvertIds(weapon, ConvertIds.MATH);
  1. Make convert
var playerWeaponEncoded = weaponConverter.encode(playerWeapon);
console.log(playerWeaponEncoded); //50010
var playerWeaponDecoded = weaponConverter.decode(playerWeaponEncoded);
console.log(playerWeaponDecoded); //{id: 1, amount: 10}1: {id: 2, amount: 0}2: {id: 3, amount: 5}

var playerArmorEncoded = armorConverter.encode(playerArmor);
console.log(playerArmorEncoded); //20
var playerArmorDecoded = armorConverter.decode(playerArmorEncoded);
console.log(playerArmorDecoded); //(2) [2, 4]