@@ -5,165 +5,47 @@ var util = require('util');
55var _ = require ( 'underscore' ) ;
66
77var log = require ( './log' ) ;
8- var cache = require ( './cache' ) ;
9- var config = require ( './config' ) ;
10- var client = require ( './leetcode_client' ) ;
11- var queue = require ( './queue' ) ;
128var h = require ( './helper' ) ;
9+ var Plugin = require ( './plugin' ) ;
10+ var session = require ( './session' ) ;
1311
14- var KEY_USER = '.user' ;
15- var KEY_PROBLEMS = 'problems' ;
16-
17- function getkey ( problem ) {
18- return problem . id + '.' + problem . slug + '.' + problem . category ;
19- }
20-
21- function saveProblem ( problem ) {
22- // it would be better to leave specific problem cache being user
23- // independent, thus try to reuse existing cache as much as possible
24- // after changing user.
25- var p = _ . omit ( problem , [ 'locked' , 'state' , 'starred' ] ) ;
26- return cache . set ( getkey ( p ) , p ) ;
27- }
28-
29- function saveUser ( user ) {
30- // when auto login enabled, have to save password to re-login later
31- // otherwise don't dump password for the sake of security.
32- var u = _ . omit ( user , config . AUTO_LOGIN ? [ ] : [ 'pass' ] ) ;
33- cache . set ( KEY_USER , u ) ;
34- }
35-
36- var core = { } ;
37-
38- core . getProblems = function ( cb ) {
39- var problems = cache . get ( KEY_PROBLEMS ) ;
40- if ( problems ) {
41- log . debug ( 'loading from problems.json' ) ;
42- return cb ( null , problems ) ;
43- }
44-
45- var user = this . getUser ( ) ;
46- var KEY_TMP = '.tmp' ;
47-
48- var doTask = function ( category , taskDone ) {
49- log . debug ( category + ': running getProblems' ) ;
50- client . getProblems ( category , user , function ( e , problems ) {
51- if ( e ) {
52- log . debug ( category + ': failed to getProblems: ' + e . msg ) ;
53- } else {
54- log . debug ( category + ': getProblems got ' +
55- problems . length + ' problems' ) ;
56- problems = cache . get ( KEY_TMP ) . concat ( problems ) ;
57- cache . set ( KEY_TMP , problems ) ;
58- }
59- return taskDone ( e ) ;
60- } ) ;
61- } ;
62-
63- cache . set ( KEY_TMP , [ ] ) ;
64- queue . run ( config . CATEGORIES , doTask , function ( e ) {
65- if ( e ) return cb ( e ) ;
66-
67- saveUser ( user ) ;
68- var problems = cache . get ( KEY_TMP ) ;
69- cache . set ( KEY_PROBLEMS , problems ) ;
70- cache . del ( KEY_TMP ) ;
71-
72- return cb ( null , problems ) ;
73- } ) ;
74- } ;
12+ var core = new Plugin ( 'core' , 'Core' , '20170722' , 'Plugins manager' ) ;
7513
7614core . getProblem = function ( keyword , cb ) {
7715 this . getProblems ( function ( e , problems ) {
7816 if ( e ) return cb ( e ) ;
7917
18+ var problem ;
8019 keyword = Number ( keyword ) || keyword ;
8120
82- var problem ;
8321 if ( keyword === undefined ) {
84- log . debug ( 'random select problem' ) ;
85- var user = core . getUser ( ) ;
22+ var user = session . getUser ( ) ;
8623 // random select one that not AC-ed yet
8724 problems = _ . filter ( problems , function ( x ) {
8825 if ( x . state === 'ac' ) return false ;
8926 if ( ! user . paid && x . locked ) return false ;
9027 return true ;
9128 } ) ;
92- problem = problems [ _ . random ( problems . length - 1 ) ] ;
29+ if ( problems . length > 0 )
30+ problem = problems [ _ . random ( problems . length - 1 ) ] ;
9331 } else {
9432 problem = _ . find ( problems , function ( x ) {
95- return x . id === keyword ||
96- x . name === keyword ||
97- x . slug === keyword ;
33+ return x . id === keyword || x . name === keyword || x . slug === keyword ;
9834 } ) ;
9935 }
10036
101- if ( ! problem )
102- return cb ( 'Problem not found!' ) ;
103-
104- var cachekey = getkey ( problem ) ;
105- var problemDetail = cache . get ( cachekey ) ;
106- if ( problemDetail ) {
107- log . debug ( 'loading from ' + cachekey + '.json' ) ;
108- _ . extendOwn ( problem , problemDetail ) ;
109- return cb ( null , problem ) ;
110- }
111-
112- log . debug ( 'running getProblem' ) ;
113- client . getProblem ( core . getUser ( ) , problem , function ( e , problem ) {
114- if ( e ) return cb ( e ) ;
115-
116- saveProblem ( problem ) ;
117- return cb ( null , problem ) ;
118- } ) ;
37+ if ( ! problem ) return cb ( 'Problem not found!' ) ;
38+ core . next . getProblem ( problem , cb ) ;
11939 } ) ;
12040} ;
12141
122- core . getSubmissions = function ( problem , cb ) {
123- log . debug ( 'running getSubmissions' ) ;
124- client . getSubmissions ( problem , cb ) ;
125- } ;
126-
127- core . getSubmission = function ( submission , cb ) {
128- log . debug ( 'running getSubmission' ) ;
129- client . getSubmission ( submission , cb ) ;
130- } ;
131-
132- core . testProblem = function ( problem , cb ) {
133- log . debug ( 'running testProblem' ) ;
134- client . testProblem ( problem , cb ) ;
135- } ;
136-
137- core . submitProblem = function ( problem , cb ) {
138- log . debug ( 'running submitProblem' ) ;
139- client . submitProblem ( problem , cb ) ;
140- } ;
141-
142- core . updateProblem = function ( problem , kv ) {
143- var problems = cache . get ( KEY_PROBLEMS ) ;
144- if ( ! problems ) return false ;
145-
146- var oldProblem = _ . find ( problems , function ( x ) {
147- return x . id === problem . id ;
148- } ) ;
149- if ( ! oldProblem ) return false ;
150-
151- _ . extend ( oldProblem , kv ) ;
152- _ . extend ( problem , kv ) ;
153-
154- var singleUpdated = saveProblem ( problem ) ;
155- var allUpdated = cache . set ( KEY_PROBLEMS , problems ) ;
156- return singleUpdated && allUpdated ;
157- } ;
158-
15942core . starProblem = function ( problem , starred , cb ) {
16043 if ( problem . starred === starred ) {
16144 log . debug ( 'problem is already ' + ( starred ? 'starred' : 'unstarred' ) ) ;
16245 return cb ( null , starred ) ;
16346 }
16447
165- log . debug ( 'running starProblem' ) ;
166- client . starProblem ( this . getUser ( ) , problem , starred , cb ) ;
48+ core . next . starProblem ( problem , starred , cb ) ;
16749} ;
16850
16951core . exportProblem = function ( problem , f , codeOnly ) {
@@ -189,7 +71,7 @@ core.exportProblem = function(problem, f, codeOnly) {
18971 var wrap = require ( 'wordwrap' ) ( 79 - input . comment . line . length ) ;
19072 input . desc = wrap ( desc ) . split ( '\n' ) ;
19173
192- var tpl = fs . readFileSync ( path . resolve ( __dirname , '../source.tpl' ) , 'utf-8' ) ;
74+ var tpl = h . getFileData ( path . resolve ( __dirname , '../source.tpl' ) ) ;
19375 output = _ . template ( tpl ) ( input ) ;
19476 }
19577
@@ -202,42 +84,4 @@ core.exportProblem = function(problem, f, codeOnly) {
20284 fs . writeFileSync ( f , output ) ;
20385} ;
20486
205- core . login = function ( user , cb ) {
206- this . logout ( false ) ;
207- client . login ( user , function ( e , user ) {
208- if ( e ) return cb ( e ) ;
209-
210- saveUser ( user ) ;
211- log . debug ( 'running getFavorites' ) ;
212- client . getFavorites ( function ( e , favorites ) {
213- if ( e ) return cb ( e ) ;
214-
215- // TODO: pick other useful values from favorites
216- var favorite = _ . find ( favorites . favorites . private_favorites , function ( f ) {
217- return f . name === 'Favorite' ;
218- } ) ;
219- user . hash = favorite . id_hash ;
220-
221- saveUser ( user ) ;
222- return cb ( null , user ) ;
223- } ) ;
224- } ) ;
225- } ;
226-
227- core . logout = function ( purge ) {
228- var user = this . getUser ( ) ;
229- if ( purge ) cache . del ( KEY_USER ) ;
230- // NOTE: need invalidate any user related cache
231- cache . del ( KEY_PROBLEMS ) ;
232- return user ;
233- } ;
234-
235- core . getUser = function ( ) {
236- return cache . get ( KEY_USER ) ;
237- } ;
238-
239- core . isLogin = function ( ) {
240- return this . getUser ( ) !== null ;
241- } ;
242-
24387module . exports = core ;
0 commit comments