@@ -6,7 +6,7 @@ var sprintf = require('sprintf-js').sprintf;
6
6
var h = require ( '../helper' ) ;
7
7
var chalk = require ( '../chalk' ) ;
8
8
var log = require ( '../log' ) ;
9
- var queue = require ( '../queue' ) ;
9
+ var Queue = require ( '../queue' ) ;
10
10
var core = require ( '../core' ) ;
11
11
var session = require ( '../session' ) ;
12
12
@@ -41,67 +41,62 @@ var cmd = {
41
41
}
42
42
} ;
43
43
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
+ }
57
57
58
58
if ( argv . extra ) {
59
59
// have to get problem details, e.g. problem description.
60
60
core . getProblem ( problem . id , function ( e , problem ) {
61
61
if ( e ) return done ( e ) ;
62
-
63
- exportSubmission ( argv , problem , done ) ;
62
+ exportSubmission ( problem , argv , onTaskDone ) ;
64
63
} ) ;
65
64
} else {
66
- exportSubmission ( argv , problem , done ) ;
65
+ exportSubmission ( problem , argv , onTaskDone ) ;
67
66
}
68
67
}
69
68
70
- function exportSubmission ( argv , problem , cb ) {
69
+ function exportSubmission ( problem , argv , cb ) {
71
70
core . getSubmissions ( problem , function ( e , submissions ) {
72
71
if ( e ) return cb ( e ) ;
73
- if ( submissions . length === 0 ) return cb ( 'no submissions?' ) ;
72
+ if ( submissions . length === 0 )
73
+ return cb ( 'No submissions?' ) ;
74
74
75
75
// get obj list contain required filetype
76
- var submissionInTargetType = _ . filter ( submissions , function ( x ) {
76
+ submissions = _ . filter ( submissions , function ( x ) {
77
77
return argv . lang === 'all' || argv . lang === x . lang ;
78
78
} ) ;
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.' ) ;
87
81
88
82
// 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' ) ;
92
87
93
- var filename = sprintf ( '%s/%d.%s.%s.%s%s' ,
88
+ var f = sprintf ( '%s/%d.%s.%s.%s%s' ,
94
89
argv . outdir ,
95
90
problem . id ,
96
91
problem . slug ,
97
92
submission . id ,
98
- submissionState ,
93
+ submission . ac ? 'ac' : 'notac' ,
99
94
h . langToExt ( submission . lang ) ) ;
100
95
96
+ h . mkdir ( argv . outdir ) ;
101
97
// 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 ) ) ;
105
100
106
101
core . getSubmission ( submission , function ( e , submission ) {
107
102
if ( e ) return cb ( e ) ;
@@ -111,29 +106,22 @@ function exportSubmission(argv, problem, cb) {
111
106
code : submission . code ,
112
107
tpl : argv . extra ? 'detailed' : 'codeonly'
113
108
} ;
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 ) ) ;
120
112
} ) ;
121
113
} ) ;
122
114
}
123
115
124
116
cmd . handler = function ( argv ) {
125
117
session . argv = argv ;
126
- var doTask = _ . partial ( onTaskRun , argv , _ , _ ) ;
118
+ var q = new Queue ( null , { argv : argv } , doTask ) ;
127
119
128
120
if ( argv . all ) {
129
121
core . getProblems ( function ( e , problems ) {
130
122
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 ( ) ;
137
125
} ) ;
138
126
return ;
139
127
}
@@ -143,8 +131,7 @@ cmd.handler = function(argv) {
143
131
144
132
core . getProblem ( argv . keyword , function ( e , problem ) {
145
133
if ( e ) return log . fail ( e ) ;
146
-
147
- queue . run ( [ problem ] , doTask ) ;
134
+ q . addTask ( problem ) . run ( ) ;
148
135
} ) ;
149
136
} ;
150
137
0 commit comments