chiark / gitweb /
f82e09cb2de18fad2d1ec7db7a195516b9e4c264
[ypp-sc-tools.web-live.git] / yarrg / CommodsWeb.pm
1 # This is part of ypp-sc-tools, a set of third-party tools for assisting
2 # players of Yohoho Puzzle Pirates.
3 #
4 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
20 # are used without permission.  This program is not endorsed or
21 # sponsored by Three Rings.
22
23 # This package is used by the Mason scripts in yarrg/web/.
24 # We look for a symlink DATA to the actual data to use, so that
25 # the data uploader and website displayer can use different code.
26
27 package CommodsWeb;
28
29 use strict;
30 use warnings;
31
32 use DBI;
33 use POSIX;
34
35 use Commods;
36 use CommodsDatabase;
37
38 our $self_url;
39 our $base_url;
40
41 BEGIN {
42     use Exporter ();
43     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
44     $VERSION     = 1.00;
45     @ISA         = qw(Exporter);
46     @EXPORT      = qw(&dbw_connect &ocean_list $sourcebasedir);
47     %EXPORT_TAGS = ( );
48
49     @EXPORT_OK   = qw();
50 }
51
52 our $datadir='.';
53 our $sourcebasedir;
54
55 for my $dir (@INC) {
56     if ($dir =~ m/\.perl-lib$/) {
57         $sourcebasedir= "$dir/..";
58         if (stat "$dir/DATA") {
59             $datadir= "$dir/DATA";
60         } elsif ($!==&ENOENT) {
61             $datadir= "$dir";
62         } else {
63             die "stat $dir/DATA $!";
64         }
65         last;
66     }
67 }
68 defined $sourcebasedir or
69     die "no source base dir in @INC";
70
71 my @ocean_list;
72
73 sub ocean_list () {
74     if (!@ocean_list) {
75         my $fn= "$datadir/master-info.txt";
76         my $f= new IO::File $fn or die $!;
77         my @r;
78         while (<$f>) {
79             next unless m/^ocean\s+(\S.*\S)\s*$/;
80             push @r, $1;
81         }
82         $f->error and die $!;
83         close $fn;
84         @ocean_list= @r;
85     }
86     return @ocean_list;
87 }
88
89 sub dbw_connect ($) {
90     my ($ocean) = @_;
91     die "unknown ocean $ocean ?"
92         unless grep { $_ eq $ocean } ocean_list();
93     return dbr_connect($datadir, $ocean);
94 }
95
96 1;