@@ -3,7 +3,7 @@ import { LRUCache } from 'lru-cache';
33import moment from 'moment' ;
44import {
55 _ , avatar , BadRequestError , ContestModel , ContestNotEndedError , Context , db , findFileSync ,
6- ForbiddenError , fs , ObjectId , parseTimeMS , PERM , PRIV , ProblemConfig , ProblemModel ,
6+ ForbiddenError , fs , ObjectId , parseTimeMS , PERM , ProblemConfig , ProblemModel ,
77 randomstring , Schema , SettingModel , STATUS , STATUS_SHORT_TEXTS , STATUS_TEXTS ,
88 SystemModel , Time , Types , UserModel , Zip ,
99} from 'hydrooj' ;
@@ -47,6 +47,7 @@ export const Config = Schema.object({
4747 ipLogin : Schema . boolean ( ) . default ( false ) ,
4848 extraFields : Schema . boolean ( ) . default ( false ) ,
4949 submit : Schema . boolean ( ) . default ( false ) . description ( 'Enable submit script' ) ,
50+ contestMode : Schema . boolean ( ) . default ( false ) . description ( 'Enable contest mode' ) ,
5051} ) ;
5152
5253export function apply ( ctx : Context , config : ReturnType < typeof Config > ) {
@@ -70,25 +71,24 @@ export function apply(ctx: Context, config: ReturnType<typeof Config>) {
7071 that . session . uid = iplogin . uid ;
7172 that . session . user = that . user ;
7273 } ) ;
73-
74- const disable = ( that ) => {
75- if ( ! that . user . hasPriv ( PRIV . PRIV_EDIT_SYSTEM ) ) throw new ForbiddenError ( 'Not available' ) ;
76- } ;
77- ctx . on ( 'handler/before/HomeDomain' , disable ) ;
78- ctx . on ( 'handler/before/DomainUser' , disable ) ;
79- ctx . on ( 'handler/before/HomeMessages#post' , disable ) ;
80- ctx . on ( 'handler/before/Files' , disable ) ;
81- ctx . on ( 'handler/before/HomeAvatar' , disable ) ;
82- ctx . on ( 'handler/before/HomeSecurity' , disable ) ;
83- ctx . on ( 'handler/before/UserDetail' , disable ) ;
84- ctx . on ( 'handler/before/UserLostPass' , disable ) ;
85-
86- ctx . on ( 'handler/before/HomeSettings' , ( that ) => {
87- if ( that . user . hasPriv ( PRIV . PRIV_EDIT_SYSTEM ) ) return ;
88- if ( [ 'domain' , 'account' ] . includes ( that . args . category ) ) throw new ForbiddenError ( 'Not available' ) ;
89- } ) ;
9074 }
9175
76+ const disable = ( that ) => {
77+ if ( that . user . contestMode ) throw new ForbiddenError ( 'Not available' ) ;
78+ } ;
79+ ctx . on ( 'handler/before/HomeDomain' , disable ) ;
80+ ctx . on ( 'handler/before/DomainUser' , disable ) ;
81+ ctx . on ( 'handler/before/HomeMessages#post' , disable ) ;
82+ ctx . on ( 'handler/before/Files' , disable ) ;
83+ ctx . on ( 'handler/before/HomeAvatar' , disable ) ;
84+ ctx . on ( 'handler/before/HomeSecurity' , disable ) ;
85+ ctx . on ( 'handler/before/UserDetail' , disable ) ;
86+ ctx . on ( 'handler/before/UserLostPass' , disable ) ;
87+ ctx . on ( 'handler/before/HomeSettings' , ( that ) => {
88+ if ( that . user . contestMode ) return ;
89+ if ( [ 'domain' , 'account' ] . includes ( that . args . category ) ) throw new ForbiddenError ( 'Not available' ) ;
90+ } ) ;
91+
9292 ctx . inject ( [ 'scoreboard' ] , ( { scoreboard } ) => {
9393 scoreboard . addView ( 'resolver-tiny' , 'Resolver(Tiny)' , { tdoc : 'tdoc' } , {
9494 async display ( { tdoc } ) {
@@ -358,7 +358,8 @@ export function apply(ctx: Context, config: ReturnType<typeof Config>) {
358358 if ( line . member4 ) line . members . push ( line . member4 ) ;
359359 const set : any = _ . pick ( line , 'school' , 'members' , 'coach' , 'seat' ) ;
360360 if ( line . school ) set . avatar = `url:/avatars/${ line . school . replace ( / [ ( ) ] / g, '' ) } .${ format } ` ;
361- if ( Object . keys ( set ) . length ) await UserModel . setById ( team . _id , set ) ;
361+ set . contestMode = true ;
362+ await UserModel . setById ( team . _id , set ) ;
362363 for ( const tdoc of tdocs ) {
363364 const tsdoc = await ContestModel . getStatus ( 'system' , tdoc . docId , team . _id ) ;
364365 if ( ! tsdoc ?. attend ) await ContestModel . attend ( 'system' , tdoc . docId , team . _id , 'rank' in line ? { unrank : ! line . rank , subscribe : 1 } : { subscribe : 1 } ) ;
@@ -393,4 +394,11 @@ export function apply(ctx: Context, config: ReturnType<typeof Config>) {
393394 ) ;
394395 } ) ;
395396 }
397+ if ( config . contestMode ) {
398+ ctx . inject ( [ 'setting' ] , ( c ) => {
399+ c . setting . AccountSetting (
400+ SettingModel . Setting ( 'setting_info' , 'contestMode' , null , 'boolean' , 'contestMode' , 'Contest Mode' , SettingModel . FLAG_DISABLED | SettingModel . FLAG_PUBLIC ) ,
401+ ) ;
402+ } ) ;
403+ }
396404}
0 commit comments