Game State
A handler only gets one slice of the state, but it often needs more, like a kill handler that wants to know the map. For that, pygsi keeps the latest full state for each player it tracks, and we can read it whenever we like.
Reading State
With one tracked player, we read it from gsi.state, which is None until pygsi stores its first payload:
from pygsi import GSIServer, PlayerMatchStats
gsi = GSIServer(player_ids="76561198000000000")
@gsi.on_local_player_kill
async def kill(
player_id: str, old: PlayerMatchStats | None, new: PlayerMatchStats
) -> None:
state = gsi.state
if state is not None and state.map is not None:
print(f"Kill on {state.map.name}, round {state.map.round}")
A GameState has a map, a round, and a player, and any of them can be None, which is why the handler checks state.map before using it. Types lists every field. When we track more than one player, we use gsi.states instead, which Multiple Players covers.
Which Payloads Count
CS2 sends payloads all the time, including in menus and during warmup. pygsi stores a payload, and runs handlers for it, only when all of these hold:
- It has a
mapblock, so the player is on a map. - Its
provideris one of the players we track. - The map’s phase is
liveorgameover.
Everything else is dropped, and the stored state stays as it was. That includes warmup, the menus, and intermission, the break at halftime. During halftime, gsi.state still shows the last payload from before it.
Note
CS2 counts the rounds already finished in
map.round, and pygsi adds one, so during a round it’s that round’s number, starting at 1. CS2 updates its count as soon as a round is over, though, so once the round’s phase isover, and after the map ends,map.roundis already the number of the round after it.
Spectating After Death
When the tracked player dies, CS2 moves their camera to a teammate, and its payloads start carrying that teammate’s data in the player block. pygsi catches this by comparing the player block’s SteamID with the provider’s, and when they differ, it sets player to None. While the player is dead and spectating, then, state.player is None and the local player events don’t fire, but the map and round keep updating. The same check applies to the gameover payload.