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