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