chiark / gitweb /
Copyright notices
[ypp-sc-tools.db-live.git] / yarrg / web / dataage
1 %# This is part of the YARRG website.  YARRG is a tool and website
2 %# for assisting players of Yohoho Puzzle Pirates.
3 %#
4 %# Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
5 %# Copyright (C) 2009 Clare Boothby
6 %#
7 %#  YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
8 %#  The YARRG website is covered by the GNU Affero GPL v3 or later, which
9 %#   basically means that every installation of the website will let you
10 %#   download the source.
11 %#
12 %# This program is free software: you can redistribute it and/or modify
13 %# it under the terms of the GNU Affero General Public License as
14 %# published by the Free Software Foundation, either version 3 of the
15 %# License, or (at your option) any later version.
16 %#
17 %# This program is distributed in the hope that it will be useful,
18 %# but WITHOUT ANY WARRANTY; without even the implied warranty of
19 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 %# GNU Affero General Public License for more details.
21 %#
22 %# You should have received a copy of the GNU Affero General Public License
23 %# along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 %#
25 %# Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
26 %# are used without permission.  This program is not endorsed or
27 %# sponsored by Three Rings.
28 %#
29 %#
30 %# This Mason component generates the core of the `data age' query.
31 %#
32 <%once>
33 my $meta_prettyprint_age= sub {
34     my ($age,$floor,$plus) = @_;
35     return <<END;
36         $age < 60 ?             'less than a minute'                    :
37         $age < 60*2 ?           '1 minute'                              :
38         $age < 3600*2 ?         $floor ($age/60) $plus' minutes'        :
39         $age < 86400*2 ?        $floor ($age/3600) $plus ' hours'       :
40                                 $floor ($age/86400) $plus ' days';
41 END
42 };
43
44 my $prettyprint_age;
45 eval '
46   $prettyprint_age= sub {
47                 my ($age) = @_;
48                 '.$meta_prettyprint_age->('$age','floor','.').'
49   };
50 ' or die "$@";
51
52 </%once>
53
54 <%perl>
55
56 my $now= time;
57
58 my $row;
59 my $sth= $dbh->prepare("SELECT archipelago, islandid, islandname, timestamp
60                                 FROM uploads NATURAL JOIN islands
61                                 ORDER BY archipelago, islandid");
62 $sth->execute();
63
64 </%perl>
65
66 <script type="text/javascript">
67 da_pageload= Date.now();
68 da_ages= { };
69 function da_Refresh() {
70   var now= Date.now();
71   debug('updating now='+now);
72   for (var elid in da_ages) {
73     var el= document.getElementById(elid);
74     var oldage= da_ages[elid];
75     var age= oldage + (now - da_pageload) / 1000;
76     var newhtml= <% $meta_prettyprint_age->('age','Math.floor','+') %>
77 % if ($ARGS{debug}) {
78     if (elid == 'daid_loaded')
79       debug('element elid='+elid+' oldage='+oldage+' age='+age+': '+newhtml);
80 % }
81     el.innerHTML= newhtml;
82   }
83 }
84 </script>
85
86 <table>
87 <tr>
88 <th>Archipelago
89 <th>Island
90 <th>Age
91 </tr>
92 % while ($row=$sth->fetchrow_hashref) {
93 %       my $elid= "daid_$row->{'islandid'}";
94 %       my $age= $now - $row->{'timestamp'};
95 <tr> <td><% $row->{'archipelago'} |h
96  %>  <td><% $row->{'islandname'} |h
97  %>  <td id="<% $elid %>"><% $prettyprint_age->($age) %> </tr>
98 <script type="text/javascript"> da_ages['<% $elid %>']= <% $age %>; </script>
99 % }
100 </table>
101
102 <p>
103 Time since this page loaded:
104 <span id="daid_loaded">(not known; times above not updating)</span>
105
106 <form action="lookup" method="get">
107 % foreach my $a (keys %ARGS) {
108 <input type="hidden" name="<% $a |h %>" value="<% $ARGS{$a} |h %>">
109 % }
110 <input type=submit name=submit value="Reload">
111 </form>
112
113 <script type="text/javascript">
114 da_ages['daid_loaded']= 0;
115 window.onload= da_Refresh;
116 window.setInterval(da_Refresh, 10000);
117 </script>
118
119 <%init>
120 use POSIX;
121 use CommodsWeb;
122 my $dbh= dbw_connect('Midnight');
123 </%init>