Some more score parsing fixes, looks like most stuff is OK now.
[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         # Player's results (header)
42         if (/^ <tr><td \s* class=dtd> .*? class="link"> (.*?) <\/a> $/x) {
43                 $player = $1;
44                 printf "INSERT INTO players (nick) VALUES ('%s');\n", $player;
45                 printf "INSERT INTO roundparticipation (tournament, round, parallel, player) VALUES (\n";
46                 print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
47                 print "   $round, $group,\n";
48                 print "   (SELECT player FROM players WHERE nick='$player')\n";
49                 print ");\n";
50         }
51
52         # Player's results (score)
53         if (/<td \s* class=dtdm>/x) {
54                 my $i = 0;
55
56                 # random songs
57                 while (s/<td \s* class=dtdm> (\d+) <\/td>//x) {
58                         printf "INSERT INTO scores (tournament, round, parallel, player, song, chosen, score) VALUES (\n";
59                         print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
60                         print "   $round, $group,\n";
61                         print "   (SELECT player FROM players WHERE nick='$player'),\n";
62                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$rsongs[$i]')),\n";
63                         print "   'f',\n";
64                         print "   $1);\n";
65                         ++$i;
66                 }
67
68                 # chosen songs
69                 while (s/<td \s* class=dtdsong> .*? class="link"> (.*?) <\/a> .*? <td class=dtdscore> (\d+) <\/td>//x) {
70                         my $song = song_map($1);
71                         $song =~ s/'/\\'/g;
72                 
73                         printf "INSERT INTO scores (tournament, round, parallel, player, song, chosen, score) VALUES (\n";
74                         print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
75                         print "   $round, $group,\n";
76                         print "   (SELECT player FROM players WHERE nick='$player'),\n";
77                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$song')),\n";
78                         print "   't',\n";
79                         print "   $2);\n";
80                 }
81         }
82
83         # New round
84         if (/<h3 \s* class=dthr> Round \s* (\d+) \s* <\/h3>/x) {
85                 $round = $1;
86
87                 print "INSERT INTO rounds (tournament, round, randomsongs, chosensongs) \n";
88                 print "  VALUES (\n";
89                 print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
90                 print "   $round,\n";
91                 print "   0, 0);\n";  # Don't worry, we'll fix it later :-P
92         }
93         
94         # New group, and the random songs for it
95         if (/<th \s* class=dthp> ( Players | Group \s* (\d+) )<\/th>/x) {
96                 # bit of an evil hack here
97                 $group = $2;
98                 $group = 0 if ($1 eq 'Players');
99
100                 print "INSERT INTO groups (tournament, round, parallel) VALUES (\n";
101                 print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
102                 print "   $round, $group);\n";
103
104                 # Find the random songs, if any
105                 @rsongs = ();
106
107                 while (s/<th \s* class=dthpsong> .*? "color:white"> (.*?) <\/a><\/th>//x) {
108                         my $song = song_map($1);
109                         $song =~ s/'/\\'/g;
110                 
111                         push @rsongs, $song;
112
113                         print "INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (\n";
114                         print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
115                         print "   $round, $group,\n";
116                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$song'))\n";
117                         print ");\n";
118                 }
119
120                 # Correct the random songs in the table
121                 printf "UPDATE rounds SET randomsongs=%u WHERE \n", scalar @rsongs;
122                 print "  tournament=(SELECT tournament FROM tournaments WHERE name='$name') AND \n";
123                 print "  round=$round;\n";
124         }
125
126         # Header for chosen song (evil)
127         if (/<th \s* class=dthp \s* colspan=2>&nbsp<\/th>/x) {
128                 printf "UPDATE rounds SET chosensongs=1 WHERE \n";
129                 print "  tournament=(SELECT tournament FROM tournaments WHERE name='$name') AND \n";
130                 print "  round=$round;\n";
131         }
132
133 }
134
135 printf "commit;\n";
136
137 # different names for songs
138 sub song_map {
139         my $song = shift;
140
141         $song =~ s/Paranoia -Rebirth-/PARANOiA Rebirth/;
142         $song =~ s/The Center of the heart/The Centre of the Heart (Stonebridge Club Mix)/;
143         $song =~ s/Can't stop falling in love/Can't Stop Fallin' in Love/;
144         $song =~ s/B4U -B4 ZA Beat Mix-/B4U (B4 Za Beat Mix)/;
145         $song =~ s/Jam Jam Reggae/Jam Jam Reggae (AM Swing Mix)/;
146         $song =~ s/Trip Machine -Climax-/Trip Machine Climax/;
147         $song =~ s/Dont try to stop it/Don't Try to Stop It/;
148         $song =~ s/Healing Vision -AM-/Healing Vision (Angelic Mix)/;
149         $song =~ s/5,6,7,8/5, 6, 7, 8/;
150         $song =~ s/Keep On Movin/Keep On Movin'/;
151         $song =~ s/So Deep/So Deep (Perfect Sphere Mix)/;
152         $song =~ s/Aarons Party/Aaron's Party (Come Get It)/;
153
154         return $song;
155 }