]> git.sesse.net Git - ccbs/blob - parse/parse-ddreurope-tournament.pl
Attempt parsing scores.
[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         }
96
97         # Player's results (score)
98         if (/<td \s* class=dtdm>/x) {
99                 my $i = 0;
100
101                 # random songs
102                 while (s/<td \s* class=dtdm> (\d+) <\/td>//x) {
103                         printf "INSERT INTO scores (tournament, round, parallel, player, song, chosen, score) VALUES (\n";
104                         print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
105                         print "   $round, $group,\n";
106                         print "   (SELECT player FROM players WHERE nick='$player'),\n";
107                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$rsongs[$i]')),\n";
108                         print "   'f',\n";
109                         print "   $1);\n";
110                         ++$i;
111                 }
112
113                 # chosen songs
114                 while (s/<td \s* class=dtdsong> .*? class="link"> (.*?) <\/a> .*? <td class=dtdscore> (\d+) <\/td>//x) {
115                         my $song = song_map($1);
116                         $song =~ s/'/\\'/g;
117                 
118                         printf "INSERT INTO scores (tournament, round, parallel, player, song, chosen, score) VALUES (\n";
119                         print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
120                         print "   $round, $group,\n";
121                         print "   (SELECT player FROM players WHERE nick='$player'),\n";
122                         print "   (SELECT song FROM songs WHERE lower(title)=lower('$song')),\n";
123                         print "   't',\n";
124                         print "   $2);\n";
125                 }
126         }
127 }
128
129 printf "commit;\n";
130
131 # different names for songs
132 sub song_map {
133         my $song = shift;
134
135         $song =~ s/Paranoia -Rebirth-/PARANOiA Rebirth/;
136         $song =~ s/The Center of the heart/The Centre of the Heart (Stonebridge Club Mix)/;
137         $song =~ s/Can't stop falling in love/Can't Stop Fallin' in Love/;
138         $song =~ s/B4U -B4 ZA Beat Mix-/B4U (B4 Za Beat Mix)/;
139         $song =~ s/Jam Jam Reggae/Jam Jam Reggae (AM Swing Mix)/;
140         $song =~ s/Trip Machine -Climax-/Trip Machine Climax/;
141         $song =~ s/Dont try to stop it/Don't Try to Stop It/;
142         $song =~ s/Healing Vision -AM-/Healing Vision (Angelic Mix)/;
143         $song =~ s/5,6,7,8/5, 6, 7, 8/;
144         $song =~ s/Keep On Movin/Keep On Movin'/;
145         $song =~ s/So Deep/So Deep (Perfect Sphere Mix)/;
146         $song =~ s/Aarons Party/Aaron's Party (Come Get It)/;
147
148         return $song;
149 }