Ejemplo n.º 1
0
def accept_invite(request, key):
    try:
        invite = GameInvite.objects.get(invite_key=key, is_active=True)
    except GameInvite.DoesNotExist:
        raise Http404

    if not request.user == invite.inviter:
        coin_toss = random.choice([0, 1])

        if coin_toss == 0:
            game = Game(player1=invite.inviter, player2=request.user)
        else:
            game = Game(player1=request.user, player2=invite.inviter)

        game.save()

        red = settings.get_redis()#Redis(REDIS_HOST)
        red.publish('%d' % invite.inviter.id, ['game_started', game.id, str(request.user.username)])

        # No reason to keep the invites around
        invite.delete()

        return redirect('view_game', game_id=game.id)

    raise Http404
Ejemplo n.º 2
0
def _sub_listener(socketio, chan):
        red = settings.get_redis()
        red.subscribe(chan)
        print 'subscribed on chan ', chan
        while True:
            for i in red.listen():
                socketio.send({'message': i})
Ejemplo n.º 3
0
 def current_rate(self):
     redis = get_redis()
     key = "rates:current:{0}".format(self.build['user'])
     output = redis.hgetall(key)
     if len(output) == 0:
         output = {'weekly_usage': 0, 'monthly_usage': 0, 'daily_usage': 0}
     return output
Ejemplo n.º 4
0
 def save_model_performance(self, ip):
     redis = get_redis()
     day = datetime.today().weekday()
     day_of_month = datetime.now().day
     tsnow = datetime.now()
     key = "rates:current:{0}".format(self.build['user'])
     redis = get_redis()
     cr_usage = redis.hgetall(key)
     if cr_usage is None or len(cr_usage) == 0:
         cr_usage = {
             'weekly_usage': 0,
             'monthly_usage': 0,
             'daily_usage': 0
         }
     cr_usage['weekly_usage'] = int(cr_usage['weekly_usage']) + 1
     cr_usage['monthly_usage'] = int(cr_usage['monthly_usage']) + 1
     cr_usage['daily_usage'] = int(cr_usage['daily_usage']) + 1
     cr_usage['day'] = day
     cr_usage['day_of_month'] = day_of_month
     cr_usage['ts'] = tsnow
     cr_usage['ip'] = ip
     for el_key in cr_usage:
         value = cr_usage[el_key]
         redis.hset(key, el_key, value)
Ejemplo n.º 5
0
 def throttle(self):
     redis = get_redis()
     key = "rates:allowed:{0}".format(self.build['user'])
     user_rates_allowed = redis.hgetall(key)
     current_usage = self.current_rate()
     if int(current_usage['weekly_usage']) > int(
             user_rates_allowed['weekly_usage']):
         return False
     if int(current_usage['monthly_usage']) > int(
             user_rates_allowed['monthly_usage']):
         return False
     if int(current_usage['daily_usage']) > int(
             user_rates_allowed['daily_usage']):
         return False
     return True
Ejemplo n.º 6
0
def game_list(request, template_name='core/game_list.html'):
    games = Game.objects.get_by_user(request.user)[:15]

    if request.POST:
        form = EmailForm(request.POST)

        if form.is_valid():
            email = form.cleaned_data["email"]

            if email == request.user.email:
                messages.add_message(request, messages.ERROR, 'You are not allowed to invite yourself.')
                form = EmailForm()
            else:
                try:
                    user = User.objects.get(email=email)
                except User.DoesNotExist:
                    user = None

                invite = GameInvite(inviter=request.user, is_active=True)

                if user:
                    invite.invitee = user

                invite.save()


                url = reverse('accept_invite', args=[invite.invite_key])
                current_site = Site.objects.get_current()
                messages.add_message(request, messages.SUCCESS, 'Invite was sent!')

                if invite.invitee:
                    red = settings.get_redis()#Redis(REDIS_HOST)
                    red.publish('%d' % invite.invitee.id, ['new_invite', str(request.user.username), url])

                send_mail('You are invited to play tic tac toe :)', 'Click here! %s%s' % (current_site.domain, url), '*****@*****.**',
                            [email], fail_silently=False)

                form = EmailForm()
    else:
        form = EmailForm()

    context = { 'games': games, 'form': form }

    return render_to_response(template_name, context,
            context_instance=RequestContext(request))
Ejemplo n.º 7
0
def create_move(request, game_id):
    game = _get_game(request.user, game_id)
    if request.POST:
        move = int(request.POST['move'])
        red = settings.get_redis()#Redis(REDIS_HOST)

        # get player of move
        tic_player = Player_X if game.player1 == request.user else Player_O

        GameMove(game=game, player=request.user, move=move).save()
        board = game.get_board()
        board.make_move(move, tic_player)

        # get opponent
        opponent_player = Player_O if tic_player == Player_X else Player_X
        opponent_user = game.player1 if tic_player == Player_O else game.player2

        # get computer
        computer_user = _get_computer() 
        playing_computer = computer_user in [game.player1, game.player2]

        # if game over, and not playing against computer,  send notification of move, and of game over
        winner = board.get_winner()

        if board.is_game_over():
            red.publish('%d' % request.user.id, ['game_over', game.id, winner])

            if not playing_computer:
                red.publish('%d' % opponent_user.id, ['opponent_moved', game.id, tic_player, move])
                red.publish('%d' % opponent_user.id, ['game_over', game.id, winner])
        else:
            if playing_computer:
                move, board = _create_computer_move(game, board)
                red.publish('%d' % request.user.id, ['opponent_moved', game.id, opponent_player, move])

                if board.is_game_over():
                    winner = board.get_winner()
                    red.publish('%d' % request.user.id, ['game_over', game.id, winner])
            else:
                red.publish('%d' % opponent_user.id, ['opponent_moved', game.id, tic_player, move])

    return HttpResponse()
Ejemplo n.º 8
0
def get_model_build(build_id):
    from settings import get_redis
    redis = get_redis()
    model_hash = redis.hgetall("builds:published:{0}".format(build_id))
    return model_hash