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