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

2048-webcomponent

v1.1.0

Published

2048 as a WebComponent.

Downloads

22

Readme

2048-webcomponent

CI CodeQL License NPM version

This is 2048 as a WebComponent. Once you integrate it as described further, the game just follows the standard rules. To make a move, just use the arrow keys or swipe.

Getting Started

Install the package via npm or yarn and deliver the script to the user. This can be done via import, require or just inserting a script tag.

npm i 2048-webcomponent
yarn add 2048-webcomponent

Attributes

| Param | Type | Description | Default | | -------------------------- | -------- | -------------------------------------------------------------------------------- | ------- | | columns | number | Amount of columns of the board. | 4 | | rows | number | Amount of rows of the board. | 4 | | swipe-distance-threshold | number | Amount of pixels required to trigger a swipe. | 50 | | restart-selector | string | If present, attaches a click event listener to the element to trigger a restart. | |

Events

By default, each event contains the current game state in event.detail.game. To discourage cheating, no events should be logged in the browser console. | Name | Type | Cancelable | Description | detail | | ---------------- | --------------- | ------------------ | ----------------------- | ------------------------------------------------------------------------------------------------------------ | | 2048:game-won | GameWonEvent | :x: | User just won the game | positions: Positions current game state | | 2048:game-lost | GameLostEvent | :x: | User just lost the game | positions: Positions current game state | | 2048:move | MoveEvent | :white_check_mark: | User made a move | positions: Positions current game statedirection: Direction direction of the move | | 2048:score | ScoreEvent | :x: | Score has been updated | positions: Positions current game stateoldScore: number \| undefined old scoredelta: number \| undefined difference between old and new scorenewScore: number current score |

Usage

Basic usage

Just a basic 9x9 / 10 Mines 2048 game. Further examples can be combined.

<game-2048></game-2048>

<script type="module">
  import '2048-webcomponent';
  // Alternatives:
  // import '2048-webcomponent/custom-element';
  // import '2048-webcomponent/custom-element.min';
</script>

Custom tag name

Define your own custom tag name

<custom-game-2048></custom-game-2048>

<script type="module">
  import { Game } from '2048-webcomponent/game-2048';
  // Alternative:
  // import { Game } from '2048-webcomponent/game-2048.min';
  window.customElements.define('custom-game-2048', Game);
</script>

Provide a restart button

Provide a selector where a "click"-event will be attached to, to restart the game.

<game-2048 restart-selector="#restart-game-button"></game-2048>
<button id="restart-game-button">Restart!</button>

<script type="module">
  import '2048-webcomponent';
</script>

Trigger Restart via JavaScript

Write custom logic to restart the game.

<game-2048 id="game"></game-2048>
<button id="restart-game-button-confirm">Restart with Confirm!</button>

<script type="module">
  import '2048-webcomponent';

  window.addEventListener('DOMContentLoaded', () => {
    const game = document.querySelector('#game');

    document.querySelector('#restart-game-button-confirm').addEventListener('click', (event) => {
      event.preventDefault();
      if (window.confirm('Are you sure, that you want to restart the game?')) {
        game.restartGame();
      }
    });
  });
</script>

Custom win/lose event listener

Attach an EventListener for the win/lose events.

<game-2048 id="game"></game-2048>

<script type="module">
  import '2048-webcomponent';

  window.addEventListener('DOMContentLoaded', () => {
    const game = document.querySelector('#game');

    game.addEventListener('game-won', () => {
      console.log('win');
    });

    game.addEventListener('game-lost', () => {
      console.log('lose');
    });
  });
</script>

Different initial game configurations

Of course you can provide different configurations for the game.

<game-2048 rows="5" columns="5"></game-2048>

<script type="module">
  import '2048-webcomponent';
</script>

Example

Try it out at CodePen.

Example Image

TypeScript

The whole package is written in TypeScript and therefore provides a strongly typed system via the core export of the package:

<!-- some-file.html -->
<game-2048 id="game"></game-2048>
// some-file.ts
import '2048-webcomponent/custom-element';
import { FieldInteractionEvent, FieldInteractionType } from '2048-webcomponent/core';
import type { Game } from '2048-webcomponent/core';

const game = document.querySelector('#game') as Game;
game.addEventListener('2048:field-interaction', (event: FieldInteractionEvent) => {
  const { interaction } = event.detail;
  switch (interaction.type) {
    case FieldInteractionType.Unveiled:
      console.log('Revealed field value:', interaction.value);
      break;
    case FieldInteractionType.FlagAction:
    case FieldInteractionType.QuestionMarkAction:
      console.log('Performed action:', interaction.action);
      break;
  }
});

Engine

The engine can be used as a standalone library (commonjs and esm) via the engine export of the package. This enables the usage of the engine in every application (server- or client-side).

import { Direction } from '2048-webcomponent/core';
import { Engine } from '2048-webcomponent/engine';

const engine = new Engine();

engine.createBoard(3, 3);

engine.move(Direction.Up);

if (engine.isGameOver) {
  // Restarts the game with the initial configuration
  engine.restart();
}
const { Engine } = require('2048-webcomponent/engine');

const engine = new Engine();

// ...

License

MIT License