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