Replay Module

Responsible for reading and writing replays in either the compact or complete replay format (see the replay format for details).

Recording a game

Recording a game is a matter of creating a game, calling :meth:record on that game, playing the game, and then saving the replay. For example:

game = create_a_game()                  # Create a game somehow
replay = record(game)                   # Create a replay that will track the game's moves
game.start()                            # Play the game
replay.write_json("my_replay.hsreplay") # Save the replay to a file

Playing back a game

Playing back a game is a matter of loading the replay, getting a game for playing it back, and then starting the game For example:

replay = Replay()                      # create a new replay object
replay.read_json("my_replay.hsreplay") # load the replay (this can be combined with the previous line)
game = playback(replay)                # create a game associated with the replay
game.start()                           # play the recorded game
class hearthbreaker.replay.Replay(filename=None)[source]

Bases: builtins.object

Encapsulates the data stored in a replay, along with functions to read and write replays. The data stored in this class can be used for either recording or playing back replays.

__init__(filename=None)[source]

Create a new Replay. This replay can be used for recording or playing back a game.

If the filename string is present, then this will also load the file located at filename for playback

Parameters:filename (string) – A string representing a filename for a replay file to load or None (the default). If present, it will load the selected replay and prepare it for playback. The replay file must be in the complete format
read(file)[source]

Read a replay in the compact format. This format is a series of directives, and isn’t as flexible or well structured as the json format (in :meth:write_json). For more info, see the replay format

Parameters:file (str or io.TextIOBase) – Either a string or an IO object. If a string, then it is assumed to be a filename describing where a replay file is to be found. If an IO object, then the IO object should be opened for reading.
read_json(file)[source]

Read a replay in the complete json format. This format is compatible with the netplay format, and is also designed to be more future proof. For more info, see the replay format

Parameters:file (str or io.TextIOBase) – Either a string or an IO object. If a string, then it is assumed to be a filename describing where a replay file is found. If an IO object, then the IO object should be opened for reading.
write(file)[source]

Write a replay in the compact format. This format is a series of directives, and isn’t as flexible or well structured as the json format (in :meth:write_json). For more info, see the replay format

Parameters:file (str or io.TextIOBase) – Either a string or an IO object. If a string, then it is assumed to be a filename describing where a replay file is to be written. If an IO object, then the IO object should be opened for writing.
write_json(file)[source]

Write a replay in the complete json format. This format is compatible with the netplay format, and is also designed to be more future proof. For more info, see the replay format

Parameters:file (str or io.TextIOBase) – Either a string or an IO object. If a string, then it is assumed to be a filename describing where a replay file should be written. If an IO object, then the IO object should be opened for writing.
hearthbreaker.replay.playback(replay)[source]

Create a game which can be replayed back out of a replay.

Parameters:replay (Replay) – The replay to load the game out of
Returns:A game which when played will perform all of the actions in the replay.
Return type:Game
hearthbreaker.replay.record(game)[source]

Ready a game for recording. This function must be called before the game is played.

Several methods of the game and its agents are modified. These modifications will not affect the operation of the game or its agents, although any further modifications to these methods will not be recorded.

Parameters:game (Game) – A game which has not been started
Returns:A replay that will track the actions of the game as it is played. Once the game is complete, this replay can be written to a file to remember the state of this game.
Return type:Replay