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

@uwu-games/uwu-state-machine

v0.0.3

Published

Game tick system

Downloads

3

Readme

State Machine: A Deterministic State Management System

State Machine is a lightweight, deterministic state management system designed for multiplayer games. It provides a robust framework for handling states, actions, and time-based computations.

Features

  • Deterministic state management
  • Action logging and replay
  • Time-based tick system
  • Efficient state caching
  • Support for custom game logic
  • Ability to rollback and recalculate states

Installation

To install and run the project:

  1. Clone the repository
  2. Run npm install to install dependencies
  3. Run npm run build to build the project

Types

  • Time: Represents time as a 48-bit number
  • Tick: Represents a game tick as a 48-bit number
  • StateLogs<S>: A record of game states indexed by ticks
  • ActionLogs<A>: A record of actions indexed by ticks
  • Mach<S, A>: The main state management object
  • Game<S, A>: Defines the game logic (init, when, tick functions)

Main Functions

  • new_mach<S, A>: Creates a new Mach instance with a specific TPS (ticks per second).
  • time_to_tick<S, A>: Converts time in milliseconds to ticks.
  • register_action<S, A>: Registers an action at the corresponding tick. Can be used for local player actions or to synchronize actions from other players in a multiplayer game.
  • compute<S, A>: Updates the game state based on ticks and registered actions up to a specific point in time.

General Explanation

  • Game: Interface that defines how the game behaves.
    • init: Initializes the game state.
    • when: Defines how the game state changes with an action.
    • tick: Defines how the game state is updated each tick.
  • The init, when, and tick functions of the game must be pure to ensure determinism.
  • The system supports state rollback, allowing recalculation from a previous point if necessary.
  • Ideal for games that require precise synchronization between multiple players.

This system is particularly useful for multiplayer games where precise synchronization and the ability to replay previous states are crucial.

Usage

import { new_mach, register_action, compute, Mach, Game, Time } from 'state-machine';

// Definition of State and Action types
type State = { /* ... */ };
type Action = 
  | { $: "SetNick", time: Time, pid: UID, name: string }
  | { $: "KeyEvent", time: Time, pid: UID, key: Key, down: boolean };

// Implementation of game logic
function init(): State {
   /* initial state */ 
  };
  
function when(action: Action, state: State): State { 
    // Logic to handle actions
    switch (action.$) {
      case "SetNick":
        // Update player nickname
        break;
      case "KeyEvent":
        // Handle keyboard events
        break;
    }
    return state;
  };

function tick(state: State): State {
    // Update game state each tick
    return state;
  };

// Create a new Game instance 
const game: Game<State, Action> = { init, tick, when };
// Create a new Mach instance
const mach: Mach<State, Action> = new_mach(60); // 60 ticks per second

// Register actions
register_action(mach, { $: "SetNick", time: 1000, pid: "player1", name: "Alice" });
register_action(mach, { $: "KeyEvent", time: 1500, pid: "player1", key: "ArrowUp", down: true });

// Compute game state at a specific time
const state = compute(mach, game, 2000);

This system is particularly useful for multiplayer games where precise synchronization and the ability to replay previous states are crucial.