pygsi-cs2 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pygsi_cs2-0.1.0/LICENSE +21 -0
- pygsi_cs2-0.1.0/PKG-INFO +96 -0
- pygsi_cs2-0.1.0/README.md +69 -0
- pygsi_cs2-0.1.0/pyproject.toml +82 -0
- pygsi_cs2-0.1.0/pyproject.toml.orig +76 -0
- pygsi_cs2-0.1.0/src/pygsi/__init__.py +29 -0
- pygsi_cs2-0.1.0/src/pygsi/_payload.py +155 -0
- pygsi_cs2-0.1.0/src/pygsi/models.py +85 -0
- pygsi_cs2-0.1.0/src/pygsi/py.typed +0 -0
- pygsi_cs2-0.1.0/src/pygsi/server.py +392 -0
pygsi_cs2-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Keon Park
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pygsi_cs2-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pygsi-cs2
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python library to interface with CS2's Game State Integration
|
|
5
|
+
Keywords: counter-strike,cs2,gsi,game-state-integration
|
|
6
|
+
Author: Parkkeo1
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Topic :: Games/Entertainment
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
17
|
+
Requires-Dist: fastapi>=0.100.0
|
|
18
|
+
Requires-Dist: pydantic>=2.0.0
|
|
19
|
+
Requires-Dist: uvicorn>=0.20.0
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Project-URL: Homepage, https://github.com/parkkeo1/pygsi-cs2
|
|
22
|
+
Project-URL: Documentation, https://parkkeo1.github.io/pygsi-cs2/
|
|
23
|
+
Project-URL: Repository, https://github.com/parkkeo1/pygsi-cs2
|
|
24
|
+
Project-URL: Issues, https://github.com/parkkeo1/pygsi-cs2/issues
|
|
25
|
+
Project-URL: Changelog, https://github.com/parkkeo1/pygsi-cs2/blob/main/CHANGELOG.md
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# pygsi
|
|
29
|
+
|
|
30
|
+
[](https://pypi.org/project/pygsi-cs2/)
|
|
31
|
+
[](https://github.com/parkkeo1/pygsi-cs2/actions/workflows/ci.yml)
|
|
32
|
+
[](https://github.com/parkkeo1/pygsi-cs2/blob/main/LICENSE)
|
|
33
|
+
|
|
34
|
+
A Python library to interface with CS2's [Game State Integration](https://developer.valvesoftware.com/wiki/Counter-Strike:_Global_Offensive_Game_State_Integration) (GSI).
|
|
35
|
+
|
|
36
|
+
pygsi runs the server that CS2 sends its game state to while you play. It parses each payload into typed Pydantic models, keeps the latest state for each player it tracks, and calls your async handlers when something happens, like a round starting, the bomb being planted, or a kill. It's built for players in a match, one or several at once, not for spectators or observers.
|
|
37
|
+
|
|
38
|
+
## Status
|
|
39
|
+
|
|
40
|
+
pygsi is in active development, so expect breaking changes between minor versions before 1.0.
|
|
41
|
+
|
|
42
|
+
## Getting Started
|
|
43
|
+
|
|
44
|
+
pygsi needs Python 3.11 or newer. It's called `pygsi-cs2` on PyPI, and you import it as `pygsi`:
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
pip install pygsi-cs2
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
CS2 also needs a config file that tells it where to send updates. [Installation and CS2 Setup][setup] has the file and where to put it. Then a script like this prints a line for every kill:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from pygsi import GSIServer, PlayerMatchStats
|
|
54
|
+
|
|
55
|
+
gsi = GSIServer(player_ids="76561198000000000") # your SteamID64
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@gsi.on_local_player_kill
|
|
59
|
+
async def kill(
|
|
60
|
+
player_id: str, old: PlayerMatchStats | None, new: PlayerMatchStats
|
|
61
|
+
) -> None:
|
|
62
|
+
print(f"Kill! That's {new.kills} this match")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
gsi.run()
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The [Quick Start][quick-start] walks through a longer version, and [`example/`](example/) has a script that logs every event.
|
|
69
|
+
|
|
70
|
+
## Documentation
|
|
71
|
+
|
|
72
|
+
The [book] covers the events, the game state, tracking several players, and every type. To build it locally, install [mdBook] and run `mdbook serve docs --open`.
|
|
73
|
+
|
|
74
|
+
## Development
|
|
75
|
+
|
|
76
|
+
You'll need [uv]. CI runs these checks:
|
|
77
|
+
|
|
78
|
+
```sh
|
|
79
|
+
uv sync --group dev --group test
|
|
80
|
+
uv run ruff format --check src tests example
|
|
81
|
+
uv run ruff check src tests example
|
|
82
|
+
uv run mypy src
|
|
83
|
+
uv run pytest tests
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The tests replay real CS2 payloads from a practice match, stored in `tests/fixtures.json`, so they don't need the game.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
[MIT](https://github.com/parkkeo1/pygsi-cs2/blob/main/LICENSE)
|
|
91
|
+
|
|
92
|
+
[book]: https://parkkeo1.github.io/pygsi-cs2/
|
|
93
|
+
[setup]: https://parkkeo1.github.io/pygsi-cs2/installation.html
|
|
94
|
+
[quick-start]: https://parkkeo1.github.io/pygsi-cs2/quick-start.html
|
|
95
|
+
[mdBook]: https://rust-lang.github.io/mdBook/
|
|
96
|
+
[uv]: https://docs.astral.sh/uv/
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# pygsi
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/pygsi-cs2/)
|
|
4
|
+
[](https://github.com/parkkeo1/pygsi-cs2/actions/workflows/ci.yml)
|
|
5
|
+
[](https://github.com/parkkeo1/pygsi-cs2/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
A Python library to interface with CS2's [Game State Integration](https://developer.valvesoftware.com/wiki/Counter-Strike:_Global_Offensive_Game_State_Integration) (GSI).
|
|
8
|
+
|
|
9
|
+
pygsi runs the server that CS2 sends its game state to while you play. It parses each payload into typed Pydantic models, keeps the latest state for each player it tracks, and calls your async handlers when something happens, like a round starting, the bomb being planted, or a kill. It's built for players in a match, one or several at once, not for spectators or observers.
|
|
10
|
+
|
|
11
|
+
## Status
|
|
12
|
+
|
|
13
|
+
pygsi is in active development, so expect breaking changes between minor versions before 1.0.
|
|
14
|
+
|
|
15
|
+
## Getting Started
|
|
16
|
+
|
|
17
|
+
pygsi needs Python 3.11 or newer. It's called `pygsi-cs2` on PyPI, and you import it as `pygsi`:
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
pip install pygsi-cs2
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
CS2 also needs a config file that tells it where to send updates. [Installation and CS2 Setup][setup] has the file and where to put it. Then a script like this prints a line for every kill:
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from pygsi import GSIServer, PlayerMatchStats
|
|
27
|
+
|
|
28
|
+
gsi = GSIServer(player_ids="76561198000000000") # your SteamID64
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@gsi.on_local_player_kill
|
|
32
|
+
async def kill(
|
|
33
|
+
player_id: str, old: PlayerMatchStats | None, new: PlayerMatchStats
|
|
34
|
+
) -> None:
|
|
35
|
+
print(f"Kill! That's {new.kills} this match")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
gsi.run()
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The [Quick Start][quick-start] walks through a longer version, and [`example/`](example/) has a script that logs every event.
|
|
42
|
+
|
|
43
|
+
## Documentation
|
|
44
|
+
|
|
45
|
+
The [book] covers the events, the game state, tracking several players, and every type. To build it locally, install [mdBook] and run `mdbook serve docs --open`.
|
|
46
|
+
|
|
47
|
+
## Development
|
|
48
|
+
|
|
49
|
+
You'll need [uv]. CI runs these checks:
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
uv sync --group dev --group test
|
|
53
|
+
uv run ruff format --check src tests example
|
|
54
|
+
uv run ruff check src tests example
|
|
55
|
+
uv run mypy src
|
|
56
|
+
uv run pytest tests
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The tests replay real CS2 payloads from a practice match, stored in `tests/fixtures.json`, so they don't need the game.
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
[MIT](https://github.com/parkkeo1/pygsi-cs2/blob/main/LICENSE)
|
|
64
|
+
|
|
65
|
+
[book]: https://parkkeo1.github.io/pygsi-cs2/
|
|
66
|
+
[setup]: https://parkkeo1.github.io/pygsi-cs2/installation.html
|
|
67
|
+
[quick-start]: https://parkkeo1.github.io/pygsi-cs2/quick-start.html
|
|
68
|
+
[mdBook]: https://rust-lang.github.io/mdBook/
|
|
69
|
+
[uv]: https://docs.astral.sh/uv/
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.11.3,<0.12"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pygsi-cs2"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A Python library to interface with CS2's Game State Integration"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.11"
|
|
13
|
+
keywords = [
|
|
14
|
+
"counter-strike",
|
|
15
|
+
"cs2",
|
|
16
|
+
"gsi",
|
|
17
|
+
"game-state-integration",
|
|
18
|
+
]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Intended Audience :: Developers",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Programming Language :: Python :: 3.14",
|
|
26
|
+
"Topic :: Games/Entertainment",
|
|
27
|
+
"Topic :: Software Development :: Libraries",
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"fastapi>=0.100.0",
|
|
31
|
+
"pydantic>=2.0.0",
|
|
32
|
+
"uvicorn>=0.20.0",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[[project.authors]]
|
|
36
|
+
name = "Parkkeo1"
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Homepage = "https://github.com/parkkeo1/pygsi-cs2"
|
|
40
|
+
Documentation = "https://parkkeo1.github.io/pygsi-cs2/"
|
|
41
|
+
Repository = "https://github.com/parkkeo1/pygsi-cs2"
|
|
42
|
+
Issues = "https://github.com/parkkeo1/pygsi-cs2/issues"
|
|
43
|
+
Changelog = "https://github.com/parkkeo1/pygsi-cs2/blob/main/CHANGELOG.md"
|
|
44
|
+
|
|
45
|
+
[dependency-groups]
|
|
46
|
+
dev = [
|
|
47
|
+
"ruff>=0.15.0",
|
|
48
|
+
"mypy>=1.0.0",
|
|
49
|
+
]
|
|
50
|
+
test = [
|
|
51
|
+
"pytest>=8.0.0",
|
|
52
|
+
"pytest-asyncio>=1.0.0",
|
|
53
|
+
"httpx>=0.27.0",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
[tool.uv.build-backend]
|
|
57
|
+
module-root = "src"
|
|
58
|
+
module-name = "pygsi"
|
|
59
|
+
|
|
60
|
+
[tool.ruff]
|
|
61
|
+
target-version = "py311"
|
|
62
|
+
|
|
63
|
+
[tool.ruff.format]
|
|
64
|
+
indent-style = "space"
|
|
65
|
+
|
|
66
|
+
[tool.ruff.lint]
|
|
67
|
+
select = [
|
|
68
|
+
"E",
|
|
69
|
+
"F",
|
|
70
|
+
"I",
|
|
71
|
+
"B",
|
|
72
|
+
"C4",
|
|
73
|
+
"UP",
|
|
74
|
+
"RUF",
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
[tool.pytest.ini_options]
|
|
78
|
+
asyncio_mode = "auto"
|
|
79
|
+
|
|
80
|
+
[tool.mypy]
|
|
81
|
+
strict = true
|
|
82
|
+
python_version = "3.11"
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.11.3,<0.12"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pygsi-cs2"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A Python library to interface with CS2's Game State Integration"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
authors = [{ name = "Parkkeo1" }]
|
|
13
|
+
requires-python = ">=3.11"
|
|
14
|
+
keywords = ["counter-strike", "cs2", "gsi", "game-state-integration"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Topic :: Games/Entertainment",
|
|
23
|
+
"Topic :: Software Development :: Libraries",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"fastapi>=0.100.0",
|
|
27
|
+
"pydantic>=2.0.0",
|
|
28
|
+
"uvicorn>=0.20.0",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Homepage = "https://github.com/parkkeo1/pygsi-cs2"
|
|
33
|
+
Documentation = "https://parkkeo1.github.io/pygsi-cs2/"
|
|
34
|
+
Repository = "https://github.com/parkkeo1/pygsi-cs2"
|
|
35
|
+
Issues = "https://github.com/parkkeo1/pygsi-cs2/issues"
|
|
36
|
+
Changelog = "https://github.com/parkkeo1/pygsi-cs2/blob/main/CHANGELOG.md"
|
|
37
|
+
|
|
38
|
+
[dependency-groups]
|
|
39
|
+
dev = [
|
|
40
|
+
"ruff>=0.15.0",
|
|
41
|
+
"mypy>=1.0.0",
|
|
42
|
+
]
|
|
43
|
+
test = [
|
|
44
|
+
"pytest>=8.0.0",
|
|
45
|
+
"pytest-asyncio>=1.0.0",
|
|
46
|
+
"httpx>=0.27.0",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[tool.uv.build-backend]
|
|
50
|
+
module-root = "src"
|
|
51
|
+
# The package installs as pygsi-cs2 but imports as pygsi.
|
|
52
|
+
module-name = "pygsi"
|
|
53
|
+
|
|
54
|
+
[tool.ruff]
|
|
55
|
+
target-version = "py311"
|
|
56
|
+
|
|
57
|
+
[tool.ruff.format]
|
|
58
|
+
indent-style = "space"
|
|
59
|
+
|
|
60
|
+
[tool.ruff.lint]
|
|
61
|
+
select = [
|
|
62
|
+
"E", # pycodestyle errors
|
|
63
|
+
"F", # pyflakes (unused imports, undefined names, etc.)
|
|
64
|
+
"I", # isort (import ordering)
|
|
65
|
+
"B", # flake8-bugbear (common bug patterns)
|
|
66
|
+
"C4", # flake8-comprehensions (better comprehension style)
|
|
67
|
+
"UP", # pyupgrade (modern Python syntax)
|
|
68
|
+
"RUF", # ruff-specific rules
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
[tool.pytest.ini_options]
|
|
72
|
+
asyncio_mode = "auto"
|
|
73
|
+
|
|
74
|
+
[tool.mypy]
|
|
75
|
+
strict = true
|
|
76
|
+
python_version = "3.11"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from .models import (
|
|
2
|
+
Activity,
|
|
3
|
+
BombStatus,
|
|
4
|
+
GameState,
|
|
5
|
+
MapPhase,
|
|
6
|
+
MapState,
|
|
7
|
+
Player,
|
|
8
|
+
PlayerMatchStats,
|
|
9
|
+
PlayerState,
|
|
10
|
+
RoundPhase,
|
|
11
|
+
RoundState,
|
|
12
|
+
Team,
|
|
13
|
+
)
|
|
14
|
+
from .server import GSIServer
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"Activity",
|
|
18
|
+
"BombStatus",
|
|
19
|
+
"GSIServer",
|
|
20
|
+
"GameState",
|
|
21
|
+
"MapPhase",
|
|
22
|
+
"MapState",
|
|
23
|
+
"Player",
|
|
24
|
+
"PlayerMatchStats",
|
|
25
|
+
"PlayerState",
|
|
26
|
+
"RoundPhase",
|
|
27
|
+
"RoundState",
|
|
28
|
+
"Team",
|
|
29
|
+
]
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Internal module for parsing raw CS2 GSI JSON payloads into public GameState types.
|
|
3
|
+
|
|
4
|
+
CS2 sends some field names that differ from our public API (e.g. win_team vs
|
|
5
|
+
winning_team). All models here use extra='ignore' so unknown or future fields
|
|
6
|
+
don't break parsing.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from pydantic import BaseModel, ConfigDict
|
|
10
|
+
|
|
11
|
+
from .models import (
|
|
12
|
+
Activity,
|
|
13
|
+
BombStatus,
|
|
14
|
+
GameState,
|
|
15
|
+
MapPhase,
|
|
16
|
+
MapState,
|
|
17
|
+
Player,
|
|
18
|
+
PlayerMatchStats,
|
|
19
|
+
PlayerState,
|
|
20
|
+
RoundPhase,
|
|
21
|
+
RoundState,
|
|
22
|
+
Team,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class _TeamPayload(BaseModel):
|
|
27
|
+
model_config = ConfigDict(extra="ignore")
|
|
28
|
+
|
|
29
|
+
score: int
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class _MapPayload(BaseModel):
|
|
33
|
+
model_config = ConfigDict(extra="ignore")
|
|
34
|
+
|
|
35
|
+
name: str
|
|
36
|
+
mode: str
|
|
37
|
+
phase: MapPhase
|
|
38
|
+
round: int
|
|
39
|
+
team_ct: _TeamPayload
|
|
40
|
+
team_t: _TeamPayload
|
|
41
|
+
round_wins: dict[str, str] = {}
|
|
42
|
+
|
|
43
|
+
def to_public(self) -> MapState:
|
|
44
|
+
return MapState(
|
|
45
|
+
name=self.name,
|
|
46
|
+
mode=self.mode,
|
|
47
|
+
phase=self.phase,
|
|
48
|
+
round=self.round + 1,
|
|
49
|
+
team_ct_score=self.team_ct.score,
|
|
50
|
+
team_t_score=self.team_t.score,
|
|
51
|
+
round_wins=self.round_wins,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class _RoundPayload(BaseModel):
|
|
56
|
+
model_config = ConfigDict(extra="ignore")
|
|
57
|
+
|
|
58
|
+
phase: RoundPhase
|
|
59
|
+
bomb: BombStatus | None = None
|
|
60
|
+
# CS2 sends "win_team", not "winning_team"
|
|
61
|
+
win_team: Team | None = None
|
|
62
|
+
|
|
63
|
+
def to_public(self) -> RoundState:
|
|
64
|
+
return RoundState(
|
|
65
|
+
phase=self.phase,
|
|
66
|
+
bomb=self.bomb,
|
|
67
|
+
winning_team=self.win_team,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class _PlayerStatePayload(BaseModel):
|
|
72
|
+
model_config = ConfigDict(extra="ignore")
|
|
73
|
+
|
|
74
|
+
health: int
|
|
75
|
+
armor: int
|
|
76
|
+
helmet: bool
|
|
77
|
+
flashed: int
|
|
78
|
+
smoked: int
|
|
79
|
+
burning: int
|
|
80
|
+
money: int
|
|
81
|
+
round_kills: int
|
|
82
|
+
round_killhs: int
|
|
83
|
+
equip_value: int
|
|
84
|
+
|
|
85
|
+
def to_public(self) -> PlayerState:
|
|
86
|
+
return PlayerState(**self.model_dump())
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class _PlayerMatchStatsPayload(BaseModel):
|
|
90
|
+
model_config = ConfigDict(extra="ignore")
|
|
91
|
+
|
|
92
|
+
kills: int
|
|
93
|
+
assists: int
|
|
94
|
+
deaths: int
|
|
95
|
+
mvps: int
|
|
96
|
+
score: int
|
|
97
|
+
|
|
98
|
+
def to_public(self) -> PlayerMatchStats:
|
|
99
|
+
return PlayerMatchStats(**self.model_dump())
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class _PlayerPayload(BaseModel):
|
|
103
|
+
model_config = ConfigDict(extra="ignore")
|
|
104
|
+
|
|
105
|
+
steamid: str
|
|
106
|
+
name: str
|
|
107
|
+
team: Team | None = None
|
|
108
|
+
activity: Activity | None = None
|
|
109
|
+
state: _PlayerStatePayload | None = None
|
|
110
|
+
match_stats: _PlayerMatchStatsPayload | None = None
|
|
111
|
+
|
|
112
|
+
def to_public(self) -> Player | None:
|
|
113
|
+
# Menu-state payloads lack team/state/match_stats — skip them
|
|
114
|
+
if self.team is None or self.state is None or self.match_stats is None:
|
|
115
|
+
return None
|
|
116
|
+
return Player(
|
|
117
|
+
steamid=self.steamid,
|
|
118
|
+
name=self.name,
|
|
119
|
+
team=self.team,
|
|
120
|
+
activity=self.activity,
|
|
121
|
+
state=self.state.to_public(),
|
|
122
|
+
match_stats=self.match_stats.to_public(),
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class _ProviderPayload(BaseModel):
|
|
127
|
+
model_config = ConfigDict(extra="ignore")
|
|
128
|
+
|
|
129
|
+
steamid: str
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class GSIPayload(BaseModel):
|
|
133
|
+
"""Top-level CS2 GSI payload.
|
|
134
|
+
|
|
135
|
+
Extra fields (previously, added, auth, etc.) are ignored.
|
|
136
|
+
"""
|
|
137
|
+
|
|
138
|
+
model_config = ConfigDict(extra="ignore")
|
|
139
|
+
|
|
140
|
+
provider: _ProviderPayload | None = None
|
|
141
|
+
map: _MapPayload | None = None
|
|
142
|
+
round: _RoundPayload | None = None
|
|
143
|
+
player: _PlayerPayload | None = None
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def provider_steamid(self) -> str | None:
|
|
147
|
+
"""The steamid of the CS2 client that sent this payload."""
|
|
148
|
+
return self.provider.steamid if self.provider else None
|
|
149
|
+
|
|
150
|
+
def to_game_state(self) -> GameState:
|
|
151
|
+
return GameState(
|
|
152
|
+
map=self.map.to_public() if self.map else None,
|
|
153
|
+
round=self.round.to_public() if self.round else None,
|
|
154
|
+
player=self.player.to_public() if self.player else None,
|
|
155
|
+
)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
from enum import StrEnum
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MapPhase(StrEnum):
|
|
7
|
+
WARMUP = "warmup"
|
|
8
|
+
LIVE = "live"
|
|
9
|
+
INTERMISSION = "intermission"
|
|
10
|
+
GAMEOVER = "gameover"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class RoundPhase(StrEnum):
|
|
14
|
+
FREEZETIME = "freezetime"
|
|
15
|
+
LIVE = "live"
|
|
16
|
+
OVER = "over"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BombStatus(StrEnum):
|
|
20
|
+
PLANTED = "planted"
|
|
21
|
+
DEFUSED = "defused"
|
|
22
|
+
EXPLODED = "exploded"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Team(StrEnum):
|
|
26
|
+
T = "T"
|
|
27
|
+
CT = "CT"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Activity(StrEnum):
|
|
31
|
+
PLAYING = "playing"
|
|
32
|
+
MENU = "menu"
|
|
33
|
+
TEXT_INPUT = "textinput"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class MapState(BaseModel):
|
|
37
|
+
name: str
|
|
38
|
+
mode: str
|
|
39
|
+
phase: MapPhase
|
|
40
|
+
round: int
|
|
41
|
+
team_ct_score: int
|
|
42
|
+
team_t_score: int
|
|
43
|
+
round_wins: dict[str, str] = {}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class RoundState(BaseModel):
|
|
47
|
+
phase: RoundPhase
|
|
48
|
+
bomb: BombStatus | None = None
|
|
49
|
+
winning_team: Team | None = None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class PlayerState(BaseModel):
|
|
53
|
+
health: int
|
|
54
|
+
armor: int
|
|
55
|
+
helmet: bool
|
|
56
|
+
flashed: int
|
|
57
|
+
smoked: int
|
|
58
|
+
burning: int
|
|
59
|
+
money: int
|
|
60
|
+
round_kills: int
|
|
61
|
+
round_killhs: int
|
|
62
|
+
equip_value: int
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class PlayerMatchStats(BaseModel):
|
|
66
|
+
kills: int
|
|
67
|
+
assists: int
|
|
68
|
+
deaths: int
|
|
69
|
+
mvps: int
|
|
70
|
+
score: int
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class Player(BaseModel):
|
|
74
|
+
steamid: str
|
|
75
|
+
name: str
|
|
76
|
+
team: Team
|
|
77
|
+
activity: Activity | None = None
|
|
78
|
+
state: PlayerState
|
|
79
|
+
match_stats: PlayerMatchStats
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class GameState(BaseModel):
|
|
83
|
+
map: MapState | None = None
|
|
84
|
+
round: RoundState | None = None
|
|
85
|
+
player: Player | None = None
|
|
File without changes
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import json
|
|
3
|
+
import logging
|
|
4
|
+
from collections.abc import Callable, Coroutine, Sequence
|
|
5
|
+
from enum import StrEnum
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import uvicorn
|
|
9
|
+
from fastapi import FastAPI, Request
|
|
10
|
+
from fastapi.exceptions import RequestValidationError
|
|
11
|
+
from fastapi.responses import JSONResponse
|
|
12
|
+
|
|
13
|
+
from ._payload import GSIPayload
|
|
14
|
+
from .models import (
|
|
15
|
+
BombStatus,
|
|
16
|
+
GameState,
|
|
17
|
+
MapPhase,
|
|
18
|
+
MapState,
|
|
19
|
+
PlayerMatchStats,
|
|
20
|
+
PlayerState,
|
|
21
|
+
RoundPhase,
|
|
22
|
+
RoundState,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
logger = logging.getLogger(__name__)
|
|
26
|
+
|
|
27
|
+
Handler = Callable[..., Coroutine[Any, Any, None]]
|
|
28
|
+
|
|
29
|
+
# Union of all state slices passed to event handlers
|
|
30
|
+
_Slice = GameState | MapState | RoundState | PlayerMatchStats | PlayerState
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Event(StrEnum):
|
|
34
|
+
ROUND_START = "on_round_start"
|
|
35
|
+
ROUND_END = "on_round_end"
|
|
36
|
+
BOMB_PLANTED = "on_bomb_planted"
|
|
37
|
+
BOMB_DEFUSED = "on_bomb_defused"
|
|
38
|
+
BOMB_EXPLODED = "on_bomb_exploded"
|
|
39
|
+
LOCAL_PLAYER_KILL = "on_local_player_kill"
|
|
40
|
+
LOCAL_PLAYER_ASSIST = "on_local_player_assist"
|
|
41
|
+
LOCAL_PLAYER_MVP = "on_local_player_mvp"
|
|
42
|
+
LOCAL_PLAYER_DEATH = "on_local_player_death"
|
|
43
|
+
STATE_UPDATE = "on_state_update"
|
|
44
|
+
MAP_START = "on_map_start"
|
|
45
|
+
MAP_END = "on_map_end"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class GSIServer:
|
|
49
|
+
"""
|
|
50
|
+
Receives live CS2 Game State Integration updates over HTTP and fires
|
|
51
|
+
typed async event handlers when game state transitions occur.
|
|
52
|
+
|
|
53
|
+
Supports tracking one or more players. Each player's state is tracked
|
|
54
|
+
independently, and event handlers receive the player_id that triggered
|
|
55
|
+
the event as the first argument.
|
|
56
|
+
|
|
57
|
+
Usage:
|
|
58
|
+
gsi = GSIServer(player_ids="76561198XXXXXXX", port=4000)
|
|
59
|
+
|
|
60
|
+
@gsi.on_round_start
|
|
61
|
+
async def handle(player_id: str, old: RoundState | None, new: RoundState):
|
|
62
|
+
print(f"Round started for {player_id}")
|
|
63
|
+
|
|
64
|
+
gsi.run()
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
def __init__(
|
|
68
|
+
self, player_ids: str | Sequence[str], port: int = 4213, host: str = "0.0.0.0"
|
|
69
|
+
) -> None:
|
|
70
|
+
if isinstance(player_ids, str):
|
|
71
|
+
self._player_ids = frozenset({player_ids})
|
|
72
|
+
else:
|
|
73
|
+
self._player_ids = frozenset(player_ids)
|
|
74
|
+
|
|
75
|
+
self.port = port
|
|
76
|
+
self.host = host
|
|
77
|
+
|
|
78
|
+
self._states: dict[str, GameState | None] = dict.fromkeys(self._player_ids)
|
|
79
|
+
self._handlers: dict[Event, list[Handler]] = {event: [] for event in Event}
|
|
80
|
+
self._app = self._build_app()
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def player_ids(self) -> frozenset[str]:
|
|
84
|
+
"""The set of player steamids being tracked."""
|
|
85
|
+
return self._player_ids
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def states(self) -> dict[str, GameState | None]:
|
|
89
|
+
"""Per-player game state. Keyed by steamid."""
|
|
90
|
+
return self._states
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def state(self) -> GameState | None:
|
|
94
|
+
"""Convenience accessor for single-player mode.
|
|
95
|
+
|
|
96
|
+
Returns the tracked player's state. Raises RuntimeError if multiple
|
|
97
|
+
players are configured — use ``states`` instead.
|
|
98
|
+
"""
|
|
99
|
+
if len(self._player_ids) != 1:
|
|
100
|
+
raise RuntimeError(
|
|
101
|
+
"GSIServer.state is only available with a single player_id. "
|
|
102
|
+
"Use GSIServer.states for multi-player."
|
|
103
|
+
)
|
|
104
|
+
return next(iter(self._states.values()))
|
|
105
|
+
|
|
106
|
+
# --- Event registration decorators ---
|
|
107
|
+
|
|
108
|
+
def on_round_start(self, fn: Handler) -> Handler:
|
|
109
|
+
"""Fires when round phase transitions from freezetime to live.
|
|
110
|
+
|
|
111
|
+
Handler signature:
|
|
112
|
+
async def handler(player_id: str, old: RoundState | None, new: RoundState)
|
|
113
|
+
"""
|
|
114
|
+
self._handlers[Event.ROUND_START].append(fn)
|
|
115
|
+
return fn
|
|
116
|
+
|
|
117
|
+
def on_round_end(self, fn: Handler) -> Handler:
|
|
118
|
+
"""Fires when round phase transitions from live to over.
|
|
119
|
+
|
|
120
|
+
Handler signature:
|
|
121
|
+
async def handler(player_id: str, old: RoundState | None, new: RoundState)
|
|
122
|
+
"""
|
|
123
|
+
self._handlers[Event.ROUND_END].append(fn)
|
|
124
|
+
return fn
|
|
125
|
+
|
|
126
|
+
def on_bomb_planted(self, fn: Handler) -> Handler:
|
|
127
|
+
"""Fires when the bomb is planted.
|
|
128
|
+
|
|
129
|
+
Handler signature:
|
|
130
|
+
async def handler(player_id: str, old: RoundState | None, new: RoundState)
|
|
131
|
+
"""
|
|
132
|
+
self._handlers[Event.BOMB_PLANTED].append(fn)
|
|
133
|
+
return fn
|
|
134
|
+
|
|
135
|
+
def on_bomb_defused(self, fn: Handler) -> Handler:
|
|
136
|
+
"""Fires when the bomb is defused.
|
|
137
|
+
|
|
138
|
+
Handler signature:
|
|
139
|
+
async def handler(player_id: str, old: RoundState | None, new: RoundState)
|
|
140
|
+
"""
|
|
141
|
+
self._handlers[Event.BOMB_DEFUSED].append(fn)
|
|
142
|
+
return fn
|
|
143
|
+
|
|
144
|
+
def on_bomb_exploded(self, fn: Handler) -> Handler:
|
|
145
|
+
"""Fires when the bomb explodes.
|
|
146
|
+
|
|
147
|
+
Handler signature:
|
|
148
|
+
async def handler(player_id: str, old: RoundState | None, new: RoundState)
|
|
149
|
+
"""
|
|
150
|
+
self._handlers[Event.BOMB_EXPLODED].append(fn)
|
|
151
|
+
return fn
|
|
152
|
+
|
|
153
|
+
def on_local_player_kill(self, fn: Handler) -> Handler:
|
|
154
|
+
"""Fires when the tracked player registers a kill.
|
|
155
|
+
|
|
156
|
+
Handler signature:
|
|
157
|
+
async def handler(
|
|
158
|
+
player_id: str,
|
|
159
|
+
old: PlayerMatchStats | None,
|
|
160
|
+
new: PlayerMatchStats,
|
|
161
|
+
)
|
|
162
|
+
"""
|
|
163
|
+
self._handlers[Event.LOCAL_PLAYER_KILL].append(fn)
|
|
164
|
+
return fn
|
|
165
|
+
|
|
166
|
+
def on_local_player_assist(self, fn: Handler) -> Handler:
|
|
167
|
+
"""Fires when the tracked player registers an assist.
|
|
168
|
+
|
|
169
|
+
Handler signature:
|
|
170
|
+
async def handler(
|
|
171
|
+
player_id: str,
|
|
172
|
+
old: PlayerMatchStats | None,
|
|
173
|
+
new: PlayerMatchStats,
|
|
174
|
+
)
|
|
175
|
+
"""
|
|
176
|
+
self._handlers[Event.LOCAL_PLAYER_ASSIST].append(fn)
|
|
177
|
+
return fn
|
|
178
|
+
|
|
179
|
+
def on_local_player_mvp(self, fn: Handler) -> Handler:
|
|
180
|
+
"""Fires when the tracked player is awarded MVP.
|
|
181
|
+
|
|
182
|
+
Handler signature:
|
|
183
|
+
async def handler(
|
|
184
|
+
player_id: str,
|
|
185
|
+
old: PlayerMatchStats | None,
|
|
186
|
+
new: PlayerMatchStats,
|
|
187
|
+
)
|
|
188
|
+
"""
|
|
189
|
+
self._handlers[Event.LOCAL_PLAYER_MVP].append(fn)
|
|
190
|
+
return fn
|
|
191
|
+
|
|
192
|
+
def on_local_player_death(self, fn: Handler) -> Handler:
|
|
193
|
+
"""Fires when the tracked player's health reaches 0.
|
|
194
|
+
|
|
195
|
+
Handler signature:
|
|
196
|
+
async def handler(player_id: str, old: PlayerState | None, new: PlayerState)
|
|
197
|
+
"""
|
|
198
|
+
self._handlers[Event.LOCAL_PLAYER_DEATH].append(fn)
|
|
199
|
+
return fn
|
|
200
|
+
|
|
201
|
+
def on_state_update(self, fn: Handler) -> Handler:
|
|
202
|
+
"""Fires on every valid payload after state is stored.
|
|
203
|
+
|
|
204
|
+
Handler signature:
|
|
205
|
+
async def handler(player_id: str, old: GameState | None, new: GameState)
|
|
206
|
+
"""
|
|
207
|
+
self._handlers[Event.STATE_UPDATE].append(fn)
|
|
208
|
+
return fn
|
|
209
|
+
|
|
210
|
+
def on_map_start(self, fn: Handler) -> Handler:
|
|
211
|
+
"""Fires when a new map begins (first live payload after warmup or prior map).
|
|
212
|
+
|
|
213
|
+
Handler signature:
|
|
214
|
+
async def handler(player_id: str, old: MapState | None, new: MapState)
|
|
215
|
+
"""
|
|
216
|
+
self._handlers[Event.MAP_START].append(fn)
|
|
217
|
+
return fn
|
|
218
|
+
|
|
219
|
+
def on_map_end(self, fn: Handler) -> Handler:
|
|
220
|
+
"""Fires when map phase transitions to gameover (map is over).
|
|
221
|
+
|
|
222
|
+
Handler signature:
|
|
223
|
+
async def handler(player_id: str, old: MapState | None, new: MapState)
|
|
224
|
+
"""
|
|
225
|
+
self._handlers[Event.MAP_END].append(fn)
|
|
226
|
+
return fn
|
|
227
|
+
|
|
228
|
+
# --- Server ---
|
|
229
|
+
|
|
230
|
+
def run(self) -> None:
|
|
231
|
+
"""Start the GSI server. Blocks until interrupted."""
|
|
232
|
+
uvicorn.run(self._app, host=self.host, port=self.port)
|
|
233
|
+
|
|
234
|
+
# --- Internal ---
|
|
235
|
+
|
|
236
|
+
def _build_app(self) -> FastAPI:
|
|
237
|
+
app = FastAPI()
|
|
238
|
+
|
|
239
|
+
@app.exception_handler(RequestValidationError)
|
|
240
|
+
async def handle_validation_error(
|
|
241
|
+
request: Request, exc: RequestValidationError
|
|
242
|
+
) -> JSONResponse:
|
|
243
|
+
logger.error("Request validation error: %s", exc)
|
|
244
|
+
return JSONResponse(status_code=200, content={"status": "ok"})
|
|
245
|
+
|
|
246
|
+
@app.post("/")
|
|
247
|
+
async def receive(request: Request) -> JSONResponse:
|
|
248
|
+
try:
|
|
249
|
+
body = await request.json()
|
|
250
|
+
if logger.isEnabledFor(logging.DEBUG):
|
|
251
|
+
logger.debug("Raw payload:\n%s", json.dumps(body, indent=2))
|
|
252
|
+
|
|
253
|
+
payload = GSIPayload.model_validate(body)
|
|
254
|
+
await self._handle_payload(payload)
|
|
255
|
+
except Exception as e:
|
|
256
|
+
logger.error("Failed to process GSI payload: %s", e)
|
|
257
|
+
|
|
258
|
+
return JSONResponse(status_code=200, content={"status": "ok"})
|
|
259
|
+
|
|
260
|
+
return app
|
|
261
|
+
|
|
262
|
+
async def _handle_payload(self, payload: GSIPayload) -> None:
|
|
263
|
+
new_state = payload.to_game_state()
|
|
264
|
+
|
|
265
|
+
if new_state.map is None:
|
|
266
|
+
return
|
|
267
|
+
|
|
268
|
+
# Identify which player sent this payload via the provider block
|
|
269
|
+
provider_id = payload.provider_steamid
|
|
270
|
+
if provider_id is None or provider_id not in self._player_ids:
|
|
271
|
+
return
|
|
272
|
+
|
|
273
|
+
# map end: fire event and stop — don't fire other events
|
|
274
|
+
if new_state.map.phase == MapPhase.GAMEOVER:
|
|
275
|
+
prev_state = self._states[provider_id]
|
|
276
|
+
prev_map = prev_state.map if prev_state else None
|
|
277
|
+
if prev_map is not None and prev_map.phase == MapPhase.LIVE:
|
|
278
|
+
await self._dispatch(
|
|
279
|
+
Event.MAP_END, provider_id, prev_map, new_state.map
|
|
280
|
+
)
|
|
281
|
+
self._states[provider_id] = new_state
|
|
282
|
+
await self._dispatch(Event.STATE_UPDATE, provider_id, prev_state, new_state)
|
|
283
|
+
return
|
|
284
|
+
|
|
285
|
+
# Only process payloads during a live map (skip warmup, menu, etc.)
|
|
286
|
+
if new_state.map.phase != MapPhase.LIVE:
|
|
287
|
+
return
|
|
288
|
+
|
|
289
|
+
# After death, CS2 sends the spectated teammate's data — null it out
|
|
290
|
+
# so state.player is always the target player or None
|
|
291
|
+
if new_state.player is not None and new_state.player.steamid != provider_id:
|
|
292
|
+
new_state = new_state.model_copy(update={"player": None})
|
|
293
|
+
|
|
294
|
+
prev_state = self._states[provider_id]
|
|
295
|
+
self._states[provider_id] = new_state
|
|
296
|
+
|
|
297
|
+
await self._fire_events(provider_id, prev_state, new_state)
|
|
298
|
+
|
|
299
|
+
async def _fire_events(
|
|
300
|
+
self, player_id: str, prev: GameState | None, curr: GameState
|
|
301
|
+
) -> None:
|
|
302
|
+
await self._dispatch(Event.STATE_UPDATE, player_id, prev, curr)
|
|
303
|
+
await self._handle_map_events(player_id, prev, curr)
|
|
304
|
+
await self._handle_round_events(player_id, prev, curr)
|
|
305
|
+
await self._handle_bomb_events(player_id, prev, curr)
|
|
306
|
+
await self._handle_player_events(player_id, prev, curr)
|
|
307
|
+
|
|
308
|
+
async def _handle_map_events(
|
|
309
|
+
self, player_id: str, prev: GameState | None, curr: GameState
|
|
310
|
+
) -> None:
|
|
311
|
+
assert curr.map is not None
|
|
312
|
+
prev_map = prev.map if prev else None
|
|
313
|
+
# Fire on the first LIVE payload after warmup (prev is None) or after a
|
|
314
|
+
# previous map ended (prev_map.phase == GAMEOVER)
|
|
315
|
+
if prev_map is None or prev_map.phase == MapPhase.GAMEOVER:
|
|
316
|
+
await self._dispatch(Event.MAP_START, player_id, prev_map, curr.map)
|
|
317
|
+
|
|
318
|
+
async def _handle_round_events(
|
|
319
|
+
self, player_id: str, prev: GameState | None, curr: GameState
|
|
320
|
+
) -> None:
|
|
321
|
+
if curr.round is None:
|
|
322
|
+
return
|
|
323
|
+
prev_round = prev.round if prev else None
|
|
324
|
+
prev_phase = prev_round.phase if prev_round else None
|
|
325
|
+
|
|
326
|
+
if prev_phase != RoundPhase.LIVE and curr.round.phase == RoundPhase.LIVE:
|
|
327
|
+
await self._dispatch(Event.ROUND_START, player_id, prev_round, curr.round)
|
|
328
|
+
elif prev_phase == RoundPhase.LIVE and curr.round.phase == RoundPhase.OVER:
|
|
329
|
+
await self._dispatch(Event.ROUND_END, player_id, prev_round, curr.round)
|
|
330
|
+
|
|
331
|
+
async def _handle_bomb_events(
|
|
332
|
+
self, player_id: str, prev: GameState | None, curr: GameState
|
|
333
|
+
) -> None:
|
|
334
|
+
if curr.round is None:
|
|
335
|
+
return
|
|
336
|
+
prev_round = prev.round if prev else None
|
|
337
|
+
prev_bomb = prev_round.bomb if prev_round else None
|
|
338
|
+
curr_bomb = curr.round.bomb
|
|
339
|
+
|
|
340
|
+
if prev_bomb != BombStatus.PLANTED and curr_bomb == BombStatus.PLANTED:
|
|
341
|
+
await self._dispatch(Event.BOMB_PLANTED, player_id, prev_round, curr.round)
|
|
342
|
+
elif prev_bomb == BombStatus.PLANTED and curr_bomb == BombStatus.DEFUSED:
|
|
343
|
+
await self._dispatch(Event.BOMB_DEFUSED, player_id, prev_round, curr.round)
|
|
344
|
+
elif prev_bomb == BombStatus.PLANTED and curr_bomb == BombStatus.EXPLODED:
|
|
345
|
+
await self._dispatch(Event.BOMB_EXPLODED, player_id, prev_round, curr.round)
|
|
346
|
+
|
|
347
|
+
async def _handle_player_events(
|
|
348
|
+
self, player_id: str, prev: GameState | None, curr: GameState
|
|
349
|
+
) -> None:
|
|
350
|
+
if curr.player is None:
|
|
351
|
+
return
|
|
352
|
+
|
|
353
|
+
prev_player = prev.player if prev else None
|
|
354
|
+
|
|
355
|
+
prev_stats = prev_player.match_stats if prev_player else None
|
|
356
|
+
curr_stats = curr.player.match_stats
|
|
357
|
+
|
|
358
|
+
if prev_stats is not None and curr_stats.kills > prev_stats.kills:
|
|
359
|
+
await self._dispatch(
|
|
360
|
+
Event.LOCAL_PLAYER_KILL, player_id, prev_stats, curr_stats
|
|
361
|
+
)
|
|
362
|
+
if prev_stats is not None and curr_stats.assists > prev_stats.assists:
|
|
363
|
+
await self._dispatch(
|
|
364
|
+
Event.LOCAL_PLAYER_ASSIST, player_id, prev_stats, curr_stats
|
|
365
|
+
)
|
|
366
|
+
if prev_stats is not None and curr_stats.mvps > prev_stats.mvps:
|
|
367
|
+
await self._dispatch(
|
|
368
|
+
Event.LOCAL_PLAYER_MVP, player_id, prev_stats, curr_stats
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
curr_health = curr.player.state.health
|
|
372
|
+
prev_health = prev_player.state.health if prev_player else None
|
|
373
|
+
if prev_health is not None and prev_health > 0 and curr_health == 0:
|
|
374
|
+
await self._dispatch(
|
|
375
|
+
Event.LOCAL_PLAYER_DEATH,
|
|
376
|
+
player_id,
|
|
377
|
+
prev_player.state if prev_player else None,
|
|
378
|
+
curr.player.state,
|
|
379
|
+
)
|
|
380
|
+
|
|
381
|
+
async def _dispatch(
|
|
382
|
+
self, event: Event, player_id: str, old: _Slice | None, new: _Slice
|
|
383
|
+
) -> None:
|
|
384
|
+
async def _run(handler: Handler) -> None:
|
|
385
|
+
try:
|
|
386
|
+
await handler(player_id, old, new)
|
|
387
|
+
except Exception as e:
|
|
388
|
+
logger.exception(
|
|
389
|
+
"Error in %s handler '%s': %s", event, handler.__name__, e
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
await asyncio.gather(*(_run(h) for h in self._handlers[event]))
|