@@ -6,44 +6,78 @@ var sprintf = require('sprintf-js').sprintf;
66
77var core = require ( '../core' ) ;
88var h = require ( '../helper' ) ;
9+ var queue = require ( '../queue' ) ;
910
1011var cmd = {
11- command : 'submission < keyword> ' ,
12+ command : 'submission [ keyword] ' ,
1213 desc : 'Retrieve earlier submission by name or index' ,
1314 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+ }
1520 }
1621} ;
1722
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+ }
2131
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 ) ;
2434
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?' ) ;
2638
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+ } ) ;
3244
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 ] ;
3747
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 ) ;
4050
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 ) ;
4354
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' ;
4567 } ) ;
68+
69+ queue . run ( problems , getSubmission ) ;
4670 } ) ;
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 ) ;
4781 } ) ;
4882} ;
4983
0 commit comments