Beispiel #1
0
    def final_challenge(self, request, queryset):
        profiles = UserProfile.objects.filter(user__is_superuser=False,
                                              user__is_active=True,
                                              current_bot__isnull=False).all()
        if not queryset:
            return HttpResponseRedirect('/admin')

        final_challenge = queryset[0]
        for up_player1, up_player2 in itertools.product(profiles, repeat=2):
            if up_player1 == up_player2:
                continue

            challenge = Challenge()
            challenge.requested_by = up_player1
            challenge.challenger_bot = up_player1.current_bot
            challenge.challenged_bot = up_player2.current_bot
            challenge.final_challenge = final_challenge
            challenge.save()
            final_challenge.challenge_set.add(challenge)
            # dispatch the new task
            players = {
                up_player1.user.username: up_player1.current_bot.code,
                up_player2.user.username: up_player2.current_bot.code,
            }
            run_match.delay(challenge.id, players)

        final_challenge.save()
        return HttpResponseRedirect('/admin')
def create_challenges(engine):
    uuid = lambda: uuid4().hex
    now = lambda: datetime.utcnow()
    inserts = []
    order = {
        'easy': 0,
        'medium': 1,
        'hard': 2,
        'impossible': 3,
    }
    mock = {
        'id': uuid(),
        'game_level': 'easy',
        'icons': {
            'icons': []
        },
        "active": 1,
        'order': 0,
        'created_at': now()
    }
    for level, data in LEVELS.items():
        for prize in data.get('prizes'):
            mock['id'] = uuid()
            mock['game_level'] = level
            mock['icons']['icons'] = prize.get('icons', [])
            mock['order'] = order.get(level, 0)
            log.debug(mock)
            inserts.append(Challenge(**mock))
            break

        engine.sync(inserts)
Beispiel #3
0
    def final_challenge(self, request, queryset):
        profiles = UserProfile.objects.filter(user__is_superuser=False, 
            user__is_active=True, current_bot__isnull=False).all()
        if not queryset:
		    return HttpResponseRedirect('/admin')
		    
        final_challenge = queryset[0]
        for up_player1, up_player2 in itertools.product(profiles, repeat=2):
            if up_player1 == up_player2:
                continue
                
            challenge = Challenge()
            challenge.requested_by = up_player1
            challenge.challenger_bot = up_player1.current_bot
            challenge.challenged_bot = up_player2.current_bot
            challenge.final_challenge = final_challenge
            challenge.save()
            final_challenge.challenge_set.add(challenge)
            # dispatch the new task
            players = {up_player1.user.username: up_player1.current_bot.code,
                       up_player2.user.username: up_player2.current_bot.code,
            }
            run_match.delay(challenge.id, players)

        final_challenge.save()
        return HttpResponseRedirect('/admin')
Beispiel #4
0
    def _test_run_match(self, bot1_code, bot2_code, checkChallenge):
        def save_bot(user_profile, bot_code):
            aBot = Bot()
            aBot.owner = user_profile
            aBot.code = bot_code
            aBot.save()
            return aBot

        bot1 = save_bot(self.user1_profile, bot1_code)
        bot2 = save_bot(self.user2_profile, bot2_code)

        aChallenge = Challenge()
        aChallenge.challenger_bot = bot1
        aChallenge.challenged_bot = bot2
        aChallenge.requested_by = self.user1_profile
        aChallenge.save()

        players = {
            'bot1': aChallenge.challenger_bot.code,
            'bot2': aChallenge.challenged_bot.code
        }

        run_match.delay(aChallenge.id, players)

        #Refresh challenge
        aChallenge = Challenge.objects.get(pk=aChallenge.id)
        checkChallenge(aChallenge)
Beispiel #5
0
def issue_challenge_view(request):
	if request.method == 'POST':
		try:
			challenger = Team.objects.get(slug=request.POST['challenger'])
			challengee = Team.objects.get(slug=request.POST['challengee'])
		except (Team.DoesNotExist, KeyError):
			pass
		else:
			challenge = Challenge(challenger=challenger, challengee=challengee)
			challenge.save()
			url = reverse('user_profile',
						  kwargs={'username': request.user.username})
			return HttpResponseRedirect(url)
	data = {
		'own_teams': request.user.team_set.all(),
		'other_teams': Team.objects.exclude(coach=request.user),
	}
	return render(request, 'game/issue-challenge.html', data)
Beispiel #6
0
    def post(cls, request, game_id=None):
        game = request.data
        challenges = request.data['challenges']

        if game_id:
            g = get_object_or_404(Game, pk=game_id)
        else:
            g = Game()

        for k, v in game.items():
            if k in ['pk', 'challenges', 'author', 'options']:
                continue
            setattr(g, k, v)

        g.author = request.user
        g.add_extra('options', game.get('options', []))
        g.save()

        pkch = {}
        for ch in challenges:
            pk = ch['pk']
            if pk < 0:
                # negative pk will create the challenge
                c = Challenge()
            else:
                c = Challenge.objects.get(pk=pk)

            for k, v in ch.items():
                if k in ['pk', 'game', 'options', 'child_challenges', 'depends']:
                    continue
                setattr(c, k, v)

            c.add_extra('options', ch.get('options', []))
            c.save()
            g.challenges.add(c)
            g.save()
            pkch[pk] = c

        for ch in challenges:
            c = pkch[ch['pk']]

            # child challenges
            c.child_challenges.clear()
            for cc in ch.get('child_challenges', []):
                c.child_challenges.add(pkch[cc['pk']])

            # depends
            c.depends.clear()
            for dep in ch.get('depends', []):
                c.depends.add(pkch[dep['pk']])

            c.save()

        return Response({'status': 'ok'})
Beispiel #7
0
    def _test_run_match(self, bot1_code, bot2_code, checkChallenge):
        def save_bot(user_profile, bot_code):
            aBot = Bot()
            aBot.owner = user_profile
            aBot.code = bot_code
            aBot.save()
            return aBot

        bot1 = save_bot(self.user1_profile, bot1_code)
        bot2 = save_bot(self.user2_profile, bot2_code)

        aChallenge = Challenge()
        aChallenge.challenger_bot = bot1
        aChallenge.challenged_bot = bot2
        aChallenge.requested_by = self.user1_profile
        aChallenge.save()

        players = {'bot1': aChallenge.challenger_bot.code,
                   'bot2': aChallenge.challenged_bot.code}

        run_match.delay(aChallenge.id, players)

        #Refresh challenge
        aChallenge = Challenge.objects.get(pk=aChallenge.id)
        checkChallenge(aChallenge)