]> git.sesse.net Git - ccbs/blob - parse/parse-ddreurope-tournament.pl
5773f13648428f863d5d2e7953573683cc873a78
[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                 # 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 tournamentname='$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, songnumber, song, chosen, score) VALUES (\n";
63                         print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
64                         print "   $round, $group,\n";
65                         print "   (SELECT player FROM players WHERE nick='$player'),\n";
66                         print "   $i,\n";
67                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$rsongs[$i]')),\n";
68                         print "   'f',\n";
69                         print "   $1);\n";
70                         ++$i;
71                 }
72
73                 # chosen songs
74                 while (s/<td \s* class=dtdsong> .*? class="link"> (.*?) <\/a> .*? <td \s* class=dtdscore> (\d+) <\/td>//x) {
75                         my $song = song_map($1);
76                         my $score = $2;
77                         $song =~ s/'/\\'/g;
78                 
79                         printf "INSERT INTO scores (tournament, round, parallel, player, songnumber, song, chosen, score) VALUES (\n";
80                         print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
81                         print "   $round, $group,\n";
82                         print "   (SELECT player FROM players WHERE nick='$player'),\n";
83                         print "   $i,\n";
84                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$song')),\n";
85                         print "   't',\n";
86                         print "   $score);\n";
87                         ++$i;
88                 }
89         }
90
91         # New round
92         if (/<h3 \s* class=dthr> Round \s* (\d+) \s* <\/h3>/x) {
93                 $round = $1;
94
95                 print "INSERT INTO rounds (tournament, round, randomsongs, chosensongs) \n";
96                 print "  VALUES (\n";
97                 print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
98                 print "   $round,\n";
99                 print "   0, 0);\n";  # Don't worry, we'll fix it later :-P
100         }
101         
102         # New group, and the random songs for it
103         if (/<th \s* class=dthp> ( Players | Group \s* (\d+) )<\/th>/x) {
104                 # bit of an evil hack here
105                 $group = $2;
106                 $group = 0 if ($1 eq 'Players');
107
108                 $position = 1;
109
110                 print "INSERT INTO groups (tournament, round, parallel) VALUES (\n";
111                 print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
112                 print "   $round, $group);\n";
113
114                 # Find the random songs, if any
115                 @rsongs = ();
116
117                 while (s/<th \s* class=dthpsong> .*? "color:white"> (.*?) <\/a><\/th>//x) {
118                         my $song = song_map($1);
119                         $song =~ s/'/\\'/g;
120                 
121                         push @rsongs, $song;
122
123                         print "INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (\n";
124                         print "   (SELECT tournament FROM tournaments WHERE tournamentname='$name'),\n";
125                         print "   $round, $group,\n";
126                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$song'))\n";
127                         print ");\n";
128                 }
129
130                 # Correct the random songs in the table
131                 printf "UPDATE rounds SET randomsongs=%u WHERE \n", scalar @rsongs;
132                 print "  tournament=(SELECT tournament FROM tournaments WHERE tournamentname='$name') AND \n";
133                 print "  round=$round;\n";
134         }
135
136         # Header for chosen song (evil)
137         if (/<th \s* class=dthp \s* colspan=2>&nbsp<\/th>/x) {
138                 printf "UPDATE rounds SET chosensongs=1 WHERE \n";
139                 print "  tournament=(SELECT tournament FROM tournaments WHERE tournamentname='$name') AND \n";
140                 print "  round=$round;\n";
141         }
142
143 }
144
145 printf "commit;\n";
146
147 # different names for songs
148 sub song_map {
149         my $song = shift;
150
151         $song =~ s/Paranoia -Rebirth-/PARANOiA Rebirth/;
152         $song =~ s/The Center of the heart/The Centre of the Heart (Stonebridge Club Mix)/;
153         $song =~ s/Can't stop falling in love/Can't Stop Fallin' in Love/;
154         $song =~ s/B4U -B4 ZA Beat Mix-/B4U (B4 Za Beat Mix)/;
155         $song =~ s/Jam Jam Reggae/Jam Jam Reggae (AM Swing Mix)/;
156         $song =~ s/Trip Machine -Climax-/Trip Machine Climax/;
157         $song =~ s/Dont try to stop it/Don't Try to Stop It/;
158         $song =~ s/Healing Vision -AM-/Healing Vision (Angelic Mix)/;
159         $song =~ s/5,6,7,8/5, 6, 7, 8/;
160         $song =~ s/Keep On Movin/Keep On Movin'/;
161         $song =~ s/So Deep/So Deep (Perfect Sphere Mix)/;
162         $song =~ s/Aarons Party/Aaron's Party (Come Get It)/;
163         $song =~ s/Candy \*/Candy/;
164         $song =~ s/www\.blondie girl/www.blonde girl (MOMO Mix)/;
165         $song =~ s/DXY/DXY!/;
166         $song =~ s/Burning the floor/Burnin' the Floor/;
167         $song =~ s/Never Gonna Make/Never Gonna Make (Factory Team Mix)/;
168         $song =~ s/Max300/Max 300/;
169         $song =~ s/Era/era (nostalmix)/;
170         $song =~ s/Electro Tuned/Electro Tuned (the SubS Mix)/;
171         $song =~ s/Make a Jam/Make A Jam!/;
172         $song =~ s/Paranoia KCET -clean mix-/Paranoia KCET (Clean Mix)/;
173         $song =~ s/Cant Stop -Speed Mix-/Can't Stop Fallin' in Love (SPEED MIX)/;
174         $song =~ s/Love This Feelin/Love This Feelin'/;
175         $song =~ s/Trip Machine -Luv mix-/Trip Machine (Luv Mix)/;
176         $song =~ s/Let The Beat Hit Them/Let the Beat Hit 'em!/;
177         $song =~ s/Luv To Me/Luv to Me (AMD Mix)/;
178         $song =~ s/20 November/20th November/;
179
180         return $song;
181 }