Exemplo n.º 1
0
def codeListInProblem(problemIndex):
    try:
        codeListSubquery = select_recent_code(
            problemIndex=problemIndex).subquery()
        codeListSubquery = dao.query(User.nickName, codeListSubquery).\
                                join(codeListSubquery,
                                     codeListSubquery.c.userIndex == User.userIndex).subquery()

        temp = select_code(problemIndex=problemIndex).subquery()
        codeListSubquery = dao.query(codeListSubquery, temp.c.isOpen).\
                                join(temp,
                                     temp.c.codeIndex == codeListSubquery.c.leastIndex).subquery()

        scoreListSuquery = select_userInformationInProblem(
            problemIndex=problemIndex).subquery()

        codeList = dao.query(scoreListSuquery.c.score, codeListSubquery).\
                        join(codeListSubquery,
                             codeListSubquery.c.userIndex == scoreListSuquery.c.userIndex).\
                        all()

        problemName = select_problem(
            problemIndex=problemIndex).first().problemName

        return render_template('codeList_problem.html',
                               codeList=codeList,
                               problemIndex=problemIndex,
                               problemName=problemName)

    except Exception as e:
        print e

        dao.rollback()

        flash('다시 시도해주세요.')
Exemplo n.º 2
0
def matchRank(problemIndex):
    try:
        userListSubquery = select_code(
            problemIndex=problemIndex,
            isCompile=True).group_by('userIndex').subquery()
        userProblemInfoSubquery = select_userInformationInProblem(
            problemIndex=problemIndex).subquery()

        topUserListSubquery = dao.query(userListSubquery.c.codeIndex, userListSubquery.c.userIndex,
                                        userListSubquery.c.problemIndex, userProblemInfoSubquery.c.score).\
                                join(userProblemInfoSubquery,
                                     userProblemInfoSubquery.c.userIndex == userListSubquery.c.userIndex).\
                                order_by(userProblemInfoSubquery.c.score.desc()).\
                                subquery()

        topUserList = dao.query(User.nickName, topUserListSubquery).\
                        join(topUserListSubquery,
                             topUserListSubquery.c.userIndex == User.userIndex).all()

        problem = select_problem(problemIndex=problemIndex).first()

        topUserList = topUserList if len(
            topUserList) < 10 else topUserList[:10]

        return render_template('userRankInProblem.html',
                               topUserList=topUserList,
                               problem=problem)

    except Exception as e:
        print e

        flash('다시 시도해주세요.')
        return redirect(url_for('.main'))
Exemplo n.º 3
0
def matchProblemList():
    try:
        userProblemSubquery = select_code(
            userIndex=session['userIndex'],
            isCompile=True).group_by('problemIndex').subquery()

        userListSubquery = select_code(isCompile=True).subquery()
        userListSubquery = dao.query(userListSubquery.c.codeIndex, userListSubquery.c.userIndex, userProblemSubquery.c.problemIndex).\
                                    join(userProblemSubquery,
                                         userProblemSubquery.c.problemIndex == userListSubquery.c.problemIndex).\
                                    group_by(userListSubquery.c.userIndex, userProblemSubquery.c.problemIndex).subquery()
        userCountSubquery = dao.query(userListSubquery.c.problemIndex, func.count(userListSubquery.c.problemIndex).label('count')).\
                                group_by(userListSubquery.c.problemIndex).subquery()

        problems = select_problem().subquery()
        userProblemListSubquery = dao.query(problems.c.problemName, userProblemSubquery.c.codeIndex,
                                            userProblemSubquery.c.problemIndex).\
                                    join(userProblemSubquery,
                                         userProblemSubquery.c.problemIndex == problems.c.problemIndex).subquery()

        userProblemList = dao.query(userCountSubquery.c.count, userProblemListSubquery).\
                            join(userProblemListSubquery,
                                 userProblemListSubquery.c.problemIndex == userCountSubquery.c.problemIndex).all()

        lastMatchTime = select_user(
            userIndex=session['userIndex']).first().lastMatchDate

        if lastMatchTime is None:
            remainingTime = 0

        else:
            remainingTime = ((lastMatchTime + timedelta(minutes=1)) -
                             datetime.now()).total_seconds()

        return render_template('matchProblemList.html',
                               userProblemList=userProblemList,
                               remainingTime=remainingTime)

    except Exception as e:
        print e

        flash('다시 시도해주세요.')
        return redirect(url_for('.main'))
Exemplo n.º 4
0
def submitProblem(problemIndex):
    try:
        isOpen = True if request.form['isOpen'] == 't' else False
        language = request.form['language']
        languageIndex = select_language(
            language=language).first().languageIndex
        code = request.form['getCode']

        temp = select_userInformationInProblem(
            userIndex=session['userIndex'], problemIndex=problemIndex).first()

        if temp is None:
            dao.add_all([
                insert_code(userIndex=session['userIndex'],
                            problemIndex=problemIndex,
                            languageIndex=languageIndex,
                            code=code,
                            date=datetime.now(),
                            isOpen=isOpen),
                insert_userInformationInProblem(userIndex=session['userIndex'],
                                                problemIndex=problemIndex)
            ])
        else:
            dao.add(
                insert_code(userIndex=session['userIndex'],
                            problemIndex=problemIndex,
                            languageIndex=languageIndex,
                            code=code,
                            date=datetime.now(),
                            isOpen=isOpen))

        dao.commit()

        compileCode.delay(codeIndex=select_code(
            userIndex=session['userIndex'], problemIndex=problemIndex).all()
                          [-1].codeIndex)

        time.sleep(0.5)
        flash('정상적으로 제출됐습니다.')
        return redirect(url_for('.myCodeInProblem', problemIndex=problemIndex))

    except Exception as e:
        print e

        dao.rollback()

        flash('다시 시도해주세요.')
        return redirect(url_for('.main'))
Exemplo n.º 5
0
def myCodeInProblem(problemIndex):
    try:
        codeList = select_code(userIndex=session['userIndex'],
                               problemIndex=problemIndex).all()
        problemName = select_problem(
            problemIndex=problemIndex).first().problemName

        return render_template('myCodeList_problem.html',
                               codeList=codeList,
                               problemIndex=problemIndex,
                               problemName=problemName)

    except Exception as e:
        print e
        dao.rollback()

        flash('다시 시도해주세요.')
Exemplo n.º 6
0
def viewCode(codeIndex):
    tempCode = select_code(codeIndex=codeIndex).subquery()
    temp = select_language().subquery()

    code = dao.query(temp.c.language, tempCode).\
                join(tempCode,
                     tempCode.c.languageIndex == temp.c.languageIndex).first()

    userInfo = select_userInformationInProblem(
        userIndex=session['userIndex']).first()

    problem = select_problem(problemIndex=code.problemIndex).first()
    user = select_user(userIndex=session['userIndex']).first()

    return render_template('viewCode.html',
                           userInfo=userInfo,
                           code=code,
                           problem=problem,
                           user=user)
Exemplo n.º 7
0
def compileCode(codeIndex):
    codeData = select_code(codeIndex=codeIndex).first()

    try:
        if 2 < codeData.languageIndex < 5:
            update_code(codeIndex=codeIndex, isCompile=True)
            dao.commit()

        else:
            fileName = '{0}{1}'.format(codeIndex,
                                       extension[codeData.languageIndex])

            codePath = os.path.join(tempDir, fileName)
            language = select_language(
                languageIndex=codeData.languageIndex).first().language
            execution = Execution()

            with open(codePath, 'w') as fp:
                fp.write(codeData.code)

            user = UserProgram(language=language,
                               savePath=tempDir,
                               fileName=fileName)
            _, _, result = execution.executeProgram(user.compile(),
                                                    user.savePath)

            os.remove(codePath)

            if result is True:
                os.remove(user.executionPath)
                update_code(codeIndex=codeIndex, isCompile=True)

                dao.commit()

    except Exception as e:
        print e
        dao.rollback()
Exemplo n.º 8
0
def matchingGame(problemIndex, challengerIndex, championIndex):
    try:
        matchIndex = select_dataOfMatch(
            problemIndex=problemIndex,
            challengerIndex=challengerIndex,
            championIndex=championIndex).all()[-1].dataOfMatchIndex

        temp = '{0}{1}{2}{3}'.format(problemIndex, challengerIndex,
                                     championIndex, random.randint(1000, 9999))
        tempPath = os.path.join(tempDir, temp)
        os.mkdir(tempPath)

        challengerCodeData = select_recent_code(
            problemIndex=problemIndex, userIndex=challengerIndex).first()
        championCodeData = select_recent_code(problemIndex=problemIndex,
                                              userIndex=championIndex).first()

        challengerCodeData = select_code(
            codeIndex=challengerCodeData.leastIndex).first()
        championCodeData = select_code(
            codeIndex=championCodeData.leastIndex).first()

        challengerFileName = '{0}{1}'.format(
            challengerIndex, extension[challengerCodeData.languageIndex])
        championFileName = '{0}{1}'.format(
            championIndex, extension[championCodeData.languageIndex])

        challengerCodePath = os.path.join(tempPath, challengerFileName)
        championCodePath = os.path.join(tempPath, championFileName)

        challengerLanguage = select_language(
            languageIndex=challengerCodeData.languageIndex).first().language
        championLanguage = select_language(
            languageIndex=championCodeData.languageIndex).first().language

        with open(challengerCodePath, 'w') as fp:
            fp.write(challengerCodeData.code)

        with open(championCodePath, 'w') as fp:
            fp.write(championCodeData.code)

        with open(os.path.join(dataDir,
                               '{0}.json'.format(problemIndex))) as fp:
            data = json.load(fp)

        challenger = UserProgram(language=challengerLanguage,
                                 savePath=tempPath,
                                 fileName=challengerFileName)
        champion = UserProgram(language=championLanguage,
                               savePath=tempPath,
                               fileName=championFileName)

        gameManager = GameManager(challenger=challenger,
                                  champion=champion,
                                  placementRule=data["placementRule"],
                                  placementOption=data['placementOption'],
                                  existRule=data['existRule'],
                                  existOption=data['existOption'],
                                  actionRule=data['actionRule'],
                                  actionOption=data['actionOption'],
                                  endingRule=data['endingRule'],
                                  endingOption=data['endingOption'],
                                  gameBoard=data['gameBoard'],
                                  dataBoard=data['dataBoard'],
                                  objectCount=data['objectCount'])

        result, positionData, boardData = gameManager.playGame()

        shutil.rmtree(tempPath)

        challengerScoreData = select_userInformationInProblem(
            userIndex=challengerIndex, problemIndex=problemIndex).first()
        championScoreData = select_userInformationInProblem(
            userIndex=championIndex, problemIndex=problemIndex).first()

        diffScore = abs(challengerScoreData.score - championScoreData.score)
        diffScore = diffScore if diffScore <= 300 else 300
        addScoreForChallenger, addScoreForChampion = 0, 0

        if result == 'win':
            addScoreForChallenger = 10 + int(diffScore *
                                             0.11) if diffScore > 15 else 10
            addScoreForChampion = -(addScoreForChallenger // 3)

        elif result == 'lose' and challengerScoreData.score > 0:
            addScoreForChallenger = -16 + int(diffScore *
                                              0.05) if diffScore > 15 else -16
            addScoreForChampion = abs(addScoreForChallenger)

        update_userInformationInProblem(userIndex=challengerIndex,
                                        problemIndex=problemIndex,
                                        score=addScoreForChallenger)
        update_userInformationInProblem(userIndex=championIndex,
                                        problemIndex=problemIndex,
                                        score=addScoreForChampion)

        update_dataOfMatch_result(dataOfMatchIndex=matchIndex,
                                  result=result,
                                  positionData=positionData,
                                  boardData=boardData)

        dao.commit()
        print matchIndex, result

    except Exception as e:
        print e, sys.exc_info()[2].tb_lineno
        dao.rollback()

        update_dataOfMatch_result(dataOfMatchIndex=matchIndex,
                                  result='server error')

        dao.commit()
Exemplo n.º 9
0
def matchUserList(problemIndex):
    try:
        userIndex = session['userIndex']
        userScore = select_userInformationInProblem(
            userIndex=userIndex, problemIndex=problemIndex).first().score

        userListSubquery = select_code(
            problemIndex=problemIndex,
            isCompile=True).group_by('userIndex').subquery()
        userProblemInfoSubquery = select_userInformationInProblem(
            problemIndex=problemIndex).subquery()
        userListSubquery = dao.query(userListSubquery.c.codeIndex, userListSubquery.c.userIndex,
                                     userListSubquery.c.problemIndex, userProblemInfoSubquery.c.score).\
                                join(userProblemInfoSubquery,
                                     userProblemInfoSubquery.c.userIndex == userListSubquery.c.userIndex).subquery()

        for i in range(50):
            userList_upper_subquery = dao.query(userListSubquery).\
                                        filter(and_(userListSubquery.c.score < userScore + i*20,
                                                    userListSubquery.c.score > userScore,
                                                    userListSubquery.c.userIndex != userIndex)).subquery()

            userList_upper = dao.query(User.nickName, userList_upper_subquery). \
                                join(userList_upper_subquery,
                                     userList_upper_subquery.c.userIndex == User.userIndex). \
                                all()

            if len(userList_upper) == 3:
                break

            elif len(userList_upper) > 3:
                while len(userList_upper) > 3:
                    userList_upper.remove(random.choice(list(userList_upper)))

                break

        for i in range(50):
            userList_lower_subquery = dao.query(userListSubquery).\
                                        filter(and_(userListSubquery.c.score > userScore - i*20,
                                                    userListSubquery.c.score <= userScore,
                                                    userListSubquery.c.userIndex != userIndex)).subquery()

            userList_lower = dao.query(User.nickName, userList_lower_subquery).\
                                join(userList_lower_subquery,
                                     userList_lower_subquery.c.userIndex == User.userIndex).\
                                all()

            if len(userList_lower) == 2:
                break

            elif len(userList_lower) > 2:
                while len(userList_lower) > 2:
                    userList_lower.remove(random.choice(list(userList_lower)))

                break

        problem = select_problem(problemIndex=problemIndex).first()

        lastMatchTime = select_user(userIndex=userIndex).first().lastMatchDate

        if lastMatchTime is None:
            remainingTime = 0

        else:
            remainingTime = ((lastMatchTime + timedelta(minutes=1)) -
                             datetime.now()).total_seconds()

        return render_template('matchUserList.html',
                               userList_upper=userList_upper,
                               userList_lower=userList_lower,
                               problem=problem,
                               score=userScore,
                               remainingTime=remainingTime)

    except Exception as e:
        print e

        flash('다시 시도해주세요.')
        return redirect(url_for('.main'))