@@ -6,44 +6,78 @@ var sprintf = require('sprintf-js').sprintf;
6
6
7
7
var core = require ( '../core' ) ;
8
8
var h = require ( '../helper' ) ;
9
+ var queue = require ( '../queue' ) ;
9
10
10
11
var cmd = {
11
- command : 'submission < keyword> ' ,
12
+ command : 'submission [ keyword] ' ,
12
13
desc : 'Retrieve earlier submission by name or index' ,
13
14
builder : {
14
- // TODO: retrieve all?? That would be very time costing...
15
+ all : {
16
+ alias : 'a' ,
17
+ type : 'boolean' ,
18
+ describe : 'Retrieve for all problems'
19
+ }
15
20
}
16
21
} ;
17
22
18
- cmd . handler = function ( argv ) {
19
- core . getProblem ( argv . keyword , function ( e , problem ) {
20
- if ( e ) return console . log ( 'ERROR:' , e ) ;
23
+ function getSubmissionDone ( e , msg , problem , cb ) {
24
+ console . log ( sprintf ( '[%3d] %-60s %s' ,
25
+ problem . id ,
26
+ problem . name ,
27
+ ( e ? chalk . red ( 'ERROR: ' + e ) : chalk . yellow . underline ( msg ) )
28
+ ) ) ;
29
+ if ( cb ) cb ( e ) ;
30
+ }
21
31
22
- core . getSubmissions ( problem , function ( e , submissions ) {
23
- if ( e ) return console . log ( 'ERROR:' , e ) ;
32
+ function getSubmission ( problem , cb ) {
33
+ var done = _ . partial ( getSubmissionDone , _ , _ , problem , cb ) ;
24
34
25
- console . log ( 'Total: %s submissions' , chalk . yellow ( submissions . length ) ) ;
35
+ core . getSubmissions ( problem , function ( e , submissions ) {
36
+ if ( e ) return done ( e ) ;
37
+ if ( submissions . length === 0 ) return done ( 'no submissions?' ) ;
26
38
27
- // Find the latest accepted one
28
- var submission = _ . find ( submissions , function ( x ) {
29
- // TODO: select by lang? or always select the latest one ?
30
- return x . state === 'Accepted' ;
31
- } ) ;
39
+ // find the latest accepted one
40
+ var submission = _ . find ( submissions , function ( x ) {
41
+ // TODO: select by lang?
42
+ return x . state === 'Accepted' ;
43
+ } ) ;
32
44
33
- if ( ! submission ) {
34
- console . log ( 'No Accepted found?' ) ;
35
- return ;
36
- }
45
+ // if no accepted, use the latest non-accepted one
46
+ submission = submission || submissions [ 0 ] ;
37
47
38
- core . getSubmission ( submission , function ( e , submission ) {
39
- if ( e ) return console . log ( 'ERROR:' , e ) ;
48
+ core . getSubmission ( submission , function ( e , submission ) {
49
+ if ( e ) return done ( e ) ;
40
50
41
- var f = problem . key + '.' + submission . id + h . langToExt ( submission . lang ) ;
42
- fs . writeFileSync ( f , submission . code ) ;
51
+ var f = sprintf ( '%s.%s.%s%s' , problem . key , submission . id ,
52
+ problem . state , h . langToExt ( submission . lang ) ) ;
53
+ fs . writeFileSync ( f , submission . code ) ;
43
54
44
- console . log ( sprintf ( 'Saved: %s' , chalk . yellow . underline ( f ) ) ) ;
55
+ done ( null , f ) ;
56
+ } ) ;
57
+ } ) ;
58
+ }
59
+
60
+ cmd . handler = function ( argv ) {
61
+ if ( argv . all ) {
62
+ core . getProblems ( function ( e , problems ) {
63
+ if ( e ) return console . log ( 'ERROR:' , e ) ;
64
+
65
+ problems = problems . filter ( function ( q ) {
66
+ return q . state === 'ac' ;
45
67
} ) ;
68
+
69
+ queue . run ( problems , getSubmission ) ;
46
70
} ) ;
71
+ return ;
72
+ }
73
+
74
+ if ( ! argv . keyword )
75
+ return console . log ( 'ERROR: missing keyword?' ) ;
76
+
77
+ core . getProblem ( argv . keyword , function ( e , problem ) {
78
+ if ( e ) return console . log ( 'ERROR:' , e ) ;
79
+
80
+ queue . run ( [ problem ] , getSubmission ) ;
47
81
} ) ;
48
82
} ;
49
83
0 commit comments