Code Monkey home page Code Monkey logo

fortuna's Introduction

Fortuna

The quick solution for everything random!

A Gacha-like system to roll random items with weights.

Usage

More weight = more common. Think of it as in terms of probability.

// Deno
import { GachaMachine } from "https://deno.land/x/fortuna/mod.ts"
// JSR
import { GachaMachine } from "jsr:@nekooftheabyss/[email protected]"
// Node
import { GachaMachine } from "@nekooftheabyss/fortuna"
// esm.sh
import { GachaMachine } from "https://esm.sh/jsr/@nekooftheabyss/[email protected]"

const items = [
    { result: "SSR cool character", chance: 1 },
    { result: "Kinda rare character", chance: 3 },
    { result: "Mob character #1", chance: 5 },
    { result: "Mob character #2", chance: 5 },
    { result: "Mob character #3", chance: 5 },
]

const machine = new GachaMachine(items)

machine.get(10) // Rolls 10x

/*
    My result:
    [
      "Kinda rare character",
      "Mob character #1",
      "Mob character #3",
      "Mob character #3",
      "Mob character #1",
      "Kinda rare character",
      "Mob character #2",
      "Mob character #2" ,
      "Mob character #1",
      "Mob character #2"
    ]
*/

Rolling distinct elements

You can roll distinct elements using the LimitedGachaMachine. It is slower than GachaMachine but there shouldn't be much noticeable difference.

The "distinct" behavior only applies to within a single .get() call. It doesn't affect the pool of items used when initializing new LimitedGachaMachine().

import { LimitedGachaMachine } from "https://deno.land/x/fortuna/mod.ts"
// JSR
import { LimitedGachaMachine } from "jsr:@nekooftheabyss/[email protected]"

const items = [
    { result: "SSR cool character", chance: 1 },
    { result: "Kinda rare character", chance: 3 },
    { result: "Mob character #1", chance: 5 },
    { result: "Mob character #2", chance: 5 },
    { result: "Mob character #3", chance: 5 },
]

const machine = new LimitedGachaMachine(items)

machine.get(4) // Rolls 4x

/*
    My result:
    [
      "Kinda rare character",
      "Mob character #1",
      "Mob character #3",
      "Mob character #2",
    ]
*/

Plain weighted random selection

You probably don't need all complicated stuff. Here's a quick way to just create a simple weight-based gacha system: (Only works on v3.0.1 and above)

import { roll } from "jsr:@nekooftheabyss/[email protected]/roll"; // wherever you are importing from.

const items = [
    { result: "SSR cool character", chance: 1 },
    { result: "Kinda rare character", chance: 3 },
    { result: "Mob character #1", chance: 5 },
    { result: "Mob character #2", chance: 5 },
    { result: "Mob character #3", chance: 5 },
];

roll(items); // Rolls one item from the list of items using linear search.

You can also provide two individual arrays for choices and weights.

import { roll } from "jsr:@nekooftheabyss/[email protected]/roll"; // wherever you are importing from.

const items = [
    "SSR cool character",
    "Kinda rare character",
    "Mob character #1",
    "Mob character #2",
    "Mob character #3",
];
const chances = [1, 3, 5, 5, 5];

roll(items, chances); // Rolls one item from the list of items using linear search.

Providing the total chance may result in faster rolls.

import { roll } from "jsr:@nekooftheabyss/[email protected]/roll"; // wherever you are importing from.

const items = [
    { result: "SSR cool character", chance: 1 },
    { result: "Kinda rare character", chance: 3 },
    { result: "Mob character #1", chance: 5 },
    { result: "Mob character #2", chance: 5 },
    { result: "Mob character #3", chance: 5 },
];

roll(items, 19); // Rolls one item from the list of items using linear search.
import { roll } from "jsr:@nekooftheabyss/[email protected]/roll"; // wherever you are importing from.

const items = [
    "SSR cool character",
    "Kinda rare character",
    "Mob character #1",
    "Mob character #2",
    "Mob character #3",
];
const chances = [1, 3, 5, 5, 5];

roll(items, chances, 19); // Rolls one item from the list of items using linear search.

Documentation

Documentation for the latest version can be found in jsr:@nekooftheabyss/fortuna

fortuna's People

Contributors

retraigo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

fortuna's Issues

[TODO]: v.4.0.0

  • Remove history.
  • Add tiers back to support featuring items.
  • Write better examples in README.md.
  • Remove extra features (features already provided by Machine) from LimitedMachine.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.