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