@@ -6,7 +6,7 @@ var sprintf = require('sprintf-js').sprintf;
66var h = require ( '../helper' ) ;
77var chalk = require ( '../chalk' ) ;
88var log = require ( '../log' ) ;
9- var queue = require ( '../queue' ) ;
9+ var Queue = require ( '../queue' ) ;
1010var core = require ( '../core' ) ;
1111var session = require ( '../session' ) ;
1212
@@ -41,67 +41,62 @@ var cmd = {
4141 }
4242} ;
4343
44- function onTaskDone ( e , msg , problem , cb ) {
45- // NOTE: msg color means different purpose:
46- // - red: error
47- // - green: accepted, fresh download
48- // - yellow: not ac-ed, fresh download
49- // - white: existed already, skip download
50- log . printf ( '[%3d] %-60s %s' , problem . id , problem . name ,
51- ( e ? chalk . red ( 'ERROR: ' + ( e . msg || e ) ) : msg ) ) ;
52- if ( cb ) cb ( e ) ;
53- }
54-
55- function onTaskRun ( argv , problem , cb ) {
56- var done = _ . partial ( onTaskDone , _ , _ , problem , cb ) ;
44+ function doTask ( problem , queue , cb ) {
45+ var argv = queue . ctx . argv ;
46+
47+ function onTaskDone ( e , msg ) {
48+ // NOTE: msg color means different purpose:
49+ // - red: error
50+ // - green: accepted, fresh download
51+ // - yellow: not ac-ed, fresh download
52+ // - white: existed already, skip download
53+ log . printf ( '[%3d] %-60s %s' , problem . id , problem . name ,
54+ ( e ? chalk . red ( 'ERROR: ' + ( e . msg || e ) ) : msg ) ) ;
55+ if ( cb ) cb ( e ) ;
56+ }
5757
5858 if ( argv . extra ) {
5959 // have to get problem details, e.g. problem description.
6060 core . getProblem ( problem . id , function ( e , problem ) {
6161 if ( e ) return done ( e ) ;
62-
63- exportSubmission ( argv , problem , done ) ;
62+ exportSubmission ( problem , argv , onTaskDone ) ;
6463 } ) ;
6564 } else {
66- exportSubmission ( argv , problem , done ) ;
65+ exportSubmission ( problem , argv , onTaskDone ) ;
6766 }
6867}
6968
70- function exportSubmission ( argv , problem , cb ) {
69+ function exportSubmission ( problem , argv , cb ) {
7170 core . getSubmissions ( problem , function ( e , submissions ) {
7271 if ( e ) return cb ( e ) ;
73- if ( submissions . length === 0 ) return cb ( 'no submissions?' ) ;
72+ if ( submissions . length === 0 )
73+ return cb ( 'No submissions?' ) ;
7474
7575 // get obj list contain required filetype
76- var submissionInTargetType = _ . filter ( submissions , function ( x ) {
76+ submissions = _ . filter ( submissions , function ( x ) {
7777 return argv . lang === 'all' || argv . lang === x . lang ;
7878 } ) ;
79- if ( submissionInTargetType . length === 0 ) {
80- return cb ( 'No previous submission in required language.' ) ;
81- }
82- var submission = _ . find ( submissionInTargetType , function ( x ) {
83- return x . status_display === 'Accepted' ;
84- } ) ;
85-
86- var submissionState = submission === undefined ? 'notac' : 'ac' ;
79+ if ( submissions . length === 0 )
80+ return cb ( 'No submissions in required language.' ) ;
8781
8882 // if no accepted, use the latest non-accepted one
89- submission = submission || submissionInTargetType [ 0 ] ;
90-
91- h . mkdir ( argv . outdir ) ;
83+ var submission = _ . find ( submissions , function ( x ) {
84+ return x . status_display === 'Accepted' ;
85+ } ) || submissions [ 0 ] ;
86+ submission . ac = ( submission . status_display === 'Accepted' ) ;
9287
93- var filename = sprintf ( '%s/%d.%s.%s.%s%s' ,
88+ var f = sprintf ( '%s/%d.%s.%s.%s%s' ,
9489 argv . outdir ,
9590 problem . id ,
9691 problem . slug ,
9792 submission . id ,
98- submissionState ,
93+ submission . ac ? 'ac' : 'notac' ,
9994 h . langToExt ( submission . lang ) ) ;
10095
96+ h . mkdir ( argv . outdir ) ;
10197 // skip the existing cached submissions
102- if ( fs . existsSync ( filename ) ) {
103- return cb ( null , chalk . underline ( filename ) ) ;
104- }
98+ if ( fs . existsSync ( f ) )
99+ return cb ( null , chalk . underline ( f ) ) ;
105100
106101 core . getSubmission ( submission , function ( e , submission ) {
107102 if ( e ) return cb ( e ) ;
@@ -111,29 +106,22 @@ function exportSubmission(argv, problem, cb) {
111106 code : submission . code ,
112107 tpl : argv . extra ? 'detailed' : 'codeonly'
113108 } ;
114- fs . writeFileSync ( filename , core . exportProblem ( problem , opts ) ) ;
115-
116- if ( submission . status_display === 'Accepted' )
117- cb ( null , chalk . green . underline ( filename ) ) ;
118- else
119- cb ( null , chalk . yellow . underline ( filename ) ) ;
109+ fs . writeFileSync ( f , core . exportProblem ( problem , opts ) ) ;
110+ cb ( null , submission . ac ? chalk . green . underline ( f )
111+ : chalk . yellow . underline ( f ) ) ;
120112 } ) ;
121113 } ) ;
122114}
123115
124116cmd . handler = function ( argv ) {
125117 session . argv = argv ;
126- var doTask = _ . partial ( onTaskRun , argv , _ , _ ) ;
118+ var q = new Queue ( null , { argv : argv } , doTask ) ;
127119
128120 if ( argv . all ) {
129121 core . getProblems ( function ( e , problems ) {
130122 if ( e ) return log . fail ( e ) ;
131-
132- problems = problems . filter ( function ( q ) {
133- return q . state === 'ac' || q . state === 'notac' ;
134- } ) ;
135-
136- queue . run ( problems , doTask ) ;
123+ problems = problems . filter ( function ( q ) { return q . state === 'ac' || q . state === 'notac' ; } ) ;
124+ q . addTasks ( problems ) . run ( ) ;
137125 } ) ;
138126 return ;
139127 }
@@ -143,8 +131,7 @@ cmd.handler = function(argv) {
143131
144132 core . getProblem ( argv . keyword , function ( e , problem ) {
145133 if ( e ) return log . fail ( e ) ;
146-
147- queue . run ( [ problem ] , doTask ) ;
134+ q . addTask ( problem ) . run ( ) ;
148135 } ) ;
149136} ;
150137
0 commit comments