Exemple #1
0
def saveSetting():
    nickName = get_request_value(request.form, 'nickName')
    password = get_request_value(request.form, 'pw')
    password = generate_password_hash(TripleDES.encrypt(
        str(password))) if password else None
    eMail = get_request_value(request.form, 'eMail')

    comment = get_request_value(request.form, 'comment')
    isOpen = True if get_request_value(request.form,
                                       'isOpen') == 't' else False
    language = get_request_value(request.form, 'language')
    thema = get_request_value(request.form, 'thema')
    languageIndex = select_language(language=language).first().languageIndex

    try:
        update_userSetting(userIndex=session['userIndex'],
                           languageIndex=languageIndex,
                           thema=thema,
                           comment=comment,
                           isOpen=isOpen)
        update_user(userIndex=session['userIndex'],
                    password=password,
                    nickName=nickName,
                    eMail=eMail)

        dao.commit()

    except Exception as e:
        dao.rollback()

        flash('정보변경에 실패했습니다.')
        return redirect(url_for('.main'))

    flash('정상적으로 정보가 변경되었습니다.')
    return redirect(url_for('.main'))
Exemple #2
0
def setting():
    thema = [
        'chrome', 'clouds', 'eclipse', 'github', 'monokai', 'textmate',
        'tomorrow'
    ]
    try:
        userInformation = get_user_information(session['userIndex']).first()
        language = select_language().all()

    except Exception as e:
        print e

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

    if userInformation is None:
        try:
            dao.add(insert_userSetting(userIndex=session['userIndex']))
            dao.commit()

            return redirect(url_for('.setting'))

        except Exception as e:
            print e

            dao.rollback()
            return redirect(url_for('.setting'))

    return render_template('setting.html',
                           userInformation=userInformation,
                           language=language,
                           thema=thema)
Exemple #3
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'))
Exemple #4
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)
Exemple #5
0
def problem(problemIndex):
    thema = [
        'chrome', 'clouds', 'eclipse', 'github', 'monokai', 'textmate',
        'tomorrow'
    ]

    try:
        langQuery = select_language()
        userIndex = session['userIndex']
        problemData = select_problem(problemIndex=problemIndex).first()

        userInfoSub = select_userSetting(userIndex=userIndex).subquery()
        temp = langQuery.subquery()

        userInfo = dao.query(temp.c.language, userInfoSub).\
                        join(userInfoSub, userInfoSub.c.languageIndex == temp.c.languageIndex).first()

        language = langQuery.all()

        if userInfo is None:
            try:
                dao.add(insert_userSetting(userIndex=userIndex))
                dao.commit()

                redirect(url_for('.problem'))

            except Exception as e:
                redirect(url_for('.problem'))

        return render_template('problem.html',
                               problemData=problemData,
                               userInfo=userInfo,
                               thema=thema,
                               language=language)

    except Exception as e:
        print e

        flash('다시 시도해주세요.')
        return redirect(url_for('.main'))
Exemple #6
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()
Exemple #7
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()