Introduce a 'games this month' column
[asr.git] / reset.pl
blobe06fa925c98c04392f925e2dc5a603b66ac102e0
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
5 use ASR;
6 use POSIX qw/strftime/;
7 use Carp qw/cluck/;
8 $SIG{__WARN__} = sub { cluck $_[0] };
10 my $ctx = ASR::Ladder->new;
12 $ctx->header('Monthly reset');
14 if ($ctx->param('pwd') ne $ctx->{adminpw}) {
15 print $ctx->p('Invalid password.');
16 $ctx->footer;
17 exit;
20 =can trigger infinite loop :/
21 if ((system('mysqldump -u asr -p'.$ctx->{password}.' asr >/tmp/ASR-`date +%F`.dump') >> 8) != 0) {
22 print $ctx->p("Error: Database backup failed: $?, $!");
23 $ctx->footer;
24 exit;
26 =cut
28 ($ctx->db_do('START TRANSACTION') and
29 $ctx->db_do('CREATE TEMPORARY TABLE inactive (id INT)') and
30 $ctx->db_do('INSERT INTO inactive SELECT id FROM raw_ladder ' .
31 'WHERE last_game IS NULL OR last_game < ' .
32 'DATE("'.$ctx->param('cutoffdate').'")') and
33 $ctx->db_do('DELETE FROM player WHERE id IN (SELECT id FROM inactive)') and
34 $ctx->db_do('DROP TABLE inactive') and
35 $ctx->db_do('COMMIT')) or
36 print $ctx->p('Removing inactive players failed.');
38 $ctx->db_do('UPDATE player SET score = 100 WHERE score < 100') or
39 print $ctx->p('Flooring score to 100 failed.');
40 $ctx->db_do('UPDATE player SET score = 200 WHERE score > 200') or
41 print $ctx->p('Ceiling score to 200 failed.');
42 $ctx->db_do('UPDATE player SET score = 100 + (score - 100) / 2 WHERE score > 100') or
43 print $ctx->p('Halving score failed.');
45 $ctx->footer;