def edit(request): if request.method == 'POST': title = request.POST['title'] desc = request.POST['desc'] ch = Challenge(title = title, desc = desc) ch.save() return HttpResponseRedirect(reverse('challenges')) return render(request, 'edit_form.html')
def make_challenge(request, username): user = get_user(username) done = False while not done: try: challenge = Challenge(destination=user) challenge.save() done = True except IntegrityError: pass # occasional cleanup if challenge.code[1] == "0": Challenge.objects.filter(valid_until__lt=datetime.now()).delete() return HttpResponse(challenge.code)
def challenge(request): if request.is_ajax(): challenge_bot_id = json.loads(request.body)['msg'] challenge_bot = Bot.objects.get(pk=challenge_bot_id) # get the user current bot user_prof = UserProfile.objects.get(user=request.user) if not user_prof.current_bot: print "Can not challenge if does not have a bot!" return HttpResponse("Error") if challenge_bot.owner == user_prof: print "[CHEATING!] - wrong challenge bot!" return HttpResponse("Error") # challenged bot must be the owners current bot if not challenge_bot.is_current_bot: print "[CHEATING!] - wrong challenge bot!, must be the owners current bot!." return HttpResponse("Error") print "Got a challenge for bot: ", challenge_bot # Get pending challenges for this user challenges = Challenge.objects.filter(requested_by=user_prof, played=False, canceled=False) if challenges.count() > 0: # has pending challenges, must wait. return HttpResponse("Can not challenge more than one bot at a time") # Check if these bots haven't already played. #played_challs = Challenge.objects.filter(challenger_bot=user_prof.current_bot, # challenged_bot=challenge_bot, played=True) #if played_challs.count() > 0: # # has already played against this bot, must upload a new one # return HttpResponse("Already played against this bot!. Upload a new one.") if (user_prof.current_bot.valid != Bot.READY or challenge_bot.valid != Bot.READY): return JsonResponse({'success': False, 'msg': 'One of the bot is not READY' }) new_challengue = Challenge() new_challengue.requested_by = user_prof new_challengue.challenger_bot = user_prof.current_bot new_challengue.challenged_bot = challenge_bot new_challengue.save() return JsonResponse({'success': True})
def login(request): if 'key' in request.COOKIES: try: user = User.objects.get(sessionID=request.COOKIES['key']) return HttpResponseRedirect("/main") except User.DoesNotExist: pass rkey = randomkey() key = Challenge(key=rkey, ip=request.META['REMOTE_ADDR']) key.save() message = "" if 'msg' in request.GET: message = msgdict[request.GET['msg']] t = loader.get_template('aponurho/login.html') c = RequestContext(request, { 'challenge': rkey, 'message': message }) return HttpResponse(t.render(c))
def challenge(request): if request.is_ajax(): challenge_bot_id = json.loads(request.body)['msg'] challenge_bot = Bot.objects.get(pk=challenge_bot_id) # get the user current bot user_prof = UserProfile.objects.get(user=request.user) if not user_prof.current_bot: print "Can not challenge if does not have a bot!" return HttpResponse("Error") if challenge_bot.owner == user_prof: print "[CHEATING!] - wrong challenge bot!" return HttpResponse("Error") # challenged bot must be the owners current bot if not challenge_bot.is_current_bot: print "[CHEATING!] - wrong challenge bot!, must be the owners current bot!." return HttpResponse("Error") print "Got a challenge for bot: ", challenge_bot # Get pending challenges for this user challenges = Challenge.objects.filter(requested_by=user_prof, played=False) if challenges.count() > 0: # has pending challenges, must wait. return HttpResponse("Can not challenge more than one bot at a time") # Check if these bots haven't already played. played_challs = Challenge.objects.filter(challenger_bot=user_prof.current_bot, challenged_bot=challenge_bot, played=True) if played_challs.count() > 0: # has already played against this bot, must upload a new one return HttpResponse("Already played against this bot!. Upload a new one.") new_chall = Challenge() new_chall.requested_by = user_prof new_chall.challenger_bot = user_prof.current_bot new_chall.challenged_bot = challenge_bot new_chall.save() return HttpResponse(json.dumps({'success' : True}), mimetype='application/json')