Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gacha

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gacha - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

9

lib/roguelike.js
var gaussian = require('gaussian');
module.exports = function(items) {
var default_filter = function() {
return true;
};
module.exports = function(items, filter) {
var item_ids = Object.keys(items);
var result = {};
filter = filter || default_filter;

@@ -26,3 +31,3 @@ for (var i = 0; i < item_ids.length; i++) {

for (var level = floor; level <= ceil; level++) {
if (level < 0) { continue; }
if (level < 0 || !filter(item)) { continue; }

@@ -29,0 +34,0 @@ var score = distribution.pdf(level) * item.weight;

{
"name": "gacha",
"version": "0.1.0",
"version": "0.2.0",
"description": "Roguelike item drop distribution calculator",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -29,5 +29,7 @@ # Node Gacha

// items must be an object where each property contains level, weight, and spread.
var items = {
"101": {
"name": "Knife",
"type": "weapon",
"level": 1,

@@ -39,2 +41,3 @@ "weight": 1.0,

"name": "Sword",
"type": "weapon",
"level": 2,

@@ -45,26 +48,41 @@ "weight": 1.0,

"103": {
"name": "Rapier",
"name": "Iron Buckler",
"type": "shield",
"level": 3,
"weight": 3.0,
"spread": 3
},
"104": {
"name": "Potion",
"type": "heal",
"level": 3,
"weight": 1.0,
"spread": 2
}
};
var result = gacha.roguelike(items);
var equip = gacha.roguelike(items, function(item) {
// Optional filter callback, defaults to `return true;`
return item.type !== 'heal';
});
console.log(result); // Shown Below
console.log(equip); // Shown below
// Which item should we spawn on level 3?
var random = Math.random() * result[3].total;
var lvl3 = equip[3];
var strata = Math.random() * lvl3.total;
var item = null;
for (var i = 0; i < result[3].strata.length; i++) {
if (random < result[3].strata[i]) {
var id = result[3].lookup[i];
console.log(items[id]);
for (var i = 0; i < lvl3.strata.length; i++) {
if (strata <= lvl3.strata[i]) {
var id = lvl3.lookup[i];
item = items[id];
break;
}
}
console.log("Receiving item for level three:", item);
```
### Result
### Resulting `equip` object

@@ -71,0 +89,0 @@ ```json

{
"101": {
"name": "Knife",
"type": "weapon",
"level": 1,

@@ -10,2 +11,3 @@ "weight": 1.0,

"name": "Sword",
"type": "weapon",
"level": 5,

@@ -17,2 +19,3 @@ "weight": 1.0,

"name": "Rapier",
"type": "weapon",
"level": 9,

@@ -25,2 +28,3 @@ "weight": 1.0,

"name": "Shield",
"type": "shield",
"level": 3,

@@ -32,2 +36,3 @@ "weight": 1.0,

"name": "Buckler",
"type": "shield",
"level": 7,

@@ -39,2 +44,3 @@ "weight": 1.0,

"name": "Epic Shield",
"type": "shield",
"level": 10,

@@ -47,2 +53,3 @@ "weight": 2.0,

"name": "Potion I",
"type": "heal",
"level": 1,

@@ -54,2 +61,3 @@ "weight": 4.0,

"name": "Potion II",
"type": "heal",
"level": 2,

@@ -61,2 +69,3 @@ "weight": 4.0,

"name": "Potion III",
"type": "heal",
"level": 3,

@@ -69,2 +78,3 @@ "weight": 4.0,

"name": "Magic Hammer",
"type": "weapon",
"level": 20,

@@ -71,0 +81,0 @@ "weight": 0.5,

@@ -105,2 +105,25 @@ var roguelike = require('../lib/roguelike.js');

});
describe('with filter', function() {
var result;
var acceptable = ['101', '102', '103', '500'];
before(function() {
result = roguelike(data_complex, function(item) {
return item.type === 'weapon';
});
});
it('only returns items where type=weapon', function() {
var levels = Object.keys(result);
for (var index = 0; index < levels.length; index++) {
var level = result[levels[index]];
for (var lookup = 0; lookup < level.lookup.length; lookup++) {
assert(acceptable.indexOf(level.lookup[lookup]) !== -1);
}
}
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc