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