@@ -7,8 +7,15 @@ import { Logger } from '../utils';
7
7
const logger = new Logger ( 'fetcher' ) ;
8
8
const fetch = ( url : string , type : 'get' | 'post' = 'get' ) => superagent [ type ] ( new URL ( url , config . server ) . toString ( ) )
9
9
. set ( 'Authorization' , config . token ) . set ( 'Accept' , 'application/json' ) ;
10
-
11
- class DomJudgeFetcher extends Service {
10
+ export interface IBasicFetcher {
11
+ contest : Record < string , any >
12
+ cron ( ) : Promise < void >
13
+ contestInfo ( ) : Promise < boolean >
14
+ teamInfo ?( ) : Promise < void >
15
+ balloonInfo ?( all : boolean ) : Promise < void >
16
+ setBalloonDone ?( bid : string ) : Promise < void >
17
+ }
18
+ class BasicFetcher extends Service implements IBasicFetcher {
12
19
contest : any ;
13
20
constructor ( ctx : Context ) {
14
21
super ( ctx , 'fetcher' , true ) ;
@@ -23,6 +30,27 @@ class DomJudgeFetcher extends Service {
23
30
await this . balloonInfo ( first ) ;
24
31
}
25
32
33
+ async contestInfo ( ) {
34
+ const old = this ?. contest ?. id ;
35
+ this . contest = { name : 'No Contest' , id : 'server-mode' } ;
36
+ return old === this . contest . id ;
37
+ }
38
+
39
+ async teamInfo ( ) {
40
+ logger . debug ( 'Found 0 teams' ) ;
41
+ }
42
+
43
+ async balloonInfo ( all ) {
44
+ logger . debug ( all ? 'Sync all balloons...' : 'Sync new balloons...' ) ;
45
+ logger . debug ( 'Found 0 balloons in Server Mode' ) ;
46
+ }
47
+
48
+ async setBalloonDone ( bid ) {
49
+ logger . debug ( `Balloon ${ bid } set done` ) ;
50
+ }
51
+ }
52
+
53
+ class DomJudgeFetcher extends BasicFetcher {
26
54
async contestInfo ( ) {
27
55
const { body } = await fetch ( '/api/v4/contests?onlyActive=true' ) ;
28
56
if ( ! body || ! body . length ) {
@@ -46,7 +74,7 @@ class DomJudgeFetcher extends Service {
46
74
for ( const team of teams ) {
47
75
await this . ctx . db . teams . update ( { id : team . id } , { $set : team } , { upsert : true } ) ;
48
76
}
49
- logger . info ( `Found ${ teams . length } teams` ) ;
77
+ logger . debug ( `Found ${ teams . length } teams` ) ;
50
78
}
51
79
52
80
async balloonInfo ( all ) {
@@ -84,19 +112,25 @@ class DomJudgeFetcher extends Service {
84
112
} ,
85
113
} , { upsert : true } ) ;
86
114
}
87
- logger . info ( `Found ${ balloons . length } balloons` ) ;
115
+ logger . debug ( `Found ${ balloons . length } balloons` ) ;
88
116
}
89
117
90
118
async setBalloonDone ( bid ) {
91
119
await fetch ( `/api/v4/contests/${ this . contest . id } /balloons/${ bid } /done` , 'post' ) ;
92
- logger . info ( `Balloon ${ bid } set done` ) ;
120
+ logger . debug ( `Balloon ${ bid } set done` ) ;
93
121
}
94
122
}
95
123
96
124
const fetcherList = {
125
+ server : BasicFetcher ,
97
126
domjudge : DomJudgeFetcher ,
127
+ hydro : BasicFetcher , // TODO: HydroFetcher
98
128
} ;
99
129
100
130
export async function apply ( ctx ) {
101
- if ( config . token && config . server ) ctx . plugin ( fetcherList [ config . type ] ) ;
131
+ if ( config . type !== 'server' ) {
132
+ logger . info ( 'Fetch mode: ' , config . type ) ;
133
+ }
134
+ ctx . provide ( 'fetcher' , undefined , true ) ;
135
+ ctx . fetcher = await new fetcherList [ config . type ] ( ctx ) ;
102
136
}
0 commit comments