Evil trick to be able to put scoreparsing into a transaction block again.
[ccbs] / parse / parse-ddreurope-tournament.pl
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 use Date::Manip;
5
6 # Parses tournament results from the DDR Europe result list
7
8 my $season = shift;
9 my ($name, $round, $group, $player, $position, @rsongs);
10 die "Missing season (give on command line)." if (!defined($season));
11
12 print "begin;\n";
13
14 while (<>) {
15         if (/<h2> \s* (.*?) \s* <\/h2>/x) {
16                 $name = $1;
17                 next;
18         }
19         if (/<br>Country: \s* (.*?) \s*
20              <br>Location: \s* (.*?) \s*
21              <br>Date: \s* (.*?) \s*
22              <br>Mix: \s* (.*?) \s*
23              <br>ScoringSystem: \s* (.*?) \s* <br>
24             /x) {
25                 my ($country, $location, $date, $mix, $system) = ($1, $2, $3, $4, $5);
26                 $mix =~ s/Euromix/EuroMix/;
27
28                 print "INSERT INTO tournaments \n";
29                 print "  (season, name, country, location, \"date\", machine, scoringsystem) VALUES (\n";
30                 print "    (SELECT season FROM seasons WHERE name='$season'),\n";
31                 print "    '$name',\n";
32                 print "    (SELECT country FROM countries WHERE name='$country'),\n";
33                 print "    '$location',\n";
34                 printf "    '%s',\n", Date::Manip::UnixDate($date, '%Y-%m-%d');
35                 print "    (SELECT machine FROM machines WHERE name='$mix'),\n";
36                 print "    (SELECT scoringsystem FROM scoringsystems WHERE name='$system')\n";
37                 print ");\n";
38         }
39         
40         # Player's results (header)
41         if (/^ <tr><td \s* class=dtd> .*? class="link"> (.*?) <\/a> $/x) {
42                 $player = $1;
43
44                 # Woot, evil
45                 printf "INSERT INTO players SELECT nextval('players_player_seq') AS player, '%s' AS nick WHERE '%s' NOT IN ( SELECT nick FROM players );\n", $player, $player;
46                 
47                 printf "INSERT INTO roundparticipation (tournament, round, parallel, player, position) VALUES (\n";
48                 print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
49                 print "   $round, $group,\n";
50                 print "   (SELECT player FROM players WHERE nick='$player'),\n";
51                 print "   $position\n";
52                 print ");\n";
53                 ++$position;
54         }
55
56         # Player's results (score)
57         if (/<td \s* class=dtdm>/x) {
58                 my $i = 0;
59
60                 # random songs
61                 while (s/<td \s* class=dtdm> (\d+) <\/td>//x) {
62                         printf "INSERT INTO scores (tournament, round, parallel, player, song, chosen, score) VALUES (\n";
63                         print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
64                         print "   $round, $group,\n";
65                         print "   (SELECT player FROM players WHERE nick='$player'),\n";
66                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$rsongs[$i]')),\n";
67                         print "   'f',\n";
68                         print "   $1);\n";
69                         ++$i;
70                 }
71
72                 # chosen songs
73                 while (s/<td \s* class=dtdsong> .*? class="link"> (.*?) <\/a> .*? <td class=dtdscore> (\d+) <\/td>//x) {
74                         my $song = song_map($1);
75                         $song =~ s/'/\\'/g;
76                 
77                         printf "INSERT INTO scores (tournament, round, parallel, player, song, chosen, score) VALUES (\n";
78                         print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
79                         print "   $round, $group,\n";
80                         print "   (SELECT player FROM players WHERE nick='$player'),\n";
81                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$song')),\n";
82                         print "   't',\n";
83                         print "   $2);\n";
84                 }
85         }
86
87         # New round
88         if (/<h3 \s* class=dthr> Round \s* (\d+) \s* <\/h3>/x) {
89                 $round = $1;
90
91                 print "INSERT INTO rounds (tournament, round, randomsongs, chosensongs) \n";
92                 print "  VALUES (\n";
93                 print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
94                 print "   $round,\n";
95                 print "   0, 0);\n";  # Don't worry, we'll fix it later :-P
96         }
97         
98         # New group, and the random songs for it
99         if (/<th \s* class=dthp> ( Players | Group \s* (\d+) )<\/th>/x) {
100                 # bit of an evil hack here
101                 $group = $2;
102                 $group = 0 if ($1 eq 'Players');
103
104                 $position = 1;
105
106                 print "INSERT INTO groups (tournament, round, parallel) VALUES (\n";
107                 print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
108                 print "   $round, $group);\n";
109
110                 # Find the random songs, if any
111                 @rsongs = ();
112
113                 while (s/<th \s* class=dthpsong> .*? "color:white"> (.*?) <\/a><\/th>//x) {
114                         my $song = song_map($1);
115                         $song =~ s/'/\\'/g;
116                 
117                         push @rsongs, $song;
118
119                         print "INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (\n";
120                         print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
121                         print "   $round, $group,\n";
122                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$song'))\n";
123                         print ");\n";
124                 }
125
126                 # Correct the random songs in the table
127                 printf "UPDATE rounds SET randomsongs=%u WHERE \n", scalar @rsongs;
128                 print "  tournament=(SELECT tournament FROM tournaments WHERE name='$name') AND \n";
129                 print "  round=$round;\n";
130         }
131
132         # Header for chosen song (evil)
133         if (/<th \s* class=dthp \s* colspan=2>&nbsp<\/th>/x) {
134                 printf "UPDATE rounds SET chosensongs=1 WHERE \n";
135                 print "  tournament=(SELECT tournament FROM tournaments WHERE name='$name') AND \n";
136                 print "  round=$round;\n";
137         }
138
139 }
140
141 printf "commit;\n";
142
143 # different names for songs
144 sub song_map {
145         my $song = shift;
146
147         $song =~ s/Paranoia -Rebirth-/PARANOiA Rebirth/;
148         $song =~ s/The Center of the heart/The Centre of the Heart (Stonebridge Club Mix)/;
149         $song =~ s/Can't stop falling in love/Can't Stop Fallin' in Love/;
150         $song =~ s/B4U -B4 ZA Beat Mix-/B4U (B4 Za Beat Mix)/;
151         $song =~ s/Jam Jam Reggae/Jam Jam Reggae (AM Swing Mix)/;
152         $song =~ s/Trip Machine -Climax-/Trip Machine Climax/;
153         $song =~ s/Dont try to stop it/Don't Try to Stop It/;
154         $song =~ s/Healing Vision -AM-/Healing Vision (Angelic Mix)/;
155         $song =~ s/5,6,7,8/5, 6, 7, 8/;
156         $song =~ s/Keep On Movin/Keep On Movin'/;
157         $song =~ s/So Deep/So Deep (Perfect Sphere Mix)/;
158         $song =~ s/Aarons Party/Aaron's Party (Come Get It)/;
159
160         return $song;
161 }