]> git.sesse.net Git - ccbs/blob - parse/parse-ddreurope-tournament.pl
Add a hack for "Fantasy" from DDREx
[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, tournamentname, country, location, \"date\", machine, scoringsystem) VALUES (\n";
30                 print "    (SELECT season FROM seasons WHERE seasonname='$season'),\n";
31                 print "    '$name',\n";
32                 print "    (SELECT country FROM countries WHERE countryname='$country'),\n";
33                 print "    '$location',\n";
34                 printf "    '%s',\n", Date::Manip::UnixDate($date, '%Y-%m-%d');
35                 print "    (SELECT machine FROM machines WHERE machinename='$mix'),\n";
36                 print "    (SELECT scoringsystem FROM scoringsystems WHERE scoringsystemname='$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                 printf "INSERT INTO roundparticipation (tournament, round, parallel, player, position) VALUES (\n";
45                 print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
46                 print "   $round, $group,\n";
47                 print "   (SELECT player FROM players WHERE nick='$player'),\n";
48                 print "   $position\n";
49                 print ");\n";
50                 ++$position;
51         }
52
53         # Player's results (score)
54         if (/<td \s* class=dtdm>/x) {
55                 my $i = 0;
56
57                 # random songs
58                 while (s/<td \s* class=dtdm> (\d+) <\/td>//x) {
59                         printf "INSERT INTO scores (tournament, round, parallel, player, songnumber, song, chosen, score) VALUES (\n";
60                         print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
61                         print "   $round, $group,\n";
62                         print "   (SELECT player FROM players WHERE nick='$player'),\n";
63                         print "   $i,\n";
64                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$rsongs[$i]')),\n";
65                         print "   'f',\n";
66                         print "   $1);\n";
67                         ++$i;
68                 }
69
70                 # chosen songs
71                 while (s/<td \s* class=dtdsong> .*? class="link"> (.*?) <\/a> .*? <td \s* class=dtdscore> (\d+) <\/td>//x) {
72                         my $song = song_map($1);
73                         my $score = $2;
74                         $song =~ s/'/\\'/g;
75                 
76                         printf "INSERT INTO scores (tournament, round, parallel, player, songnumber, song, chosen, score) VALUES (\n";
77                         print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
78                         print "   $round, $group,\n";
79                         print "   (SELECT player FROM players WHERE nick='$player'),\n";
80                         print "   $i,\n";
81                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$song')),\n";
82                         print "   't',\n";
83                         print "   $score);\n";
84                         ++$i;
85                 }
86         }
87
88         # New round
89         if (/<h3 \s* class=dthr> Round \s* (\d+) \s* <\/h3>/x) {
90                 $round = $1;
91
92                 print "INSERT INTO rounds (tournament, round, randomsongs, chosensongs) \n";
93                 print "  VALUES (\n";
94                 print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
95                 print "   $round,\n";
96                 print "   0, 0);\n";  # Don't worry, we'll fix it later :-P
97         }
98         
99         # New group, and the random songs for it
100         if (/<th \s* class=dthp> ( Players | Group \s* (\d+) )<\/th>/x) {
101                 # bit of an evil hack here
102                 $group = $2;
103                 $group = 0 if ($1 eq 'Players');
104
105                 $position = 1;
106
107                 print "INSERT INTO groups (tournament, round, parallel) VALUES (\n";
108                 print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
109                 print "   $round, $group);\n";
110
111                 # Find the random songs, if any
112                 @rsongs = ();
113
114                 while (s/<th \s* class=dthpsong> .*? "color:white"> (.*?) <\/a><\/th>//x) {
115                         my $song = song_map($1);
116                         $song =~ s/'/\\'/g;
117                 
118                         push @rsongs, $song;
119
120                         print "INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (\n";
121                         print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
122                         print "   $round, $group,\n";
123                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$song'))\n";
124                         print ");\n";
125                 }
126
127                 # Correct the random songs in the table
128                 printf "UPDATE rounds SET randomsongs=%u WHERE \n", scalar @rsongs;
129                 print "  tournament=(SELECT tournament FROM tournaments WHERE tournamentname='$name') AND \n";
130                 print "  round=$round;\n";
131         }
132
133         # Header for chosen song (evil)
134         if (/<th \s* class=dthp \s* colspan=2>&nbsp<\/th>/x) {
135                 printf "UPDATE rounds SET chosensongs=1 WHERE \n";
136                 print "  tournament=(SELECT tournament FROM tournaments WHERE tournamentname='$name') AND \n";
137                 print "  round=$round;\n";
138         }
139
140         # First place
141         if (/A \s* well-earned \s* victory \s* for .*? class="link"> (.*?) <\/a> (?: , \s* who \s* got \s* (\d+) \s* point )?/x) {
142                 my $nick = $1;
143                 my $points = $2;
144
145                 # Whoa :-)
146                 $points = 'NULL' unless defined($points);
147
148                 # Woot, evil
149                 printf "INSERT INTO players SELECT nextval('players_player_seq') AS player, '%s' AS nick WHERE '%s' NOT IN ( SELECT nick FROM players );\n", $nick, $nick;
150                 
151                 print "INSERT INTO tournamentrankings (tournament, ranking, player, points) VALUES (\n";
152                 print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
153                 print "   1,\n";
154                 print "   (SELECT player FROM players WHERE nick='$nick'),\n";
155                 print "   $points);\n";
156         }
157
158         # All other places
159         while (s/(\d+) (?: st | nd | rd | th ) \s* place: .*? class="link"> (.*?) <\/a> \s* (?: got \s* (\d+) \s* point )?//x) {
160                 my $ranking = $1;       
161                 my $nick = $2;
162                 my $points = $3;
163
164                 # Whoa :-)
165                 $points = 'NULL' unless defined($points);
166
167                 # Woot, evil
168                 printf "INSERT INTO players SELECT nextval('players_player_seq') AS player, '%s' AS nick WHERE '%s' NOT IN ( SELECT nick FROM players );\n", $nick, $nick;
169                 
170                 print "INSERT INTO tournamentrankings (tournament, ranking, player, points) VALUES (\n";
171                 print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
172                 print "   $ranking,\n";
173                 print "   (SELECT player FROM players WHERE nick='$nick'),\n";
174                 print "   $points);\n";
175         }       
176 }
177
178 printf "commit;\n";
179
180 # different names for songs
181 sub song_map {
182         my $song = shift;
183
184         $song =~ s/Paranoia -Rebirth-/PARANOiA Rebirth/;
185         $song =~ s/The Center of the heart/The Centre of the Heart (Stonebridge Club Mix)/;
186         $song =~ s/Can't stop falling in love/Can't Stop Fallin' in Love/;
187         $song =~ s/B4U -B4 ZA Beat Mix-/B4U (B4 Za Beat Mix)/;
188         $song =~ s/Jam Jam Reggae/Jam Jam Reggae (AM Swing Mix)/;
189         $song =~ s/Trip Machine -Climax-/Trip Machine Climax/;
190         $song =~ s/Dont try to stop it/Don't Try to Stop It/;
191         $song =~ s/Healing Vision -AM-/Healing Vision (Angelic Mix)/;
192         $song =~ s/5,6,7,8/5, 6, 7, 8/;
193         $song =~ s/Keep On Movin/Keep On Movin'/;
194         $song =~ s/So Deep/So Deep (Perfect Sphere Mix)/;
195         $song =~ s/Aarons Party/Aaron's Party (Come Get It)/;
196         $song =~ s/Candy \*/Candy/;
197         $song =~ s/www\.blondie girl/www.blonde girl (MOMO Mix)/;
198         $song =~ s/DXY/DXY!/;
199         $song =~ s/Burning the floor/Burnin' the Floor/;
200         $song =~ s/Never Gonna Make/Never Gonna Make (Factory Team Mix)/;
201         $song =~ s/Max300/Max 300/;
202         $song =~ s/Era/era (nostalmix)/;
203         $song =~ s/Electro Tuned/Electro Tuned (the SubS Mix)/;
204         $song =~ s/Make a Jam/Make A Jam!/;
205         $song =~ s/Paranoia KCET -clean mix-/Paranoia KCET (Clean Mix)/;
206         $song =~ s/Cant Stop -Speed Mix-/Can't Stop Fallin' in Love (SPEED MIX)/;
207         $song =~ s/Love This Feelin/Love This Feelin'/;
208         $song =~ s/Trip Machine -Luv mix-/Trip Machine (Luv Mix)/;
209         $song =~ s/Let The Beat Hit Them/Let the Beat Hit 'em!/;
210         $song =~ s/Luv To Me/Luv to Me (AMD Mix)/;
211         $song =~ s/20 November/20th November/;
212
213         return $song;
214 }