示例#1
0
 def get(self, request):
     if request.user.is_authenticated:
         if Account.objects.filter(provider='ustc',
                                   user=request.user).exists():
             try:
                 request.user.ustceligible
             except UstcEligible.DoesNotExist:
                 return redirect('ustcprofile')
     context = Context.from_request(request)
     try:
         challenges = Challenge.get_enabled(context)
         challenges = {'value': [obj.json for obj in challenges]}
     except ProfileRequired as e:
         messages.info(request, e.message)
         return redirect('profile')
     except TermsRequired as e:
         messages.info(request, e.message)
         return redirect('terms')
     except Error as e:
         challenges = {'error': e.json}
     try:
         announcement = Announcement.get_latest(context).json
     except NotFound:
         announcement = None
     if request.user.is_authenticated:
         user = User.get(context, request.user.pk)
         if user.group == 'other':
             ranking = Submission.get_user_ranking(context, request.user.pk)
         else:
             ranking = Submission.get_user_ranking(context,
                                                   request.user.pk,
                                                   group=user.group)
     else:
         ranking = {}
     return TemplateResponse(
         request, 'hub.html', {
             'announcement': announcement,
             'challenges': challenges,
             'progress': Submission.get_user_progress(
                 context, request.user.pk),
             'ranking': ranking,
             'clear_count': Submission.get_clear_count(context),
         })
示例#2
0
 def post(self, request):
     try:
         matches = Submission.submit(Context.from_request(request),
                                     request.user.pk,
                                     request.POST['challenge'],
                                     request.POST['flag'].strip())
         if matches:
             messages.success(request, '答案正确')
         else:
             messages.error(request, '答案错误')
     except Error as e:
         messages.info(request, e.message)
     return redirect('hub')
示例#3
0
 def handle(self, *args, **options):
     context = Context(elevated=True)
     Challenge.regen_all(context)
     Submission.regen_all(context)
示例#4
0
 def do_get_board(self, context, **kwargs):
     return Submission.get_board(context, **kwargs)
示例#5
0
 def do_get_first(self, context, **kwargs):
     return Submission.get_first(context, **kwargs)
示例#6
0
 def do_get_clear_count(self, context, **kwargs):
     return Submission.get_clear_count(context, **kwargs)
示例#7
0
 def do_get_user_history(self, context, **kwargs):
     return Submission.get_user_history(context, **kwargs)
示例#8
0
 def do_get_user_progress(self, context, **kwargs):
     return Submission.get_user_progress(context, **kwargs)
示例#9
0
 def do_get_violations(self, context, **kwargs):
     return Submission.get_violations(context, **kwargs)
示例#10
0
 def do_get_log(self, context, **kwargs):
     return Submission.get_log(context, **kwargs)
示例#11
0
    def handle(self, fake_complex_challenges, fake_simple_challenges,
               fake_users, fake_submissions, game_started_seconds,
               **options):
        root = User.create(
            Context(elevated=True),
            group='other',
            nickname='root',
        ).user
        root.is_staff = True
        root.is_superuser = True
        root.save()
        root.refresh_from_db()
        Account.objects.create(provider='debug', identity='root', user=root)

        c1 = Challenge.create(
            Context(root),
            name='签到题',
            category='checkin',
            detail='签到题描述',
            url='https://example.com/{token}',
            prompt='flag{hackergame}',
            index=-100,
            enabled=True,
            flags=[{
                'name': '',
                'score': 10,
                'type': 'text',
                'flag': 'flag{hackergame}',
            }],
        )
        Submission.submit(Context(root), root.pk, c1.pk, 'flag{hackergame}')

        for i in range(1, fake_complex_challenges + 1):
            Challenge.create(
                Context(root),
                name=f'复杂题 {i}',
                category='complex',
                detail='<code>flag{FLAG_INDEX}</code> 或 '
                       '<code>flag{FLAG_INDEX:USER_ID}</code>',
                url='',
                prompt='flag{...}',
                index=random.randrange(100),
                enabled=random.choice((False, True, True, True, True)),
                flags=[{
                    'name': f'flag {j}',
                    'score': random.randrange(100),
                    'type': ('expr', 'text')[j % 2],
                    'flag': ("f'flag{{%s:{token.partition(\":\")[0]}}}'" % j,
                             f'flag{{{j}}}')[j % 2],
                } for j in range(random.randrange(1, 4))],
            )

        for i in range(1, fake_simple_challenges + 1):
            Challenge.create(
                Context(root),
                name=f'简单题 {i}',
                category='simple',
                detail='',
                url='',
                prompt=('flag{0}', 'flag{0:USER_ID}')[i % 2],
                index=random.randrange(100),
                enabled=random.choice((False, True, True, True, True)),
                flags=[{
                    'name': '',
                    'score': random.randrange(100),
                    'type': ('text', 'expr')[i % 2],
                    'flag': ('flag{0}',
                             "'flag{0:'+token.partition(':')[0]+'}'")[i % 2],
                }],
            )

        terms = Terms.create(Context(root), name='条款', content='1 2 3 ...',
                             enabled=True)

        now = timezone.now()
        timestamps = []
        for i in range(fake_submissions):
            delta = random.randrange(game_started_seconds)
            timestamps.append(now - timezone.timedelta(seconds=delta))
        timestamps.sort()

        Trigger.create(Context(root), time=min(timestamps), state=True)

        groups = list(set(User.groups.keys()) - {'staff', 'banned'})
        for i in range(fake_users):
            print('user', i, end='\r')
            u = User.create(
                Context(elevated=True),
                group=random.choice(groups),
                nickname='用户 ' * 8 + str(i),
                name='姓名',
                sno='PB11111111',
                tel='123456789',
                email='*****@*****.**',
                gender=random.choice(('female', 'male')),
                qq='12345',
                school='foo',
                grade='1',
            )
            Terms.get(Context(u.user), terms.pk).agree(u.pk)
            Account.objects.create(provider='debug', identity=f'{i}',
                                   user=u.user)

        users = [i.pk for i in User.get_all(Context(root))]
        challenges = [i.pk for i in Challenge.get_all(Context(root))]

        for i in range(fake_submissions):
            print('submission', i, end='\r')
            try:
                u = random.choice(users)
                c = random.choice(challenges)
                fs = len(Challenge.get(Context(root), c).flags)
                Submission.submit(
                    Context(
                        User.get(Context(root), u).user,
                        timestamps[i]
                    ), u, c, f'flag{{{random.choice(range(fs))}:{u}}}'
                )
                Submission.submit(
                    Context(
                        User.get(Context(root), u).user,
                        timestamps[i] + timezone.timedelta(seconds=20)
                    ), u, c, f'flag{{{random.choice(range(fs))}}}'
                )
            except (NotFound, SlowDown):
                pass

        Challenge.create(
            Context(root),
            name='难题',
            category='hard',
            detail='难题描述',
            url='https://example.com/{token}',
            prompt='flag{hackergame}',
            index=100,
            enabled=True,
            flags=[{
                'name': '',
                'score': 100,
                'type': 'text',
                'flag': 'flag{hackergame}',
            }],
        )