MP Fab Farts
<?
session_start();
$title = 'MP Fab Farts'; $script = 'fabfarts.js'; $style = 'fabfarts.css';
require 'site.inc'; ?>
<p>You know you love it, and it's just a bit of fun (as for the title,
<em>think about it...</em>). This is an example of the
<a href="http://www.theyworkforyou.com/api/">TheyWorkForYou API</a>,
that I wrote. <em>Currently requires JavaScript (to reveal the card and
so on) and cookies (to remember which cards you have), sorry.</em>
You can also <a href="./?start=1">restart with a new deck of cards</a>,
or play the <a href="two-player/">two player version</a>.</p>
<?
if (isset($_GET['start']) || !isset($_SESSION['pack1']) || !isset($_SESSION['pack2'])) {
$mps = cache('getMPs?output=php', 365);
shuffle($mps);
$_SESSION['pack1'] = array_slice($mps, 0, 20);
$_SESSION['pack2'] = array_slice($mps, 20, 20);
}
$win = isset($_POST['win']) ? $_POST['win'] : null;
if ($win) {
if ($win==1) {
$top = array_shift($_SESSION['pack1']);
if ($top) $_SESSION['pack1'][] = $top;
$top = array_shift($_SESSION['pack2']);
if ($top) $_SESSION['pack1'][] = $top;
} elseif ($win==2) {
$top = array_shift($_SESSION['pack2']);
if ($top) $_SESSION['pack2'][] = $top;
$top = array_shift($_SESSION['pack1']);
if ($top) $_SESSION['pack2'][] = $top;
} elseif ($win==-1) {
$top = array_shift($_SESSION['pack1']);
if ($top) $_SESSION['pack1'][] = $top;
$top = array_shift($_SESSION['pack2']);
if ($top) $_SESSION['pack2'][] = $top;
}
}
$mp1 = isset($_SESSION['pack1'][0]) ? $_SESSION['pack1'][0] : null;
$mp2 = isset($_SESSION['pack2'][0]) ? $_SESSION['pack2'][0] : null;
if (!$mp1) { ?>
<p id="instruction">You have lost! <a href="./?start=1">Play again</a></p>
<? } elseif (!$mp2) { ?>
<p id="instruction">You have won! <a href="./?start=1">Play again</a></p>
<? } else { ?>
<form method="post" action="./">
<p id="instruction">Pick a category:</p>
</form>
<?
if ($mp1) output($mp1, 1);
print '<p id="vs">vs</p>';
if ($mp2) output($mp2, 2);
}
footer();
function cache($uri, $days=7) {
$cache = 'cache/' . md5($uri);
if (is_file($cache) && time()-filemtime($cache)<86400*$days)
return unserialize(file_get_contents($cache));
$data = file_get_contents('http://www.theyworkforyou.com/api/' . $uri . '&key=Gbr9QgCDzHExFzRwPWGAiUJ5');
$fp = fopen($cache, 'w');
if ($fp) {
fwrite($fp, $data);
fclose($fp);
}
return unserialize($data);
}
function output($mp, $card) {
# Firstly, the data from getMPs
$pid = $mp['person_id'];
$name = $mp['name'];
$party = $mp['party'];
$const = $mp['constituency'];
# Find out if they have an image using getMP
$data = cache('getMP?output=php&id='.$pid);
$image = isset($data[0]['image']) ? $data[0]['image'] : null;
$member_id = $data[0]['member_id'];
# All other stats from getMPinfo
$data = cache('getMPInfo?output=php&id='.$pid);
$which_mid = 0;
foreach ($data['by_member_id'] as $k => $v) {
if ($v['member_id'] == $member_id) $which_mid = $k;
}
$categories = array(
'comments_on_speeches' => 'Comments on speeches',
'debate_sectionsspoken_inlastyear' => 'Debates spoken in',
'three_word_alliterations' => 'Use of three word alliterations',
'wrans_asked_inlastyear' => 'Written questions asked',
'writetothem_responsiveness_mean_2005' => 'WriteToThem responsiveness',
'writetothem_sent_2005' => 'WriteToThem messages'
);
$mid_categories = array(
'swing_to_lose_seat_today' => 'Swing to lose seat today',
'public_whip_division_attendance' => 'Division attendance',
'public_whip_rebellions' => 'Rebelliousness',
'majority_in_seat' => 'Majority'
);
?>
<div class="card" id="card<?=$card?>">
<p style="font-size: 83%; margin:0; float: right"><?=count($_SESSION['pack'.$card]) ?> cards</p>
<? if ($card==2) print '<div id="cardhidden">'; ?>
<h2><?=$name ?></h2>
<p style="margin-top:0"><?="$const, $party" ?></p>
<p id="picture">
<? if ($image) {
print '<img align="middle" src="http://www.theyworkforyou.com'.$image.'">';
} else {
print '<em>No picture</em>';
}
print '</p> <ul>';
foreach ($categories as $cat => $pretty) {
print '<li id="'.$cat.$card.'">';
if ($card==1) print '<span class="click" onclick="reveal(\''.$cat.'\'); return false;">';
print "$pretty: ";
if (isset($data[$cat])) print $data[$cat];
else print '<em>n/a</em>';
if ($card==1) print '</span>';
}
foreach ($mid_categories as $cat => $pretty) {
print '<li id="'.$cat.$card.'">';
if ($card==1) print '<span class="click" onclick="reveal(\'' . $cat . '\'); return false;">';
print "$pretty: ";
if (isset($data['by_member_id'][$which_mid][$cat])) print $data['by_member_id'][$which_mid][$cat];
else print '<em>n/a</em>';
if ($card==1) print '</span>';
}
print '</ul>';
if ($card==2) print '</div>';
?>
</div>
<?
}
Navigation