npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

neuro-game-sdk

v1.0.4

Published

A TypeScript/JavaScript SDK for integrating games with Neuro-sama AI streamer.

Downloads

316

Readme

Neuro Game SDK for TypeScript and JavaScript

A TypeScript and JavaScript SDK for integrating games with Neuro-sama AI streamer, allowing developers to write games that Neuro-sama can interact.

This SDK is based on the original Neuro Game SDK and provides an implementation compatible with both Node.js and browser environments. It is designed to work seamlessly in both JavaScript and TypeScript projects.

Installation

Install the SDK via npm:

npm install neuro-game-sdk

Usage

In Node.js

import { NeuroClient } from 'neuro-game-sdk'

const NEURO_SERVER_URL = 'ws://localhost:8000'
const GAME_NAME = 'Guess the Number'

const neuroClient = new NeuroClient(NEURO_SERVER_URL, GAME_NAME, () => {
  // Game initialization code. Check the example code
})

In the Browser

Using unpkg:

<script src="https://unpkg.com/neuro-game-sdk/dist/browser/neuro-game-sdk.min.js"></script>

Using jsDelivr:

<script src="https://cdn.jsdelivr.net/npm/neuro-game-sdk/dist/browser/neuro-game-sdk.min.js"></script>

This will load the SDK into the global namespace as NeuroGameSdk. You can then use it in your scripts:

<script>
  const { NeuroClient } = NeuroGameSdk

  const NEURO_SERVER_URL = 'ws://localhost:8000'
  const GAME_NAME = 'Guess the Number'

  const neuroClient = new NeuroClient(NEURO_SERVER_URL, GAME_NAME, () => {
    // Game initialization code. Check the example code
  })
</script>

Quick Start Example

Here's an example of a simple game where Neuro-sama tries to guess a number between 1 and 10. When she guesses correctly, a new number is generated.

import { NeuroClient } from 'neuro-game-sdk'

const NEURO_SERVER_URL = 'ws://localhost:8000'
const GAME_NAME = 'Guess the Number'

const neuroClient = new NeuroClient(NEURO_SERVER_URL, GAME_NAME, () => {
  neuroClient.registerActions([
    {
      name: 'guess_number',
      description: 'Guess the number between 1 and 10.',
      schema: {
        type: 'object',
        properties: {
          number: { type: 'integer', minimum: 1, maximum: 10 },
        },
        required: ['number'],
      },
    },
  ])

  let targetNumber = Math.floor(Math.random() * 10) + 1

  neuroClient.onAction(actionData => {
    if (actionData.name === 'guess_number') {
      const guessedNumber = actionData.params.number
      if (
        typeof guessedNumber !== 'number' ||
        guessedNumber < 1 ||
        guessedNumber > 10
      ) {
        neuroClient.sendActionResult(
          actionData.id,
          false,
          'Invalid number. Please guess a number between 1 and 10.'
        )
        return
      }

      if (guessedNumber === targetNumber) {
        neuroClient.sendActionResult(
          actionData.id,
          true,
          `Correct! The number was ${targetNumber}. Generating a new number.`
        )
        targetNumber = Math.floor(Math.random() * 10) + 1
        promptNeuroAction()
      } else {
        neuroClient.sendActionResult(
          actionData.id,
          true,
          `Incorrect. The number is ${
            guessedNumber < targetNumber ? 'higher' : 'lower'
          }. Try again.`
        )
        promptNeuroAction()
      }
    } else {
      neuroClient.sendActionResult(actionData.id, false, 'Unknown action.')
    }
  })

  neuroClient.sendContext(
    'Game started. I have picked a number between 1 and 10.',
    false
  )

  function promptNeuroAction() {
    const availableActions = ['guess_number']
    const query = 'Please guess a number between 1 and 10.'
    const state = 'Waiting for your guess.'
    neuroClient.forceActions(query, availableActions, state)
  }

  promptNeuroAction()
})

Happy coding! <3 - ArieX