]> git.sesse.net Git - ccbs/blob - sql/ccbs.sql
Shape text using Pango and HarfBuzz; gives us nice ligatures and exotic scripts.
[ccbs] / sql / ccbs.sql
1 -- vim:set tw=0:
2
3 CREATE TABLE machines (
4         machine SERIAL PRIMARY KEY,
5         machinename VARCHAR NOT NULL,
6
7         UNIQUE ( machinename )
8 );
9
10 CREATE TABLE countries (
11         country SERIAL PRIMARY KEY,
12         countryname VARCHAR NOT NULL,
13         countrycode CHAR(3) NOT NULL,   -- IOC country code
14
15         UNIQUE ( countryname ),
16         UNIQUE ( countrycode )
17 );
18
19 CREATE TABLE clubs (
20         club SERIAL PRIMARY KEY,
21         clubname VARCHAR NOT NULL,
22         clubcode CHAR(3) NOT NULL,
23
24         UNIQUE ( clubname ),
25         UNIQUE ( clubcode )
26 );
27
28 CREATE TABLE songs (
29         song SERIAL PRIMARY KEY,
30         title VARCHAR NOT NULL,
31         artist VARCHAR NOT NULL,
32         minbpm INTEGER NOT NULL,
33         maxbpm INTEGER NOT NULL,
34
35         UNIQUE ( title )
36 );
37
38 CREATE TABLE songshorttitles (
39         song INTEGER NOT NULL REFERENCES songs,
40         shorttitle VARCHAR NOT NULL,
41
42         PRIMARY KEY ( song, shorttitle ),
43         UNIQUE ( shorttitle )
44 );
45
46 CREATE TABLE machinesongs (
47         song INTEGER NOT NULL REFERENCES songs,
48         machine INTEGER NOT NULL REFERENCES machines,
49
50         PRIMARY KEY ( song, machine )
51 );
52
53 CREATE TABLE scoringsystems (
54         scoringsystem SERIAL PRIMARY KEY,
55         scoringsystemname VARCHAR NOT NULL,
56         
57         UNIQUE ( scoringsystemname )
58 );
59
60 CREATE TABLE songratings (
61         song INTEGER NOT NULL REFERENCES songs,
62         machine INTEGER NOT NULL REFERENCES machines,
63         playmode VARCHAR NOT NULL CHECK (playmode IN ('single','double')),
64         difficulty VARCHAR NOT NULL CHECK (difficulty IN ('beginner','standard','difficult','expert','challenge')),
65         feetrating INTEGER NOT NULL CHECK (feetrating >= 0 AND feetrating <= 10),
66
67         PRIMARY KEY (song, machine, playmode, difficulty)
68 );
69 CREATE INDEX songratings_feetrating ON songratings ( feetrating );
70
71 CREATE VIEW max_single_feetrating AS SELECT machine,song,MAX(feetrating) AS feetrating FROM songratings WHERE playmode='single' GROUP BY machine,song;
72
73 CREATE TABLE players (
74         player SERIAL PRIMARY KEY,
75         nick VARCHAR NOT NULL,
76         country INTEGER NOT NULL REFERENCES countries,
77         club INTEGER REFERENCES clubs,
78
79         UNIQUE ( nick )
80 );
81
82 CREATE TABLE seasons (
83         season SERIAL PRIMARY KEY,
84         seasonname VARCHAR NOT NULL,
85
86         UNIQUE ( seasonname )
87 );
88
89 CREATE TABLE tournaments (
90         tournament SERIAL PRIMARY KEY,
91         season INTEGER NOT NULL REFERENCES seasons,
92         tournamentname VARCHAR NOT NULL,
93         country INTEGER NOT NULL REFERENCES countries,
94         location VARCHAR NOT NULL,
95         "date" DATE NOT NULL,
96         machine INTEGER NOT NULL REFERENCES machines,
97         scoringsystem INTEGER NOT NULL REFERENCES scoringsystems,
98
99         UNIQUE ( season, tournamentname )
100 );
101
102 CREATE TABLE tournamentparticipation (
103         tournament INTEGER NOT NULL REFERENCES tournaments,
104         player INTEGER NOT NULL REFERENCES players,
105         paid BOOLEAN NOT NULL,
106
107         PRIMARY KEY ( tournament, player )
108 );
109
110 CREATE TABLE tournamentrankings (
111         tournament INTEGER NOT NULL REFERENCES tournaments,
112         ranking INTEGER NOT NULL,
113         player INTEGER NOT NULL REFERENCES players,
114         points INTEGER,
115
116         PRIMARY KEY (tournament, player)
117 );
118
119 CREATE TABLE rounds (
120         tournament INTEGER NOT NULL REFERENCES tournaments,
121         round INTEGER NOT NULL,
122         randomsongs INTEGER NOT NULL,
123         chosensongs INTEGER NOT NULL,
124         numqualifying INTEGER,
125
126         UNIQUE (tournament, player),
127         PRIMARY KEY (tournament, round)
128 );
129
130 CREATE TABLE groups (
131         tournament INTEGER NOT NULL REFERENCES tournaments,
132         round INTEGER NOT NULL,
133         parallel INTEGER NOT NULL,
134
135         FOREIGN KEY (tournament, round) REFERENCES rounds (tournament, round),
136         PRIMARY KEY (tournament, round, parallel)
137 );
138
139 CREATE TABLE roundrandomsongs (
140         tournament INTEGER NOT NULL,
141         round INTEGER NOT NULL,
142         parallel INTEGER NOT NULL,
143         song INTEGER NOT NULL REFERENCES songs,
144
145         FOREIGN KEY (tournament, round, parallel) REFERENCES groups (tournament, round, parallel),
146         PRIMARY KEY (tournament, round, parallel, song)
147 );
148
149 CREATE TABLE roundparticipation (
150         tournament INTEGER NOT NULL,
151         round INTEGER NOT NULL,
152         parallel INTEGER NOT NULL,
153         player INTEGER NOT NULL REFERENCES players,
154         position INTEGER NOT NULL,
155
156         UNIQUE (tournament, round, player),
157         UNIQUE (tournament, round, parallel, position),
158         FOREIGN KEY (tournament, round, parallel) REFERENCES groups (tournament, round, parallel),
159         PRIMARY KEY (tournament, round, parallel, player)
160 );
161
162 CREATE TABLE scores (
163         tournament INTEGER NOT NULL,
164         round INTEGER NOT NULL,
165         parallel INTEGER NOT NULL,
166         player INTEGER NOT NULL REFERENCES players,
167         songnumber INTEGER NOT NULL,
168         
169         song INTEGER REFERENCES songs,
170         playmode VARCHAR CHECK (playmode IS NULL OR playmode IN ('single','double')),
171         difficulty VARCHAR CHECK (difficulty IS NULL OR difficulty IN ('beginner','standard','difficult','expert','challenge')),
172         
173         chosen BOOLEAN,
174         score INTEGER CHECK (score IS NULL OR (score >= 0 AND score <= 10000)),
175         
176         -- FOREIGN KEY (song, playmode, difficulty) REFERENCES songratings (song, playmode, difficulty),
177         FOREIGN KEY (tournament, round, parallel, player) REFERENCES roundparticipation (tournament, round, parallel, player),
178         PRIMARY KEY (tournament, round, parallel, player, songnumber)
179 );
180
181 CREATE TABLE randomsongsused (
182         song INTEGER NOT NULL PRIMARY KEY REFERENCES songs
183 );
184
185 CREATE SCHEMA bigscreen;
186
187 CREATE TABLE bigscreen.active_tournament (
188         tournament INTEGER NOT NULL PRIMARY KEY REFERENCES tournaments
189 );
190 CREATE TABLE bigscreen.active_groups (
191         tournament INTEGER NOT NULL REFERENCES bigscreen.active_tournament,
192         round INTEGER NOT NULL,
193         parallel INTEGER NOT NULL,
194         num_machines INTEGER NOT NULL,
195         players_per_machine INTEGER NOT NULL CHECK ( players_per_machine IN (1, 2) ),
196         last_updated TIMESTAMP NOT NULL,
197
198         PRIMARY KEY ( tournament, round, parallel ),
199         FOREIGN KEY ( tournament, round, parallel ) REFERENCES groups
200 );
201 CREATE TABLE bigscreen.active_screens (
202         id VARCHAR NOT NULL PRIMARY KEY
203 );
204
205 -- get_max_score_for_songs(tournament, playmode)
206 CREATE TYPE max_score AS (
207         song INTEGER,
208         max_score INTEGER
209 );
210
211 CREATE FUNCTION get_max_score_for_songs(integer, varchar) RETURNS SETOF max_score
212     AS $$
213         SELECT
214             song,
215             MAX(feetrating)*1000 AS max_score
216         FROM songratings
217         WHERE
218             machine=(
219                 SELECT machine FROM tournaments WHERE tournament=$1
220             )
221             AND playmode=$2
222         GROUP BY song
223         ;
224 $$
225     LANGUAGE SQL
226     STABLE
227     RETURNS NULL ON NULL INPUT;
228
229 -- get_max_score_for_players(tournament, round, playmode)
230 CREATE FUNCTION get_max_score_for_players(integer, integer, varchar) RETURNS SETOF max_score
231     AS $$
232         SELECT player,
233           (
234              SELECT feetrating
235              FROM songratings
236              WHERE machine=(         -- only find songs on the machine we use
237                SELECT machine FROM tournaments WHERE tournament=$1
238              )
239              AND song NOT IN (       -- not a song that has been in elimination or seeding
240                SELECT song FROM scores 
241                WHERE tournament=$1
242                  AND song IS NOT NULL
243                  AND parallel=0
244                  AND chosen='f'
245              )
246              AND (player,song) NOT IN (  -- not a song the player has chosen before, or is a random song in this round
247                SELECT player,song FROM scores
248                WHERE tournament=$1
249                  AND song IS NOT NULL
250                  AND ( chosen='t' OR round=$2 )
251              )
252              AND playmode=$3
253              ORDER BY feetrating DESC LIMIT 1
254           ) * 1000 AS max_score FROM tournamentparticipation WHERE tournament=$1;
255 $$
256     LANGUAGE SQL
257     STABLE
258     RETURNS NULL ON NULL INPUT;
259
260 -- Find out, for each player in the group, what the maximum remaining score
261 -- possibly can be, using get_max_score_for_{songs,players} (the first for
262 -- selected but not played songs, the second for random songs that have not
263 -- been chosen yet).
264 --
265 -- This doesn't solve problems we'd face with more than one chosen song, but it
266 -- should be good enough.
267
268 CREATE TYPE minmax_score AS (
269         player INTEGER,
270         min_score INTEGER,
271         max_score INTEGER
272 );
273
274 -- get_minmax_score_for_players(tournament, round, parallel, playmode)
275 CREATE FUNCTION get_minmax_score_for_players(integer, integer, integer, varchar) RETURNS SETOF minmax_score
276     AS $$
277 DECLARE
278         ret minmax_score;
279         tp RECORD;
280         ts RECORD;
281 BEGIN
282         FOR tp IN SELECT player FROM roundparticipation WHERE tournament=$1 AND round=$2 AND parallel=$3 LOOP
283                 ret.player := tp.player;
284                 ret.min_score := 0;
285                 ret.max_score := 0;
286
287                 FOR ts IN SELECT song,score FROM scores WHERE tournament=$1 AND round=$2 AND parallel=$3 AND player=tp.player LOOP
288                         IF ts.score IS NOT NULL THEN
289                                 -- score is already given
290                                 ret.min_score := ret.min_score + ts.score;
291                                 ret.max_score := ret.max_score + ts.score;
292                         ELSE
293                                 IF ts.song IS NOT NULL THEN
294                                         ret.max_score := ret.max_score + ( SELECT max_score FROM get_max_score_for_songs($1, $4) WHERE song=ts.song );
295                                 ELSE
296                                         ret.max_score := ret.max_score + ( SELECT max_score FROM get_max_score_for_players($1, $2, $4) WHERE song=tp.player );
297                                 END IF;
298                         END IF;
299                 END LOOP;
300
301                 RETURN NEXT ret;
302         END LOOP;
303         RETURN;
304 END;
305 $$
306     LANGUAGE plpgsql
307     STABLE
308     RETURNS NULL ON NULL INPUT;
309
310 -- use the minmax_score as a basis for finding best and worst possible ranks
311 CREATE TYPE minmax_rank AS (
312         player INTEGER,
313         best_rank INTEGER,
314         worst_rank INTEGER
315 );
316
317 -- get_minmax_score_for_players(tournament, round, parallel, playmode)
318 CREATE TABLE temp_minmax (
319         player INTEGER,
320         min_score INTEGER,
321         max_score INTEGER
322 );
323
324 CREATE FUNCTION get_minmax_rank_for_players(integer, integer, integer, varchar) RETURNS SETOF minmax_rank
325     AS $$
326 DECLARE
327         ret minmax_rank;
328         tp RECORD;
329 BEGIN
330         INSERT INTO temp_minmax SELECT * FROM get_minmax_score_for_players($1, $2, $3, $4);
331         
332         FOR tp IN SELECT * FROM temp_minmax LOOP
333                 ret.player = tp.player;
334                 ret.best_rank = 1 + ( SELECT COUNT(*) FROM temp_minmax WHERE min_score > tp.max_score AND player<>tp.player);
335                 ret.worst_rank = 1 + ( SELECT COUNT(*) FROM temp_minmax WHERE max_score >= tp.min_score AND player<>tp.player );
336
337                 RETURN NEXT ret;
338         END LOOP;
339         TRUNCATE temp_minmax;
340         RETURN;
341 END;
342     $$
343     LANGUAGE plpgsql
344     VOLATILE    -- not really, but needed for the TEMPORARY TABLE
345     RETURNS NULL ON NULL INPUT;
346
347 -- get_minmax_score_for_players(tournament, playmode)
348 CREATE TYPE minmax_rank_entire_tournament AS (
349         round INTEGER,
350         parallel INTEGER,
351         player INTEGER,
352         best_rank INTEGER,
353         worst_rank INTEGER
354 );
355 CREATE FUNCTION get_minmax_rank_for_players(integer, varchar) RETURNS SETOF minmax_rank_entire_tournament
356     AS $$
357 DECLARE
358         ret minmax_rank_entire_tournament;
359         tr record;
360         tp record;
361 BEGIN
362         FOR tr IN SELECT * FROM groups WHERE tournament=$1 LOOP
363                 ret.round = tr.round;
364                 ret.parallel = tr.parallel;
365
366                 FOR tp IN SELECT * FROM get_minmax_rank_for_players($1, tr.round, tr.parallel, $2) LOOP
367                         ret.player = tp.player;
368                         ret.best_rank = tp.best_rank;
369                         ret.worst_rank = tp.worst_rank;
370                         RETURN NEXT ret;
371                 END LOOP;
372         END LOOP;
373         RETURN;
374 END;
375     $$
376     LANGUAGE plpgsql
377     STABLE
378     RETURNS NULL ON NULL INPUT;