chiark / gitweb /
46b32e6763ebb25d2b75f0f1a3a6670b63caf1a2
[ypp-sc-tools.web-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 our $self_url;
47 our $base_url;
48
49 BEGIN {
50     use Exporter ();
51     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
52     $VERSION     = 1.00;
53     @ISA         = qw(Exporter);
54     @EXPORT      = qw(&dbw_connect &ocean_list $sourcebasedir
55                       to_json to_json_protecttags);
56     %EXPORT_TAGS = ( );
57
58     @EXPORT_OK   = qw();
59 }
60
61 our $datadir='.';
62 our $sourcebasedir;
63
64 for my $dir (@INC) {
65     if ($dir =~ m/\.perl-lib$/) {
66         $sourcebasedir= "$dir/..";
67         if (stat "$dir/DATA") {
68             $datadir= "$dir/DATA";
69         } elsif ($!==&ENOENT) {
70             $datadir= "$dir";
71         } else {
72             die "stat $dir/DATA $!";
73         }
74         last;
75     }
76 }
77 defined $sourcebasedir or
78     die "no source base dir in @INC";
79
80 my @ocean_list;
81
82 sub ocean_list () {
83     if (!@ocean_list) {
84         my $fn= "$datadir/master-info.txt";
85         my $f= new IO::File $fn or die $!;
86         my @r;
87         while (<$f>) {
88             next unless m/^ocean\s+(\S.*\S)\s*$/;
89             push @r, $1;
90         }
91         $f->error and die $!;
92         close $fn;
93         @ocean_list= @r;
94     }
95     return @ocean_list;
96 }
97
98 sub dbw_connect ($) {
99     my ($ocean) = @_;
100     die "unknown ocean $ocean ?"
101         unless grep { $_ eq $ocean } ocean_list();
102     return dbr_connect($datadir, $ocean);
103 }
104
105 sub to_json_protecttags ($) {
106     my ($v) = @_;
107     my $j= to_json($v);
108     $j =~ s,/,\\/,g;
109     return $j;
110 }
111
112 1;