Пример #1
0
def admin():
	form = ChallengeForm()
	if request.method == 'POST':
		if form.validate_on_submit():
			challenge = Challenge(form.ChallengeName.data, g.user.PersonID, datetime.datetime.now())
			commit_to_db(challenge)
			flash("Successfullly created new challenge")
			return redirect(url_for('discussions_all'))
		else:
			flash("Couldn't initiate your new challenge")
			return redirect(url_for('admin'))
	if request.method == 'GET':
		new_users = get_all_persons(Confirmed = False)
		return render_template('admin.html', form = form, new_users = new_users)
Пример #2
0
def challenge(challenge_id):
	valid = db.session.query(Challenge).filter_by(active=True).filter_by(id=challenge_id).first()
	if not valid:
		flash(('Not an active challenge, sorry!'))
		return redirect(url_for('home'))

	timer = db.session.query(Timer).filter_by(user_id=g.user.id).filter_by(challenge_id=challenge_id).first()
	if not timer:
		newTimer = Timer(time.time(), challenge_id, g.user.id)
		db.session.add(newTimer)
		db.session.commit()
	else:
		if timer.end_time:
			flash(("You've already completed that challenge!"))
			return redirect(url_for('home'))

	form = ChallengeForm() if request.method == 'POST' else ChallengeForm(request.args)
	
	if form.validate_on_submit():
		if form.answer.data == valid.answer:
			end = time.time()
			user = db.session.query(User).filter_by(id=g.user.id).first()

			rank = db.session.query(Timer).filter(Timer.end_time > 0).filter_by(challenge_id=challenge_id).count()
			timer.end_time = end

			timeDelta = end - timer.start_time
			score = math.floor(10000 + max((((86400 - timeDelta)/86400.00) * 1000) + ((15 - (rank + 1))/15.0 * 2000), -2500))
			user.score = valid.weight * score + user.score
			timer.rank = rank + 1

			db.session.commit()
			flash(('Your answer was correct! You received a score of ' + str(score) ))
			return redirect(url_for('home'))
		else:
			flash(('Ooh sorry...wrong answer, try again!'))
			return redirect(url_for('challenge', challenge_id=challenge_id))
	return render_template('challenge.html', form=form, challenge=valid)