# ducanh271/werewolfbackend architecture

> Architecture diagram of the GitHub repository ducanh271/werewolfbackend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ducanh271/werewolfbackend
- Repository: https://github.com/ducanh271/werewolfbackend

## Overview

Masoi is a phone-mediated Werewolf game server. A host creates a room over HTTP; players join through WebSockets, and each room processes game commands into events and state updates before broadcasting messages. The engine contains the game rules and role behavior, while room management supplies timers, identity, and event storage. The tree indicates distinct day, night, election, and endgame rules. No README was provided, and several engine and protocol files were not sampled, so those internal links are shown only where the excerpts support them.

Main entry points: [`internal/room/room.go`](https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/room.go) (Room actor), [`internal/engine/decide.go`](https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/decide.go) (Command decisions), [`internal/transport/ws/server.go`](https://github.com/ducanh271/werewolfbackend/blob/main/internal/transport/ws/server.go) (HTTP endpoints), [`internal/transport/ws/session.go`](https://github.com/ducanh271/werewolfbackend/blob/main/internal/transport/ws/session.go) (WebSocket session)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Server entry"]
  node_servermain["Server startup<br/>[main.go]"]
end

subgraph group_transport["WebSocket transport"]
  node_httpapi["HTTP endpoints<br/>[server.go]"]
  node_wsession["WebSocket session<br/>[session.go]"]
  node_protocol["Message handling<br/>[commands.go]"]
end

subgraph group_rooms["Room runtime"]
  node_manager["Room manager<br/>[manager.go]"]
  node_room["Room actor<br/>[room.go]"]
  node_roomjoin["Join and identity<br/>[join.go]"]
  node_roombroadcast["Room broadcasts<br/>[broadcast.go]"]
  node_roomtimers["Room timers<br/>[timers.go]"]
  node_eventstore[("Event log<br/>[store.go]")]
end

subgraph group_game["Game engine"]
  node_enginecommand["Command decisions<br/>[decide.go]"]
  node_gamestate["Game state<br/>[state.go]"]
  node_dayrules["Day phases<br/>[day_trial.go]"]
  node_nightrules["Night phases<br/>[night.go]"]
  node_roles["Role behavior<br/>[visitor.go]"]
  node_views["Player views<br/>[view_project.go]"]
  node_localization["Game messages<br/>[keys.go]"]
end

node_host(("Host / player"))

node_host -->|"creates room"| node_httpapi
node_host -->|"sends commands"| node_wsession
node_servermain -->|"creates"| node_manager
node_servermain -->|"configures"| node_httpapi
node_httpapi -->|"creates or finds"| node_manager
node_httpapi -->|"starts session"| node_wsession
node_manager -->|"creates"| node_room
node_wsession -.->|"handles messages"| node_protocol
node_wsession -->|"receives deliveries"| node_roombroadcast
node_room -->|"decides commands"| node_enginecommand
node_room -->|"folds events"| node_gamestate
node_room -->|"appends events"| node_eventstore
node_room -.->|"delegates joining"| node_roomjoin
node_room -.->|"notifies clients"| node_roombroadcast
node_room -.->|"uses timers"| node_roomtimers
node_enginecommand -.->|"routes day actions"| node_dayrules
node_enginecommand -.->|"routes night actions"| node_nightrules
node_enginecommand -.->|"dispatches role actions"| node_roles
node_gamestate -.->|"projects state"| node_views
node_views -.->|"uses message keys"| node_localization

click node_servermain "https://github.com/ducanh271/werewolfbackend/blob/main/cmd/server/main.go"
click node_httpapi "https://github.com/ducanh271/werewolfbackend/blob/main/internal/transport/ws/server.go"
click node_wsession "https://github.com/ducanh271/werewolfbackend/blob/main/internal/transport/ws/session.go"
click node_protocol "https://github.com/ducanh271/werewolfbackend/blob/main/internal/transport/ws/commands.go"
click node_manager "https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/manager.go"
click node_room "https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/room.go"
click node_roomjoin "https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/join.go"
click node_roombroadcast "https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/broadcast.go"
click node_roomtimers "https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/timers.go"
click node_eventstore "https://github.com/ducanh271/werewolfbackend/blob/main/internal/store/store.go"
click node_enginecommand "https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/decide.go"
click node_gamestate "https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/state.go"
click node_dayrules "https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/day_trial.go"
click node_nightrules "https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/night.go"
click node_roles "https://github.com/ducanh271/werewolfbackend/blob/main/internal/roles/visitor.go"
click node_views "https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/view_project.go"
click node_localization "https://github.com/ducanh271/werewolfbackend/blob/main/internal/i18n/keys.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_servermain toneBlue
class node_httpapi,node_wsession,node_protocol toneAmber
class node_manager,node_room,node_roomjoin,node_roombroadcast,node_roomtimers,node_eventstore toneMint
class node_enginecommand,node_gamestate,node_dayrules,node_nightrules,node_roles,node_views,node_localization toneRose
class node_host toneIndigo
```

## Components

### Server entry

- [Server startup](https://github.com/ducanh271/werewolfbackend/blob/main/cmd/server/main.go): `cmd/server/main.go`

### WebSocket transport

- [HTTP endpoints](https://github.com/ducanh271/werewolfbackend/blob/main/internal/transport/ws/server.go): `internal/transport/ws/server.go`
- [WebSocket session](https://github.com/ducanh271/werewolfbackend/blob/main/internal/transport/ws/session.go): `internal/transport/ws/session.go`
- [Message handling](https://github.com/ducanh271/werewolfbackend/blob/main/internal/transport/ws/commands.go): `internal/transport/ws/commands.go`

### Room runtime

- [Room manager](https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/manager.go): `internal/room/manager.go`
- [Room actor](https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/room.go): `internal/room/room.go`
- [Join and identity](https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/join.go): `internal/room/join.go`
- [Room broadcasts](https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/broadcast.go): `internal/room/broadcast.go`
- [Room timers](https://github.com/ducanh271/werewolfbackend/blob/main/internal/room/timers.go): `internal/room/timers.go`
- [Event log](https://github.com/ducanh271/werewolfbackend/blob/main/internal/store/store.go): `internal/store/store.go`

### Game engine

- [Command decisions](https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/decide.go): `internal/engine/decide.go`
- [Game state](https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/state.go): `internal/engine/state.go`
- [Day phases](https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/day_trial.go): `internal/engine/day_trial.go`
- [Night phases](https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/night.go): `internal/engine/night.go`
- [Role behavior](https://github.com/ducanh271/werewolfbackend/blob/main/internal/roles/visitor.go): `internal/roles/visitor.go`
- [Player views](https://github.com/ducanh271/werewolfbackend/blob/main/internal/engine/view_project.go): `internal/engine/view_project.go`
- [Game messages](https://github.com/ducanh271/werewolfbackend/blob/main/internal/i18n/keys.go): `internal/i18n/keys.go`

### Other components

- **Host / player**

## Connections

- Host / player → HTTP endpoints: creates room (evidence: `internal/transport/ws/server.go`)
- Host / player → WebSocket session: sends commands
- Server startup → Room manager: creates (evidence: `cmd/server/main.go`)
- Server startup → HTTP endpoints: configures (evidence: `cmd/server/main.go`)
- HTTP endpoints → Room manager: creates or finds (evidence: `internal/transport/ws/server.go`)
- HTTP endpoints → WebSocket session: starts session (evidence: `internal/transport/ws/server.go`)
- Room manager → Room actor: creates (evidence: `internal/room/manager.go`)
- WebSocket session → Message handling: handles messages
- WebSocket session → Room broadcasts: receives deliveries (evidence: `internal/transport/ws/session.go`)
- Room actor → Command decisions: decides commands (evidence: `internal/room/room.go`)
- Room actor → Game state: folds events (evidence: `internal/room/room.go`)
- Room actor → Event log: appends events (evidence: `internal/room/room.go`)
- Room actor → Join and identity: delegates joining
- Room actor → Room broadcasts: notifies clients
- Room actor → Room timers: uses timers
- Command decisions → Day phases: routes day actions
- Command decisions → Night phases: routes night actions
- Command decisions → Role behavior: dispatches role actions
- Game state → Player views: projects state
- Player views → Game messages: uses message keys
