Skip to content

Commit

Permalink
Fixed formatting bugs, typo in database_updates.sql, updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
SteGriff committed Sep 24, 2013
1 parent 728eb2a commit 04d15bc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
11 changes: 3 additions & 8 deletions DAL.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$CardTable = "mtg_cards";
$CardTable = 'mtg_cards';

function sqlString($s, $db){
return trim($db->real_escape_string($s));
Expand Down Expand Up @@ -28,7 +28,7 @@ function DB_insert_card($name, $mana_cost, $converted_mana_cost, $types, $card_t
return $id;
}
else {
echo " [[ " . $db->error . " ]] ";
echo " [[ {$db->error} ]] ";
return false;
}
}
Expand All @@ -47,12 +47,7 @@ function SQL_create_card($name, $mana_cost, $converted_mana_cost, $types, $card_
$rarity = sqlString($rarity, $db);
$card_number = sqlString($card_number, $db);
$artist = sqlString($artist, $db);

/*
$card_text = addslashes($card_text);
$flavor_text = addslashes($flavor_text);
*/


$SQL = "insert into $CardTable(name, mana_cost, converted_mana_cost, types, card_text, flavor_text, power_toughness, expansion, rarity, card_number, artist)
values( '$name', '$mana_cost', '$converted_mana_cost', '$types', '$card_text', '$flavor_text', '$power_toughness', '$expansion', '$rarity', '$card_number', '$artist' );";
//echo $SQL;
Expand Down
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ Popular MTG card references, such as Gatherer, feature human-readable informatio
##Features
* Search for cards by name
* Returns JSON object of card (see API)
* 2.5 seconds to initially fetch a card, relying on Wizards' Gatherer
* 2-10 seconds to initially fetch a card, relying on Wizards' Gatherer
* 0.01 seconds to return a card from database once cached

##Setup
**Requires PHP5 with allow_url_fopen**
Put your database connection information in db.php and install all the files together in a directory. `index.php` links all the files together, so you can see the required files there.
**Requires PHP5 with `allow_url_fopen`**
Put your database connection information in `db.php` and install all the files together in a directory. `index.php` links all the files together, so you can see the required files there.

##API
For demonstration, my installation is kept at [http://stegriff.co.uk/host/magic](http://stegriff.co.uk/host/magic)
For demonstration, my installation is kept at [http://stegriff.co.uk/host/magic](http://stegriff.co.uk/host/magic).
You can try it out using the [magic-api interface](http://stegriff.co.uk/host/magic/interface).
If you are building an app using **magic-api** as a server, I recommend hosting your own instance.

###Parameters
`magic?name=black lotus` or `magic?name="black lotus"` to search for a card.
`magic?name=boros recruit&metrics=true` to find out whether that card was from the cache or whether its cache insertion was successful
`magic?name=boros recruit&metrics=true` to find out whether that card was from the cache or whether its cache insertion was successful.

###Returned
**magic-api** returns a JSON object, with the following possible fields. Fields with no value will not be in the returned object.
Expand All @@ -39,19 +41,23 @@ For demonstration, my installation is kept at [http://stegriff.co.uk/host/magic]
from_cache^,
into_cache^,
request_time,
error

^Fields only returned when metrics is turned on in the query string.
^Fields only returned when metrics is turned on in the query string.

###Card encoding
####Icons
Icons are encoded with {Braces}, including mana costs, `{2}{Blue}`, and the tap icon, `{Tap}`. Multi-coloured mana is represented like `{Red or White}`. These values are automatically extracted from the `alt` attribute of any images inside a field on Gatherer.
Icons are encoded with {Braces}, like mana costs (`{2}{U}`), and the tap icon,
`{Tap}`. Single coloured mana is shortened to W/U/B/R/G as is standard
(U is blue). Multi-coloured mana is represented like `{Red or White}`. These values are automatically extracted from the `alt` attribute of any images inside a field on Gatherer, and then mana symbols are shortened. `{Variable Colorless}` is also shortened, to `{X}`.

####Line breaks
Lines of card text are ended by an underscore flanked by spaces, for example:

"card_text": "{Tap}: Exile target land card from a graveyard. Add one mana of any color to your mana pool. _ {Black}, {Tap}: Exile target instant or sorcery card from a graveyard. Each opponent loses 2 life. _ {Green}, {Tap}: Exile target creature card from a graveyard"
####Escaping
* The final output has been passed through `add_slashes()`, so single and double quotes appear with a backslash.
* Carriage returns in flavor text currently appear as `\r`. This is considered a bug, and should be encoded the same as card text in a later version.
* Single and double quotes appear with a backslash.
* Carriage returns (`\r`) in flavor text are replaced with a space.
* The em-dash in creature type is a Unicode character but should be rendered correctly as an em-dash by your JSON parser. I recommend JSONview for Firefox or Chrome to view the returned values from **magic-api**.

-----
Expand Down
4 changes: 2 additions & 2 deletions card_extractors.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
function get_line_content($l){
//cardtextbox divs represent seperate lines, which we'll represent with underscores
$v = str_replace('</div><div class=\"cardtextbox\">', ' _ ', $l);
$v = str_replace("</div><div class=\"cardtextbox\">", ' _ ', $l);

//Replace all images of icons with their alt text
$v = preg_replace("#\<img[ a-zA-Z0-9/\.\?=&;\"]+alt=\"([\w ]+)\"[ a-zA-Z0-9/\.\?=&;\"]+\>#", '{$1}', $v);

Expand Down
2 changes: 1 addition & 1 deletion database_updates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ alter table mtg_cards
modify column name varchar(141) not null

alter table mtg_cards
modify column name varchar(500) not null
modify column card_text varchar(500) not null
16 changes: 13 additions & 3 deletions interface.htm
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
}
#cardImage {
float: right;
max-width: 40%;
}
input#name{
width: 50%;
width: 40%;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
Expand Down Expand Up @@ -43,6 +44,9 @@
var mcInfoExpansion = "";
//TODO Add more data here
switch (magicApiExpansionName){
case "Magic 2013 Core Set":
mcInfoExpansion = 'm14';
break;
case "Return to Ravnica":
mcInfoExpansion = "rtr";
break;
Expand All @@ -52,6 +56,12 @@
case "Dragon's Maze":
mcInfoExpansion = 'dgm';
break;
case "Magic 2014 Core Set":
mcInfoExpansion = 'm14';
break;
case "Theros":
mcInfoExpansion = 'ths';
break;
}
var url = baseUrl + mcInfoExpansion + "/" + cardId + ".jpg";
console.log(url);
Expand All @@ -63,8 +73,8 @@
</head>
<body>

<h1>Magic Card Retriever</h1>
<img id="cardImage">
<h1>Magic Card Retriever</h1>
<h2 id="status"></h2>

<form action="#">
Expand All @@ -75,6 +85,6 @@ <h2 id="status"></h2>

<dl id="results"></dl>

<p><small>Card images only retrieved for RtR, GTC & DGM. Please modify Javascript getImage() to reach more images.</small></p>
<p><small>Card images only retrieved for cards in standard. Modify Javascript getImage() to reach more images.</small></p>
</body>
</html>

0 comments on commit 04d15bc

Please sign in to comment.