95006e090af36204f39a8a5ac9481f2b282787d0
[ccbs] / html / do-edit-scores.pl
1 #! /usr/bin/perl
2
3 use ccbs;
4 use strict;
5 use warnings;
6
7 my $dbh = ccbs::db_connect();
8 my $cgi = new CGI;
9
10 my $tournament = $cgi->param('tournament');
11 my $round = $cgi->param('round');
12 my $group = $cgi->param('group');
13
14 $dbh->{AutoCommit} = 0;
15
16 my $tournamentdata = $dbh->selectrow_hashref('SELECT * FROM tournaments NATURAL JOIN machines WHERE tournament=?',
17         undef, $tournament);
18
19 my %checked_songs = ();
20
21 # Loop through all parameters and see what parameters differ between old- and current
22 # versions
23 for my $p ($cgi->param()) {
24         next if ($p =~ /^old-(.*?)$/);
25         next unless (defined($cgi->param('old-' . $p)));
26         next if ($cgi->param($p) eq $cgi->param('old-' . $p));
27
28         my $val = $cgi->param($p);
29         undef $val if ($val =~ /^\s*$/);
30
31         # _Before_ we do changes to the database, check if this is possibly something
32         # that will trigger a foreign key error. There will be an error if the tuple
33         # (song,playmode,difficulty) is set (ie. all members are set) and the combination
34         # doesn't exist in songratings, so check that. However, only check once per
35         # song (or else we'll be doing lots of unneeded database calls).
36         if ($p =~ /^(?:playmode|difficulty|song)(\d+)-(\d+)$/ && !defined($checked_songs{"$1-$2"})) {
37                 # check that the entire triple is set
38                 if (defined($cgi->param("playmode$1-$2"))   && $cgi->param("playmode$1-$2") !~ /^\s*$/ &&
39                     defined($cgi->param("difficulty$1-$2")) && $cgi->param("difficulty$1-$2") !~ /^\s*$/ &&
40                     defined($cgi->param("song$1-$2"))       && $cgi->param("song$1-$2") !~ /^\s*$/) {
41                         my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_songs FROM songratings WHERE song=? AND playmode=? AND difficulty=? AND machine=?',
42                                 undef, $cgi->param("song$1-$2"), $cgi->param("playmode$1-$2"),
43                                 $cgi->param("difficulty$1-$2"), $tournamentdata->{'machine'});
44                         if ($ref->{'num_songs'} != 1) {
45                                 # Fetch the name of the song if we can
46                                 $ref = $dbh->selectrow_hashref('SELECT title FROM songs WHERE song=?',
47                                         undef, $cgi->param("song$1-$2"));
48                                 ccbs::user_error("Sangen $ref->{'title'} har ikke steps for " .
49                                         $cgi->param("playmode$1-$2") . " " . $cgi->param("difficulty$1-$2") . " på ".
50                                         $tournamentdata->{'machinename'} . ".");
51                         }
52                         $checked_songs{"$1-$2"} = 1;
53                 }
54         }
55
56         if ($p =~ /^score(\d+)-(\d+)/) {
57                 if (defined($val) && ($val < 0 || $val > 10000)) {
58                         ccbs::user_error("Alle poengsummer må være mellom 0 og 10000 (inklusive).");
59                 }
60         
61                 $dbh->do('UPDATE scores SET score=? WHERE tournament=? AND round=? AND parallel=? AND player=? AND songnumber=?', undef,
62                         $val, $tournament, $round, $group, $1, $2);
63         } elsif ($p =~ /^playmode(\d+)-(\d+)/) {
64                 $dbh->do('UPDATE scores SET playmode=? WHERE tournament=? AND round=? AND parallel=? AND player=? AND songnumber=?', undef,
65                         $val, $tournament, $round, $group, $1, $2);
66         } elsif ($p =~ /^difficulty(\d+)-(\d+)/) {
67                 $dbh->do('UPDATE scores SET difficulty=? WHERE tournament=? AND round=? AND parallel=? AND player=? AND songnumber=?', undef,
68                         $val, $tournament, $round, $group, $1, $2);
69         } elsif ($p =~ /^song(\d+)-(\d+)/) {
70                 $dbh->do('UPDATE scores SET song=? WHERE tournament=? AND round=? AND parallel=? AND player=? AND songnumber=? AND chosen=\'t\'', undef,
71                         $val, $tournament, $round, $group, $1, $2);
72         }
73 }
74
75 $dbh->do('UPDATE bigscreen.active_groups SET last_updated=now() WHERE tournament=? AND round=? AND parallel=?',
76         undef, $tournament, $round, $group);
77 $dbh->do('NOTIFY scores');
78
79 $dbh->commit;
80 $dbh->disconnect;
81
82 ccbs::print_see_other('show-tournament.pl?id=' . $tournament . '#round' . $round);
83