0.4.0 • Published 3 years ago

go-dice-api v0.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

GoDice JavaScript API Wrapper

This package is a work in progress. Trying to wrap the API to support TypeScript and a nicer API interface.

Due to licensing reasons... this package doesn't actually contain the API, and must be downloaded from the official repo: https://github.com/ParticulaCode/GoDiceJavaScriptAPI

Overview:

This supports the following features of the GoDice API:

  • Connecting to dice
  • Reading the value of the dice
  • Setting LED colors
  • Getting the battery level
  • Getting the colour of the die
  • React hooks (via go-dice-react)

Future features:

  • Hex support for LEDs
  • Self implemented API
  • RPG Dice Shell support

Support

Requires the Bluetooth Browser APIs, which are only available in Chrome and Chromium based browsers at time of writing.

Check MDN's browser support page for up-to-date info

Install

# install from NPM
npm install go-dice-api

# or with yarn if that's your cup-o-tea:
yarn add go-dice-api

Add the Official API to the page

<head>
    <script src="/path/to/godice.js"></script>

    <!-- Export the API so the module file can find it -->
    <script>window.GoDice = GoDice</script>

    <script src="/path/to/bundle.js"></script>
</head>

Usage

import { diceSet, Die, LED_OFF } from 'go-dice-api'

// Ask the user to connect a die
diceSet.requestDie()

diceSet.on('connected', (die: Die) => {
  // a die was connected!

  // Set the colour of the leds
  die.setLed([0, 0, 255]) // [Red, Green, Blue]
  die.setLed([0, 0, 255], [255, 0, 0]) // Set the two lights separately 
  die.setLed(LED_OFF)

  // Listen for the user to start rolling the dice
  die.on('rollStart', () => console.log("Rolling..."))

  // And then get the value they rolled
  die.on('value', (value: number) => console.log(`You rolled a ${value}`))

  // Actively request the battery level
  die.getBatteryLevel().then((level: number) => console.log(level))

  // Passively listen for the battery level
  die.on('batteryLevel', (level: number) => console.log(`Battery: ${level}%`)) // level: 0-100

  // Request the colour of the Die
  die.getColour().then((color: string) => console.log(color))
})
0.4.0

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago