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 PRIMARY KEY (tournament, round)
129 CREATE TABLE groups (
130 tournament INTEGER NOT NULL REFERENCES tournaments,
131 round INTEGER NOT NULL,
132 parallel INTEGER NOT NULL,
134 FOREIGN KEY (tournament, round) REFERENCES rounds (tournament, round),
135 PRIMARY KEY (tournament, round, parallel)
138 CREATE TABLE roundrandomsongs (
139 tournament INTEGER NOT NULL,
140 round INTEGER NOT NULL,
141 parallel INTEGER NOT NULL,
142 song INTEGER NOT NULL REFERENCES songs,
144 FOREIGN KEY (tournament, round, parallel) REFERENCES groups (tournament, round, parallel),
145 PRIMARY KEY (tournament, round, parallel, song)
148 CREATE TABLE roundparticipation (
149 tournament INTEGER NOT NULL,
150 round INTEGER NOT NULL,
151 parallel INTEGER NOT NULL,
152 player INTEGER NOT NULL REFERENCES players,
153 position INTEGER NOT NULL,
155 UNIQUE (tournament, round, player),
156 UNIQUE (tournament, round, parallel, position),
157 FOREIGN KEY (tournament, round, parallel) REFERENCES groups (tournament, round, parallel),
158 PRIMARY KEY (tournament, round, parallel, player)
161 CREATE TABLE scores (
162 tournament INTEGER NOT NULL,
163 round INTEGER NOT NULL,
164 parallel INTEGER NOT NULL,
165 player INTEGER NOT NULL REFERENCES players,
166 songnumber INTEGER NOT NULL,
168 song INTEGER REFERENCES songs,
169 playmode VARCHAR CHECK (playmode IS NULL OR playmode IN ('single','double')),
170 difficulty VARCHAR CHECK (difficulty IS NULL OR difficulty IN ('beginner','standard','difficult','expert','challenge')),
173 score INTEGER CHECK (score IS NULL OR (score >= 0 AND score <= 10000)),
175 -- FOREIGN KEY (song, playmode, difficulty) REFERENCES songratings (song, playmode, difficulty),
176 FOREIGN KEY (tournament, round, parallel, player) REFERENCES roundparticipation (tournament, round, parallel, player),
177 PRIMARY KEY (tournament, round, parallel, player, songnumber)
180 CREATE TABLE randomsongsused (
181 song INTEGER NOT NULL PRIMARY KEY REFERENCES songs
184 CREATE SCHEMA bigscreen;
186 CREATE TABLE bigscreen.active_tournament (
187 tournament INTEGER NOT NULL PRIMARY KEY REFERENCES tournaments
189 CREATE TABLE bigscreen.active_groups (
190 tournament INTEGER NOT NULL REFERENCES bigscreen.active_tournament,
191 round INTEGER NOT NULL,
192 parallel INTEGER NOT NULL,
193 num_machines INTEGER NOT NULL,
194 players_per_machine INTEGER NOT NULL CHECK ( players_per_machine IN (1, 2) ),
195 last_updated TIMESTAMP NOT NULL,
197 PRIMARY KEY ( tournament, round, parallel ),
198 FOREIGN KEY ( tournament, round, parallel ) REFERENCES groups
200 CREATE TABLE bigscreen.active_screens (
201 id VARCHAR NOT NULL PRIMARY KEY
204 -- get_max_score_for_songs(tournament, playmode)
205 CREATE TYPE max_score AS (
210 CREATE FUNCTION get_max_score_for_songs(integer, varchar) RETURNS SETOF max_score
214 MAX(feetrating)*1000 AS max_score
218 SELECT machine FROM tournaments WHERE tournament=$1
226 RETURNS NULL ON NULL INPUT;
228 -- get_max_score_for_players(tournament, round, playmode)
229 CREATE FUNCTION get_max_score_for_players(integer, integer, varchar) RETURNS SETOF max_score
235 WHERE machine=( -- only find songs on the machine we use
236 SELECT machine FROM tournaments WHERE tournament=$1
238 AND song NOT IN ( -- not a song that has been in elimination or seeding
239 SELECT song FROM scores
245 AND (player,song) NOT IN ( -- not a song the player has chosen before, or is a random song in this round
246 SELECT player,song FROM scores
249 AND ( chosen='t' OR round=$2 )
252 ORDER BY feetrating DESC LIMIT 1
253 ) * 1000 AS max_score FROM tournamentparticipation;
257 RETURNS NULL ON NULL INPUT;
259 -- Find out, for each player in the group, what the maximum remaining score
260 -- possibly can be, using get_max_score_for_{songs,players} (the first for
261 -- selected but not played songs, the second for random songs that have not
264 -- This doesn't solve problems we'd face with more than one chosen song, but it
265 -- should be good enough.
267 CREATE TYPE minmax_score AS (
273 -- get_minmax_score_for_players(tournament, round, parallel, playmode)
274 CREATE FUNCTION get_minmax_score_for_players(integer, integer, integer, varchar) RETURNS SETOF minmax_score
281 FOR tp IN SELECT player FROM roundparticipation WHERE tournament=$1 AND round=$2 AND parallel=$3 LOOP
282 ret.player := tp.player;
286 FOR ts IN SELECT song,score FROM scores WHERE tournament=$1 AND round=$2 AND parallel=$3 AND player=tp.player LOOP
287 IF ts.score IS NOT NULL THEN
288 -- score is already given
289 ret.min_score := ret.min_score + ts.score;
290 ret.max_score := ret.max_score + ts.score;
292 IF ts.song IS NOT NULL THEN
293 ret.max_score := ret.max_score + ( SELECT max_score FROM get_max_score_for_songs($1, $4) WHERE song=ts.song );
295 ret.max_score := ret.max_score + ( SELECT max_score FROM get_max_score_for_players($1, $4) WHERE song=tp.player );
307 RETURNS NULL ON NULL INPUT;
309 -- use the minmax_score as a basis for finding best and worst possible ranks
310 CREATE TYPE minmax_rank AS (
316 -- get_minmax_score_for_players(tournament, round, parallel, playmode)
317 CREATE TABLE temp_minmax (
323 CREATE FUNCTION get_minmax_rank_for_players(integer, integer, integer, varchar) RETURNS SETOF minmax_rank
329 INSERT INTO temp_minmax SELECT * FROM get_minmax_score_for_players($1, $2, $3, $4);
331 FOR tp IN SELECT * FROM temp_minmax LOOP
332 ret.player = tp.player;
333 ret.best_rank = 1 + ( SELECT COUNT(*) FROM temp_minmax WHERE min_score >= tp.max_score AND player<>tp.player);
334 ret.worst_rank = 1 + ( SELECT COUNT(*) FROM temp_minmax WHERE max_score > tp.min_score AND player<>tp.player );
338 TRUNCATE temp_minmax;
343 VOLATILE -- not really, but needed for the TEMPORARY TABLE
344 RETURNS NULL ON NULL INPUT;
346 -- get_minmax_score_for_players(tournament, playmode)
347 CREATE TYPE minmax_rank_entire_tournament AS (
354 CREATE FUNCTION get_minmax_rank_for_players(integer, varchar) RETURNS SETOF minmax_rank_entire_tournament
357 ret minmax_rank_entire_tournament;
361 FOR tr IN SELECT * FROM groups WHERE tournament=$1 LOOP
362 ret.round = tr.round;
363 ret.parallel = tr.parallel;
365 FOR tp IN SELECT * FROM get_minmax_rank_for_players($1, tr.round, tr.parallel, $2) LOOP
366 ret.player = tp.player;
367 ret.best_rank = tp.best_rank;
368 ret.worst_rank = tp.worst_rank;
377 RETURNS NULL ON NULL INPUT;