Skip to content

Commit 4f5ce6d

Browse files
FMorsbachjrepp
authored andcommitted
Adding tutorial from FMorsbach (#79)
This tutorial written by FMorsbach covers usage of the programmatic API within a python console. ---- Step by step explanations and decode example for game events and message events. * A basic walk-through * Example of event documentation * Filter and JSON export example
1 parent 956eebd commit 4f5ce6d

File tree

3 files changed

+108
-18
lines changed

3 files changed

+108
-18
lines changed

docs/events.rst

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Events
2+
======
3+
4+
There are a lot of different event types used in SC2Replays. This list will cover some events but won´t claim any sort of completness. If you stumble across a game event not mentioned here please feel free to contribute.
5+
6+
Every event is a python dictionary. Each event contains the following keys:
7+
8+
* ``_eventid``
9+
* ``_event``
10+
* ``_gameloop``
11+
* ``_userid``
12+
* ``_bits``
13+
14+
15+
NNet.Game.SChatMessage
16+
----------------------
17+
* ``m_string'`` - contains the chat message
18+
19+
20+
NNet.Game.SCameraUpdateEvent
21+
----------------------------
22+
* ``m_target`` - contains x and y coordinates
23+
* ``m_reason``
24+
* ``m_distance``
25+
* ``m_pitch``
26+
* ``m_follow``
27+
* ``m_yaw``
28+
29+
NNet.Game.SControlGroupUpdateEvent
30+
----------------------------------
31+
tbc
32+
33+
34+
NNet.Game.SSelectionDeltaEvent
35+
------------------------------
36+
tbc
37+
38+
39+
NNet.Game.SCmdEvent
40+
-------------------
41+
tbc

docs/tutorial.rst

-18
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,3 @@ To control the output format:
7171

7272
* ``--json``: Outputs events as JSON structured documents
7373
* ``--ndjson``: Like --json but is each event is newline delimited
74-
75-
76-
Using s2protocol Programmatically
77-
=================================
78-
79-
If you're looking to do more than just look at a single replay's output once,
80-
you'll want to explore the programmatic API. Loading a replay & printing its
81-
header looks like::
82-
83-
>>> import mpyq
84-
85-
# Using mpyq, load the replay file.
86-
>>> archive = mpyq.MPQArchive('~/Desktop/my_pvp_replay.SC2Replay')
87-
>>> contents = archive.header['user_data_header']['content']
88-
89-
# Now parse the header information.
90-
>>> from s2protocol import versions
91-
>>> header = versions.latest().decode_replay_header(contents)

docs/tutorial_API.rst

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Using the s2protocol API
2+
=================================
3+
4+
If you're looking to do more than just look at a single replay's output once,
5+
you'll want to explore the programmatic API.
6+
7+
SC2Replay files are MPQ archives. MPQ archives are used by blizzard in several contexts. The SC2Replay archives contain mostly header/meta information and multiple files. The replay data itself is grouped and encoded in different files. With the mpyq package you can open and decode these archives and look inside:
8+
9+
>>> import mpyq
10+
>>> archive = mpyq.MPQArchive('~/Desktop/my_pvp_replay.SC2Replay')
11+
12+
With an attached debugger you can now explore the archive or print out a list of all files inside the archive:
13+
14+
>>> print archive.files
15+
16+
which returns:
17+
18+
* ``replay.attributes.events``
19+
* ``replay.details``
20+
* ``replay.details.backup``
21+
* ``replay.game.events``
22+
* ``replay.gamemetadata.json``
23+
* ``replay.initData``
24+
* ``replay.initData.backup``
25+
* ``replay.load.info``
26+
* ``replay.message.events``
27+
* ``replay.resumable.events``
28+
* ``replay.server.battlelobby``
29+
* ``replay.smartcam.events``
30+
* ``replay.sync.events``
31+
* ``replay.sync.history``
32+
* ``replay.tracker.events``
33+
34+
To decode any further information you need to determine the build version with which the replay was recorded. You can do this by accessing the header information and extract the build version from it:
35+
36+
>>> contents = archive.header['user_data_header']['content']
37+
>>> from s2protocol import versions
38+
>>> header = versions.latest().decode_replay_header(contents)
39+
>>> baseBuild = header['m_version']['m_baseBuild']
40+
>>> protocol = versions.build(baseBuild)
41+
42+
43+
The variable ``protocol`` now contains a decoder for the replay specific game version. You can now decode the files inside the archive correctly. For example to decode the game events you can do:
44+
45+
>>> contents = archive.read_file('replay.game.events')
46+
>>> gameEvents = protocol.decode_replay_game_events(contents)
47+
48+
Or to get the ingame messages you can do:
49+
50+
>>> contents = archive.read_file('replay.message.events')
51+
>>> messageEvents = protocol.decode_replay_message_events(contents)
52+
53+
54+
Now you can perfom any operation you like on these objects. For example if you are just interested in ``NNet.Game.SCmdEvent`` you can filter the game events like this:
55+
56+
>>> cmdEventList = []
57+
>>> for event in gameEvents:
58+
>>> if event['_event'] == 'NNet.Game.SCmdEvent':
59+
>>> cmdEventList.append(event)
60+
61+
Of course you always have the option to export your current selection as JSON to a textfile and use other environments or tools for your exploratory data analysis. To export your data to a JSON file you can do:
62+
63+
>>> import json
64+
>>> json_data = json.dumps(cmdEventList, indent=4)
65+
>>> output_file = open("someJSONFile.json", "w")
66+
>>> output_file.write(json_data)
67+
>>> output_file.close()

0 commit comments

Comments
 (0)