3 CREATE TABLE machines (
4 machine SERIAL PRIMARY KEY,
5 machinename VARCHAR NOT NULL,
10 CREATE TABLE countries (
11 country SERIAL PRIMARY KEY,
12 countryname VARCHAR NOT NULL,
13 countrycode CHAR(3) NOT NULL, -- IOC country code
15 UNIQUE ( countryname ),
16 UNIQUE ( countrycode )
20 club SERIAL PRIMARY KEY,
21 clubname VARCHAR NOT NULL,
22 clubcode CHAR(3) NOT NULL,
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,
38 CREATE TABLE songshorttitles (
39 song INTEGER NOT NULL REFERENCES songs,
40 shorttitle VARCHAR NOT NULL,
42 PRIMARY KEY ( song, shorttitle ),
46 CREATE TABLE machinesongs (
47 song INTEGER NOT NULL REFERENCES songs,
48 machine INTEGER NOT NULL REFERENCES machines,
50 PRIMARY KEY ( song, machine )
53 CREATE TABLE scoringsystems (
54 scoringsystem SERIAL PRIMARY KEY,
55 scoringsystemname VARCHAR NOT NULL,
57 UNIQUE ( scoringsystemname )
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),
67 PRIMARY KEY (song, machine, playmode, difficulty)
69 CREATE INDEX songratings_feetrating ON songratings ( feetrating );
71 CREATE VIEW max_single_feetrating AS SELECT machine,song,MAX(feetrating) AS feetrating FROM songratings WHERE playmode='single' GROUP BY machine,song;
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,
82 CREATE TABLE seasons (
83 season SERIAL PRIMARY KEY,
84 seasonname VARCHAR NOT NULL,
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,
96 machine INTEGER NOT NULL REFERENCES machines,
97 scoringsystem INTEGER NOT NULL REFERENCES scoringsystems,
99 UNIQUE ( season, tournamentname )
102 CREATE TABLE tournamentparticipation (
103 tournament INTEGER NOT NULL REFERENCES tournaments,
104 player INTEGER NOT NULL REFERENCES players,
105 paid BOOLEAN NOT NULL,
107 PRIMARY KEY ( tournament, player )
110 CREATE TABLE tournamentrankings (
111 tournament INTEGER NOT NULL REFERENCES tournaments,
112 ranking INTEGER NOT NULL,
113 player INTEGER NOT NULL REFERENCES players,
116 PRIMARY KEY (tournament, player)
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,
126 UNIQUE (tournament, player),
127 PRIMARY KEY (tournament, round)
130 CREATE TABLE groups (
131 tournament INTEGER NOT NULL REFERENCES tournaments,
132 round INTEGER NOT NULL,
133 parallel INTEGER NOT NULL,
135 FOREIGN KEY (tournament, round) REFERENCES rounds (tournament, round),
136 PRIMARY KEY (tournament, round, parallel)
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,
145 FOREIGN KEY (tournament, round, parallel) REFERENCES groups (tournament, round, parallel),
146 PRIMARY KEY (tournament, round, parallel, song)
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,
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)
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,
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')),
174 score INTEGER CHECK (score IS NULL OR (score >= 0 AND score <= 10000)),
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)
181 CREATE TABLE randomsongsused (
182 song INTEGER NOT NULL PRIMARY KEY REFERENCES songs
185 CREATE SCHEMA bigscreen;
187 CREATE TABLE bigscreen.active_tournament (
188 tournament INTEGER NOT NULL PRIMARY KEY REFERENCES tournaments
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,
198 PRIMARY KEY ( tournament, round, parallel ),
199 FOREIGN KEY ( tournament, round, parallel ) REFERENCES groups
201 CREATE TABLE bigscreen.active_screens (
202 id VARCHAR NOT NULL PRIMARY KEY
205 -- get_max_score_for_songs(tournament, playmode)
206 CREATE TYPE max_score AS (
211 CREATE FUNCTION get_max_score_for_songs(integer, varchar) RETURNS SETOF max_score
215 MAX(feetrating)*1000 AS max_score
219 SELECT machine FROM tournaments WHERE tournament=$1
227 RETURNS NULL ON NULL INPUT;
229 -- get_max_score_for_players(tournament, round, playmode)
230 CREATE FUNCTION get_max_score_for_players(integer, integer, varchar) RETURNS SETOF max_score
236 WHERE machine=( -- only find songs on the machine we use
237 SELECT machine FROM tournaments WHERE tournament=$1
239 AND song NOT IN ( -- not a song that has been in elimination or seeding
240 SELECT song FROM scores
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
250 AND ( chosen='t' OR round=$2 )
253 ORDER BY feetrating DESC LIMIT 1
254 ) * 1000 AS max_score FROM tournamentparticipation WHERE tournament=$1;
258 RETURNS NULL ON NULL INPUT;
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
265 -- This doesn't solve problems we'd face with more than one chosen song, but it
266 -- should be good enough.
268 CREATE TYPE minmax_score AS (
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
282 FOR tp IN SELECT player FROM roundparticipation WHERE tournament=$1 AND round=$2 AND parallel=$3 LOOP
283 ret.player := tp.player;
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;
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 );
296 ret.max_score := ret.max_score + ( SELECT max_score FROM get_max_score_for_players($1, $2, $4) WHERE song=tp.player );
308 RETURNS NULL ON NULL INPUT;
310 -- use the minmax_score as a basis for finding best and worst possible ranks
311 CREATE TYPE minmax_rank AS (
317 -- get_minmax_score_for_players(tournament, round, parallel, playmode)
318 CREATE TABLE temp_minmax (
324 CREATE FUNCTION get_minmax_rank_for_players(integer, integer, integer, varchar) RETURNS SETOF minmax_rank
330 INSERT INTO temp_minmax SELECT * FROM get_minmax_score_for_players($1, $2, $3, $4);
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 );
339 TRUNCATE temp_minmax;
344 VOLATILE -- not really, but needed for the TEMPORARY TABLE
345 RETURNS NULL ON NULL INPUT;
347 -- get_minmax_score_for_players(tournament, playmode)
348 CREATE TYPE minmax_rank_entire_tournament AS (
355 CREATE FUNCTION get_minmax_rank_for_players(integer, varchar) RETURNS SETOF minmax_rank_entire_tournament
358 ret minmax_rank_entire_tournament;
362 FOR tr IN SELECT * FROM groups WHERE tournament=$1 LOOP
363 ret.round = tr.round;
364 ret.parallel = tr.parallel;
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;
378 RETURNS NULL ON NULL INPUT;