chiark / gitweb /
Vessel capacities
[ypp-sc-tools.db-live.git] / yarrg / db-idempotent-populate
1 #!/usr/bin/perl -w
2 #
3 # Normally run from
4 #  update-master-info
5 #
6 # usage: ./db-idempotent-populate <Oceanname>
7 #  creates or updates OCEAN-Oceanname.db
8 #  from master-master.txt
9
10 # This is part of ypp-sc-tools, a set of third-party tools for assisting
11 # players of Yohoho Puzzle Pirates.
12 #
13 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
14 #
15 # This program is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 #
28 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
29 # are used without permission.  This program is not endorsed or
30 # sponsored by Three Rings.
31
32 use strict (qw(vars));
33
34 use DBI;
35
36 use Commods;
37 use CommodsDatabase;
38
39 @ARGV==1 or die;
40 my ($oceanname) = @ARGV;
41
42 #---------- setup ----------
43
44 parse_info_serverside();
45
46 db_setocean($oceanname);
47 db_writer();
48 db_connect();
49
50 #---------- schema ----------
51
52 foreach my $bs (qw(buy sell)) {
53     db_doall(<<END)
54  CREATE TABLE IF NOT EXISTS $bs (
55         commodid        INTEGER                 NOT NULL,
56         islandid        INTEGER                 NOT NULL,
57         stallid         INTEGER                 NOT NULL,
58         price           INTEGER                 NOT NULL,
59         qty             INTEGER                 NOT NULL,
60         PRIMARY KEY (commodid, islandid, stallid)
61  );
62  CREATE INDEX IF NOT EXISTS ${bs}_by_island ON $bs (commodid, islandid, price);
63  CREATE INDEX IF NOT EXISTS ${bs}_by_price  ON $bs (commodid, price, islandid);
64 END
65     ;
66 }
67
68 db_doall(<<END)
69  CREATE TABLE IF NOT EXISTS commods (
70         commodid        INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
71         commodname      TEXT    UNIQUE          NOT NULL,
72         unitmass        INTEGER,
73         unitvolume      INTEGER
74  );
75  CREATE TABLE IF NOT EXISTS islands (
76         islandid        INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
77         islandname      TEXT    UNIQUE          NOT NULL,
78         archipelago     TEXT                    NOT NULL
79  );
80  CREATE TABLE IF NOT EXISTS stalls (
81         stallid         INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
82         islandid        INTEGER                 NOT NULL,
83         stallname       TEXT                    NOT NULL,
84         UNIQUE (islandid, stallname)
85  );
86  CREATE TABLE IF NOT EXISTS uploads (
87         islandid        INTEGER PRIMARY KEY     NOT NULL,
88         timestamp       INTEGER                 NOT NULL,
89         message         TEXT                    NOT NULL,
90         clientspec      TEXT                    NOT NULL,
91         serverspec      TEXT                    NOT NULL
92  );
93  CREATE TABLE IF NOT EXISTS dists (
94         aiid            INTEGER                 NOT NULL,
95         biid            INTEGER                 NOT NULL,
96         dist            INTEGER                 NOT NULL,
97         PRIMARY KEY (aiid, biid)
98  );
99  CREATE TABLE IF NOT EXISTS routes (
100         aiid            INTEGER                 NOT NULL,
101         biid            INTEGER                 NOT NULL,
102         dist            INTEGER                 NOT NULL,
103         PRIMARY KEY (aiid, biid)
104  );
105  CREATE TABLE IF NOT EXISTS vessels (
106         name            TEXT                    NOT NULL,
107         mass            INTEGER                 NOT NULL,
108         volume          INTEGER                 NOT NULL,
109         shot            INTEGER                 NOT NULL,
110         PRIMARY KEY (name)
111  );
112 END
113     ;
114
115 $dbh->commit;
116
117 #---------- commodity list ----------
118
119 {
120     my $insert= $dbh->prepare(<<'END')
121  INSERT OR IGNORE INTO commods
122      (unitmass,
123       unitvolume,
124       commodname)
125      VALUES (?,?,?);
126 END
127     ;
128     my $update= $dbh->prepare(<<'END')
129  UPDATE commods
130      SET unitmass = ?,
131          unitvolume = ?
132      WHERE commodname = ?
133 END
134     ;
135     foreach my $commod (sort keys %commods) {
136         my $c= $commods{$commod};
137         die "no mass for $commod" unless defined $c->{Mass};
138         die "no colume for $commod" unless defined $c->{Volume};
139         my @qa= ($c->{Mass}, $c->{Volume}, $commod);
140         $insert->execute(@qa);
141         $update->execute(@qa);
142     }
143     $dbh->commit;
144 }
145
146 #---------- vessel types ----------
147 {
148     my $idempotent= $dbh->prepare(<<'END')
149  INSERT OR REPLACE INTO vessels (name, shot, mass, volume)
150                          VALUES (?,?,?,?)
151 END
152     ;
153     foreach my $name (sort keys %vessels) {
154         my $v= $vessels{$name};
155         my $shotdamage= $shotname2damage{$v->{Shot}};
156         die "no shot damage for shot $v->{Shot} for vessel $name"
157             unless defined $shotdamage;
158         my @qa= ($name, $shotdamage, map { $v->{$_} } qw(Mass Volume));
159         $idempotent->execute(@qa);
160     }
161     $dbh->commit;
162 }
163
164 #---------- island list ----------
165 #---------- routes ----------
166 # now done by yppedia-chart-parser
167
168 __DATA__