Events
CS2 doesn’t send events. Each payload is a snapshot of the game as it is right now, so pygsi works out the events itself by comparing each payload with the last one it stored for that player. When the round’s phase was freezetime before and is live now, a round has started.
Every event has a decorator on GSIServer, and every handler takes the same three arguments: the player_id the event is about, then old and new, the slice of state before and after the payload. This table lists each event, when it fires, and the type of that slice:
| Event | Fires when | old and new |
|---|---|---|
on_round_start | The round’s phase becomes live | RoundState |
on_round_end | The round’s phase goes from live to over | RoundState |
on_bomb_planted | The bomb becomes planted | RoundState |
on_bomb_defused | The bomb goes from planted to defused | RoundState |
on_bomb_exploded | The bomb goes from planted to exploded | RoundState |
on_map_start | The first live payload of a new map | MapState |
on_map_end | The map’s phase goes from live to gameover | MapState |
on_local_player_kill | The player’s match kills go up | PlayerMatchStats |
on_local_player_assist | The player’s match assists go up | PlayerMatchStats |
on_local_player_mvp | The player’s MVPs go up | PlayerMatchStats |
on_local_player_death | The player’s health drops to 0 | PlayerState |
on_state_update | Every payload pygsi stores | GameState |
The type of old also allows None, but only four events ever pass it: on_round_start, on_bomb_planted, on_map_start, and on_state_update, when pygsi has no earlier state to compare with. The others only fire after a comparison, so their old is always set.
Handlers
A handler is an async def function, and we can register as many of them for one event as we like. Because each decorator hands back the function it was given, we can also stack decorators to have one function handle several events:
from pygsi import GSIServer, RoundState
gsi = GSIServer(player_ids="76561198000000000")
@gsi.on_bomb_defused
@gsi.on_bomb_exploded
async def bomb_done(player_id: str, old: RoundState | None, new: RoundState) -> None:
print(f"The bomb is {new.bomb}")
Here, bomb_done runs when a planted bomb is either defused or explodes, and new.bomb tells us which.
Rounds
A round starts when its phase becomes live, and that’s when on_round_start fires. Usually it’s the end of freeze time, but if we start the script in the middle of a round, the first payload pygsi sees is already live, so the event fires then, with old set to None.
When the phase goes from live to over, on_round_end fires, and new.winning_team tells us who won.
The Bomb
The bomb events fire for every bomb in the player’s match, not only the ones the player plants or defuses. on_bomb_planted fires as soon as round.bomb becomes planted, even on the first payload pygsi sees. The other two are stricter, because they only fire when the payload before had a planted bomb. If pygsi starts after a plant, we won’t hear how that bomb ends.
Maps
pygsi ignores warmup, so on_map_start fires when warmup ends rather than when the map loads. More exactly, it fires on the first live payload pygsi sees for the player, with old set to None, and again on the first live payload of each new map after that, with old set to the map before. A new map is one that comes after a gameover payload, or has a different name, or has fewer rounds played than the last map pygsi stored. That last check catches a player who leaves a match early and joins another, since they never send a gameover payload. If the new match is on the same map and has played at least as many rounds as the old one, pygsi can’t tell them apart.
On a new map, pygsi compares nothing with the old one, so the round and bomb events treat its first payload like the first payload pygsi sees.
At the end of the map, the phase goes from live to gameover, which fires on_map_end with the final score in new. A gameover payload fires nothing else except on_state_update, which runs first, even when other things changed in it.
The Local Player
The kill, assist, and MVP events watch the player’s match totals in PlayerMatchStats, and they fire when one of those goes up. If two kills land in the same payload, we get one event, and new.kills is 2 higher than old.kills. These events compare with the last payload that had the player’s own data in the current map. They never fire on the first one pygsi sees, but they do catch what happened while the player was dead and spectating: an assist or MVP earned then fires as soon as the player’s data comes back, with old set to their stats from before they died.
A death is when health goes from above 0 to exactly 0. on_local_player_death fires once for it and stays quiet while the player is dead.
State Updates
When nothing else fits, we can use on_state_update, which fires for every payload pygsi stores and gets the whole GameState before and after. For example, here’s a handler that prints the player’s money:
from pygsi import GameState
@gsi.on_state_update
async def money(player_id: str, old: GameState | None, new: GameState) -> None:
if new.player is not None:
print(f"${new.player.state.money}")
Game State explains which payloads pygsi stores and which it skips.
Order
One payload can set off several events, like the last kill of a round that also ends the round. In that case, pygsi runs them in this order:
on_state_updateon_map_starton_round_startoron_round_end- The bomb events
- The local player events
Each event finishes before the next one starts. If an event has more than one handler, though, pygsi runs those handlers at the same time with asyncio.gather and waits for all of them. By the time a handler runs, gsi.states already holds the new state.