Bump version and update Changes
[gruntmaster-data.git] / gruntmaster-problem
CommitLineData
014ee8a6
MG
1#!/usr/bin/perl -w
2use v5.14;
3
4use Gruntmaster::Data;
014ee8a6
MG
5
6use IO::Prompter [ -style => 'bold', '-stdio', '-verbatim' ];
7use File::Slurp qw/read_file/;
756368d0 8use JSON qw/encode_json/;
014ee8a6 9use Term::ANSIColor qw/RED RESET/;
0d2f8987 10use Getopt::Long qw/:config require_order/;
014ee8a6
MG
11
12##################################################
13
30e287c3
MG
14my $dsn = $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:';
15my $db = Gruntmaster::Data->connect($dsn);
014ee8a6
MG
16
17sub cmd_help{
18 exec perldoc => $0
19}
20
014ee8a6
MG
21sub cmd_add{
22 my $id = shift;
23 my $name = prompt 'Problem name';
646feed4 24 my $private = prompt('Private?', '-yn') eq 'y';
756368d0 25 my $contest = prompt 'Contest';
014ee8a6 26 my $author = prompt 'Problem author (full name)';
6544497e 27 my $writer = prompt 'Problem statement writer (full name)';
014ee8a6
MG
28 my $owner = prompt 'Problem owner (username)';
29 my $level = prompt 'Problem level', -menu => "beginner\neasy\nmedium\nhard";
30 my $statement = read_file prompt 'File with problem statement', -complete => 'filenames';
cd25d613
MG
31 my $generator = prompt 'Generator', -menu => "File\nRun\nUndef";
32 my $runner = prompt 'Runner', -menu => "File\nVerifier\nInteractive";
33 my $judge = prompt 'Judge', -menu => "Absolute\nPoints";
34 my $testcnt = prompt 'Test count', '-i';
014ee8a6 35
cd25d613
MG
36 my $timeout = prompt 'Time limit (seconds)', '-n';
37 my $olimit = prompt 'Output limit (bytes)', '-i';
014ee8a6
MG
38 say 'Memory limits are broken, so I won\'t ask you for one';
39
756368d0 40 my (@tests, $gensource, $genformat, $versource, $verformat);
014ee8a6 41
756368d0
MG
42 if ($generator eq 'Run') {
43 $gensource = read_file prompt, '[Generator::Run] Generator file name', -complete => 'filenames';
44 $genformat = prompt '[Generator::Run] Generator format', -menu => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/];
014ee8a6
MG
45 }
46
756368d0
MG
47 if ($runner eq 'File') {
48 my $default = $judge eq 'Points' ? 10 : 'Ok';
49 $tests[$_ - 1] = prompt "[Runner::File] Score for test ${_} [$default]", -default => $default for 1 .. $testcnt;
50 }
014ee8a6 51
756368d0
MG
52 if ($runner eq 'Verifier' || $runner eq 'Interactive') {
53 say RED, 'WARNING: Runner::Interactive is experimental', RESET if $runner eq 'Interactive';
54 $versource = prompt "[Runner::$runner] Verifier file name", -complete => 'filenames';
55 $verformat = prompt "[Runner::$runner] Verifier format", -menu => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/];
014ee8a6
MG
56 }
57
4af36605 58 my %options = (
756368d0 59 id => $id,
646feed4
MG
60 name => $name,
61 level => $level,
62 statement => $statement,
63 author => $author,
6544497e 64 writer => $writer,
646feed4
MG
65 owner => $owner,
66 generator => $generator,
67 runner => $runner,
68 judge => $judge,
69 testcnt => $testcnt,
4af36605
MG
70 );
71 $options{private} = $private if $private;
72 $options{timeout} = $timeout if $timeout;
73 $options{olimit} = $olimit if $olimit;
74 $options{tests} = encode_json \@tests if @tests;
75 $options{gensource} = $gensource if $gensource;
76 $options{genformat} = $genformat if $genformat;
77 $options{versource} = $versource if $versource;
78 $options{verformat} = $verformat if $verformat;
79 $db->problems->create (\%options);
756368d0
MG
80
81 $db->contest_problems->create({problem => $id, contest => $contest}) if $contest;
014ee8a6
MG
82}
83
84sub cmd_set{
85 my $file;
86 GetOptions ( 'file!' => \$file );
4af36605 87 my ($id, %values) = @_;
014ee8a6 88 %values = map { $_ => scalar read_file $values{$_} } keys %values if $file;
4af36605 89 $db->problem($id)->update(\%values)
014ee8a6
MG
90}
91
5214523a 92sub cmd_get{
4af36605 93 my ($id, $col) = @_;
5214523a
MG
94 say $db->problem($id)->get_column($col)
95}
96
014ee8a6
MG
97sub cmd_list{
98 local $, = "\n";
4af36605 99 say map {$_->id} $db->problems->all
014ee8a6
MG
100}
101
102sub cmd_rm{
4af36605 103 $db->problem(shift)->delete
014ee8a6
MG
104}
105
106sub cmd_show{
4af36605
MG
107 my %columns = $db->problem(shift)->get_columns;
108 print <<END
109Name: $columns{name}
110Author: $columns{author}
111Statement written by: $columns{writer}
112Owner: $columns{owner}
113Level: $columns{level}
114Output limit: $columns{olimit}
115Time limit: $columns{timeout}
116Test count: $columns{testcnt}
117Generator: $columns{generator}
118Runner: $columns{runner}
119Judge: $columns{judge}
120Private: $columns{private}
121END
014ee8a6
MG
122}
123
124##################################################
125
014ee8a6
MG
126my $cmd = 'cmd_' . shift;
127cmd_help unless exists $main::{$cmd};
128no strict 'refs';
756368d0 129$cmd->(@ARGV);
014ee8a6
MG
130
1311;
132__END__
133
134=encoding utf-8
135
136=head1 NAME
137
138gruntmaster-problem - shell interface to Gruntmaster 6000 problems
139
140=head1 SYNOPSIS
141
5214523a
MG
142 gruntmaster-problem add problem_id
143 gruntmaster-problem list
144 gruntmaster-problem rm problem_id
145 gruntmaster-problem show problem_id
146 gruntmaster-problem set [--file] problem_id key value
147 gruntmaster-problem get problem_id key
014ee8a6
MG
148
149=head1 DESCRIPTION
150
e90402be
MG
151gruntmaster-problem is a tool for managing problems.
152
e90402be
MG
153=over
154
155=item B<list>
156
157Prints the list of problems in the selected contest.
158
159=item B<show> I<id>
160
161Prints detailed information about problem I<id>.
162
163=item B<add> I<id>
164
165Adds a new problem with id I<id>.
166
167=item B<rm> I<id>
168
169Removes the problem with id I<id>.
170
171=item B<set> I<id> I<key> I<value>
172
173Sets the I<key> configuration option of problem I<id> to I<value>.
174
5214523a
MG
175=item B<get> I<id> I<key>
176
177Get the value of the I<key> configuration option of problem I<id>
178
e90402be
MG
179=item B<set> --file I<id> I<key> I<file>
180
181Sets the I<key> configuration option of problem I<id> to the contents of the file I<file>.
182
183=back
184
185=head1 AUTHOR
186
187Marius Gavrilescu E<lt>marius@ieval.roE<gt>
188
189=head1 COPYRIGHT AND LICENSE
190
191Copyright (C) 2014 by Marius Gavrilescu
192
4af36605
MG
193This library is free software; you can redistribute it and/or modify
194it under the same terms as Perl itself, either Perl version 5.18.1 or,
195at your option, any later version of Perl 5 you may have available.
014ee8a6
MG
196
197
198=cut
This page took 0.022627 seconds and 4 git commands to generate.