cs2-gsi-z
Version:
A modular, event-driven Game State Integration (GSI) handler for Counter-Strike 2 built with Node.js.
257 lines (165 loc) β’ 7.13 kB
Markdown
# π― CS2-GSI-Z



> A modern, modular, and event-driven Game State Integration (GSI) handler for **Counter-Strike 2**, built with Node.js.
## π Project Overview
`cs2-gsi-z` is an advanced event processing library for Counter-Strike 2 Game State Integration (GSI) data.
It transforms raw GSI JSON updates into high-level, context-aware events, enabling developers to easily build real-time HUDs, dashboards, analytics systems, bots, or esports tools.
Unlike traditional GSI listeners that emit low-level or noisy data, `cs2-gsi-z` structures and simplifies information, significantly reducing client-side complexity.
## β¨ Why CS2-GSI-Z?
> βThere are other GSI tools... why use this one?β
| Feature | CS2-GSI-Z | Traditional GSI Handlers |
|----------------------------------|:---------:|:-----------------------:|
| Built for CS2 from scratch | β
| β (often CS:GO ports) |
| Modular differ-based architecture| β
| β (monolithic parsing) |
| Delta (`previously`/`added`) support| β
| β |
| Granular, high-level events | β
| β (raw dumps) |
| Dynamic differ extension | β
| β |
Whether you're building a companion HUD, tournament backend, stream overlay, or an esports analytics dashboard β **this is the toolkit designed for you**.
## π Key Features
- **π Structured, Context-Aware Events**
> **Problem Solved:** No need to manually parse complex GSI dumps.
> **Benefit:** Focus immediately on meaningful gameplay changes.
- **π§© Modular Differ System**
> **Problem Solved:** Avoids tangled monolithic update handlers.
> **Benefit:** Easier maintenance, faster extension, safer upgrades.
- **β¨ Delta-Aware Processing**
> **Problem Solved:** Deep-cloning and manual patching are error-prone.
> **Benefit:** Fast, reliable diff calculation with minimal memory footprint.
- **π Dynamic Diff Manager**
> **Problem Solved:** Hardcoding all diffs upfront limits flexibility.
> **Benefit:** Extend or replace behaviors easily at runtime.
- **π― Flexible Event Subscription**
> **Problem Solved:** Handling noisy events on the client.
> **Benefit:** Listen only to what matters, improving performance.
- **πͺ Optimized for Real-Time Systems**
> **Problem Solved:** Raw GSI processing can cause lags or instability.
> **Benefit:** Low-latency, high-frequency event handling.
## π Quick Start
### Prerequisites
To receive data from CS2, you must set up a Game State Integration (GSI) configuration file.
*Reference: [Valve Developer Wiki β Game State Integration](https://developer.valvesoftware.com/wiki/Counter-Strike:_Global_Offensive_Game_State_Integration)*
Or you can simply generate a valid GSI configuration file automatically using the included helper in this library:
```javascript
import { GSIConfigWriter } from 'cs2-gsi-z';
// Generate the config file on your desktop
const configPath = GSIConfigWriter.generate({
name: 'cs2-gsi-z',
uri: 'http://localhost:3000'
});
console.log(`Config file created at: ${configPath}`);
```
*Remember to move the generated **.cfg** file to your CS2 cfg directory*
### Installation
```bash
npm install cs2-gsi-z
```
*β οΈ Note: This package is not yet published on npm. You can install it directly from GitHub for now.*
### Basic Usage
```javascript
import { GsiService, EVENTS } from 'cs2-gsi-z';
const gsi = new GsiService({
httpPort: 3000
});
gsi.start();
gsi.on(EVENTS.player.weaponChanged, (payload) => {
console.log(`weapon changed:`, payload);
});
```
## π Event Model
When a new GSI payload is received, `cs2-gsi-z`:
1. π Applies delta changes efficiently.
2. π Detects relevant modifications via differs.
3. πͺ Emits structured, context-aware events.
Each emitted event includes:
```javascript
{
previous: <previous value>,
current: <current value>,
previouslyBlock: <raw previous GSI block>,
addedBlock: <raw new GSI block>
}
```
## π Supported Events
**Player:** `hpChanged`, `armorChanged`, `teamChanged`, `weaponChanged`, `activityChanged`, `helmetChanged`, `flashedChanged`, `smokedChanged`, `burningChanged`, `killsChanged`, `deathsChanged`, `assistsChanged`, `scoreChanged`
**Round:** `phaseChanged`, `started`, `ended`, `won`
**Map:** `nameChanged`, `phaseChanged`, `roundChanged`, `teamCTScoreChanged`, `teamTScoreChanged`
(Some of them are not yet implemented, but you can easily do it)
## π’ Differs and Diff Manager
Each differ handles one domain: player, round, map, etc.
The `DifferManager`:
- Manages active differs
- Calls them on each GSI update
- Emits clean events when changes are detected
> Extend or replace differs without touching core functionality.
## π― Extending with Custom Differs
Register your own differ easily:
```javascript
const customDiffer = {
diff(prev, curr, emitter) {
const p = prev?.map?.team_t;
const c = curr.map?.team_t;
if (p && c && p.score !== c.score) {
emitter.emit('map:teamTScoreChanged', { previous: p.score, current: c.score });
}
}
};
gsi.differManager.registerDiffer(customDiffer);
```
## π Advanced Event Subscription
Subscribe to:
- Specific event (`player:hpChanged`)
- Whole namespace (`player:*`)
```javascript
gsi.on('player:*', (eventName, payload) => {
console.log(`[Player Event] ${eventName}:`, payload);
});
```
## πΉ Examples
### Log Health Changes
```javascript
gsi.on('player:hpChanged', ({ previous, current }) => {
console.log(`HP: ${previous} β ${current}`);
});
```
### Log Map Changes
```javascript
gsi.on('map:nameChanged', ({ previous, current }) => {
console.log(`Map: ${previous} β ${current}`);
});
```
## π Use Cases
- ποΈ Live HUDs for players and spectators
- π Stream overlays with real-time updates
- π Tournament backends and dashboards
- πΎ Automated bots reacting to events
- π Esports analytics and match reviews
- πͺ Coaching and training tools
## πΉ Requirements
- **Node.js** v18+
- Support for `async/await`, optional chaining (`?.`), and ES Modules.
## π§³ Contributing
Pull requests, ideas, and feedback are welcome!
If you are using `cs2-gsi-z` for your tools, streams, or projects, feel free to share and contribute back.
## π Acknowledgments
β’ Valve Corporation for the CS Game State Integration API
β’ CS community for inspiration and feedback
## π License
Distributed under the **MIT License**. See [LICENSE](./LICENSE) for full text.
_*Made with β€οΈ for the CS2 community*_