# uno-game-engine

> This is a game engine for the card game UNO

Latest version **3.4.1** (published 2023-10-07) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install uno-game-engine
pnpm add uno-game-engine
yarn add uno-game-engine
bun add uno-game-engine
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.4.1 |
| Published | 2023-10-07 |
| First published | 2023-01-10 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 96.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | 1euro7cent |
| Maintainers | 1euro7cent |
| Keywords | uno, game, game-engine, card-game, card |

## Links

- npm: https://www.npmjs.com/package/uno-game-engine
- Repository: https://github.com/1Euro7Cent/uno-game-engine
- Homepage: https://github.com/1Euro7Cent/uno-game-engine#readme
- Issues: https://github.com/1Euro7Cent/uno-game-engine/issues
- npm.io page: https://npm.io/package/uno-game-engine

## Recent versions

- 3.4.1 (latest) — 2023-10-07
- 3.4.0 — 2023-09-06
- 3.3.0 — 2023-08-07
- 3.2.1 — 2023-04-28
- 3.2.0 — 2023-04-04
- 3.1.3 — 2023-03-29
- 3.1.2 — 2023-01-22
- 3.1.0 — 2023-01-10

## README

<div style="text-align: center; ">

<img alt="npm" src="https://img.shields.io/npm/dt/uno-game-engine">
<img alt="npm" src="https://img.shields.io/npm/dw/uno-game-engine">
<img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/1Euro7Cent/uno-game-engine">
<img alt="GitHub forks" src="https://img.shields.io/github/forks/1Euro7Cent/uno-game-engine">
<img alt="GitHub watchers" src="https://img.shields.io/github/watchers/1Euro7Cent/uno-game-engine">
<img alt="GitHub code size in bytes" src="https://img.shields.io/github/languages/code-size/1Euro7Cent/uno-game-engine">

</div>

# UNO Game Engine

## Installing

```
npm i uno-game-engine
```

## Basic Usage

```js

const { Game, events } = require('uno-game-engine')

let game = new Game(["player1", "player2", "player3", "player4"]) // this also accepts an actual player objects array. but you need to create it yourself

game.eventManager.addEvent(new events.PlayerChangeEvent((oldPlayer, newPlayer) => {
    // do something with it
    console.log(`It is now ${newPlayer.name}'s turn`)

}))

game.start()

// playing cards
let cardToPlay = game.currentPlayer.hand.getCard("RED", "FIVE") // assuming the current player has a red five in their hand

let success = game.play(game.currentPlayer, cardToPlay) // returns true if the card was played, false if it was not

//drawing cards. all functions below return true if they succeed

// draws one card for the current player
game.draw(game.currentPlayer)

// draws five cards for "player3"
game.draw(game.getPlayerByName("player3"), 5)
// draw three cards for player with id 2. (default starts at 0 and increments by 1)
game.draw(game.getPlayerById(2),3)
```

## Configs

```js
const { Game, Config } = require('uno-game-engine') 

let config = new Config()

config.setDefaultRotation("CW") // CCW (counter clock wise) or CW (clock wise)
    .setInitialCards(7) // how many cards each player starts with
    .setPlayersPerDeck(4) // how many players per deck of cards 
                          //(if more players are present, another deck (to draw from) will be created)

let game = new Game(["player1", "player2", "player3", "player4"], config)

// alternatively you can do this:
game = new Game(["player1", "player2", "..."], new Config().setInitialCards(7).setPlayersPerDeck(4))
```

## Events

```js
const { Game, events } = require('uno-game-engine')
let game = new Game(["Player 1", "Player 2"])

game.eventManager.addEvent(new events.PlayerPlayEvent((player, card) => {
    console.log(`${player} played ${card}`)
}))

game.eventManager.addEvent(new events.PlayerChangeEvent((oldPlayer, newPlayer) => {
    console.log(`It is now ${newPlayer}'s turn`)
}))

game.eventManager.addEvent(new events.PlayerDrawEvent((player, cards) => {
    console.log(`${player} drew ${cards.length} cards`)
}))

// manually fire an event

game.eventManager.fireEvent(events.PlayerChangeEvent.fire(game.currentPlayer, game.getNextPlayer()))

// making own events

let Event = events.Event // sadly you need this
class MyEvent extends Event {
    /**
     * i recommend doing this. but is it NOT required
     * @param {(something: number, somethingElse: string) => any} callback 
     * @param {boolean} once 
     */
    constructor(callback, once = false) {
        super(callback, "myEvent", once)
    }

    /**
     * i also recommend doing this
     * @param {number} something 
     * @param {string} somethingElse 
     * @returns 
     */
    static fire(something, somethingElse) {
        return new events.FireEvent("myEvent", something, somethingElse)
    }
}

// using own events

game.eventManager.addEvent(new MyEvent((something, somethingElse) => {
    // do something with it
    console.log(`something: ${something}, somethingElse: ${somethingElse}`)
}))

// fireing the event
game.eventManager.fireEvent(MyEvent.fire(1, "hello"))
```

# More coming soon

---
_Source: https://npm.io/package/uno-game-engine · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
