Bump version and update Changes
[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 }
92
93 sub cmd_set{
94 my $file;
95 GetOptions ( 'file!' => \$file );
96 my ($id, %values) = @_;
97 %values = map { $_ => scalar read_file $values{$_} } keys %values if $file;
98 $db->problem($id)->update(\%values)
99 }
100
101 sub cmd_get{
102 my ($id, $col) = @_;
103 say $db->problem($id)->get_column($col)
104 }
105
106 sub cmd_list{
107 local $, = "\n";
108 say map {$_->id} $db->problems->all
109 }
110
111 sub cmd_rm{
112 $db->problem(shift)->delete
113 }
114
115 sub cmd_show{
116 my %columns = $db->problem(shift)->get_columns;
117 print <<END
118 Name: $columns{name}
119 Author: $columns{author}
120 Statement written by: $columns{writer}
121 Owner: $columns{owner}
122 Level: $columns{level}
123 Output limit: $columns{olimit}
124 Time limit: $columns{timeout}
125 Test count: $columns{testcnt}
126 Generator: $columns{generator}
127 Runner: $columns{runner}
128 Judge: $columns{judge}
129 Private: $columns{private}
130 END
131 }
132
133 ##################################################
134
135 my $cmd = 'cmd_' . shift;
136 cmd_help unless exists $main::{$cmd};
137 no strict 'refs';
138 $cmd->(@ARGV);
139
140 1;
141 __END__
142
143 =encoding utf-8
144
145 =head1 NAME
146
147 gruntmaster-problem - shell interface to Gruntmaster 6000 problems
148
149 =head1 SYNOPSIS
150
151 gruntmaster-problem add problem_id
152 gruntmaster-problem list
153 gruntmaster-problem rm problem_id
154 gruntmaster-problem show problem_id
155 gruntmaster-problem set [--file] problem_id key value
156 gruntmaster-problem get problem_id key
157
158 =head1 DESCRIPTION
159
160 gruntmaster-problem is a tool for managing problems.
161
162 =over
163
164 =item B<list>
165
166 Prints the list of problems in the selected contest.
167
168 =item B<show> I<id>
169
170 Prints detailed information about problem I<id>.
171
172 =item B<add> I<id>
173
174 Adds a new problem with id I<id>.
175
176 =item B<rm> I<id>
177
178 Removes the problem with id I<id>.
179
180 =item B<set> I<id> I<key> I<value>
181
182 Sets the I<key> configuration option of problem I<id> to I<value>.
183
184 =item B<get> I<id> I<key>
185
186 Get the value of the I<key> configuration option of problem I<id>
187
188 =item B<set> --file I<id> I<key> I<file>
189
190 Sets the I<key> configuration option of problem I<id> to the contents of the file I<file>.
191
192 =back
193
194 =head1 AUTHOR
195
196 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
197
198 =head1 COPYRIGHT AND LICENSE
199
200 Copyright (C) 2014 by Marius Gavrilescu
201
202 This library is free software; you can redistribute it and/or modify
203 it under the same terms as Perl itself, either Perl version 5.18.1 or,
204 at your option, any later version of Perl 5 you may have available.
205
206
207 =cut
This page took 0.03387 seconds and 4 git commands to generate.