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