]> git.sesse.net Git - ccbs/blob - parse/store-ccbs-tournament.pl
Registration now supports countries and clubs.
[ccbs] / parse / store-ccbs-tournament.pl
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 use Date::Manip;
5 require "../html/ccbs.pm";
6
7 $SIG{__DIE__} = sub {
8         print STDERR shift;
9         exit;
10 };
11
12 #
13 # Not really a parser, but lets us export entire tournaments from one CCBS
14 # database to another in a simple way without having to worry about the
15 # numerical details.
16 #
17
18 my $tno = shift;
19 die "Missing tournament number (give on command line)." if (!defined($tno));
20
21 print "begin;\n";
22
23 my $dbh = ccbs::db_connect();
24
25 # The tournament itself
26 my $tournament = $dbh->selectrow_hashref('SELECT * FROM tournaments NATURAL JOIN countries NATURAL JOIN seasons NATURAL JOIN machines NATURAL JOIN scoringsystems WHERE tournament=?', undef, $tno);
27
28 print "INSERT INTO tournaments \n";
29 print "  (season, tournamentname, country, location, \"date\", machine, scoringsystem) VALUES (\n";
30 print "    (SELECT season FROM seasons WHERE seasonname='$tournament->{'seasonname'}' ),\n";
31 print "    '$tournament->{'tournamentname'}',\n";
32 print "    (SELECT country FROM countries WHERE countryname='$tournament->{'countryname'}'),\n";
33 print "    '$tournament->{'location'}',\n";
34 print "    '$tournament->{'date'}',\n";
35 print "    (SELECT machine FROM machines WHERE machinename='$tournament->{'machinename'}'),\n";
36 print "    (SELECT scoringsystem FROM scoringsystems WHERE scoringsystemname='$tournament->{'scoringsystemname'}')\n";
37 print ");\n";
38
39 # useful
40 my $tsql = "(SELECT tournament FROM tournaments WHERE tournamentname='$tournament->{'tournamentname'}' AND season=( SELECT season FROM seasons WHERE seasonname='$tournament->{'seasonname'}' ))";
41
42 # Rankings, and players (FIXME: do clubs as well)
43 my $rankings = ccbs::db_fetch_all($dbh, 'SELECT * FROM tournamentrankings NATURAL JOIN players NATURAL JOIN countries WHERE tournament=?', $tno);
44 for my $rank (@$rankings) {
45         printf "INSERT INTO players SELECT nextval('players_player_seq') AS player, '%s' AS nick, ( SELECT country FROM countries WHERE countryname='%s' ) AS country WHERE '%s' NOT IN ( SELECT nick FROM players );\n", $rank->{'nick'}, $rank->{'countryname'}, $rank->{'nick'};
46
47         my $p = $rank->{'points'};
48         $p = 'NULL' if (!defined($p));
49
50         print "INSERT INTO tournamentrankings (tournament, ranking, player, points) VALUES (\n";
51         print "   $tsql,";
52         print "   $rank->{'ranking'},\n";
53         print "   (SELECT player FROM players WHERE nick='$rank->{'nick'}'),\n";
54         print "   $p);\n";
55 }
56
57 # Skip tournamentparticipation, it is not needed except during the tournament.
58
59 # Rounds
60 my $rounds = ccbs::db_fetch_all($dbh, 'SELECT * FROM rounds WHERE tournament=?', $tno);
61 for my $round (@$rounds) {
62         print "INSERT INTO rounds ( tournament, round, randomsongs, chosensongs, numqualifying ) VALUES (\n";
63         print "   $tsql,\n";
64         print "   $round->{'round'},\n";
65         print "   $round->{'randomsongs'},\n";
66         print "   $round->{'chosensongs'},\n";
67         print "   $round->{'numqualifying'});\n";
68 }
69
70 # Groups
71 my $groups = ccbs::db_fetch_all($dbh, 'SELECT * FROM groups WHERE tournament=?', $tno);
72 for my $group (@$groups) {
73         print "INSERT INTO groups ( tournament, round, parallel ) VALUES (\n";
74         print "   $tsql,\n";
75         print "   $group->{'round'},\n";
76         print "   $group->{'parallel'});\n";
77 }
78
79 # Round random songs
80 my $rrs = ccbs::db_fetch_all($dbh, 'SELECT * FROM roundrandomsongs NATURAL JOIN songs WHERE tournament=?', $tno);
81 for my $rr (@$rrs) {
82         (my $title = $rr->{'title'}) =~ s/'/\\'/g;
83
84         print "INSERT INTO roundrandomsongs ( tournament, round, parallel, song ) VALUES (\n";
85         print "   $tsql,\n";
86         print "   $rr->{'round'},\n";
87         print "   $rr->{'parallel'},\n";
88         print "   ( SELECT song FROM songs WHERE lower(title)=lower('$title') ));\n";
89 }
90
91 # Round participation
92 my $rps = ccbs::db_fetch_all($dbh, 'SELECT * FROM roundparticipation NATURAL JOIN players WHERE tournament=?', $tno);
93 for my $rp (@$rps) {
94         print "INSERT INTO roundparticipation ( tournament, round, parallel, player, position ) VALUES (\n";
95         print "   $tsql,\n";
96         print "   $rp->{'round'},\n";
97         print "   $rp->{'parallel'},\n";
98         print "   ( SELECT player FROM players WHERE nick='$rp->{'nick'}' ),\n";
99         print "   $rp->{'position'});\n";
100 }
101
102 # And finally, scores
103 my $scores = ccbs::db_fetch_all($dbh, 'SELECT * FROM scores NATURAL JOIN songs NATURAL JOIN players WHERE tournament=?', $tno);
104 for my $score (@$scores) {
105         my $chosen = $score->{'chosen'} ? 't' : 'f';
106         (my $title = $score->{'title'}) =~ s/'/\\'/g;
107
108         print "INSERT INTO scores ( tournament, round, parallel, player, songnumber, song, playmode, difficulty, chosen, score ) VALUES (\n";
109         print "   $tsql,\n";
110         print "   $score->{'round'},\n";
111         print "   $score->{'parallel'},\n";
112         print "   ( SELECT player FROM players WHERE nick='$score->{'nick'}' ),\n";
113         print "   $score->{'songnumber'},\n";
114         print "   ( SELECT song FROM songs WHERE lower(title)=lower('$title') ),\n";
115         print "   '$score->{'playmode'}',\n";
116         print "   '$score->{'difficulty'}',\n";
117         print "   '$chosen',\n";
118         print "   $score->{'score'});\n";
119 }
120
121 print "commit;\n";
122
123 $dbh->disconnect;
124