# @ancademy/bespoke-server

> **@ancademy/elf-xxx** is a set of kit for multiplayer web game development based on NodeJS and TypeScript. It allows you to focus on your scene rendering and gameplay instead of bothering about networking or database.

Latest version **2.9.27** (published 2024-01-16) · 0 weekly downloads

## Install

```sh
npm install @ancademy/bespoke-server
pnpm add @ancademy/bespoke-server
yarn add @ancademy/bespoke-server
bun add @ancademy/bespoke-server
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.9.27 |
| Published | 2024-01-16 |
| First published | 2020-06-16 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 71.2 KB |
| Known vulnerabilities | 0 (+4 in 2 direct dependencies) |
| Install scripts | no |
| Maintainers | guokan |

## Links

- npm: https://www.npmjs.com/package/@ancademy/bespoke-server
- npm.io page: https://npm.io/package/@ancademy/bespoke-server

## Dependencies (7)

- [queue](https://npm.io/package/queue.md) 6.0.2
- [lodash](https://npm.io/package/lodash.md) 4.17.21
- [socket.io](https://npm.io/package/socket.io.md) 4.5.1
- [async-lock](https://npm.io/package/async-lock.md) 1.3.2
- [@msgpack/msgpack](https://npm.io/package/@msgpack/msgpack.md) 2.7.2
- [@ancademy/util-server](https://npm.io/package/@ancademy/util-server.md) 2.9.27
- [@ancademy/bespoke-share](https://npm.io/package/@ancademy/bespoke-share.md) 2.9.20

## Recent versions

- 2.9.27 (latest) — 2024-01-16
- 2.9.24 — 2023-04-25
- 2.9.23 — 2023-04-13
- 2.9.22 — 2023-04-13
- 2.9.21 — 2023-03-16
- 2.9.20 — 2023-03-13
- 2.9.19 — 2023-02-16
- 2.9.17 — 2023-01-30
- 2.9.10 — 2022-11-22
- 2.9.5 — 2022-09-07
- 2.8.4-patch1.1 — 2022-09-06
- 2.8.4-patch1 — 2022-09-06
- 2.9.3 — 2022-08-30
- 2.9.2 — 2022-08-30
- 2.9.0 — 2022-08-22
- … 110 more at https://npm.io/package/@ancademy/bespoke-server/versions

## README

# About
**@ancademy/elf-xxx** is a set of kit for multiplayer web game development based on NodeJS and TypeScript. It allows you to focus on your scene rendering and gameplay instead of bothering about networking or database.

## Features
- **Multiplayer**: Game state is kept synchronized in realtime
- **TypeSafe**: Declare player actions and states with strict generics
- **Logs**: Game logs with the ability to time travel
- **Non-intrusive**: Use React, PixiJS or anything other for view-layer rendering
- **Availability**: Resume the game state after restarting
- **Extendable**: Build regular http service with express

## Prepare
This project runs on [NodeJS v10](https://nodejs.org) and uses [Mongodb](https://www.mongodb.com) and [Redis](https://redis.io) as the persistence layer, so we need to install them first.

## Tutorial
This tutorial walks through the demo Tic-Tac-Toe, we use React as the renderer in this demo.

### Init
Init project according to the guide of **[bespoke-cli](../bespoke-cli/README.md)**

### Concepts
- **namespace** : The game will be mounted at the route : `/namespace`
- **ICreateParams** : We can configure some parameters when creating a game, but in this demo it could be ignored.
- **State**: The state of the world is divided into two parts, **GameState** is visible to everyone, while each player has his own **PlayerState**, they are passed around everywhere and maintained on both client and server seamlessly.
- **Move** : **MoveType** and **IMoveParams** declare what actions will the players make and what parameters would be sent to the server.
- **Push** : **PushType** and **IPushParams** declare what messages would be sent to players, which is independent of the state, and they won't be persisted.

### Define State
Players are divided into groups of 2, we record number of players and who is the turn in **IGameGroupState**, the piece player held in **IPlayerState**

```ts
export enum Piece {
    null,
    x,
    o,
}

export enum GameStatus {
    matching,
    play,
    result,
}

export interface IGameGroupState {
    playerNum: number
    status: GameStatus
    pieces: Array<Piece>
    pieceTurn: Piece
}

export interface IGameState {
    groups: Array<IGameGroupState>
}

export interface IPlayerState {
    groupIndex: number
    piece: Piece
}
```

### Define Move

```ts
export enum MoveType {
    getGroup,
    move
}
export interface IMoveParams {
    index: number // piece index
}
```

### Client
In `Play.tsx`, as a player we can read `gameState` and `playerState`, and emit moves by `frameEmitter`, what you're doing next is to render your chessboard with React.

### Server
In `Logic.ts`, we can init `gameState` and `playerState`, and then handle the player moves, just modify the state objects, they would be sync to client automatically.

### Demo
You can find the complete code [here](../../example/TicTacToe)

---
_Source: https://npm.io/package/@ancademy/bespoke-server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
