An open API service indexing awesome lists of open source software.

https://github.com/burakjs/online-xox-nodejs


https://github.com/burakjs/online-xox-nodejs

nodejs socket-io tic-tac-toe typescript xox

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

          

# Online XOX Game - Backend Service ๐ŸŽฎ

[![Node.js](https://img.shields.io/badge/Node.js-18.x-339933?logo=node.js)](https://nodejs.org)
[![Express.js](https://img.shields.io/badge/Express.js-4.x-000000?logo=express)](https://expressjs.com)
[![Socket.IO](https://img.shields.io/badge/Socket.IO-4.x-010101?logo=socket.io)](https://socket.io)
[![TypeScript](https://img.shields.io/badge/TypeScript-5.x-3178C6?logo=typescript)](https://typescriptlang.org)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Real-time multiplayer Tic-tac-toe game server built with Node.js and Socket.IO. Designed to work seamlessly with the Flutter mobile client.

## โœจ Features

- ๐ŸŽฎ Real-time multiplayer gameplay
- ๐Ÿ”„ WebSocket-based communication
- ๐ŸŽฏ Game session management
- ๐Ÿ›ก๏ธ Move validation
- ๐Ÿ”Œ Reconnection support
- ๐Ÿงน Automatic cleanup

## ๐Ÿ› ๏ธ Tech Stack

- **Runtime:** Node.js
- **Framework:** Express.js
- **WebSocket:** Socket.IO
- **Language:** TypeScript
- **Logging:** Winston
- **Utils:** UUID

## ๐Ÿ—๏ธ Project Structure

```
backend/
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ config/ # Configuration
โ”‚ โ”œโ”€โ”€ models/ # Data models
โ”‚ โ”œโ”€โ”€ services/ # Business logic
โ”‚ โ”œโ”€โ”€ types/ # TypeScript types
โ”‚ โ”œโ”€โ”€ utils/ # Utilities
โ”‚ โ””โ”€โ”€ app.ts # App entry
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ tsconfig.json
```

## ๐Ÿš€ Getting Started

1. **Clone and Install**
```bash
npm install
```

2. **Environment Setup**
```bash
cp .env.example .env
```

3. **Development**
```bash
npm run dev
```

4. **Production**
```bash
npm run build
npm start
```

## ๐Ÿ“ก WebSocket Events

### Server to Client
```typescript
interface ServerToClientEvents {
game_update: (data: GameState) => void;
game_created: (data: { gameId: string }) => void;
game_joined: (data: GameState) => void;
game_over: (data: { winner?: string, gameState: GameState }) => void;
player_left: (data: { playerId: string }) => void;
error: (data: { message: string, code: string }) => void;
move_error: (data: { message: string }) => void;
}
```

### Client to Server
```typescript
interface ClientToServerEvents {
create_game: (data: { playerName: string }) => void;
join_game: (data: { gameId: string, playerName: string }) => void;
make_move: (data: PlayerMove) => void;
cancel_game: (data: { gameId: string }) => void;
leave_game: (data: { gameId: string }) => void;
disconnect_game: () => void;
}
```

## ๐ŸŽฏ Game Rules

1. Two players take turns (X and O)
2. First to get 3 in a row wins
3. Game ends on:
- Win condition met
- Board full (draw)
- Player disconnection
- Game cancellation

## ๐Ÿ“ฑ Mobile Support

- App lifecycle handling
- Efficient reconnection
- Network state management
- Background state handling

## ๐Ÿ”ง Available Scripts

```json
{
"dev": "ts-node-dev --respawn src/app.ts",
"build": "tsc",
"start": "node dist/app.js",
"lint": "eslint . --ext .ts",
"format": "prettier --write \"src/**/*.ts\""
}
```

## ๐Ÿงช Testing

To test with Flutter client:

1. Start the backend server
2. Configure client WebSocket URL
3. Test scenarios:
- Game creation/joining
- Move validation
- Disconnection handling
- Game completion

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.