Last active
May 20, 2018 21:41
-
-
Save bhughes339/1c31b99f316c10b7ace57f6946b8e822 to your computer and use it in GitHub Desktop.
Tool that converts a csv-formatted MTGO collection to an importable binder of Penny Dreadful cards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$error = ''; | |
$files = $_FILES['uploaded_file']; | |
if (!empty($files['tmp_name'])) { | |
$found = preg_match('/(.*)\.csv$/', $files['name'], $file_matches); | |
if (!$found) { | |
$error = 'Error: Wrong format. Please upload a .csv file.'; | |
} else { | |
$binder_name = $file_matches[1]; | |
$tmpfile = $files['tmp_name']; | |
$contents = explode("\n", file_get_contents($tmpfile)); | |
$header = array_shift($contents); | |
if (!preg_match('/Card Name,Quantity,ID #,Rarity,Set,Collector #,Premium/', $header)) { | |
$error = 'Error: Not a valid MTGO binder file.'; | |
} else { | |
$is_deck = (trim(explode(",", $header)[7])) ? true : false; | |
$removed = []; | |
if ($is_deck) { | |
$removed['main'] = []; | |
$removed['side'] = []; | |
} | |
$output = $header . "\n"; | |
$legal_cards = explode("\n", file_get_contents('http://pdmtgo.com/legal_cards.txt')); | |
foreach ($contents as $line) { | |
$fields = str_getcsv(trim($line), ",", '"'); | |
$cardname = $fields[0]; | |
if (in_array($cardname, $legal_cards)) { | |
$output .= $line . "\n"; | |
} else { | |
if ($is_deck) { | |
$board = ($fields[7] == 'No') ? 'main' : 'side'; | |
if (! in_array($cardname, $removed[$board])) { | |
$removed[$board][] = $cardname; | |
} | |
} else { | |
if (! in_array($cardname, $removed)) { | |
$removed[] = $cardname; | |
} | |
} | |
} | |
} | |
if ($is_deck) { | |
sort($removed['main']); | |
sort($removed['side']); | |
} else { | |
sort($removed); | |
} | |
} | |
} | |
} elseif ($_POST['output']) { | |
$binderout = base64_decode($_POST['output']); | |
$binder_name = $_POST['bindername']; | |
header('Content-Description: File Transfer'); | |
header('Content-Type: application/octet-stream'); | |
header("Content-Disposition: attachment; filename=\"${binder_name}-pd.csv\""); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate'); | |
header('Pragma: public'); | |
header('Content-Length: ' . strlen($binderout)); | |
echo $binderout; | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Penny Dreadful Binder Generator</title> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" | |
crossorigin="anonymous"> | |
<style> | |
body { | |
margin: 10px; | |
} | |
th { | |
text-decoration: underline; | |
} | |
</style> | |
</head> | |
<body> | |
<p>Upload your CSV-formatted MTGO trade binder or deck to create a copy that contains only Penny Dreadful legal cards.</p> | |
<p><strong>How to export your MTGO collection:</strong></p> | |
<ol> | |
<li>Find your "Full Trade List" binder from the Collection tab in MTGO</li> | |
<li>Right click the binder and select "Export"</li> | |
<li>Select "Spreadsheet CSV format (*.csv)" in the Save As Type dropdown</li> | |
</ol> | |
</p>Import the downloaded file as a new MTGO binder creating a binder and clicking "Import".</p> | |
<form enctype="multipart/form-data" action="" method="POST"> | |
<div class="submit"> | |
<input type="file" accept=".csv" name="uploaded_file" id="selectedFile" style="display: none;" onchange="this.form.submit();"/> | |
<input type="button" class="btn btn-primary" value="Choose File" onclick="document.getElementById('selectedFile').click();" /> | |
<?php | |
if ($output) { | |
$bindertype = ($is_deck) ? 'Deck' : 'Binder'; | |
echo "<span style=\"color:black;\">${bindertype}: <strong>${binder_name}</strong></span>"; | |
} else { | |
echo "<span style=\"color:red;\">${error}</span>"; | |
} | |
?> | |
</div> | |
</form> | |
<?php | |
if ($output) { | |
$b64out = base64_encode($output); | |
echo '<br /><form enctype="multipart/form-data" action="" method="POST">'; | |
echo "<input type=\"hidden\" name=\"output\" value=\"${b64out}\" />"; | |
echo "<input type=\"hidden\" name=\"bindername\" value=\"${binder_name}\" />"; | |
echo '<input type="submit" class="btn btn-success" value="Download Penny Binder" /></form>'; | |
echo "<br /><h5 style=\"color:red;\">Removed cards:</h5><table>"; | |
if ($is_deck) { | |
if ($removed['main']) { | |
echo "<tr><th>Mainboard</th></tr>"; | |
foreach ($removed['main'] as $card) { | |
echo "<tr><td>${card}</td></tr>"; | |
} | |
} | |
if ($removed['side']) { | |
echo "<tr><th>Sideboard</th></tr>"; | |
foreach ($removed['side'] as $card) { | |
echo "<tr><td>${card}</td></tr>"; | |
} | |
} | |
} else { | |
foreach ($removed as $card) { | |
echo "<tr><td>${card}</td></tr>"; | |
} | |
} | |
echo "</table>"; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment