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

piwpew-bot

v5.1.1

Published

Library to build bots for PiwPew

Downloads

21

Readme

About

Framework to build bots for PiwPew

Installation

npm install piwpew-bot

Usage

Use the piwpew-bot tool to run a bot writen with this framework. It works for bots written both in JavaScript or TypeScript.

$ piwpew-bot -h
Options:
  --help        Show help                                              [boolean]
  --version     Show version number                                    [boolean]
  -i, --id      Bot id                                       [string] [required]
  -m, --module  Module implementing the BotAPI               [string] [required]
  -r, --replay  Log file to be replayed                                 [string]
  -s, --server  Address of the game engine
                                     [string] [default: "wss://game.piwpew.com"]

For example:

npx piwpew-bot -i my-amazing-bot -m ./handler.ts

The framework will write a log file with all the messages sent and received by the bot. The log file will be named <bot-id>-messages.log.

About the game

Piwpew is a game where bots, coded by you and others, fight against each other. The game dynamics are quite simple:

  1. Your bot registers in the game. This is automatically done for you by the framework.
  2. The game engine notifies the bot when the game starts.
  3. Your bot sends requests to the game engine. Requests can be: move forward, move backward, shoot, etc.
  4. The game engine takes all the requests received from the different players in one tick, evaluates the requests with the current game state and sends the appropiate responses and notifications.
  5. Your bot receives responses and notifications and decides what to do next.

Steps 1 and 2 happen only once per game. 3, 4 and 5 repeat until the game ends or your bot is destroyed.

Game protocol

Following is a summary of the game protocol, check out the piwpew-docs for a reference documentation about requests and notifications.

Bots interact with the game engine by sending requests. The game engine validates the requests, executes them and updates its internal state. If the request was successful the response includes data that describes how the bot state changed as a consequence of it. Responses are sent at the end of each game tick.

Bots can only have one in-flight request per game tick. That is, they shouldn't send a request before receiving the notification that signals a new tick, as later requests will overwrite previous ones.

The game engine sends notifications to bots in response to game events that happen asynchronously to requests.

Writing a bot

To write a bot all you have to do is implement the parts you want from the following interface and export it with the name bot in the module that is passed to piwpew-bot. The methods in the interface map to the request's responses and notifications sent by the game engine. You can find example implementations in the examples/ folder.

interface BotAPI<S> {
  initState?: () => S

  handlers: {
    radarScanNotification?: (
      data: RadarScanNotification,
      state: S
    ) => { state: S }

    registerPlayerResponse?: (
      data: SuccessfulRegisterPlayerResponse | FailedRegisterPlayerResponse,
      state: S
    ) => { state: S }

    rotatePlayerResponse?: (
      data: SuccessfulRotatePlayerResponse | FailedRotatePlayerResponse,
      state: S
    ) => { state: S }

    movePlayerResponse?: (
      data: SuccessfulMovePlayerResponse | FailedMovePlayerResponse,
      state: S
    ) => { state: S }

    shootResponse?: (
      data: SuccessfulShootResponse | FailedShootResponse,
      state: S
    ) => { state: S }

    deployMineResponse?: (
      data: SuccessfulDeployMineResponse | FailedDeployMineResponse,
      state: S
    ) => { state: S }

    hitNotification?: (
      data: PlayerHitNotification,
      state: S
    ) => { state: S }

    tickNotification?: (
      state: S,
      context: { inFlightRequest?: Request }
    ) => { state: S, request?: Request }

    startGameNotification?: (
      state: S
    ) => { state: S }

    joinGameNotification?: (
      data: JoinGameNotification,
      state: S
    ) => { state: S }
  }
}

For a list of all the types mentioned in this interface check: types and requests. The npm package exports the required type definitions to build a bot in TypeScript.

Notice that you can only return a request to be sent to the server from the tickNotification handler.

Helpers

The library includes some helpers that can facilitate writing a bot. These helpers are:

declare function radiansToDegrees (radians: number): number

radiansToDegrees converts a angle in radians to its value in degrees

declare function calculateAngleBetweenPoints (pointA: Position, pointB: Position): number

calculateAngleBetweenPoints calculates the angle between two points. The returned value is an angle in degrees.

declare function calculateDistanceBetweenTwoPoints (pointA: Position, pointB: Position): number

calculateDistanceBetweenTwoPoints return the distance between two points in the arena.

The helpers can be imported directly from the library. For example:

import { calculateAngleBetweenPoints } from 'piwpew-bot'

Bot registration

You don't have to take care of registering the bot in the game, the library will take care of doing that for you.

Bot state

Each handler method takes as an argument the bot state that you can use to keep information between handler invocations. Each handler method must return the new state that will be passed in as an argument to the next handler invocation. You can init the state before receiving any message from the game engine by implementing the initState method. The default bot state is an empty object {}.

Logs playback

If you want to you can replay the logs by running bin/bot with the -r flag. When replaying the logs, you can pause the playback (for example because you want to set a breakpoint) by adding a line with the text [break] in the log file being replayed with -r.

License

MIT