chiark / gitweb /
Merge branch 'web' of /u/ijackson/things/ypp-sc-tools
[ypp-sc-tools.db-live.git] / yarrg / CommodsWeb.pm
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 Perl module is used by the Mason scripts in yarrg/web/.
31 # We look for a symlink DATA to the actual data to use, so that
32 # the data uploader and website displayer can use different code.
33
34 package CommodsWeb;
35
36 use strict;
37 use warnings;
38
39 use DBI;
40 use POSIX;
41 use JSON;
42
43 use Commods;
44 use CommodsDatabase;
45
46 BEGIN {
47     use Exporter ();
48     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
49     $VERSION     = 1.00;
50     @ISA         = qw(Exporter);
51     @EXPORT      = qw(&dbw_connect &ocean_list &sourcebasedir
52                       &to_json_shim &to_json_protecttags
53                       &set_ctype_utf8
54                       &prettyprint_age &meta_prettyprint_age);
55     %EXPORT_TAGS = ( );
56
57     @EXPORT_OK   = qw();
58 }
59
60 sub sourcebasedir () {
61     my $dir;
62     
63     for my $dir (@INC) {
64         if ($dir =~ m/\.perl-lib$/) {
65             return "$dir/..";
66         }
67     }
68     die "no source base dir in @INC";
69     return $dir;
70 }
71
72 sub datadir () {
73     my $dir= sourcebasedir();
74     if (stat "$dir/DATA") {
75         return "$dir/DATA";
76     } elsif ($!==&ENOENT) {
77         return "$dir";
78     } else {
79         die "stat $dir/DATA $!";
80     }
81     return '.';
82 }
83
84 my @ocean_list;
85
86 sub ocean_list () {
87     my $datadir= datadir();
88     if (!@ocean_list) {
89         my $fn= "$datadir/master-info.txt";
90         my $f= new IO::File $fn or die $!;
91         my @r;
92         while (<$f>) {
93             next unless m/^ocean\s+(\S.*\S)\s*$/;
94             push @r, $1;
95         }
96         $f->error and die $!;
97         close $fn;
98         @ocean_list= @r;
99     }
100     return @ocean_list;
101 }
102
103 sub dbw_connect ($) {
104     my ($ocean) = @_;
105     die "unknown ocean $ocean ?"
106         unless grep { $_ eq $ocean } ocean_list();
107     return dbr_connect(datadir(), $ocean);
108 }
109
110 sub to_json_shim ($) {
111     my ($obj) = @_;
112     # In JSON.pm 2.x, jsonToObj prints a warning to stderr which
113     # our callers don't like at all.
114     if ($JSON::VERSION >= 2.0) {
115         return to_json($obj);
116     } else {
117         return objToJson($obj);
118     }
119 }
120
121 sub to_json_protecttags ($) {
122     my ($v) = @_;
123     my $j= to_json_shim($v);
124     $j =~ s,/,\\/,g;
125     return $j;
126 }
127
128 sub meta_prettyprint_age ($$$) {
129     my ($age,$floor,$plus) = @_;
130     return <<END;
131         $age < 60 ?             'less than a minute'                    :
132         $age < 60*2 ?           '1 minute'                              :
133         $age < 3600*2 ?         $floor ($age/60) $plus' minutes'        :
134         $age < 86400*2 ?        $floor ($age/3600) $plus ' hours'       :
135                                 $floor ($age/86400) $plus ' days';
136 END
137 };
138
139 BEGIN { eval '
140   sub prettyprint_age ($) {
141                 my ($age) = @_;
142                 '.meta_prettyprint_age('$age','floor','.').'
143   };
144   1;
145 ' or die "$@";
146 }
147
148
149 1;