Пример #1
0
def game():
    """Returns the webpage for the javascript based game."""
    if not current_user.is_authenticated:
        username = None
        login_url = url_for('login')
        new_login_url = url_for('new_login')
        header_extra = f"<br><a href={login_url}>Sign in</a> or <a href={new_login_url}>create a profile</a> to track your scores."
    else:
        username = current_user.username
        header_extra = ""

    now = time()
    score = Score(username=username,
                  score=0,
                  token_used=False,
                  created_at=now,
                  last_modified_at=now)
    score.save()

    game_token = score.issue_web_token()

    top_scores = Score.get_best_scores()

    return render_template(
        'game.html',
        header=
        f"Try and beat the high score!<br>Use arrow keys to move and jump, reload the page to start over.{header_extra}",
        token=game_token,
        footer=
        'I made this game using the <a href="https://phaser.io/tutorials/making-your-first-phaser-3-game/part1">PhaserJS</a> tutorial.',
        scores=top_scores)
Пример #2
0
def post_score(request, id):

    star = request.POST['star']

    user_1 = User.objects.get(id=request.user.id)
    mov = Movie.objects.get(pk=id)
    score_1 = Score(value=star, user=user_1, movie=mov)
    score_1.save()
    return redirect('app:ranking')
Пример #3
0
 def post(self, request, *args, **kwargs):
     data = request.data
     user = data['user']
     codeid = data['code']
     smells = eval(data['smells'])
     CodeSmell.objects.filter(code_id=codeid, user=user).delete()
     for s in smells:
         smell = CodeSmell(code_id=codeid,
                           user_id=user,
                           line=s['line'],
                           smell=s['smell'])
         try:
             smell.clean_fields()
             smell.save()
         except Exception as error:
             return Response(error, status=status.HTTP_400_BAD_REQUEST)
     smells = map(lambda x: (x['line'], x['smell']), smells)
     origsmells = CodeSmell.objects.filter(
         code_id=codeid, user=Code.objects.filter(id=codeid)[0].creator)
     origsmells = map(lambda x: (x.line, x.smell), origsmells)
     score = 0
     correct = []
     incorrect = []
     missed = []
     if len(origsmells) > 0:
         for s in smells:
             if s in origsmells:
                 score += 1
                 correct.append({'line': s[0], 'smell': s[1]})
             else:
                 incorrect.append({'line': s[0], 'smell': s[1]})
         incorrect_lines = map(lambda x: x['line'], incorrect)
         for s in origsmells:
             if s not in smells and s[0] not in incorrect_lines:
                 missed.append({'line': s[0], 'smell': s[1]})
         score -= 0.5 * (len(missed) + len(incorrect))
         score = score / len(origsmells) * 100
         score = max(0, score)
     Score.objects.filter(code_id=codeid, user_id=user).delete()
     score = Score(code_id=codeid, user_id=user, score=score)
     score.save()
     scores = Score.objects.filter(code_id=codeid)
     scores = map(lambda x: x.score, scores)
     avg = sum(scores) / len(scores)
     code = Code.objects.get(pk=codeid)
     code.difficulty = (min(len(origsmells) * 10, 100) + 100 - avg) / 2
     code.save()
     data = {
         'score': score.score,
         'correct': correct,
         'incorrect': incorrect,
         'missed': missed
     }
     return Response(data, status=status.HTTP_200_OK)
Пример #4
0
def review_heart(request):
    score = Score()
    review = Review.objects.get(id=request.POST['review'])
    score.critic = Critic.objects.get(user=request.user)
    score.avg = 1
    score.save()

    review.scores.add(score)

    review.save()

    return HttpResponse(score.id)
Пример #5
0
def store(request, user_id, game_id):
	userToAddScore = UserCourse.objects.get(user_id=user_id)
	gamePlayed = Game.objects.get(pk=game_id)


	newScore = Score()

	newScore.game =  gamePlayed
	newScore.student = userToAddScore.user
	newScore.value = request.POST['score']
	newScore.save()

	return JsonResponse({'status':'OK'})
Пример #6
0
 def post(self, request, *args, **kwargs):
     data = request.data
     user = data['user']
     codeid = data['code']
     smells = eval(data['smells'])
     CodeSmell.objects.filter(code_id=codeid, user=user).delete()
     for s in smells:
         smell = CodeSmell(code_id=codeid, user_id=user, line=s['line'], smell=s['smell'])
         try: 
             smell.clean_fields()
             smell.save()
         except Exception as error:
             return Response(error, status=status.HTTP_400_BAD_REQUEST)
     smells = map(lambda x:(x['line'], x['smell']), smells)
     origsmells = CodeSmell.objects.filter(code_id=codeid, user=Code.objects.filter(id=codeid)[0].creator)
     origsmells = map(lambda x:(x.line, x.smell), origsmells)
     score = 0
     correct = []
     incorrect = []
     missed = []
     if len(origsmells) > 0:
         for s in smells:
             if s in origsmells:
                 score += 1
                 correct.append({'line': s[0], 'smell': s[1]})
             else:
                 incorrect.append({'line': s[0], 'smell': s[1]})
         incorrect_lines = map(lambda x:x['line'], incorrect)
         for s in origsmells:
             if s not in smells and s[0] not in incorrect_lines:
                 missed.append({'line': s[0], 'smell': s[1]})
         score -= 0.5 * (len(missed) + len(incorrect))
         score = score/len(origsmells) * 100
         score = max(0, score)
     Score.objects.filter(code_id=codeid, user_id=user).delete()
     score = Score(code_id=codeid, user_id=user, score=score)
     score.save()
     scores = Score.objects.filter(code_id=codeid)
     scores = map(lambda x: x.score, scores)
     avg = sum(scores)/len(scores)
     code = Code.objects.get(pk=codeid)
     code.difficulty = (min(len(origsmells) * 10, 100) + 100 - avg) / 2
     code.save()
     data = { 'score': score.score,
              'correct': correct,
              'incorrect': incorrect,
              'missed': missed }
     return Response(data, status=status.HTTP_200_OK)
Пример #7
0
					codesmell.save()

				# calculate score for test's answers
				score = 0
				correct = []
				incorrect = []
				missed = []
				if len(origsmells) > 0:
					for s in usersmells:
						if s in origsmells:
							score += 1
							correct.append({'line': s[0], 'smell': s[1]})
						else:
							incorrect.append({'line': s[0], 'smell': s[1]})
					incorrect_lines = map(lambda x:x['line'], incorrect)
					for s in origsmells:
						if s not in usersmells and s[0] not in incorrect_lines:
							missed.append({'line': s[0], 'smell': s[1]})
					score -= 0.5 * (len(missed) + len(incorrect))
					score = score/len(origsmells) * 100
					score = max(0, score)
				score = Score(code=code, user=user, score=score)
				score.save()

			flag = True
			title = line[2:]
			content = ''
			num_lines = 0
			continue
		content += line
		num_lines += 1
Пример #8
0
    def post(self, request, *args, **kwargs):
        """
        Submit a list of code smells and check it against the original uploader's code snippets to calculate a score for the user. 

        Example POST body:
        <code>

            {
                "user" : 17,
                "code" : 38,
                "smells" : "[{'line': 1, 'smell': 'Vague string'}, {'line': 2, 'smell': 'Bad variable name'}]"
            }

        </code>

        Get back a list of incorrect, missed, and correct code smells labelled.
        ---
        parameters:
            - name: body
              paramType: body
              description: See example POST body above
        consumes:
            - application/json
        """
        data = request.data
        user = data['user']
        codeid = data['code']
        smells = eval(data['smells'])
        CodeSmell.objects.filter(code_id=codeid, user=user).delete()
        for s in smells:
            smell = CodeSmell(code_id=codeid, user_id=user, line=s['line'], smell=s['smell'])
            try: 
                smell.clean_fields()
                smell.save()
            except Exception as error:
                return Response(error, status=status.HTTP_400_BAD_REQUEST)
        smells = map(lambda x:(x['line'], x['smell']), smells)
        """
        Code smells are compared against answers (code smells assigned by creator)
        """
        origsmells = CodeSmell.objects.filter(code_id=codeid, user=Code.objects.filter(id=codeid)[0].creator)
        origsmells = map(lambda x:(x.line, x.smell), origsmells)
        score = 0
        correct = []
        incorrect = []
        missed = []
        """
        Score is calculated as follows: ((# correct answers) - 0.5*((# missed answers) + (# incorrect answers)))/(# originally assigned code smells)
        """
        if len(origsmells) > 0:
            for s in smells:
                if s in origsmells:
                    score += 1
                    correct.append({'line': s[0], 'smell': s[1]})
                else:
                    incorrect.append({'line': s[0], 'smell': s[1]})
            incorrect_lines = map(lambda x:x['line'], incorrect)
            for s in origsmells:
                if s not in smells and s[0] not in incorrect_lines:
                    missed.append({'line': s[0], 'smell': s[1]})
            score -= 0.5 * (len(missed) + len(incorrect))
            score = score/len(origsmells) * 100
            score = max(0, score)
        Score.objects.filter(code_id=codeid, user_id=user).delete()
        score = Score(code_id=codeid, user_id=user, score=score)
        score.save()
        """
        Code's difficulty is assigned based on number of code smells (capped at 10) and average score
        """
        scores = Score.objects.filter(code_id=codeid)
        scores = map(lambda x: x.score, scores)
        avg = sum(scores)/len(scores)
        code = Code.objects.get(pk=codeid)
        code.difficulty = (min(len(origsmells) * 10, 100) + 100 - avg) / 2
        code.save()
        data = { 'score': score.score,
                 'correct': correct,
                 'incorrect': incorrect,
                 'missed': missed }
        return Response(data, status=status.HTTP_200_OK)