山东大学-VirtualJudge-总结6

最近的几天时间内,主要是进一步开发SDUVJcontest的其他功能中,并在最终完成了contest的基本的开发,并实现了基本的代码提交界面,接下来需要进一步完善其功能并改善性能。

Contest的实现代码####

views.py中完成了了 对contest前端请求进行处理的函数

def contest(req):
    search = req.GET.get('search')
    if search:
        query = Contest.objects.filter(Q(name__icontains=search) | Q(uid__username__icontains=search))
    else:
        query = Contest.objects.all()
    pg = req.GET.get('pg')
    if not pg:
        pg = 1
    pg = int(pg)

    max_cnt = query.count()
    start = max(pg - PAGE_NUMBER_EVERY_PAGE, 1)
    end = min(pg + PAGE_NUMBER_EVERY_PAGE, max_cnt)

    lst = query[(pg - 1) * LIST_NUMBER_EVERY_PAGE:pg * LIST_NUMBER_EVERY_PAGE]

    return ren2res('contest.html', req, {'page': range(start, end + 1), 'list': lst})


@login_required
def contest_detail(req, cid):
    contest = Contest.objects.get(id=cid)
    # time = datetime.datetime.now(pytz.timezone(pytz.country_timezones('cn')[0]))
    # time = datetime.datetime.now()
    time = timezone.now()
    if time > contest.start_time:
        start = True
    else:
        start = False
    if contest.private:
        #print(problems)
        # print('contest.accounts')
        # print(contest.accounts.all())
        if req.user.info not in contest.accounts.all():
            return ren2res("contest/contest.html", req, {'contest': contest, 'err': "You do not have access to this contest."})
    if start:
        problems = contest.get_problem_list()
        length = len(problems)
        problems_status = [0 for i in range(length)]

        for i in range(length):
            problems[i].append(len(Status.objects.filter(user = req.user).filter(pro = problems[i][2]).filter(result = 'Accepted')))#changes
        return ren2res("contest/contest.html", req, {'contest': contest, 'problems': problems, 'problem': problems[0][2]})
    else:
        return ren2res("contest/contest.html", req, {'contest': contest, 'err': "Just wait."})


@login_required
def contest_get_problem(req, cid):
    if req.is_ajax():
        contest = Contest.objects.get(id=cid)
        pid = req.GET.get('pid')
        t = loader.get_template('contest/contest_problem.html')
        problem = Problem.objects.get(proid=pid)
        if contest.private:
            if req.user.info not in contest.accounts.all():
                problem = []
        content_html = t.render(Context({'problem': problem, 'user' : req.user}))
        return HttpResponse(content_html)


@login_required
def contest_status(req, cid):#has understood
    if req.is_ajax():
        contest = Contest.objects.get(id=cid)
        t = loader.get_template('contest/contest_status.html')
        status_list = Status.objects.filter(cid=cid).order_by('-time')#need change
        if contest.private:
            if req.user.info not in contest.accounts.all():
                status_list = []
        pg = req.GET.get('pg')
        if not pg:
            pg = 1
        pg = int(pg)

        max_cnt = status_list.count() // 20 + 1
        start = max(pg - PAGE_NUMBER_EVERY_PAGE, 1)
        end = min(pg + PAGE_NUMBER_EVERY_PAGE, max_cnt)

        lst = status_list[(pg - 1) * LIST_NUMBER_EVERY_PAGE:pg * LIST_NUMBER_EVERY_PAGE]

        content_html = t.render(Context({'status_list': lst, 'page': range(start, end + 1), 'contest_id': cid, 'user': req.user}))
        return HttpResponse(content_html)
    else:
        raise Http404


@login_required
def contest_submit(req, cid):
    contest = Contest.objects.get(id=cid)
    time = datetime.datetime.now(pytz.timezone(pytz.country_timezones('cn')[0]))
    if time > contest.start_time + contest.duration_time:
        finish = True
    else:
        finish = False

    if contest.private:
        if req.user.info not in contest.accounts.all():
            return HttpResponseRedirect("/contest/" + cid + "/")

    if req.method == 'GET':
        return ren2res("contest/contest_submit.html", req, {'contest': contest, 'problems': contest.get_problem_list()})
    elif req.method == 'POST':
        pid = req.POST.get('pid')
        #need change start
        sub = Status(pro=Problem.objects.get(proid=pid), user=req.user, lang=req.POST.get('lang'))
        if not finish:
            sub.cid = contest.id
        else:
            sub.cid = -1
        sub.save()
        if req.POST.get('code'):
            content_file = ContentFile(req.POST.get('code'))
        elif req.FILES:
            content_file = ContentFile(req.FILES['file'].read())
        else:
            return ren2res("contest/contest_submit.html", req,
                           {'contest': contest, 'problems': contest.get_problem_list(), 'err': 'No Submit!'})
        sub.source_code.save(name=str(sub.id), content=content_file)
        sub.save()
        #judger.Judger(sub)
        result=judge_delay.delay(sub)
    if not finish:
        return HttpResponseRedirect("/contest/" + cid + "/")
    else:
        return HttpResponseRedirect("/contest/"+cid+"/status?pid=" + pid)
        #need change end

def contest_time(req, cid):#don't need change
    if req.is_ajax():
        contest = Contest.objects.get(id = cid)
        startTime = contest.start_time.strftime('%Y-%m-%d %H:%M:%S UTC')

        days = contest.duration_time.days
        seconds = contest.duration_time.seconds

        durationTime = days * 3600 * 24 + seconds;

        timeData = {'start' : startTime,
                    'duration' : durationTime}

        print(timeData)
        return JsonResponse(timeData)

@login_required
def contest_rank(req, cid):
    if req.is_ajax():
        contest = Contest.objects.get(id = cid)
        if contest.private:
            if req.user.info not in contest.accounts.all():
                return JsonResponse("{}")
        rank_cache = contest.rank
        # print("rank_cache:")
        # print(rank_cache)
        status_list = Status.objects.filter(cid = cid).filter(id__gt = contest.last_submit_id).order_by("time")
        # print("status_list")
        # print(status_list)
        rank_dict = json.loads(rank_cache)
        # print("rank_dict")
        # print(rank_dict)
        statsinfo = {}
        pos = 0
        problem_list = contest.get_problem_list()
        length = len(problem_list)

        
        if contest.last_submit_id==0:
            rank_dict["statsinfo"] = [{} for i in range(length)]
            for item in problem_list:
                rank_dict["statsinfo"][pos] = {"probid" : chr(pos + 65) ,"acNum" : 0, "tryNum" : 0}
                statsinfo[item[2].title] = {"pos" : pos}
                pos += 1
        else:
            for item in problem_list:
                statsinfo[item[2].title] = {"pos" : pos}
                pos += 1

        for item in status_list:
            if item.uid.is_staff :
                continue
            name = item.uid.username
            contest.last_submit_id = max(contest.last_submit_id, item.id)
            if name not in rank_dict.keys():
                rank_dict[name] = {"name" : name, "solved":0, "penalty":0, "probs" : [{"failNum" : 0, "acNum" : 0, "acTime" : 0} for i in range(length)]}

            pos = statsinfo[item.pid.title]["pos"]

            if item.status == 3: #Waiting
                break

            if item.status == 0: #Accepted
                rank_dict["statsinfo"][pos]["acNum"] += 1
            rank_dict["statsinfo"][pos]["tryNum"] += 1

            if rank_dict[name]["probs"][pos]["acNum"] == 0:
                if item.status == 0:
                    rank_dict[name]["probs"][pos]["acNum"] += 1
                    rank_dict[name]["probs"][pos]["acTime"] = dateToInt(item.time - contest.start_time, 1)
                    rank_dict[name]["penalty"] += 20 * rank_dict[name]["probs"][pos]["failNum"] + dateToInt(item.time - contest.start_time, 0)
                    rank_dict[name]["solved"] += 1
                else:
                    rank_dict[name]["probs"][pos]["failNum"] += 1
        contest.rank = json.dumps(rank_dict)
        # print("contest.rank")
        # print(contest.rank)
        contest.save()
        return JsonResponse(rank_dict)

同时,contest的界面如下,已经可以实现在contest中显示问题的详细说明界面:

contest主界面
problem详细说明

contest.submit的界面如下:

contest提交问题
代码提交时问题的选择
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,561评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,218评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,162评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,470评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,550评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,806评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,951评论 3 407
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,712评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,166评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,510评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,643评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,306评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,930评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,745评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,983评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,351评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,509评论 2 348

推荐阅读更多精彩内容