X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=gruntmaster-problem;h=7e979cdc05164e6c8c4788efb6d19756e7ada04b;hb=ca77adce9ec73cdbb45cf3fe35fc04191b0f2b3d;hp=1b240e0c4b3772973ac5d53f66d140b4bf961acd;hpb=5214523a16467b3ce47a7b5783f5171c419e80a5;p=gruntmaster-data.git diff --git a/gruntmaster-problem b/gruntmaster-problem index 1b240e0..7e979cd 100755 --- a/gruntmaster-problem +++ b/gruntmaster-problem @@ -3,15 +3,25 @@ use v5.14; use Gruntmaster::Data; +use File::Temp qw/tempfile/; + use IO::Prompter [ -style => 'bold', '-stdio', '-verbatim' ]; -use File::Slurp qw/read_file/; +use File::Slurp qw/read_file write_file/; use JSON qw/encode_json/; use Term::ANSIColor qw/RED RESET/; use Getopt::Long qw/:config require_order/; +use constant LEVEL_VALUES => { + beginner => 100, + easy => 250, + medium => 500, + hard => 1000, +}; + ################################################## -my $db = Gruntmaster::Data->connect('dbi:Pg:'); +my $dsn = $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:'; +my $db = Gruntmaster::Data->connect($dsn); sub cmd_help{ exec perldoc => $0 @@ -23,8 +33,10 @@ sub cmd_add{ my $private = prompt('Private?', '-yn') eq 'y'; my $contest = prompt 'Contest'; my $author = prompt 'Problem author (full name)'; + my $writer = prompt 'Problem statement writer (full name)'; my $owner = prompt 'Problem owner (username)'; my $level = prompt 'Problem level', -menu => "beginner\neasy\nmedium\nhard"; + my $value = LEVEL_VALUES->{$level}; my $statement = read_file prompt 'File with problem statement', -complete => 'filenames'; my $generator = prompt 'Generator', -menu => "File\nRun\nUndef"; my $runner = prompt 'Runner', -menu => "File\nVerifier\nInteractive"; @@ -53,30 +65,31 @@ sub cmd_add{ $verformat = prompt "[Runner::$runner] Verifier format", -menu => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/]; } - $db->problems->create ({ + my %options = ( id => $id, name => $name, level => $level, + value => $value, statement => $statement, author => $author, + writer => $writer, owner => $owner, generator => $generator, runner => $runner, judge => $judge, testcnt => $testcnt, - (private => $private)x!! $private, - (timeout => $timeout)x!! $timeout, - (olimit => $olimit)x!! $olimit, - (tests => encode_json \@tests)x!! @tests, - (gensource => $gensource)x!! $gensource, - (genformat => $genformat)x!! $genformat, - (versource => $versource)x!! $versource, - (verformat => $verformat)x!! $verformat, - }); + ); + $options{private} = $private if $private; + $options{timeout} = $timeout if $timeout; + $options{olimit} = $olimit if $olimit; + $options{tests} = encode_json \@tests if @tests; + $options{gensource} = $gensource if $gensource; + $options{genformat} = $genformat if $genformat; + $options{versource} = $versource if $versource; + $options{verformat} = $verformat if $verformat; + $db->problems->create (\%options); $db->contest_problems->create({problem => $id, contest => $contest}) if $contest; - #PUBLISH genpage => $contest ? "ct/$contest/pb/index.html" : 'pb/index.html'; - #PUBLISH genpage => $contest ? "ct/$contest/pb/$id.html" : "pb/$id.html"; } sub cmd_set{ @@ -85,27 +98,49 @@ sub cmd_set{ my ($id, %values) = @ARGV; %values = map { $_ => scalar read_file $values{$_} } keys %values if $file; $db->problem($id)->update(\%values); - #PUBLISH genpage => 'pb/index.html'; - #PUBLISH genpage => "pb/$id.html"; } sub cmd_get{ - my ($id, $col) = @ARGV; + my ($id, $col) = @_; say $db->problem($id)->get_column($col) } +sub cmd_edit{ + my ($id, $col) = @_; + my ($fh, $file) = tempfile 'gruntmaster-problem-editXXXX', TMPDIR => 1, UNLINK => 1; + write_file $fh, $db->problem($id)->get_column($col); + close $fh; + my $editor = $ENV{EDITOR} // 'editor'; + system $editor, $file; + $db->problem($id)->update({$col => read_file $file}); +} + sub cmd_list{ local $, = "\n"; - say map {$_->id} $db->problems->all; + say map {$_->id} $db->problems->all } sub cmd_rm{ - $db->problem(shift)->delete; - #PUBLISH genpage => $contest ? "ct/$contest/pb/index.html" : 'pb/index.html'; + my ($id) = @_; + $db->problem($id)->delete; } sub cmd_show{ - local $_ = shift or goto &cmd_list; + my %columns = $db->problem(shift)->get_columns; + print <. - =over =item B @@ -165,6 +199,10 @@ Sets the I configuration option of problem I to I. Get the value of the I configuration option of problem I +=item B I I + +Opens an editor with the value of the I configuration option. After the editor exits, the option is updated to the value of the file. + =item B --file I I I Sets the I configuration option of problem I to the contents of the file I. @@ -179,10 +217,9 @@ Marius Gavrilescu Emarius@ieval.roE Copyright (C) 2014 by Marius Gavrilescu -This library is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.18.1 or, +at your option, any later version of Perl 5 you may have available. =cut