Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Created July 10, 2019 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgestephanis/aa07687cd492958fc06c8c8731696344 to your computer and use it in GitHub Desktop.
Save georgestephanis/aa07687cd492958fc06c8c8731696344 to your computer and use it in GitHub Desktop.
<?php
define( 'MTGA_LANG', 'EN' );
define( 'MTGA_PATH', '/Applications/MTGArena.app/Contents/Resources/drive_c/Program Files/Wizards of the Coast/MTGA' );
define( 'MTGA_LOGS', MTGA_PATH . '/MTGA_Data/Logs/Logs' );
define( 'MTGA_DATA', MTGA_PATH . '/MTGA_Data/Downloads/Data' );
// Get all the logs!
$logs = glob( MTGA_LOGS . '/UTC_Log - * - 1.htm' );
$data_files = glob( MTGA_DATA . '/*.mtga' );
$data = array();
foreach ( $data_files as $data_file_path ) {
if ( preg_match( '#/Downloads/Data/data_(?<type>\w+?)_\w+.mtga$#i', $data_file_path, $matches ) ) {
$data[ $matches['type'] ] = $data_file_path;
}
}
$data_files = $data;
// Load all translations into `$__` so that we can use `$__[361]` to get `Goblin` in the appropriate language.
$locales = json_decode( file_get_contents( $data['loc'] ) );
$__ = null;
foreach ( $locales as $l ) {
if ( MTGA_LANG === $l->langkey ) {
foreach( $l->keys as $string ) {
$__[ $string->id ] = $string->text;
}
break;
}
}
unset( $l, $locales );
$abilities = json_decode( file_get_contents( $data['abilities'] ) );
$enums = json_decode( file_get_contents( $data['enums'] ) );
$cards = json_decode( file_get_contents( $data['cards'] ) );
$cards = array_column( $cards, null, 'grpid' );
$CardType = null;
$Color = null;
$CounterType = null;
$FailureReason = null;
$MatchState = null;
$Phase = null;
$ResultCode = null;
$Step = null;
$SubType = null;
$SuperType = null;
foreach ( $enums as $enum ) {
${$enum->name} = array_column( $enum->values, 'text', 'id' );
${$enum->name} = array_map( function( $x ){ return $GLOBALS['__'][$x]; }, ${$enum->name} );
// echo "\r\n$enum->name\r\n===\r\n";
// print_r( ${$enum->name} );
}
/*
function parseAbilities( $x ) {
$x->text = $GLOBALS['__'][ $x->textId ];
return $x;
}
foreach ( $cards as &$card ) {
$card->titleId = $__[ $card->titleId ];
$card->abilities = array_map( 'parseAbilities', $card->abilities );
}
unset $card;
*/
// Identify the latest log file!
$latest_logfile = array_shift( $logs );
$latest_filemtime = filemtime( $latest_logfile );
foreach ( $logs as $logfile ) {
if ( 10 * 1024 > filesize( $logfile ) ) {
continue;
}
if ( filemtime( $logfile ) > $latest_filemtime ) {
$latest_logfile = $logfile;
$latest_filemtime = filemtime( $latest_logfile );
}
}
echo "Found latest logfile: " . basename( $latest_logfile ) . "\r\n\r\n";
// Extract the data we care about from the logfile.
$player_inventory = null;
$player_id = null;
$player_cards = null;
$decks = null;
$logfile = fopen( $latest_logfile, 'r' );
while ( ! feof( $logfile ) ) {
$line = trim( fgets( $logfile ) );
if ( empty( $player_inventory ) && '<== PlayerInventory.GetPlayerInventory(' === substr( $line, 0, 39 ) ) {
$json = '';
while ( ! feof( $logfile ) ) {
$line = trim( fgets( $logfile ) );
if ( '}</div></div>' !== $line ) {
$json .= $line;
} else {
$player_inventory = json_decode( $json . '}' );
$player_id = $player_inventory->playerId;
break;
}
}
}
if ( empty( $player_cards ) && '<== PlayerInventory.GetPlayerCardsV' === substr( $line, 0, 35 ) ) {
echo "found ";
$json = '';
while ( ! feof( $logfile ) ) {
$line = trim( fgets( $logfile ) );
if ( '}</div></div>' !== $line ) {
$json .= $line;
} else {
$player_cards = json_decode( $json . '}' );
break;
}
}
}
if ( empty( $decks ) && '<== Deck.GetDeckListsV' === substr( $line, 0, 22 ) ) {
$json = '';
while ( ! feof( $logfile ) ) {
$line = trim( fgets( $logfile ) );
if ( ']</div></div>' !== $line ) {
$json .= $line;
} else {
$decks = json_decode( $json . ']' );
break;
}
}
}
// If we've got all we came for, let's blow this popcicle stand and go home!
if ( $player_inventory && $player_id && $player_cards && $decks ) {
break;
}
}
if ( $decks ) {
foreach ( $decks as $deck ) {
if ( 'precon' === $deck->format ) {
continue;
}
echo "Deck: $deck->name \r\n";
echo '==========' . "\r\n\r\n";
while ( $grpid = array_shift( $deck->mainDeck ) ) {
$qty = array_shift( $deck->mainDeck );
echo "{$qty}x " . $__[ $cards[ $grpid ]->titleId ] . "\r\n";
}
if ( $deck->sideboard ) {
echo "\r\nSideboard:\r\n";
echo '==========' . "\r\n\r\n";
while ( $grpid = array_shift( $deck->sideboard ) ) {
$qty = array_shift( $deck->sideboard );
echo "{$qty}x " . $__[ $cards[ $grpid ]->titleId ] . "\r\n";
}
}
echo "\r\n\r\n\r\n";
}
}
echo "Inventory:\r\n===\r\n";
foreach ( $player_cards as $grpid => $qty ) {
printf( "%dx %s\r\n", $qty, $__[ $cards[ $grpid ]->titleId ] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment