Permalink
Browse files

Add places page and supporting code (perl+js)

  • Loading branch information...
1 parent 5926d3e commit 1dad8de1dd9effb49aaad45379d1d20717b3a824 @castaway committed Apr 25, 2012
View
17 geotrader.cgi
@@ -110,8 +110,23 @@ sub dispatch_request {
## Place page for a specific card
## Recheck user is logged in, that latest coords are near it, and latest coords were updated > XX min ago.
- sub (GET + /cards/*) {
+ sub (GET + /card/*) {
my ($self, $card_desc) = @_;
+
+ my ($id, $desc) = $card_desc =~ /^(\d+)-([\w\s])+/;
+ print STDERR "Looking for card: $id, $desc\n";
+ my $place = $self->model->find_place($id);
+
+ print STDERR "Found card: ", $place->id, "\n";
+ return [200, ['Content-type', 'text/html' ], [$self->view->place_page($place, $user) ]];
+ },
+
+ sub (POST + /_take_card + %card_id=) {
+ my ($self, $card_id) = @_;
+
+ my $result = $self->model->take_card($card_id, $user);
+
+ return [200, ['Content-type', 'application/json' ], [ encode_json($result) ]];
}
}
View
6 gp.css
@@ -17,7 +17,7 @@ ul#minimenu, ul#login {
display: inline;
}
-.table {
- display: table; /* Allow the centering to work */
- margin: 0 auto;
+#large_photo {
+ width: 600px;
+ height: 400px;
}
View
33 gp.js
@@ -33,6 +33,27 @@ GP.log = function(message) {
}
}
+if(!GP.update_take_button) {
+ GP.update_take_button = function(card_state) {
+ console.log(card_state);
+ alert(card_state);
+ };
+}
+
+if(!GP.take_card) {
+ GP.take_card = function() {
+ // animate card?
+
+ jQuery.post(
+ '/cgi-bin/geotrader.cgi/_take_card',
+ { card_id: GP.current_card.id },
+ GP.update_take_button
+ );
+
+ // tiemout
+ };
+}
+
// Everytime the GPS reports a new location, redraw the users position on the
// map, and re-centre the map to that position.
// If settings.following is false, only store the current position, don't update the map.
@@ -178,7 +199,9 @@ GP.on_feature_select = function (event) {
"featurePopup",
feature.geometry.getBounds().getCenterLonLat(),
new OpenLayers.Size(100,100),
- "<h2>"+feature.attributes.title + "</h2>"
+ "<h2>"+feature.attributes.title +
+ " (" + feature.attributes.id + ")" +
+ "</h2>"
+ feature.attributes.description
,null, true, GP.on_popup_close);
feature.popup = GP.popup;
@@ -307,5 +330,13 @@ jQuery(document).ready(function() {
jQuery('#toggle_following').change(function() {
GP.settings.following = !GP.settings.following;
});
+
+ // Do we "reserve" the card while the user is looking at the page
+ // in case of multiple people standing here?
+ jQuery('#take_card').click(function() {
+ // ajax, plus replace button, some sorta animation to "hand"?
+ GP.take_card();
+ return false;
+ });
});
View
2 lib/GeoTrader/Schema/Result/Card.pm
@@ -37,6 +37,8 @@ __PACKAGE__->add_columns(
__PACKAGE__->set_primary_key('id');
__PACKAGE__->has_many('tags', 'GeoTrader::Schema::Result::Tag', 'card_id');
+__PACKAGE__->has_many('user_cards', 'GeoTrader::Schema::Result::UserCards', 'card_id');
+
# has an osm / origin ID?
'collected';
View
2 lib/GeoTrader/Schema/Result/User.pm
@@ -30,7 +30,7 @@ __PACKAGE__->add_columns(
__PACKAGE__->set_primary_key('id');
__PACKAGE__->add_unique_constraint('username' => ['username']);
-__PACKAGE__->has_many('cards', 'GeoTrader::Schema::Result::UserCards', 'user_id');
+__PACKAGE__->has_many('user_cards', 'GeoTrader::Schema::Result::UserCards', 'user_id');
__PACKAGE__->might_have('current_latlon', 'GeoTrader::Schema::Result::UserLatLon', 'user_id',);
View
32 lib/GoingPlaces/Model.pm
@@ -7,6 +7,7 @@ use Data::Dumper;
use GeoTrader::Schema;
use Template;
use Path::Class;
+use Geo::Ellipsoid;
use URI::Escape;
use Authen::Passphrase::SaltedDigest;
use Moo;
@@ -22,6 +23,37 @@ sub _build_schema {
}
sub find_place {
+ my ($self, $id) = @_;
+
+ return $self->schema->resultset('Card')->find({ id => $id });
+}
+
+sub take_card {
+ my ($self, $id, $user) = @_;
+
+ my $card = $self->schema->resultset('Card')->find({ id => $id });
+ return { error => 'No such card' } if(!$card);
+
+ my $earth = Geo::Ellipsoid->new(ell => 'WGS84',
+ units => 'degrees',
+ # 1 -- symmetric -- -180..180
+ longitude => 1,
+ bearing => 1,
+ );
+ # How far is the (last-known-location) of the user from the cards loc?
+ ## This should account for last update time of loc too!
+ my $dist = $earth->range($card->location_lat, $card->location_lon,
+ $user->current_latlon->latitude,
+ $user->current_latlon->longitude);
+
+ ## Magic number!
+ if($dist > 35) {
+ return { error => "Too far away ($dist)"};
+ }
+
+ $user->user_cards->create({card_id => $card->id});
+
+ return { $card->get_columns };
}
View
16 lib/GoingPlaces/View.pm
@@ -29,14 +29,26 @@ sub place_page {
my ($self, $card_row, $user_row) = @_;
# pre-template data mungings
- my $has_card = $user_row && $user_row->user_cards_rs->find({ $card_row->id });
-
+ my $has_card = $user_row && $user_row->user_cards_rs->search({ card_id => $card_row->id })->count;
+ my $is_here = $user_row && 1; # check coords last updated against card coords!
+ my $cards_remaining = $card_row->max_available - $card_row->user_cards_rs->count;
+
+ ## Will splodey if there is no "amenity" value
+ ## Ideally we display a "pub" image?
+ my $is_a = $card_row->tags_rs->find({ key => 'amenity'})->value();
+
+ ## Do we "reserve" the card while the user is looking at the page
+ ## in case of multiple people standing here?
my $output = '';
$self->tt->process('place_page.tt',
{
+ static_uri => $self->static_uri,
+ base_uri => $self->base_uri,
user => $user_row,
card => $card_row,
has_card => $has_card,
+ is_here => $is_here,
+ cards_remaining => $cards_remaining,
},
\$output )
|| die $self->tt->error;
View
3 templates/map_page.tt
@@ -1,10 +1,11 @@
+[% SET page_name = 'Map' %]
+
<div id="log" style="display:block; height: 50px; overflow: scroll"></div>
<div id="stats" style="" ></div>
<!-- <div class="table"> -->
<ul id="minimenu">
<li><input type="checkbox" id="toggle_tracking" checked="checked" /> Toggle tracking</li>
<li><input type="checkbox" id="toggle_following" checked="checked" /> Toggle following</li>
-[% SET page_name = 'Map' %]
<li><input type="button" id="profile" value="View Profile"></li>
</ul>
<ul id="login">
View
35 templates/place_page.tt
@@ -0,0 +1,35 @@
+[% SET page_name = 'Place ' _ card.name %]
+ <script>
+ // card.json?
+ GP.current_card = { id: [% card.id %], name: '[% card.name %]' };
+ </script>
+ <div>
+ <ul id="minimenu">
+ <li><a href="[% base_uri %]/map">Back to map</a></li>
+ <li><input type="checkbox" id="toggle_tracking" checked="checked" /> Toggle tracking</li>
+ <li><input type="button" id="profile" value="View Profile"></li>
+ </ul>
+ </div>
+ <div style="clear: both;"></div>
+ <h3>[% card.name %]</h3>
+ <div id="large_photo"> <!-- make div photo size / fill space with $category default? -->
+ [% IF card.photo %]
+ <img src="[% static_uri %]/photos/[% card.photo %]" alt="[% card.name %]">
+ [% END %]
+ </div>
+ <div id="user_status">
+ [% IF !user %]
+ ## login form here ## Login to pick up this card
+ [% ELSIF has_card %]
+ You are carrying this card <input type="button" value="drop card" >
+ [% ELSIF cards_remaining && is_here %]
+ <input id="take_card" type="button" value="take card ">
+ [% ELSIF is_here %]
+ Sorry, there are no more cards available. You can try and trade with ## list of people who have the card ##
+ [% ELSE %]
+ Visit this location to see if any cards are available.
+ [% END %]
+ </div>
+ <div id="achievements">
+ </div>
+

0 comments on commit 1dad8de

Please sign in to comment.