Esempio n. 1
0
    def manual(self, request, obj_id):
        """ Handles manual game entries """
        res = Result()
        message = ''
        error = False
        context = self._processRequest(request, obj_id)
        red = Team.objects.get(pk=request.POST.get('red', None))
        blu = Team.objects.get(name=request.POST.get('blu', None))
        games = request.POST.get('games', '').split(',')
        
        if request.user in red.members.all() and request.user in blu.members.all():
            error = True
            message = IN_BOTH_TEAMS
        try:
            red.laddermembership_set.get(ladder=context['object'])
        except ObjectDoesNotExist:
            message = RED_NOT_MEMBER
            error = True
        try:
            blu.laddermembership_set.get(ladder=context['object'])
        except ObjectDoesNotExist:
            message = BLU_NOT_MEMBER
            error = True
        if error:
            res.isError = True
            res.message = message
            return JsonResponse(res)

        forMail = []

        for game in games:
            winner = 'red' if game == '0' else 'blu'
            game = Game(
                ladder = context['object'],
                red = red,
                blu = blu,
                status = Game.Closed
            )
            game.save()
            game.update(winner)
            forMail.append(game)
            time.sleep(1)

        self.mail_manual(request.user, forMail)

        res.isSuccess = True
        res.message = message

        return JsonResponse(res)
Esempio n. 2
0
    def challenge(self, request, obj_id):
        """ Handles manual challenges """
        res = Result()
        context = self._processRequest(request, obj_id)
        red = Team.objects.get(pk=request.POST.get('red', None))
        blu = Team.objects.get(pk=request.POST.get('blu', None))
        if request.user in blu.members.all():
            res.isError = True
            res.message = CH_SAME_TEAM

            return JsonResponse(res)

        game = Game(ladder=context['object'], red=red, blu=blu, status=Game.Open)
        game.save()

        res.isSuccess = True
        res.message = CH_SUCCESS

        #self.mail_challenge(challenge)

        return JsonResponse(res)
Esempio n. 3
0
    def create(self, request):
        res = Result()
        #context = self._processRequest(request)

        ladder = Ladder.objects.get(pk=request.POST.get('lid', None))
        red = Team.objects.get(pk=request.POST.get('red', None))
        blu = Team.objects.get(pk=request.POST.get('blu', None))

        if len(set(red.members.all()).intersection(blu.members.all())) > 0:
            res.isError = True
            res.message = CH_SAME_TEAM
            
            return JsonResponse(res)

        game = Game()
        game.ladder = ladder
        game.red = red
        game.blu = blu
        game.save()

        res.isSuccess = True
        res.message = CH_SUCCESS

        #mail('You have been challenged!', 'ladder/email/game.html', {'object': game}, [user.email for user in game.blu.members.all()])

        return JsonResponse(res)
Esempio n. 4
0
    def language(self, request):
        res = Result()
        if request.method != 'POST':
            res.isError = True
            res.message = "Invalid method, use POST"

            return JsonResponse(res)
        
        query = request.POST.get('q', None)
        
        if query:
            try:
                obj = parse(query)
            except:
                res.isError = True
                res.message = "Could not understand your request"
                return JsonResponse(res, 500)
            try:
                ladder = Ladder.objects.get(name=obj['ladder'])
            except ObjectDoesNotExist:
                res.isError = True
                res.message = "The ladder requested was not found"
                return JsonResponse(res, 500)
            ## Red
            if obj['red'] in ['i', 'me']:
                try:
                    red = request.user.teams.filter(ladder=ladder)[0]
                    if ladder.teams.filter(name=obj['blu']).count() == 0:
                        res.isError = True
                        res.message = "Your opponent (%s) does not have any teams in the %s ladder" % (obj['blu'], ladder.name)
                        return JsonResponse(res, 500)
                except:
                    res.isError = True
                    res.message = "You do not have any teams in the %s ladder" % ladder.name
                    return JsonResponse(res, 500)
            else:
                try:
                    red = Team.objects.get(name=obj['red'])
                except ObjectDoesNotExist:
                    res.isError = True
                    res.message = "The team '%s' was not found" % obj['red']
                    return JsonResponse(res, 500)
            ## Blue
            if obj['blu'] in ['i', 'me']:
                try:
                    blu = request.user.teams.filter(ladder=ladder)[0]
                    if ladder.teams.filter(name=obj['red']).count() == 0:
                        res.isError = True
                        res.message = "Your opponent (%s) does not have any teams in the %s ladder" % (obj['red'], ladder.name)
                        return JsonResponse(res, 500)
                except:
                    res.isError = True
                    res.message = "You do not have any teams in the %s ladder" % ladder.name
                    return JsonResponse(res, 500)
            else:
                try:
                    blu = Team.objects.get(name=obj['blu'])
                except ObjectDoesNotExist:
                    res.isError = True
                    res.message = "The team '%s' was not found" % obj['blu']
                    return JsonResponse(res, 500)
            
            winner = red if obj['winner'] == 'red' else blu
            g = Game(red=red, blu=blu, ladder=ladder)
            g.save()
            g.update(winner)

            res.isSuccess = True
            res.message = g.json()
        else:
            res.isError = True
            res.message = "No query provided"

        return JsonResponse(res)
Esempio n. 5
0
 def quick(self, request):
     res = Result()
     context = self._processRequest(request)
     serobj = context['request'].POST.get('q', False)
     if serobj:
         obj = json.loads(serobj)
         ladder = Ladder.objects.get(pk=obj['ladder'])
         red = Team.objects.get(pk=obj['red'])
         blu = Team.objects.get(pk=obj['blu'])
         games = []
         for n in xrange(obj['red_wins']):
             g = Game(ladder=ladder, red=red, blu=blu)
             g.save()
             g.update(red)
             games.append(g)
         for n in xrange(obj['blu_wins']):
             g = Game(ladder=ladder, red=red, blu=blu)
             g.save()
             g.update(blu)
             games.append(g)
         
         res.isSuccess = True    
         res.append([g.json() for g in games])
     else:
         res.isError = True
         res.message = "No query provided"
     
     return JsonResponse(res)