Monopoly City Streets Street Finder
Copyright Information
/* Copyright © 2009 Adam Wolenc
* Copying and distribution of this file, with or without modification,
* are permitted in any medium without royalty provided the copyright
* notice and this notice are preserved. This file is offered as-is,
* without any warranty.
*/
Either "Adam Wolenc" or "@adamuu" or both are valid for attribution purposes. Enjoy!
Source Code of listing.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Monopoly City Streets Data Viewer</title>
<?php
/* Copyright © 2009 Adam Wolenc
* Copying and distribution of this file, with or without modification,
* are permitted in any medium without royalty provided the copyright
* notice and this notice are preserved. This file is offered as-is,
* without any warranty.
*/
/* listing.php version 0.2 */
error_reporting(0);
include("mcs_site.php");
define(MULTIPLIER, 10000000);
//Parse GET data
//Parse google maps url if present. parse lat and lng
if (isset($_REQUEST{'tile_size'}))
$tile_size = (integer)($_REQUEST{'tile_size'});
else $tile_size = 50312;
if (isset($_REQUEST{'gmap'}) && $_REQUEST{'gmap'} != "") {
$gmap = $_REQUEST{'gmap'};
//http://maps.google.com/maps?f=q&source=s_q&hl=en&q=Benld,+Illinois&
//sll=37.0625,-95.677068&sspn=33.901528,79.013672&ie=UTF8
//&cd=1&geocode=FUmDVAIdb7ql-g&split=0&ll=39.094564,-89.80216
//&spn=0.032441,0.077162&z=14&iwloc=A
if (preg_match('/&ll=([0-9.-]+),([0-9.-]+)/', $gmap, $ll)) {
$lng = $ll[2]*MULTIPLIER;
$lat = $ll[1]*MULTIPLIER;
$do = true;
} else {
$parseError="Invalid Google maps url. Try a url like "
. "\"http://maps.google.com/maps?f=q&source=s_q"
. "&hl=en&q=Benld,+Illinois&sll=37.0625,-95.677068"
. "&sspn=33.901528,79.013672&ie=UTF8&cd=1&geocode=FUmDVAIdb7ql-g"
. "&split=0&ll=39.094564,-89.80216&spn=0.032441,0.077162&z=14&iwloc=A\"";
}
} else if (isset($_REQUEST{'lng'}) && isset($_REQUEST{'lat'})
&& $_REQUEST{'lng'} != "" && $_REQUEST{'lat'} != "" ) {
$lng = (integer)($_REQUEST{'lng'}*MULTIPLIER);
$lat = (integer)($_REQUEST{'lat'}*MULTIPLIER);
$do = true;
}
//2D array quicksort function borrowed from
// http://uk.php.net/manual/en/function.sort.php#50099
//Attribution: timc at hlyw dot com
function array_qsort (&$array, $column, $order='SORT_ASC', $first=0,
$last= -2)
{
// $array - the array to be sorted
// $column - index (column) on which to sort
// can be a string if using an associative array
// $order - SORT_ASC (default) for ascending or SORT_DESC for descending
// $first - start index (row) for partial array sort
// $last - stop index (row) for partial array sort
// $keys - array of key values for hash array sort
if (is_array($array)) {
$keys = array_keys($array);
if($last == -2) $last = count($array) - 1;
if($last > $first) {
$alpha = $first;
$omega = $last;
$key_alpha = $keys[$alpha];
$key_omega = $keys[$omega];
$guess = $array[$key_alpha][$column];
while($omega >= $alpha) {
if($order == 'SORT_ASC') {
while($array[$key_alpha][$column] < $guess) {$alpha++; $key_alpha
= $keys[$alpha]; }
while($array[$key_omega][$column] > $guess) {$omega--; $key_omega
= $keys[$omega]; }
} else {
while($array[$key_alpha][$column] > $guess) {$alpha++; $key_alpha
= $keys[$alpha]; }
while($array[$key_omega][$column] < $guess) {$omega--; $key_omega
= $keys[$omega]; }
}
if($alpha > $omega) break;
$temporary = $array[$key_alpha];
$array[$key_alpha] = $array[$key_omega]; $alpha++;
$key_alpha = $keys[$alpha];
$array[$key_omega] = $temporary; $omega--;
$key_omega = $keys[$omega];
}
array_qsort ($array, $column, $order, $first, $omega);
array_qsort ($array, $column, $order, $alpha, $last);
}
}
return $array;
}
//End of code borrowed from http://uk.php.net/manual/en/function.sort.php#50099
?>
</head>
<body>
<?php mcs_header(); //display menu bar ?>
<table width="85%">
<tr>
<th>
<form method="get" action="">Enter an Longitude and Latitude<sup>1</sup>:
Lng: <input type="text" name="lng"
value="<?php echo $lng/MULTIPLIER; ?>"> Lat: <input type="text"
name="lat" value="<?php echo $lat/MULTIPLIER; ?>"> <br>
-or- Enter a Google Maps URL<sup>2</sup> <input type="text"
name="gmap" value="" size="60"><input type="submit"
name="find_streets" value="Find Streets"></form>
</th>
</tr>
<tr>
<td><?php if ($do) {
//HTTP GET contained enough valid information to generate a results table
$querynum=0;
$streets = array(); //accumulation of all results goes in this array
//fetch a 5x5 grid surrounding desired lat and lng
for ($ai = -2; $ai<=2; $ai++) {
for ($bi = -2; $bi<=2; $bi++) {
$a = $lng + $ai*$tile_size;
$b = $lat + $bi*$tile_size;
$url = "http://www.monopolycitystreets.com/buy/getstreetstobuy?lng=$a&lat=$b";
$raw = file_get_contents($url);
//hack: sometimes o:null is not reported for free streets.
//add o:null if missing to avoid parse error later in the code
$corr = str_replace('"free","p"', '"free","o":"null","p"', $raw);
//hack: special characters escaped by \.
//Eliminate \ to avoid parse error later in code
$corr = str_replace('\\', '', $corr);
$s = split("[{},\\\\/:\"]+", $corr);
array_shift($s); //discard first entry
$streetset = array_chunk($s, 10, false);
array_pop($streetset); //discard last entry
//add result set to accumulation table
array_splice($streets, count($streets), 0, $streetset);
$querynum++;
if ($ai == 0 && $bi == 0 ) {
//for the center query only, fetch the name of one of the
// streets and display it
//display vacinity information
//http://www.monopolycitystreets.com/map/getstreet?id=
// -727043652%2F413477321
$id = $streetset[0][0] . "%2F" . $streetset[0][1];
$url= "http://www.monopolycitystreets.com/map/getstreet?id=$id";
$raw = file_get_contents($url);
//output is something like this:
//"re":"Guilford Connecticut","cy":"USA"
if (preg_match('/"n":"([^"]+)"/', $raw, $n)
&& preg_match('/"re":"([^"]+)","cy":"([^"]+)"/',
$raw, $recy)) {
$name = $n[1];
$re = $recy[1];
$cy = $recy[2];
echo "Vicinity of <i>$name, $re, $cy</i>.";
} else {
echo "Unknown region.";
}
}
}
}
//sort the whole array by column 9 (asking price)
array_qsort ($streets, 9, $order='SORT_DESC');
//compose output table free of duplicates
echo "<table border=\"1\">\n";
echo "<tr><th>Lng.</th><th>Lat.</th><th>Street Name</th><th>Owner</th>";
echo "<th>Price/1000</th></tr>\n";
foreach ($streets as $street) {
//for each row, compose a key based on street name, lng, and lat
//if this key has been seen before (according to $seen hashset),
// skip this row. Otherwise, display the row and
// enter this key into the $seen hashset.
//maintain $unique and $duplicate counts accordingly
$key=$street[1].$street[7].$street[0];
$link="http://www.monopolycitystreets.com/game.html#".$street[0]."z".$street[1];
if (!$seen{$key}) {
echo "<tr>";
echo "<td>".sprintf("%11.7f", $street[0]/MULTIPLIER)."</td>";
echo "<td>".sprintf("%11.7f", $street[1]/MULTIPLIER)."</td>";
echo "<td><a href=\"$link\">$street[3]</a></td>";
if ($street[7] == "null")
echo "<td bgcolor=\"#00ff00\"> </td>";
else echo "<td>$street[7]</td>";
echo "<td align=\"right\">$street[9]</td>";
echo "</tr>\n";
$seen{$key}=true;
$unique++;
} else {
$duplicate++;
}
}
echo "</table>\n";
echo "Tile Size: $tile_size Unique: $unique Duplicate: $duplicate<br />\n";
} else {
echo " ";
}?></td>
</tr>
<tr>
<td><?php if ($parseError) echo "$parseError<br>"; ?> <sup>1</sup>For
western longitudes, use a negative number. For southern latitudes, use
a negative number.<br>
Example: Mountain View, CA = Lng: "-122.072868" Lat: "37.402619"<br>
<sup>2</sup>If anything appears in the Google Maps URL box, the Lng
and Lat text boxes will be ignored.</td>
</tr>
</table>
<?php mcs_footer(); ?>
</body>
</html>
Site created by Adam Wolenc
Not affiliated with Monopoly®, which is a trademark of Hasbro, Inc.